Optimised Callback Class to Reduce Code and Fix Errors (#4688)
* added callbacks * added back callback to main * added save_dir to callback output * reduced code count * updated callbacks * added default callback class to main, added missing parameters to on_model_save * Glenn updates Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
1 parent
548745181a
commit
2317f86ca4
3 files changed
+23
-126
No files matched your search
@@ -56,7 +56,7 @@ WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))
|
||||
def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
opt,
|
||||
device,
|
||||
callbacks=Callbacks()
|
||||
callbacks
|
||||
):
|
||||
save_dir, epochs, batch_size, weights, single_cls, evolve, data, cfg, resume, noval, nosave, workers, freeze, = \
|
||||
Path(opt.save_dir), opt.epochs, opt.batch_size, opt.weights, opt.single_cls, opt.evolve, opt.data, opt.cfg, \
|
||||
@@ -231,7 +231,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz)
|
||||
model.half().float() # pre-reduce anchor precision
|
||||
|
||||
callbacks.on_pretrain_routine_end()
|
||||
callbacks.run('on_pretrain_routine_end')
|
||||
|
||||
# DDP mode
|
||||
if cuda and RANK != -1:
|
||||
@@ -333,7 +333,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB)
|
||||
pbar.set_description(('%10s' * 2 + '%10.4g' * 5) % (
|
||||
f'{epoch}/{epochs - 1}', mem, *mloss, targets.shape[0], imgs.shape[-1]))
|
||||
callbacks.on_train_batch_end(ni, model, imgs, targets, paths, plots, opt.sync_bn)
|
||||
callbacks.run('on_train_batch_end', ni, model, imgs, targets, paths, plots, opt.sync_bn)
|
||||
# end batch ------------------------------------------------------------------------------------------------
|
||||
|
||||
# Scheduler
|
||||
@@ -342,7 +342,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
|
||||
if RANK in [-1, 0]:
|
||||
# mAP
|
||||
callbacks.on_train_epoch_end(epoch=epoch)
|
||||
callbacks.run('on_train_epoch_end', epoch=epoch)
|
||||
ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'names', 'stride', 'class_weights'])
|
||||
final_epoch = (epoch + 1 == epochs) or stopper.possible_stop
|
||||
if not noval or final_epoch: # Calculate mAP
|
||||
@@ -364,7 +364,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
if fi > best_fitness:
|
||||
best_fitness = fi
|
||||
log_vals = list(mloss) + list(results) + lr
|
||||
callbacks.on_fit_epoch_end(log_vals, epoch, best_fitness, fi)
|
||||
callbacks.run('on_fit_epoch_end', log_vals, epoch, best_fitness, fi)
|
||||
|
||||
# Save model
|
||||
if (not nosave) or (final_epoch and not evolve): # if save
|
||||
@@ -381,7 +381,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
if best_fitness == fi:
|
||||
torch.save(ckpt, best)
|
||||
del ckpt
|
||||
callbacks.on_model_save(last, epoch, final_epoch, best_fitness, fi)
|
||||
callbacks.run('on_model_save', last, epoch, final_epoch, best_fitness, fi)
|
||||
|
||||
# Stop Single-GPU
|
||||
if RANK == -1 and stopper(epoch=epoch, fitness=fi):
|
||||
@@ -418,7 +418,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
for f in last, best:
|
||||
if f.exists():
|
||||
strip_optimizer(f) # strip optimizers
|
||||
callbacks.on_train_end(last, best, plots, epoch)
|
||||
callbacks.run('on_train_end', last, best, plots, epoch)
|
||||
LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}")
|
||||
|
||||
torch.cuda.empty_cache()
|
||||
@@ -467,7 +467,7 @@ def parse_opt(known=False):
|
||||
return opt
|
||||
|
||||
|
||||
def main(opt):
|
||||
def main(opt, callbacks=Callbacks()):
|
||||
# Checks
|
||||
set_logging(RANK)
|
||||
if RANK in [-1, 0]:
|
||||
@@ -505,7 +505,7 @@ def main(opt):
|
||||
|
||||
# Train
|
||||
if not opt.evolve:
|
||||
train(opt.hyp, opt, device)
|
||||
train(opt.hyp, opt, device, callbacks)
|
||||
if WORLD_SIZE > 1 and RANK == 0:
|
||||
_ = [print('Destroying process group... ', end=''), dist.destroy_process_group(), print('Done.')]
|
||||
|
||||
@@ -585,7 +585,7 @@ def main(opt):
|
||||
hyp[k] = round(hyp[k], 5) # significant digits
|
||||
|
||||
# Train mutation
|
||||
results = train(hyp.copy(), opt, device)
|
||||
results = train(hyp.copy(), opt, device, callbacks)
|
||||
|
||||
# Write mutation results
|
||||
print_mutation(results, hyp.copy(), save_dir, opt.bucket)
|
||||
|
||||
Reference in new issue
Block a user