|
|
@ -12,24 +12,43 @@ try:
|
|
|
|
except (ImportError, AssertionError, TypeError):
|
|
|
|
except (ImportError, AssertionError, TypeError):
|
|
|
|
SummaryWriter = None
|
|
|
|
SummaryWriter = None
|
|
|
|
|
|
|
|
|
|
|
|
writer = None # TensorBoard SummaryWriter instance
|
|
|
|
WRITER = None # TensorBoard SummaryWriter instance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _log_scalars(scalars, step=0):
|
|
|
|
def _log_scalars(scalars, step=0):
|
|
|
|
"""Logs scalar values to TensorBoard."""
|
|
|
|
"""Logs scalar values to TensorBoard."""
|
|
|
|
if writer:
|
|
|
|
if WRITER:
|
|
|
|
for k, v in scalars.items():
|
|
|
|
for k, v in scalars.items():
|
|
|
|
writer.add_scalar(k, v, step)
|
|
|
|
WRITER.add_scalar(k, v, step)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _log_tensorboard_graph(trainer):
|
|
|
|
|
|
|
|
# Log model graph to TensorBoard
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from ultralytics.utils.torch_utils import de_parallel, torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
imgsz = trainer.args.imgsz
|
|
|
|
|
|
|
|
imgsz = (imgsz, imgsz) if isinstance(imgsz, int) else imgsz
|
|
|
|
|
|
|
|
p = next(trainer.model.parameters()) # for device, type
|
|
|
|
|
|
|
|
im = torch.zeros((1, 3, *imgsz), device=p.device, dtype=p.dtype) # input (WARNING: must be zeros, not empty)
|
|
|
|
|
|
|
|
with warnings.catch_warnings(category=UserWarning):
|
|
|
|
|
|
|
|
warnings.simplefilter('ignore') # suppress jit trace warning
|
|
|
|
|
|
|
|
WRITER.add_graph(torch.jit.trace(de_parallel(trainer.model), im, strict=False), [])
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
LOGGER.warning(f'WARNING ⚠️ TensorBoard graph visualization failure {e}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_pretrain_routine_start(trainer):
|
|
|
|
def on_pretrain_routine_start(trainer):
|
|
|
|
"""Initialize TensorBoard logging with SummaryWriter."""
|
|
|
|
"""Initialize TensorBoard logging with SummaryWriter."""
|
|
|
|
if SummaryWriter:
|
|
|
|
if SummaryWriter:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
global writer
|
|
|
|
global WRITER
|
|
|
|
writer = SummaryWriter(str(trainer.save_dir))
|
|
|
|
WRITER = SummaryWriter(str(trainer.save_dir))
|
|
|
|
prefix = colorstr('TensorBoard: ')
|
|
|
|
prefix = colorstr('TensorBoard: ')
|
|
|
|
LOGGER.info(f"{prefix}Start with 'tensorboard --logdir {trainer.save_dir}', view at http://localhost:6006/")
|
|
|
|
LOGGER.info(f"{prefix}Start with 'tensorboard --logdir {trainer.save_dir}', view at http://localhost:6006/")
|
|
|
|
|
|
|
|
_log_tensorboard_graph(trainer)
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
LOGGER.warning(f'WARNING ⚠️ TensorBoard not initialized correctly, not logging this run. {e}')
|
|
|
|
LOGGER.warning(f'WARNING ⚠️ TensorBoard not initialized correctly, not logging this run. {e}')
|
|
|
|
|
|
|
|
|
|
|
|