yolov10/docs/callbacks.md
Glenn Jocher 9047d737f4
ultralytics 8.0.40 TensorRT metadata and Results visualizer (#1014)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Laughing <61612323+Laughing-q@users.noreply.github.com>
Co-authored-by: Bogdan Gheorghe <112427971+bogdan-galileo@users.noreply.github.com>
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: Jaap van de Loosdrecht <jaap@vdlmv.nl>
Co-authored-by: Noobtoss <96134731+Noobtoss@users.noreply.github.com>
Co-authored-by: nerdyespresso <106761627+nerdyespresso@users.noreply.github.com>
2023-02-17 20:06:06 +01:00

1.5 KiB

Callbacks

Ultralytics framework supports callbacks as entry points in strategic stages of train, val, export, and predict modes. Each callback accepts a Trainer, Validator, or Predictor object depending on the operation type. All properties of these objects can be found in Reference section of the docs.

Examples

Returning additional information with Prediction

In this example, we want to return the original frame with each result object. Here's how we can do that

def on_predict_batch_end(predictor):
    # results -> List[batch_size]
    _, _, im0s, _, _ = predictor.batch
    im0s = im0s if isinstance(im0s, list) else [im0s]
    predictor.results = zip(predictor.results, im0s)

model = YOLO(f"yolov8n.pt")
model.add_callback("on_predict_batch_end", on_predict_batch_end)
for (result, frame) in model.track/predict():
    pass

All callbacks

Here are all supported callbacks.

Trainer

on_pretrain_routine_start

on_pretrain_routine_end

on_train_start

on_train_epoch_start

on_train_batch_start

optimizer_step

on_before_zero_grad

on_train_batch_end

on_train_epoch_end

on_fit_epoch_end

on_model_save

on_train_end

on_params_update

teardown

Validator

on_val_start

on_val_batch_start

on_val_batch_end

on_val_end

Predictor

on_predict_start

on_predict_batch_start

on_predict_postprocess_end

on_predict_batch_end

on_predict_end

Exporter

on_export_start

on_export_end