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

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

์ฐธ๊ณ 

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



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

์‚ฌ์ „ ํ›ˆ๋ จ ๋ฃจํ‹ด์ด ๋๋‚˜๋ฉด ํ›ˆ๋ จ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ MLflow์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์ด ํ•จ์ˆ˜๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋ฐ ํŠธ๋ ˆ์ด๋„ˆ ์ธ์ˆ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ MLflow ๋กœ๊น…์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ถ”์  URI๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค, ์‹คํ—˜ ์ด๋ฆ„ ๋ฐ ์‹คํ–‰ ์ด๋ฆ„์„ ์„ค์ •ํ•œ ๋‹ค์Œ ์•„์ง ํ™œ์„ฑํ™”๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ MLflow ์‹คํ–‰์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ํŠธ๋ ˆ์ด๋„ˆ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฅผ ๋กœ๊น…ํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
trainer BaseTrainer

๊ธฐ๋กํ•  ์ธ์ˆ˜์™€ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ํŠธ๋ ˆ์ด๋‹ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
๊ธ€๋กœ๋ฒŒ

mlflow: ๋กœ๊น…์— ์‚ฌ์šฉํ•  ๊ฐ€์ ธ์˜จ mlflow ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค.

ํ™˜๊ฒฝ ๋ณ€์ˆ˜

MLFLOW_TRACKING_URI: MLflow ์ถ”์ ์„ ์œ„ํ•œ URI์ž…๋‹ˆ๋‹ค. ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ 'runs/mlflow'์ž…๋‹ˆ๋‹ค. mlflow_experiment_name: MLflow ์‹คํ—˜์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ trainer.args.project์ž…๋‹ˆ๋‹ค. MLFLOW_RUN: MLflow ์‹คํ–‰์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ trainer.args.name์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/mlflow.py
def on_pretrain_routine_end(trainer):
    """
    Log training parameters to MLflow at the end of the pretraining routine.

    This function sets up MLflow logging based on environment variables and trainer arguments. It sets the tracking URI,
    experiment name, and run name, then starts the MLflow run if not already active. It finally logs the parameters
    from the trainer.

    Args:
        trainer (ultralytics.engine.trainer.BaseTrainer): The training object with arguments and parameters to log.

    Global:
        mlflow: The imported mlflow module to use for logging.

    Environment Variables:
        MLFLOW_TRACKING_URI: The URI for MLflow tracking. If not set, defaults to 'runs/mlflow'.
        MLFLOW_EXPERIMENT_NAME: The name of the MLflow experiment. If not set, defaults to trainer.args.project.
        MLFLOW_RUN: The name of the MLflow run. If not set, defaults to trainer.args.name.
    """
    global mlflow

    uri = os.environ.get("MLFLOW_TRACKING_URI") or str(RUNS_DIR / "mlflow")
    LOGGER.debug(f"{PREFIX} tracking uri: {uri}")
    mlflow.set_tracking_uri(uri)

    # Set experiment and run names
    experiment_name = os.environ.get("MLFLOW_EXPERIMENT_NAME") or trainer.args.project or "/Shared/YOLOv8"
    run_name = os.environ.get("MLFLOW_RUN") or trainer.args.name
    mlflow.set_experiment(experiment_name)

    mlflow.autolog()
    try:
        active_run = mlflow.active_run() or mlflow.start_run(run_name=run_name)
        LOGGER.info(f"{PREFIX}logging run_id({active_run.info.run_id}) to {uri}")
        if Path(uri).is_dir():
            LOGGER.info(f"{PREFIX}view at http://127.0.0.1:5000 with 'mlflow server --backend-store-uri {uri}'")
        LOGGER.info(f"{PREFIX}disable with 'yolo settings mlflow=False'")
        mlflow.log_params(dict(trainer.args))
    except Exception as e:
        LOGGER.warning(f"{PREFIX}WARNING โš ๏ธ Failed to initialize: {e}\n" f"{PREFIX}WARNING โš ๏ธ Not tracking this run")



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

๊ฐ ํ›ˆ๋ จ ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค ํ›ˆ๋ จ ๋ฉ”ํŠธ๋ฆญ์„ MLflow์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/mlflow.py
def on_train_epoch_end(trainer):
    """Log training metrics at the end of each train epoch to MLflow."""
    if mlflow:
        mlflow.log_metrics(
            metrics={
                **SANITIZE(trainer.lr),
                **SANITIZE(trainer.label_loss_items(trainer.tloss, prefix="train")),
            },
            step=trainer.epoch,
        )



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

๊ฐ ํ• ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค ํ›ˆ๋ จ ๋ฉ”ํŠธ๋ฆญ์„ MLflow์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/mlflow.py
def on_fit_epoch_end(trainer):
    """Log training metrics at the end of each fit epoch to MLflow."""
    if mlflow:
        mlflow.log_metrics(metrics=SANITIZE(trainer.metrics), step=trainer.epoch)



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

๊ต์œก์ด ๋๋‚˜๋ฉด ๋ชจ๋ธ ์•„ํ‹ฐํŒฉํŠธ๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/mlflow.py
def on_train_end(trainer):
    """Log model artifacts at the end of the training."""
    if mlflow:
        mlflow.log_artifact(str(trainer.best.parent))  # log save_dir/weights directory with best.pt and last.pt
        for f in trainer.save_dir.glob("*"):  # log all other files in save_dir
            if f.suffix in {".png", ".jpg", ".csv", ".pt", ".yaml"}:
                mlflow.log_artifact(str(f))

        mlflow.end_run()
        LOGGER.info(
            f"{PREFIX}results logged to {mlflow.get_tracking_uri()}\n"
            f"{PREFIX}disable with 'yolo settings mlflow=False'"
        )





์ƒ์„ฑ๋จ 2023-11-12, ์—…๋ฐ์ดํŠธ๋จ 2023-12-01
์ž‘์„ฑ์ž: glenn-jocher (4), Laughing-q (1)