Reference for ultralytics/engine/predictor.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/predictor.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.engine.predictor.BasePredictor
BasePredictor.
A base class for creating predictors.
Attributes:
Name | Type | Description |
---|---|---|
args | SimpleNamespace | Configuration for the predictor. |
save_dir | Path | Directory to save results. |
done_warmup | bool | Whether the predictor has finished setup. |
model | Module | Model used for prediction. |
data | dict | Data configuration. |
device | device | Device used for prediction. |
dataset | Dataset | Dataset used for prediction. |
vid_writer | dict | Dictionary of {save_path: video_writer, ...} writer for saving video output. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg | str | Path to a configuration file. Defaults to DEFAULT_CFG. | DEFAULT_CFG |
overrides | dict | Configuration overrides. Defaults to None. | None |
Source code in ultralytics/engine/predictor.py
__call__
Performs inference on an image or stream.
Source code in ultralytics/engine/predictor.py
add_callback
inference
Runs inference on a given image using the specified model and arguments.
Source code in ultralytics/engine/predictor.py
postprocess
pre_transform
Pre-transform input image before inference.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im | List(np.ndarray | (N, 3, h, w) for tensor, [(h, w, 3) x N] for list. | required |
Returns:
Type | Description |
---|---|
list | A list of transformed images. |
Source code in ultralytics/engine/predictor.py
predict_cli
Method used for Command Line Interface (CLI) prediction.
This function is designed to run predictions using the CLI. It sets up the source and model, then processes the inputs in a streaming manner. This method ensures that no outputs accumulate in memory by consuming the generator without storing results.
Note
Do not modify this function or remove the generator. The generator ensures that no outputs are accumulated in memory, which is critical for preventing memory issues during long-running predictions.
Source code in ultralytics/engine/predictor.py
preprocess
Prepares input image before inference.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im | torch.Tensor | List(np.ndarray | BCHW for tensor, [(HWC) x B] for list. | required |
Source code in ultralytics/engine/predictor.py
run_callbacks
save_predicted_images
Save video predictions as mp4 at specified path.
Source code in ultralytics/engine/predictor.py
setup_model
Initialize YOLO model with given parameters and set it to evaluation mode.
Source code in ultralytics/engine/predictor.py
setup_source
Sets up source and inference mode.
Source code in ultralytics/engine/predictor.py
show
Display an image in a window using the OpenCV imshow function.
Source code in ultralytics/engine/predictor.py
stream_inference
Streams real-time inference on camera feed and saves results to file.
Source code in ultralytics/engine/predictor.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
|
write_results
Write inference results to a file or directory.