Skip to content

Commit

Permalink
Yaml constructor posixpath --resume bug fix (#1390)
Browse files Browse the repository at this point in the history
* resume fix for yaml constructor posixpath error

* fix update

* remove weights/ dir backup
  • Loading branch information
glenn-jocher committed Nov 13, 2020
1 parent c4addd7 commit 379396e
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
def train(hyp, opt, device, tb_writer=None, wandb=None):
logger.info(f'Hyperparameters {hyp}')
save_dir, epochs, batch_size, total_batch_size, weights, rank = \
opt.save_dir, opt.epochs, opt.batch_size, opt.total_batch_size, opt.weights, opt.global_rank
Path(opt.save_dir), opt.epochs, opt.batch_size, opt.total_batch_size, opt.weights, opt.global_rank

# Directories
wdir = save_dir / 'weights'
Expand Down Expand Up @@ -143,7 +143,6 @@ def train(hyp, opt, device, tb_writer=None, wandb=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))
Expand Down Expand Up @@ -431,9 +430,8 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
# 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
opt.save_dir = Path(ckpt).parent.parent # runs/train/exp
assert os.path.isfile(ckpt), 'ERROR: --resume checkpoint does not exist'
with open(opt.save_dir / 'opt.yaml') as f:
with open(Path(ckpt).parent.parent / '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)
Expand All @@ -443,7 +441,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
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)
opt.name = 'evolve' if opt.evolve else opt.name
opt.save_dir = Path(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok)) # increment run
opt.save_dir = increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok) # increment run

# DDP mode
device = select_device(opt.device, batch_size=opt.batch_size)
Expand Down Expand Up @@ -517,7 +515,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
assert opt.local_rank == -1, 'DDP mode not implemented for --evolve'
opt.notest, opt.nosave = True, True # only test/save final epoch
# ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
yaml_file = opt.save_dir / 'hyp_evolved.yaml' # save best result here
yaml_file = Path(opt.save_dir) / 'hyp_evolved.yaml' # save best result here
if opt.bucket:
os.system('gsutil cp gs://%s/evolve.txt .' % opt.bucket) # download evolve.txt if exists

Expand Down

0 comments on commit 379396e

Please sign in to comment.