์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

์ฐธ์กฐ ultralytics/utils/callbacks/tensorboard.py

์ฐธ๊ณ 

์ด ํŒŒ์ผ์€ https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/utils/callbacks/tensorboard .py์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ํ’€ ๋ฆฌํ€˜์ŠคํŠธ ๐Ÿ› ๏ธ ์— ๊ธฐ์—ฌํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋„๋ก ๋„์™€์ฃผ์„ธ์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ™!



ultralytics.utils.callbacks.tensorboard._log_scalars(scalars, step=0)

์Šค์นผ๋ผ ๊ฐ’์„ ํ…์„œ๋ณด๋“œ์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/tensorboard.py
def _log_scalars(scalars, step=0):
    """Logs scalar values to TensorBoard."""
    if WRITER:
        for k, v in scalars.items():
            WRITER.add_scalar(k, v, step)



ultralytics.utils.callbacks.tensorboard._log_tensorboard_graph(trainer)

๋ชจ๋ธ ๊ทธ๋ž˜ํ”„๋ฅผ ํ…์„œ๋ณด๋“œ์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/tensorboard.py
def _log_tensorboard_graph(trainer):
    """Log model graph to TensorBoard."""

    # Input image
    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 image (must be zeros, not empty)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=UserWarning)  # suppress jit trace warning
        warnings.simplefilter("ignore", category=torch.jit.TracerWarning)  # suppress jit trace warning

        # Try simple method first (YOLO)
        with contextlib.suppress(Exception):
            trainer.model.eval()  # place in .eval() mode to avoid BatchNorm statistics changes
            WRITER.add_graph(torch.jit.trace(de_parallel(trainer.model), im, strict=False), [])
            LOGGER.info(f"{PREFIX}model graph visualization added โœ…")
            return

        # Fallback to TorchScript export steps (RTDETR)
        try:
            model = deepcopy(de_parallel(trainer.model))
            model.eval()
            model = model.fuse(verbose=False)
            for m in model.modules():
                if hasattr(m, "export"):  # Detect, RTDETRDecoder (Segment and Pose use Detect base class)
                    m.export = True
                    m.format = "torchscript"
            model(im)  # dry run
            WRITER.add_graph(torch.jit.trace(model, im, strict=False), [])
            LOGGER.info(f"{PREFIX}model graph visualization added โœ…")
        except Exception as e:
            LOGGER.warning(f"{PREFIX}WARNING โš ๏ธ TensorBoard graph visualization failure {e}")



ultralytics.utils.callbacks.tensorboard.on_pretrain_routine_start(trainer)

SummaryWriter๋กœ TensorBoard ๋กœ๊น…์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/tensorboard.py
def on_pretrain_routine_start(trainer):
    """Initialize TensorBoard logging with SummaryWriter."""
    if SummaryWriter:
        try:
            global WRITER
            WRITER = SummaryWriter(str(trainer.save_dir))
            LOGGER.info(f"{PREFIX}Start with 'tensorboard --logdir {trainer.save_dir}', view at http://localhost:6006/")
        except Exception as e:
            LOGGER.warning(f"{PREFIX}WARNING โš ๏ธ TensorBoard not initialized correctly, not logging this run. {e}")



ultralytics.utils.callbacks.tensorboard.on_train_start(trainer)

๋กœ๊ทธ ํ…์„œ๋ณด๋“œ ๊ทธ๋ž˜ํ”„.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/tensorboard.py
def on_train_start(trainer):
    """Log TensorBoard graph."""
    if WRITER:
        _log_tensorboard_graph(trainer)



ultralytics.utils.callbacks.tensorboard.on_train_epoch_end(trainer)

ํŠธ๋ ˆ์ด๋‹ ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ ์Šค์นผ๋ผ ํ†ต๊ณ„๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/tensorboard.py
def on_train_epoch_end(trainer):
    """Logs scalar statistics at the end of a training epoch."""
    _log_scalars(trainer.label_loss_items(trainer.tloss, prefix="train"), trainer.epoch + 1)
    _log_scalars(trainer.lr, trainer.epoch + 1)



ultralytics.utils.callbacks.tensorboard.on_fit_epoch_end(trainer)

๊ต์œก ์—ํฌํฌ ์ข…๋ฃŒ ์‹œ ์—ํฌํฌ ๋ฉ”ํŠธ๋ฆญ์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/tensorboard.py
def on_fit_epoch_end(trainer):
    """Logs epoch metrics at end of training epoch."""
    _log_scalars(trainer.metrics, trainer.epoch + 1)





์ž‘์„ฑ 2023-11-12, ์—…๋ฐ์ดํŠธ 2024-06-02
์ €์ž: glenn-jocher (6), Burhan-Q (1), Laughing-q (1)