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.
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
defon_predict_batch_end(predictor):# Retrieve the batch data_,im0s,_,_=predictor.batch# Ensure that im0s is a listim0s=im0sifisinstance(im0s,list)else[im0s]# Combine the prediction results with the corresponding framespredictor.results=zip(predictor.results,im0s)# Create a YOLO model instancemodel=YOLO(f'yolov8n.pt')# Add the custom callback to the modelmodel.add_callback("on_predict_batch_end",on_predict_batch_end)# Iterate through the results and framesfor(result,frame)inmodel.track/predict():pass
Here are all supported callbacks. See callbacks source code for additional details.
Triggered at the beginning of pre-training routine
Triggered at the end of pre-training routine
Triggered when the training starts
Triggered at the start of each training epoch
Triggered at the start of each training batch
Triggered during the optimizer step
Triggered before gradients are zeroed
Triggered at the end of each training batch
Triggered at the end of each training epoch
Triggered at the end of each fit epoch
Triggered when the model is saved
Triggered when the training process ends
Triggered when model parameters are updated
Triggered when the training process is being cleaned up