glenn-jocher commited on
Commit
281d78c
·
unverified ·
1 Parent(s): d681799

Update train.py (#902)

Browse files

* Update train.py with simplified ckpt names

* Return default hyps to hyp.scratch.yaml

Leave line commented for future use once mystery of best finetuning hyps to apply becomes clearer.

* Force test_batch*_pred.jpg replot on final epoch

This will allow you to see predictions final testing run after training completes in runs/exp0

Files changed (1) hide show
  1. train.py +6 -3
train.py CHANGED
@@ -1,4 +1,5 @@
1
  import argparse
 
2
  import logging
3
  import math
4
  import os
@@ -311,6 +312,8 @@ def train(hyp, opt, device, tb_writer=None):
311
  ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'gr', 'names', 'stride'])
312
  final_epoch = epoch + 1 == epochs
313
  if not opt.notest or final_epoch: # Calculate mAP
 
 
314
  results, maps, times = test.test(opt.data,
315
  batch_size=total_batch_size,
316
  imgsz=imgsz_test,
@@ -359,7 +362,7 @@ def train(hyp, opt, device, tb_writer=None):
359
 
360
  if rank in [-1, 0]:
361
  # Strip optimizers
362
- n = ('_' if len(opt.name) and not opt.name.isnumeric() else '') + opt.name
363
  fresults, flast, fbest = 'results%s.txt' % n, wdir / f'last{n}.pt', wdir / f'best{n}.pt'
364
  for f1, f2 in zip([wdir / 'last.pt', wdir / 'best.pt', 'results.txt'], [flast, fbest, fresults]):
365
  if os.path.exists(f1):
@@ -382,7 +385,7 @@ if __name__ == '__main__':
382
  parser.add_argument('--weights', type=str, default='yolov5s.pt', help='initial weights path')
383
  parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
384
  parser.add_argument('--data', type=str, default='data/coco128.yaml', help='data.yaml path')
385
- parser.add_argument('--hyp', type=str, default='', help='hyperparameters path, i.e. data/hyp.scratch.yaml')
386
  parser.add_argument('--epochs', type=int, default=300)
387
  parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs')
388
  parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='[train, test] image sizes')
@@ -425,7 +428,7 @@ if __name__ == '__main__':
425
  logger.info('Resuming training from %s' % ckpt)
426
 
427
  else:
428
- opt.hyp = opt.hyp or ('data/hyp.finetune.yaml' if opt.weights else 'data/hyp.scratch.yaml')
429
  opt.data, opt.cfg, opt.hyp = check_file(opt.data), check_file(opt.cfg), check_file(opt.hyp) # check files
430
  assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified'
431
  opt.img_size.extend([opt.img_size[-1]] * (2 - len(opt.img_size))) # extend to 2 sizes (train, test)
 
1
  import argparse
2
+ import glob
3
  import logging
4
  import math
5
  import os
 
312
  ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'gr', 'names', 'stride'])
313
  final_epoch = epoch + 1 == epochs
314
  if not opt.notest or final_epoch: # Calculate mAP
315
+ if final_epoch: # replot predictions
316
+ [os.remove(x) for x in glob.glob(str(log_dir / 'test_batch*_pred.jpg')) if os.path.exists(x)]
317
  results, maps, times = test.test(opt.data,
318
  batch_size=total_batch_size,
319
  imgsz=imgsz_test,
 
362
 
363
  if rank in [-1, 0]:
364
  # Strip optimizers
365
+ n = opt.name if opt.name.isnumeric() else ''
366
  fresults, flast, fbest = 'results%s.txt' % n, wdir / f'last{n}.pt', wdir / f'best{n}.pt'
367
  for f1, f2 in zip([wdir / 'last.pt', wdir / 'best.pt', 'results.txt'], [flast, fbest, fresults]):
368
  if os.path.exists(f1):
 
385
  parser.add_argument('--weights', type=str, default='yolov5s.pt', help='initial weights path')
386
  parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
387
  parser.add_argument('--data', type=str, default='data/coco128.yaml', help='data.yaml path')
388
+ parser.add_argument('--hyp', type=str, default='data/hyp.scratch.yaml', help='hyperparameters path')
389
  parser.add_argument('--epochs', type=int, default=300)
390
  parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs')
391
  parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='[train, test] image sizes')
 
428
  logger.info('Resuming training from %s' % ckpt)
429
 
430
  else:
431
+ # opt.hyp = opt.hyp or ('hyp.finetune.yaml' if opt.weights else 'hyp.scratch.yaml')
432
  opt.data, opt.cfg, opt.hyp = check_file(opt.data), check_file(opt.cfg), check_file(opt.hyp) # check files
433
  assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified'
434
  opt.img_size.extend([opt.img_size[-1]] * (2 - len(opt.img_size))) # extend to 2 sizes (train, test)