|
|
@@ -1,9 +1,10 @@ |
|
|
|
import argparse |
|
|
|
import logging |
|
|
|
import math |
|
|
|
import os |
|
|
|
import random |
|
|
|
import shutil |
|
|
|
import time |
|
|
|
import logging |
|
|
|
from pathlib import Path |
|
|
|
|
|
|
|
import numpy as np |
|
|
@@ -34,10 +35,10 @@ logger = logging.getLogger(__name__) |
|
|
|
def train(hyp, opt, device, tb_writer=None): |
|
|
|
logger.info(f'Hyperparameters {hyp}') |
|
|
|
log_dir = Path(tb_writer.log_dir) if tb_writer else Path(opt.logdir) / 'evolve' # logging directory |
|
|
|
wdir = str(log_dir / 'weights') + os.sep # weights directory |
|
|
|
wdir = log_dir / 'weights' # weights directory |
|
|
|
os.makedirs(wdir, exist_ok=True) |
|
|
|
last = wdir + 'last.pt' |
|
|
|
best = wdir + 'best.pt' |
|
|
|
last = wdir / 'last.pt' |
|
|
|
best = wdir / 'best.pt' |
|
|
|
results_file = str(log_dir / 'results.txt') |
|
|
|
epochs, batch_size, total_batch_size, weights, rank = \ |
|
|
|
opt.epochs, opt.batch_size, opt.total_batch_size, opt.weights, opt.global_rank |
|
|
@@ -131,6 +132,7 @@ def train(hyp, opt, device, tb_writer=None): |
|
|
|
start_epoch = ckpt['epoch'] + 1 |
|
|
|
if opt.resume: |
|
|
|
assert start_epoch > 0, '%s training to %g epochs is finished, nothing to resume.' % (weights, epochs) |
|
|
|
shutil.copytree(wdir, wdir.parent / f'weights_backup_epoch{start_epoch - 1}') # save previous weights |
|
|
|
if epochs < start_epoch: |
|
|
|
logger.info('%s has been trained for %g epochs. Fine-tuning for %g additional epochs.' % |
|
|
|
(weights, ckpt['epoch'], epochs)) |
|
|
@@ -365,13 +367,13 @@ def train(hyp, opt, device, tb_writer=None): |
|
|
|
if rank in [-1, 0]: |
|
|
|
# Strip optimizers |
|
|
|
n = ('_' if len(opt.name) and not opt.name.isnumeric() else '') + opt.name |
|
|
|
fresults, flast, fbest = 'results%s.txt' % n, wdir + 'last%s.pt' % n, wdir + 'best%s.pt' % n |
|
|
|
for f1, f2 in zip([wdir + 'last.pt', wdir + 'best.pt', 'results.txt'], [flast, fbest, fresults]): |
|
|
|
fresults, flast, fbest = 'results%s.txt' % n, wdir / f'last{n}.pt', wdir / f'best{n}.pt' |
|
|
|
for f1, f2 in zip([wdir / 'last.pt', wdir / 'best.pt', 'results.txt'], [flast, fbest, fresults]): |
|
|
|
if os.path.exists(f1): |
|
|
|
os.rename(f1, f2) # rename |
|
|
|
ispt = f2.endswith('.pt') # is *.pt |
|
|
|
strip_optimizer(f2) if ispt else None # strip optimizer |
|
|
|
os.system('gsutil cp %s gs://%s/weights' % (f2, opt.bucket)) if opt.bucket and ispt else None # upload |
|
|
|
if str(f2).endswith('.pt'): # is *.pt |
|
|
|
strip_optimizer(f2) # strip optimizer |
|
|
|
os.system('gsutil cp %s gs://%s/weights' % (f2, opt.bucket)) if opt.bucket else None # upload |
|
|
|
# Finish |
|
|
|
if not opt.evolve: |
|
|
|
plot_results(save_dir=log_dir) # save as results.png |
|
|
@@ -421,8 +423,9 @@ if __name__ == '__main__': |
|
|
|
# Resume |
|
|
|
if opt.resume: # resume an interrupted run |
|
|
|
ckpt = opt.resume if isinstance(opt.resume, str) else get_latest_run() # specified or most recent path |
|
|
|
log_dir = Path(ckpt).parent.parent # runs/exp0 |
|
|
|
assert os.path.isfile(ckpt), 'ERROR: --resume checkpoint does not exist' |
|
|
|
with open(Path(ckpt).parent.parent / 'opt.yaml') as f: |
|
|
|
with open(log_dir / 'opt.yaml') as f: |
|
|
|
opt = argparse.Namespace(**yaml.load(f, Loader=yaml.FullLoader)) # replace |
|
|
|
opt.cfg, opt.weights, opt.resume = '', ckpt, True |
|
|
|
logger.info('Resuming training from %s' % ckpt) |
|
|
@@ -432,6 +435,7 @@ if __name__ == '__main__': |
|
|
|
opt.data, opt.cfg, opt.hyp = check_file(opt.data), check_file(opt.cfg), check_file(opt.hyp) # check files |
|
|
|
assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified' |
|
|
|
opt.img_size.extend([opt.img_size[-1]] * (2 - len(opt.img_size))) # extend to 2 sizes (train, test) |
|
|
|
log_dir = increment_dir(Path(opt.logdir) / 'exp', opt.name) # runs/exp1 |
|
|
|
|
|
|
|
device = select_device(opt.device, batch_size=opt.batch_size) |
|
|
|
|
|
|
@@ -453,7 +457,7 @@ if __name__ == '__main__': |
|
|
|
tb_writer = None |
|
|
|
if opt.global_rank in [-1, 0]: |
|
|
|
logger.info('Start Tensorboard with "tensorboard --logdir %s", view at http://localhost:6006/' % opt.logdir) |
|
|
|
tb_writer = SummaryWriter(log_dir=increment_dir(Path(opt.logdir) / 'exp', opt.name)) # runs/exp |
|
|
|
tb_writer = SummaryWriter(log_dir=log_dir) # runs/exp0 |
|
|
|
|
|
|
|
train(hyp, opt, device, tb_writer) |
|
|
|
|