COCO evolution fix (#3388)
Browse files* COCO evolution fix
* cleanup
* update print
* print fix
train.py
CHANGED
@@ -62,7 +62,6 @@ def train(hyp, opt, device, tb_writer=None):
|
|
62 |
init_seeds(2 + rank)
|
63 |
with open(opt.data) as f:
|
64 |
data_dict = yaml.safe_load(f) # data dict
|
65 |
-
is_coco = opt.data.endswith('coco.yaml')
|
66 |
|
67 |
# Logging- Doing this before checking the dataset. Might update data_dict
|
68 |
loggers = {'wandb': None} # loggers dict
|
@@ -78,6 +77,7 @@ def train(hyp, opt, device, tb_writer=None):
|
|
78 |
nc = 1 if opt.single_cls else int(data_dict['nc']) # number of classes
|
79 |
names = ['item'] if opt.single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
|
80 |
assert len(names) == nc, '%g names found for nc=%g dataset in %s' % (len(names), nc, opt.data) # check
|
|
|
81 |
|
82 |
# Model
|
83 |
pretrained = weights.endswith('.pt')
|
@@ -358,6 +358,7 @@ def train(hyp, opt, device, tb_writer=None):
|
|
358 |
single_cls=opt.single_cls,
|
359 |
dataloader=testloader,
|
360 |
save_dir=save_dir,
|
|
|
361 |
verbose=nc < 50 and final_epoch,
|
362 |
plots=plots and final_epoch,
|
363 |
wandb_logger=wandb_logger,
|
@@ -409,41 +410,38 @@ def train(hyp, opt, device, tb_writer=None):
|
|
409 |
# end epoch ----------------------------------------------------------------------------------------------------
|
410 |
# end training
|
411 |
if rank in [-1, 0]:
|
412 |
-
|
413 |
if plots:
|
414 |
plot_results(save_dir=save_dir) # save as results.png
|
415 |
if wandb_logger.wandb:
|
416 |
files = ['results.png', 'confusion_matrix.png', *[f'{x}_curve.png' for x in ('F1', 'PR', 'P', 'R')]]
|
417 |
wandb_logger.log({"Results": [wandb_logger.wandb.Image(str(save_dir / f), caption=f) for f in files
|
418 |
if (save_dir / f).exists()]})
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
wandb_logger.wandb.log_artifact(str(final), type='model',
|
445 |
-
name='run_' + wandb_logger.wandb_run.id + '_model',
|
446 |
-
aliases=['latest', 'best', 'stripped'])
|
447 |
wandb_logger.finish_run()
|
448 |
else:
|
449 |
dist.destroy_process_group()
|
|
|
62 |
init_seeds(2 + rank)
|
63 |
with open(opt.data) as f:
|
64 |
data_dict = yaml.safe_load(f) # data dict
|
|
|
65 |
|
66 |
# Logging- Doing this before checking the dataset. Might update data_dict
|
67 |
loggers = {'wandb': None} # loggers dict
|
|
|
77 |
nc = 1 if opt.single_cls else int(data_dict['nc']) # number of classes
|
78 |
names = ['item'] if opt.single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
|
79 |
assert len(names) == nc, '%g names found for nc=%g dataset in %s' % (len(names), nc, opt.data) # check
|
80 |
+
is_coco = opt.data.endswith('coco.yaml') and nc == 80 # COCO dataset
|
81 |
|
82 |
# Model
|
83 |
pretrained = weights.endswith('.pt')
|
|
|
358 |
single_cls=opt.single_cls,
|
359 |
dataloader=testloader,
|
360 |
save_dir=save_dir,
|
361 |
+
save_json=is_coco and final_epoch,
|
362 |
verbose=nc < 50 and final_epoch,
|
363 |
plots=plots and final_epoch,
|
364 |
wandb_logger=wandb_logger,
|
|
|
410 |
# end epoch ----------------------------------------------------------------------------------------------------
|
411 |
# end training
|
412 |
if rank in [-1, 0]:
|
413 |
+
logger.info(f'{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.\n')
|
414 |
if plots:
|
415 |
plot_results(save_dir=save_dir) # save as results.png
|
416 |
if wandb_logger.wandb:
|
417 |
files = ['results.png', 'confusion_matrix.png', *[f'{x}_curve.png' for x in ('F1', 'PR', 'P', 'R')]]
|
418 |
wandb_logger.log({"Results": [wandb_logger.wandb.Image(str(save_dir / f), caption=f) for f in files
|
419 |
if (save_dir / f).exists()]})
|
420 |
+
|
421 |
+
if not opt.evolve:
|
422 |
+
if is_coco: # COCO dataset
|
423 |
+
for m in [last, best] if best.exists() else [last]: # speed, mAP tests
|
424 |
+
results, _, _ = test.test(opt.data,
|
425 |
+
batch_size=batch_size * 2,
|
426 |
+
imgsz=imgsz_test,
|
427 |
+
conf_thres=0.001,
|
428 |
+
iou_thres=0.7,
|
429 |
+
model=attempt_load(m, device).half(),
|
430 |
+
single_cls=opt.single_cls,
|
431 |
+
dataloader=testloader,
|
432 |
+
save_dir=save_dir,
|
433 |
+
save_json=True,
|
434 |
+
plots=False,
|
435 |
+
is_coco=is_coco)
|
436 |
+
|
437 |
+
# Strip optimizers
|
438 |
+
for f in last, best:
|
439 |
+
if f.exists():
|
440 |
+
strip_optimizer(f) # strip optimizers
|
441 |
+
if wandb_logger.wandb: # Log the stripped model
|
442 |
+
wandb_logger.wandb.log_artifact(str(best if best.exists() else last), type='model',
|
443 |
+
name='run_' + wandb_logger.wandb_run.id + '_model',
|
444 |
+
aliases=['latest', 'best', 'stripped'])
|
|
|
|
|
|
|
445 |
wandb_logger.finish_run()
|
446 |
else:
|
447 |
dist.destroy_process_group()
|