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

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

์ฐธ๊ณ 

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



ultralytics.utils.callbacks.dvc._log_images(path, prefix='')

DVCLive๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ํƒ์  ์ ‘๋‘์‚ฌ์™€ ํ•จ๊ป˜ ์ง€์ •๋œ ๊ฒฝ๋กœ์— ์ด๋ฏธ์ง€๋ฅผ ๋กœ๊น…ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/dvc.py
def _log_images(path, prefix=""):
    """Logs images at specified path with an optional prefix using DVCLive."""
    if live:
        name = path.name

        # Group images by batch to enable sliders in UI
        if m := re.search(r"_batch(\d+)", name):
            ni = m[1]
            new_stem = re.sub(r"_batch(\d+)", "_batch", path.stem)
            name = (Path(new_stem) / ni).with_suffix(path.suffix)

        live.log_image(os.path.join(prefix, name), path)



ultralytics.utils.callbacks.dvc._log_plots(plots, prefix='')

์ด์ „์— ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ต์œก ์ง„ํ–‰ ์ƒํ™ฉ์— ๋Œ€ํ•œ ํ”Œ๋กฏ ์ด๋ฏธ์ง€๋ฅผ ๋กœ๊ทธ์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/dvc.py
def _log_plots(plots, prefix=""):
    """Logs plot images for training progress if they have not been previously processed."""
    for name, params in plots.items():
        timestamp = params["timestamp"]
        if _processed_plots.get(name) != timestamp:
            _log_images(name, prefix)
            _processed_plots[name] = timestamp



ultralytics.utils.callbacks.dvc._log_confusion_matrix(validator)

DVCLive๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •๋œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ธฐ์— ๋Œ€ํ•œ ํ˜ผ๋™ ํ–‰๋ ฌ์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/dvc.py
def _log_confusion_matrix(validator):
    """Logs the confusion matrix for the given validator using DVCLive."""
    targets = []
    preds = []
    matrix = validator.confusion_matrix.matrix
    names = list(validator.names.values())
    if validator.confusion_matrix.task == "detect":
        names += ["background"]

    for ti, pred in enumerate(matrix.T.astype(int)):
        for pi, num in enumerate(pred):
            targets.extend([names[ti]] * num)
            preds.extend([names[pi]] * num)

    live.log_sklearn_plot("confusion_matrix", targets, preds, name="cf.json", normalized=True)



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

์‚ฌ์ „ ํŠธ๋ ˆ์ด๋‹ ๋ฃจํ‹ด ์ค‘์— ํŠธ๋ ˆ์ด๋‹ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ DVCLive ๋กœ๊ฑฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/dvc.py
def on_pretrain_routine_start(trainer):
    """Initializes DVCLive logger for training metadata during pre-training routine."""
    try:
        global live
        live = dvclive.Live(save_dvc_exp=True, cache_images=True)
        LOGGER.info("DVCLive is detected and auto logging is enabled (run 'yolo settings dvc=False' to disable).")
    except Exception as e:
        LOGGER.warning(f"WARNING โš ๏ธ DVCLive installed but not initialized correctly, not logging this run. {e}")



ultralytics.utils.callbacks.dvc.on_pretrain_routine_end(trainer)

์‚ฌ์ „ ํ›ˆ๋ จ ๋ฃจํ‹ด์ด ๋๋‚  ๋•Œ ํ›ˆ๋ จ ๊ณผ์ •๊ณผ ๊ด€๋ จ๋œ ํ”Œ๋กฏ์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/dvc.py
def on_pretrain_routine_end(trainer):
    """Logs plots related to the training process at the end of the pretraining routine."""
    _log_plots(trainer.plots, "train")



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

DVCLive ๋กœ๊น…์ด ํ™œ์„ฑํ™”๋œ ๊ฒฝ์šฐ ํŠธ๋ ˆ์ด๋‹ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋กœ๊น…ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/dvc.py
def on_train_start(trainer):
    """Logs the training parameters if DVCLive logging is active."""
    if live:
        live.log_params(trainer.args)



ultralytics.utils.callbacks.dvc.on_train_epoch_start(trainer)

๊ฐ ์—ํฌํฌ์˜ ํŠธ๋ ˆ์ด๋‹์ด ์‹œ์ž‘๋  ๋•Œ ์ „์—ญ ๋ณ€์ˆ˜ _training_epoch ๊ฐ’์„ True๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/dvc.py
def on_train_epoch_start(trainer):
    """Sets the global variable _training_epoch value to True at the start of training each epoch."""
    global _training_epoch
    _training_epoch = True



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

ํ•™์Šต ์ง€ํ‘œ์™€ ๋ชจ๋ธ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•˜๊ณ  ๊ฐ ๋งž์ถค ๊ธฐ๊ฐ„์ด ๋๋‚˜๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/dvc.py
def on_fit_epoch_end(trainer):
    """Logs training metrics and model info, and advances to next step on the end of each fit epoch."""
    global _training_epoch
    if live and _training_epoch:
        all_metrics = {**trainer.label_loss_items(trainer.tloss, prefix="train"), **trainer.metrics, **trainer.lr}
        for metric, value in all_metrics.items():
            live.log_metric(metric, value)

        if trainer.epoch == 0:
            from ultralytics.utils.torch_utils import model_info_for_loggers

            for metric, value in model_info_for_loggers(trainer).items():
                live.log_metric(metric, value, plot=False)

        _log_plots(trainer.plots, "train")
        _log_plots(trainer.validator.plots, "val")

        live.next_step()
        _training_epoch = False



ultralytics.utils.callbacks.dvc.on_train_end(trainer)

DVCLive๊ฐ€ ํ™œ์„ฑํ™”๋œ ๊ฒฝ์šฐ ํŠธ๋ ˆ์ด๋‹ ์ข…๋ฃŒ ์‹œ ์ตœ์ƒ์˜ ์ง€ํ‘œ, ํ”Œ๋กฏ ๋ฐ ํ˜ผ๋™ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/dvc.py
def on_train_end(trainer):
    """Logs the best metrics, plots, and confusion matrix at the end of training if DVCLive is active."""
    if live:
        # At the end log the best metrics. It runs validator on the best model internally.
        all_metrics = {**trainer.label_loss_items(trainer.tloss, prefix="train"), **trainer.metrics, **trainer.lr}
        for metric, value in all_metrics.items():
            live.log_metric(metric, value, plot=False)

        _log_plots(trainer.plots, "val")
        _log_plots(trainer.validator.plots, "val")
        _log_confusion_matrix(trainer.validator)

        if trainer.best.exists():
            live.log_artifact(trainer.best, copy=True, type="model")

        live.end()





Created 2023-11-12, Updated 2024-06-02
Authors: glenn-jocher (5), Burhan-Q (1), Laughing-q (1)