You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.5 KiB
48 lines
1.5 KiB
2 years ago
|
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||
2 years ago
|
|
||
1 year ago
|
from ultralytics.utils import LOGGER, TESTS_RUNNING, colorstr
|
||
2 years ago
|
|
||
2 years ago
|
try:
|
||
|
from torch.utils.tensorboard import SummaryWriter
|
||
2 years ago
|
|
||
2 years ago
|
assert not TESTS_RUNNING # do not log pytest
|
||
|
except (ImportError, AssertionError):
|
||
|
SummaryWriter = None
|
||
2 years ago
|
|
||
2 years ago
|
writer = None # TensorBoard SummaryWriter instance
|
||
|
|
||
|
|
||
|
def _log_scalars(scalars, step=0):
|
||
2 years ago
|
"""Logs scalar values to TensorBoard."""
|
||
2 years ago
|
if writer:
|
||
|
for k, v in scalars.items():
|
||
|
writer.add_scalar(k, v, step)
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
def on_pretrain_routine_start(trainer):
|
||
2 years ago
|
"""Initialize TensorBoard logging with SummaryWriter."""
|
||
2 years ago
|
if SummaryWriter:
|
||
|
try:
|
||
|
global writer
|
||
|
writer = SummaryWriter(str(trainer.save_dir))
|
||
|
prefix = colorstr('TensorBoard: ')
|
||
|
LOGGER.info(f"{prefix}Start with 'tensorboard --logdir {trainer.save_dir}', view at http://localhost:6006/")
|
||
|
except Exception as e:
|
||
|
LOGGER.warning(f'WARNING ⚠️ TensorBoard not initialized correctly, not logging this run. {e}')
|
||
2 years ago
|
|
||
|
|
||
|
def on_batch_end(trainer):
|
||
2 years ago
|
"""Logs scalar statistics at the end of a training batch."""
|
||
2 years ago
|
_log_scalars(trainer.label_loss_items(trainer.tloss, prefix='train'), trainer.epoch + 1)
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
def on_fit_epoch_end(trainer):
|
||
2 years ago
|
"""Logs epoch metrics at end of training epoch."""
|
||
2 years ago
|
_log_scalars(trainer.metrics, trainer.epoch + 1)
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
callbacks = {
|
||
2 years ago
|
'on_pretrain_routine_start': on_pretrain_routine_start,
|
||
|
'on_fit_epoch_end': on_fit_epoch_end,
|
||
|
'on_batch_end': on_batch_end}
|