Skip to content

Register now for

# Reference for `ultralytics/utils/callbacks/clearml.py`

Note

This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/callbacks/clearml.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!

## `` ultralytics.utils.callbacks.clearml._log_debug_samples

    _log_debug_samples(files, title: str = 'Debug Samples') -> None

Log files (images) as debug samples in the ClearML task.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`files` | `list[Path]` | A list of file paths in PosixPath format. | required
`title` | `str` | A title that groups together images with the same values. | `'Debug Samples'`

Source code in `ultralytics/utils/callbacks/clearml.py`

    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    def _log_debug_samples(files, title: str = "Debug Samples") -> None:
        """
        Log files (images) as debug samples in the ClearML task.

        Args:
            files (list[Path]): A list of file paths in PosixPath format.
            title (str): A title that groups together images with the same values.
        """
        import re

        if task := Task.current_task():
            for f in files:
                if f.exists():
                    it = re.search(r"_batch(\d+)", f.name)
                    iteration = int(it.groups()[0]) if it else 0
                    task.get_logger().report_image(
                        title=title, series=f.name.replace(it.group(), ""), local_path=str(f), iteration=iteration
                    )

## `` ultralytics.utils.callbacks.clearml._log_plot

    _log_plot(title: str, plot_path: str) -> None

Log an image as a plot in the plot section of ClearML.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`title` | `str` | The title of the plot. | required
`plot_path` | `str` | The path to the saved image file. | required

Source code in `ultralytics/utils/callbacks/clearml.py`

    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    def _log_plot(title: str, plot_path: str) -> None:
        """
        Log an image as a plot in the plot section of ClearML.

        Args:
            title (str): The title of the plot.
            plot_path (str): The path to the saved image file.
        """
        import matplotlib.image as mpimg
        import matplotlib.pyplot as plt

        img = mpimg.imread(plot_path)
        fig = plt.figure()
        ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect="auto", xticks=[], yticks=[])  # no ticks
        ax.imshow(img)

        Task.current_task().get_logger().report_matplotlib_figure(
            title=title, series="", figure=fig, report_interactive=False
        )

## `` ultralytics.utils.callbacks.clearml.on_pretrain_routine_start

    on_pretrain_routine_start(trainer) -> None

Initialize and connect ClearML task at the start of pretraining routine.

Source code in `ultralytics/utils/callbacks/clearml.py`

    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    def on_pretrain_routine_start(trainer) -> None:
        """Initialize and connect ClearML task at the start of pretraining routine."""
        try:
            if task := Task.current_task():
                # WARNING: make sure the automatic pytorch and matplotlib bindings are disabled!
                # We are logging these plots and model files manually in the integration
                from clearml.binding.frameworks.pytorch_bind import PatchPyTorchModelIO
                from clearml.binding.matplotlib_bind import PatchedMatplotlib

                PatchPyTorchModelIO.update_current_task(None)
                PatchedMatplotlib.update_current_task(None)
            else:
                task = Task.init(
                    project_name=trainer.args.project or "Ultralytics",
                    task_name=trainer.args.name,
                    tags=["Ultralytics"],
                    output_uri=True,
                    reuse_last_task_id=False,
                    auto_connect_frameworks={"pytorch": False, "matplotlib": False},
                )
                LOGGER.warning(
                    "ClearML Initialized a new task. If you want to run remotely, "
                    "please add clearml-init and connect your arguments before initializing YOLO."
                )
            task.connect(vars(trainer.args), name="General")
        except Exception as e:
            LOGGER.warning(f"ClearML installed but not initialized correctly, not logging this run. {e}")

## `` ultralytics.utils.callbacks.clearml.on_train_epoch_end

    on_train_epoch_end(trainer) -> None

Log debug samples for the first epoch and report current training progress.

Source code in `ultralytics/utils/callbacks/clearml.py`

    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    def on_train_epoch_end(trainer) -> None:
        """Log debug samples for the first epoch and report current training progress."""
        if task := Task.current_task():
            # Log debug samples for first epoch only
            if trainer.epoch == 1:
                _log_debug_samples(sorted(trainer.save_dir.glob("train_batch*.jpg")), "Mosaic")
            # Report the current training progress
            for k, v in trainer.label_loss_items(trainer.tloss, prefix="train").items():
                task.get_logger().report_scalar("train", k, v, iteration=trainer.epoch)
            for k, v in trainer.lr.items():
                task.get_logger().report_scalar("lr", k, v, iteration=trainer.epoch)

## `` ultralytics.utils.callbacks.clearml.on_fit_epoch_end

    on_fit_epoch_end(trainer) -> None

Report model information and metrics to logger at the end of an epoch.

Source code in `ultralytics/utils/callbacks/clearml.py`

    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    def on_fit_epoch_end(trainer) -> None:
        """Report model information and metrics to logger at the end of an epoch."""
        if task := Task.current_task():
            # Report epoch time and validation metrics
            task.get_logger().report_scalar(
                title="Epoch Time", series="Epoch Time", value=trainer.epoch_time, iteration=trainer.epoch
            )
            for k, v in trainer.metrics.items():
                title = k.split("/")[0]
                task.get_logger().report_scalar(title, k, v, iteration=trainer.epoch)
            if trainer.epoch == 0:
                from ultralytics.utils.torch_utils import model_info_for_loggers

                for k, v in model_info_for_loggers(trainer).items():
                    task.get_logger().report_single_value(k, v)

## `` ultralytics.utils.callbacks.clearml.on_val_end

    on_val_end(validator) -> None

Log validation results including labels and predictions.

Source code in `ultralytics/utils/callbacks/clearml.py`

    117
    118
    119
    120
    121
    def on_val_end(validator) -> None:
        """Log validation results including labels and predictions."""
        if Task.current_task():
            # Log validation labels and predictions
            _log_debug_samples(sorted(validator.save_dir.glob("val*.jpg")), "Validation")

## `` ultralytics.utils.callbacks.clearml.on_train_end

    on_train_end(trainer) -> None

Log final model and training results on training completion.

Source code in `ultralytics/utils/callbacks/clearml.py`

    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    def on_train_end(trainer) -> None:
        """Log final model and training results on training completion."""
        if task := Task.current_task():
            # Log final results, confusion matrix and PR plots
            files = [
                "results.png",
                "confusion_matrix.png",
                "confusion_matrix_normalized.png",
                *(f"{x}_curve.png" for x in ("F1", "PR", "P", "R")),
            ]
            files = [(trainer.save_dir / f) for f in files if (trainer.save_dir / f).exists()]  # filter existing files
            for f in files:
                _log_plot(title=f.stem, plot_path=f)
            # Report final metrics
            for k, v in trainer.validator.metrics.results_dict.items():
                task.get_logger().report_single_value(k, v)
            # Log the final model
            task.update_output_model(model_path=str(trainer.best), model_name=trainer.args.name, auto_delete_file=False)

📅 Created 1 year ago ✏️ Updated 1 year ago