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

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

์ฐธ๊ณ 

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



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

์‚ฌ์ „ ํ›ˆ๋ จ ๋ฃจํ‹ด์ด ์‹œ์ž‘๋˜๊ธฐ ์ „์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_pretrain_routine_start(trainer):
    """Called before the pretraining routine starts."""
    pass



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

์‚ฌ์ „ ๊ต์œก ๋ฃจํ‹ด์ด ์ข…๋ฃŒ๋œ ํ›„ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_pretrain_routine_end(trainer):
    """Called after the pretraining routine ends."""
    pass



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

๊ต์œก์ด ์‹œ์ž‘๋˜๋ฉด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_train_start(trainer):
    """Called when the training starts."""
    pass



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

๊ฐ ๊ต์œก ๊ธฐ๊ฐ„์ด ์‹œ์ž‘๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_train_epoch_start(trainer):
    """Called at the start of each training epoch."""
    pass



ultralytics.utils.callbacks.base.on_train_batch_start(trainer)

๊ฐ ๊ต์œก ๋ฐฐ์น˜๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_train_batch_start(trainer):
    """Called at the start of each training batch."""
    pass



ultralytics.utils.callbacks.base.optimizer_step(trainer)

์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def optimizer_step(trainer):
    """Called when the optimizer takes a step."""
    pass



ultralytics.utils.callbacks.base.on_before_zero_grad(trainer)

๊ทธ๋ผ๋ฐ์ด์…˜์ด 0์œผ๋กœ ์„ค์ •๋˜๊ธฐ ์ „์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_before_zero_grad(trainer):
    """Called before the gradients are set to zero."""
    pass



ultralytics.utils.callbacks.base.on_train_batch_end(trainer)

๊ฐ ๊ต์œก ๋ฐฐ์น˜๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_train_batch_end(trainer):
    """Called at the end of each training batch."""
    pass



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

๊ฐ ํ›ˆ๋ จ ๊ธฐ๊ฐ„์ด ๋๋‚  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_train_epoch_end(trainer):
    """Called at the end of each training epoch."""
    pass



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

๊ฐ ํ• ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค(๊ธฐ์ฐจ + ๋ฐธ).

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_fit_epoch_end(trainer):
    """Called at the end of each fit epoch (train + val)."""
    pass



ultralytics.utils.callbacks.base.on_model_save(trainer)

๋ชจ๋ธ์ด ์ €์žฅ๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_model_save(trainer):
    """Called when the model is saved."""
    pass



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

๊ต์œก์ด ์ข…๋ฃŒ๋˜๋ฉด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_train_end(trainer):
    """Called when the training ends."""
    pass



ultralytics.utils.callbacks.base.on_params_update(trainer)

๋ชจ๋ธ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—…๋ฐ์ดํŠธ๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_params_update(trainer):
    """Called when the model parameters are updated."""
    pass



ultralytics.utils.callbacks.base.teardown(trainer)

๊ต์œก ๊ณผ์ •์„ ํ•ด์ฒดํ•˜๋Š” ๋™์•ˆ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def teardown(trainer):
    """Called during the teardown of the training process."""
    pass



ultralytics.utils.callbacks.base.on_val_start(validator)

์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_val_start(validator):
    """Called when the validation starts."""
    pass



ultralytics.utils.callbacks.base.on_val_batch_start(validator)

๊ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋ฐฐ์น˜๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_val_batch_start(validator):
    """Called at the start of each validation batch."""
    pass



ultralytics.utils.callbacks.base.on_val_batch_end(validator)

๊ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋ฐฐ์น˜๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_val_batch_end(validator):
    """Called at the end of each validation batch."""
    pass



ultralytics.utils.callbacks.base.on_val_end(validator)

์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_val_end(validator):
    """Called when the validation ends."""
    pass



ultralytics.utils.callbacks.base.on_predict_start(predictor)

์˜ˆ์ธก์ด ์‹œ์ž‘๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_predict_start(predictor):
    """Called when the prediction starts."""
    pass



ultralytics.utils.callbacks.base.on_predict_batch_start(predictor)

๊ฐ ์˜ˆ์ธก ๋ฐฐ์น˜๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_predict_batch_start(predictor):
    """Called at the start of each prediction batch."""
    pass



ultralytics.utils.callbacks.base.on_predict_batch_end(predictor)

๊ฐ ์˜ˆ์ธก ๋ฐฐ์น˜๊ฐ€ ๋๋‚  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_predict_batch_end(predictor):
    """Called at the end of each prediction batch."""
    pass



ultralytics.utils.callbacks.base.on_predict_postprocess_end(predictor)

์˜ˆ์ธก์˜ ์‚ฌํ›„ ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚œ ํ›„ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_predict_postprocess_end(predictor):
    """Called after the post-processing of the prediction ends."""
    pass



ultralytics.utils.callbacks.base.on_predict_end(predictor)

์˜ˆ์ธก์ด ์ข…๋ฃŒ๋˜๋ฉด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_predict_end(predictor):
    """Called when the prediction ends."""
    pass



ultralytics.utils.callbacks.base.on_export_start(exporter)

๋ชจ๋ธ ๋‚ด๋ณด๋‚ด๊ธฐ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_export_start(exporter):
    """Called when the model export starts."""
    pass



ultralytics.utils.callbacks.base.on_export_end(exporter)

๋ชจ๋ธ ๋‚ด๋ณด๋‚ด๊ธฐ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def on_export_end(exporter):
    """Called when the model export ends."""
    pass



ultralytics.utils.callbacks.base.get_default_callbacks()

๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋ชฉ๋ก์ด ํฌํ•จ๋œ default_callbacks ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ณต์‚ฌ๋ณธ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
defaultdict

default_์ฝœ๋ฐฑ ๋ฐ ๋นˆ ๋ชฉ๋ก์˜ ํ‚ค๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” defaultdict์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def get_default_callbacks():
    """
    Return a copy of the default_callbacks dictionary with lists as default values.

    Returns:
        (defaultdict): A defaultdict with keys from default_callbacks and empty lists as default values.
    """
    return defaultdict(list, deepcopy(default_callbacks))



ultralytics.utils.callbacks.base.add_integration_callbacks(instance)

๋‹ค์–‘ํ•œ ์†Œ์Šค์˜ ํ†ตํ•ฉ ์ฝœ๋ฐฑ์„ ์ธ์Šคํ„ด์Šค์˜ ์ฝœ๋ฐฑ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

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

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
instance (Trainer, Predictor, Validator, Exporter)

์ฝœ๋ฐฑ ๋ชฉ๋ก์˜ ๋”•์…”๋„ˆ๋ฆฌ์ธ 'callbacks' ์†์„ฑ์„ ๊ฐ€์ง„ ๊ฐ์ฒด ์†์„ฑ์„ ๊ฐ€์ง„ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/callbacks/base.py
def add_integration_callbacks(instance):
    """
    Add integration callbacks from various sources to the instance's callbacks.

    Args:
        instance (Trainer, Predictor, Validator, Exporter): An object with a 'callbacks' attribute that is a dictionary
            of callback lists.
    """

    # Load HUB callbacks
    from .hub import callbacks as hub_cb

    callbacks_list = [hub_cb]

    # Load training callbacks
    if "Trainer" in instance.__class__.__name__:
        from .clearml import callbacks as clear_cb
        from .comet import callbacks as comet_cb
        from .dvc import callbacks as dvc_cb
        from .mlflow import callbacks as mlflow_cb
        from .neptune import callbacks as neptune_cb
        from .raytune import callbacks as tune_cb
        from .tensorboard import callbacks as tb_cb
        from .wb import callbacks as wb_cb

        callbacks_list.extend([clear_cb, comet_cb, dvc_cb, mlflow_cb, neptune_cb, tune_cb, tb_cb, wb_cb])

    # Add the callbacks to the callbacks dictionary
    for callbacks in callbacks_list:
        for k, v in callbacks.items():
            if v not in instance.callbacks[k]:
                instance.callbacks[k].append(v)





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