wb.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. from ultralytics.utils import SETTINGS, TESTS_RUNNING
  3. from ultralytics.utils.torch_utils import model_info_for_loggers
  4. try:
  5. import wandb as wb
  6. assert hasattr(wb, '__version__')
  7. assert not TESTS_RUNNING # do not log pytest
  8. assert SETTINGS['wandb'] is True # verify integration is enabled
  9. except (ImportError, AssertionError):
  10. wb = None
  11. _processed_plots = {}
  12. def _log_plots(plots, step):
  13. for name, params in plots.items():
  14. timestamp = params['timestamp']
  15. if _processed_plots.get(name) != timestamp:
  16. wb.run.log({name.stem: wb.Image(str(name))}, step=step)
  17. _processed_plots[name] = timestamp
  18. def on_pretrain_routine_start(trainer):
  19. """Initiate and start project if module is present."""
  20. wb.run or wb.init(project=trainer.args.project or 'YOLOv8', name=trainer.args.name, config=vars(trainer.args))
  21. def on_fit_epoch_end(trainer):
  22. """Logs training metrics and model information at the end of an epoch."""
  23. wb.run.log(trainer.metrics, step=trainer.epoch + 1)
  24. _log_plots(trainer.plots, step=trainer.epoch + 1)
  25. _log_plots(trainer.validator.plots, step=trainer.epoch + 1)
  26. if trainer.epoch == 0:
  27. wb.run.log(model_info_for_loggers(trainer), step=trainer.epoch + 1)
  28. def on_train_epoch_end(trainer):
  29. """Log metrics and save images at the end of each training epoch."""
  30. wb.run.log(trainer.label_loss_items(trainer.tloss, prefix='train'), step=trainer.epoch + 1)
  31. wb.run.log(trainer.lr, step=trainer.epoch + 1)
  32. if trainer.epoch == 1:
  33. _log_plots(trainer.plots, step=trainer.epoch + 1)
  34. def on_train_end(trainer):
  35. """Save the best model as an artifact at end of training."""
  36. _log_plots(trainer.validator.plots, step=trainer.epoch + 1)
  37. _log_plots(trainer.plots, step=trainer.epoch + 1)
  38. art = wb.Artifact(type='model', name=f'run_{wb.run.id}_model')
  39. if trainer.best.exists():
  40. art.add_file(trainer.best)
  41. wb.run.log_artifact(art, aliases=['best'])
  42. callbacks = {
  43. 'on_pretrain_routine_start': on_pretrain_routine_start,
  44. 'on_train_epoch_end': on_train_epoch_end,
  45. 'on_fit_epoch_end': on_fit_epoch_end,
  46. 'on_train_end': on_train_end} if wb else {}