tensorboard.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. from ultralytics.utils import LOGGER, SETTINGS, TESTS_RUNNING, colorstr
  3. try:
  4. from torch.utils.tensorboard import SummaryWriter
  5. assert not TESTS_RUNNING # do not log pytest
  6. assert SETTINGS['tensorboard'] is True # verify integration is enabled
  7. # TypeError for handling 'Descriptors cannot not be created directly.' protobuf errors in Windows
  8. except (ImportError, AssertionError, TypeError):
  9. SummaryWriter = None
  10. WRITER = None # TensorBoard SummaryWriter instance
  11. def _log_scalars(scalars, step=0):
  12. """Logs scalar values to TensorBoard."""
  13. if WRITER:
  14. for k, v in scalars.items():
  15. WRITER.add_scalar(k, v, step)
  16. def _log_tensorboard_graph(trainer):
  17. """Log model graph to TensorBoard."""
  18. try:
  19. import warnings
  20. from ultralytics.utils.torch_utils import de_parallel, torch
  21. imgsz = trainer.args.imgsz
  22. imgsz = (imgsz, imgsz) if isinstance(imgsz, int) else imgsz
  23. p = next(trainer.model.parameters()) # for device, type
  24. im = torch.zeros((1, 3, *imgsz), device=p.device, dtype=p.dtype) # input image (must be zeros, not empty)
  25. with warnings.catch_warnings():
  26. warnings.simplefilter('ignore', category=UserWarning) # suppress jit trace warning
  27. WRITER.add_graph(torch.jit.trace(de_parallel(trainer.model), im, strict=False), [])
  28. except Exception as e:
  29. LOGGER.warning(f'WARNING ⚠️ TensorBoard graph visualization failure {e}')
  30. def on_pretrain_routine_start(trainer):
  31. """Initialize TensorBoard logging with SummaryWriter."""
  32. if SummaryWriter:
  33. try:
  34. global WRITER
  35. WRITER = SummaryWriter(str(trainer.save_dir))
  36. prefix = colorstr('TensorBoard: ')
  37. LOGGER.info(f"{prefix}Start with 'tensorboard --logdir {trainer.save_dir}', view at http://localhost:6006/")
  38. except Exception as e:
  39. LOGGER.warning(f'WARNING ⚠️ TensorBoard not initialized correctly, not logging this run. {e}')
  40. def on_train_start(trainer):
  41. """Log TensorBoard graph."""
  42. if WRITER:
  43. _log_tensorboard_graph(trainer)
  44. def on_batch_end(trainer):
  45. """Logs scalar statistics at the end of a training batch."""
  46. _log_scalars(trainer.label_loss_items(trainer.tloss, prefix='train'), trainer.epoch + 1)
  47. def on_fit_epoch_end(trainer):
  48. """Logs epoch metrics at end of training epoch."""
  49. _log_scalars(trainer.metrics, trainer.epoch + 1)
  50. callbacks = {
  51. 'on_pretrain_routine_start': on_pretrain_routine_start,
  52. 'on_train_start': on_train_start,
  53. 'on_fit_epoch_end': on_fit_epoch_end,
  54. 'on_batch_end': on_batch_end}