mirror of
https://gitee.com/nanjing-yimao-information/ieemoo-ai-gift.git
synced 2025-08-23 23:50:25 +00:00
update
This commit is contained in:
101
docs/en/usage/callbacks.md
Normal file
101
docs/en/usage/callbacks.md
Normal file
@ -0,0 +1,101 @@
|
||||
---
|
||||
comments: true
|
||||
description: Learn how to utilize callbacks in the Ultralytics framework during train, val, export, and predict modes for enhanced functionality.
|
||||
keywords: Ultralytics, YOLO, callbacks guide, training callback, validation callback, export callback, prediction callback
|
||||
---
|
||||
|
||||
## 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.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/GsXGnb-A4Kc?start=67"
|
||||
title="YouTube video player" frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||
allowfullscreen>
|
||||
</iframe>
|
||||
<br>
|
||||
<strong>Watch:</strong> Mastering Ultralytics YOLOv8: Callbacks
|
||||
</p>
|
||||
|
||||
## 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
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def on_predict_batch_end(predictor):
|
||||
# Retrieve the batch data
|
||||
_, image, _, _ = predictor.batch
|
||||
|
||||
# Ensure that image is a list
|
||||
image = image if isinstance(image, list) else [image]
|
||||
|
||||
# Combine the prediction results with the corresponding frames
|
||||
predictor.results = zip(predictor.results, image)
|
||||
|
||||
|
||||
# Create a YOLO model instance
|
||||
model = YOLO(f'yolov8n.pt')
|
||||
|
||||
# Add the custom callback to the model
|
||||
model.add_callback("on_predict_batch_end", on_predict_batch_end)
|
||||
|
||||
# Iterate through the results and frames
|
||||
for (result, frame) in model.predict(): # or model.track()
|
||||
pass
|
||||
```
|
||||
|
||||
## All callbacks
|
||||
|
||||
Here are all supported callbacks. See callbacks [source code](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/callbacks/base.py) for additional details.
|
||||
|
||||
### Trainer Callbacks
|
||||
|
||||
| Callback | Description |
|
||||
|-----------------------------|---------------------------------------------------------|
|
||||
| `on_pretrain_routine_start` | Triggered at the beginning of pre-training routine |
|
||||
| `on_pretrain_routine_end` | Triggered at the end of pre-training routine |
|
||||
| `on_train_start` | Triggered when the training starts |
|
||||
| `on_train_epoch_start` | Triggered at the start of each training epoch |
|
||||
| `on_train_batch_start` | Triggered at the start of each training batch |
|
||||
| `optimizer_step` | Triggered during the optimizer step |
|
||||
| `on_before_zero_grad` | Triggered before gradients are zeroed |
|
||||
| `on_train_batch_end` | Triggered at the end of each training batch |
|
||||
| `on_train_epoch_end` | Triggered at the end of each training epoch |
|
||||
| `on_fit_epoch_end` | Triggered at the end of each fit epoch |
|
||||
| `on_model_save` | Triggered when the model is saved |
|
||||
| `on_train_end` | Triggered when the training process ends |
|
||||
| `on_params_update` | Triggered when model parameters are updated |
|
||||
| `teardown` | Triggered when the training process is being cleaned up |
|
||||
|
||||
### Validator Callbacks
|
||||
|
||||
| Callback | Description |
|
||||
|----------------------|-------------------------------------------------|
|
||||
| `on_val_start` | Triggered when the validation starts |
|
||||
| `on_val_batch_start` | Triggered at the start of each validation batch |
|
||||
| `on_val_batch_end` | Triggered at the end of each validation batch |
|
||||
| `on_val_end` | Triggered when the validation ends |
|
||||
|
||||
### Predictor Callbacks
|
||||
|
||||
| Callback | Description |
|
||||
|------------------------------|---------------------------------------------------|
|
||||
| `on_predict_start` | Triggered when the prediction process starts |
|
||||
| `on_predict_batch_start` | Triggered at the start of each prediction batch |
|
||||
| `on_predict_postprocess_end` | Triggered at the end of prediction postprocessing |
|
||||
| `on_predict_batch_end` | Triggered at the end of each prediction batch |
|
||||
| `on_predict_end` | Triggered when the prediction process ends |
|
||||
|
||||
### Exporter Callbacks
|
||||
|
||||
| Callback | Description |
|
||||
|-------------------|------------------------------------------|
|
||||
| `on_export_start` | Triggered when the export process starts |
|
||||
| `on_export_end` | Triggered when the export process ends |
|
270
docs/en/usage/cfg.md
Normal file
270
docs/en/usage/cfg.md
Normal file
@ -0,0 +1,270 @@
|
||||
---
|
||||
comments: true
|
||||
description: Master YOLOv8 settings and hyperparameters for improved model performance. Learn to use YOLO CLI commands, adjust training settings, and optimize YOLO tasks & modes.
|
||||
keywords: YOLOv8, settings, hyperparameters, YOLO CLI commands, YOLO tasks, YOLO modes, Ultralytics documentation, model optimization, YOLOv8 training
|
||||
---
|
||||
|
||||
YOLO settings and hyperparameters play a critical role in the model's performance, speed, and accuracy. These settings and hyperparameters can affect the model's behavior at various stages of the model development process, including training, validation, and prediction.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/GsXGnb-A4Kc?start=87"
|
||||
title="YouTube video player" frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||
allowfullscreen>
|
||||
</iframe>
|
||||
<br>
|
||||
<strong>Watch:</strong> Mastering Ultralytics YOLOv8: Configuration
|
||||
</p>
|
||||
|
||||
Ultralytics commands use the following syntax:
|
||||
|
||||
!!! Example
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo TASK MODE ARGS
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a YOLOv8 model from a pre-trained weights file
|
||||
model = YOLO('yolov8n.pt')
|
||||
|
||||
# Run MODE mode using the custom arguments ARGS (guess TASK)
|
||||
model.MODE(ARGS)
|
||||
```
|
||||
|
||||
Where:
|
||||
|
||||
- `TASK` (optional) is one of ([detect](../tasks/detect.md), [segment](../tasks/segment.md), [classify](../tasks/classify.md), [pose](../tasks/pose.md))
|
||||
- `MODE` (required) is one of ([train](../modes/train.md), [val](../modes/val.md), [predict](../modes/predict.md), [export](../modes/export.md), [track](../modes/track.md))
|
||||
- `ARGS` (optional) are `arg=value` pairs like `imgsz=640` that override defaults.
|
||||
|
||||
Default `ARG` values are defined on this page from the `cfg/defaults.yaml` [file](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/default.yaml).
|
||||
|
||||
#### Tasks
|
||||
|
||||
YOLO models can be used for a variety of tasks, including detection, segmentation, classification and pose. These tasks differ in the type of output they produce and the specific problem they are designed to solve.
|
||||
|
||||
- **Detect**: For identifying and localizing objects or regions of interest in an image or video.
|
||||
- **Segment**: For dividing an image or video into regions or pixels that correspond to different objects or classes.
|
||||
- **Classify**: For predicting the class label of an input image.
|
||||
- **Pose**: For identifying objects and estimating their keypoints in an image or video.
|
||||
- **OBB**: Oriented (i.e. rotated) bounding boxes suitable for satellite or medical imagery.
|
||||
|
||||
| Argument | Default | Description |
|
||||
|----------|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `task` | `'detect'` | Specifies the YOLO task to be executed. Options include `detect` for object detection, `segment` for segmentation, `classify` for classification, `pose` for pose estimation and `OBB` for oriented bounding boxes. Each task is tailored to specific types of output and problems within image and video analysis. |
|
||||
|
||||
[Tasks Guide](../tasks/index.md){ .md-button }
|
||||
|
||||
#### Modes
|
||||
|
||||
YOLO models can be used in different modes depending on the specific problem you are trying to solve. These modes include:
|
||||
|
||||
- **Train**: For training a YOLOv8 model on a custom dataset.
|
||||
- **Val**: For validating a YOLOv8 model after it has been trained.
|
||||
- **Predict**: For making predictions using a trained YOLOv8 model on new images or videos.
|
||||
- **Export**: For exporting a YOLOv8 model to a format that can be used for deployment.
|
||||
- **Track**: For tracking objects in real-time using a YOLOv8 model.
|
||||
- **Benchmark**: For benchmarking YOLOv8 exports (ONNX, TensorRT, etc.) speed and accuracy.
|
||||
|
||||
| Argument | Default | Description |
|
||||
|----------|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `mode` | `'train'` | Specifies the mode in which the YOLO model operates. Options are `train` for model training, `val` for validation, `predict` for inference on new data, `export` for model conversion to deployment formats, `track` for object tracking, and `benchmark` for performance evaluation. Each mode is designed for different stages of the model lifecycle, from development through deployment. |
|
||||
|
||||
[Modes Guide](../modes/index.md){ .md-button }
|
||||
|
||||
## Train Settings
|
||||
|
||||
The training settings for YOLO models encompass various hyperparameters and configurations used during the training process. These settings influence the model's performance, speed, and accuracy. Key training settings include batch size, learning rate, momentum, and weight decay. Additionally, the choice of optimizer, loss function, and training dataset composition can impact the training process. Careful tuning and experimentation with these settings are crucial for optimizing performance.
|
||||
|
||||
| Argument | Default | Description |
|
||||
|-------------------|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `model` | `None` | Specifies the model file for training. Accepts a path to either a `.pt` pretrained model or a `.yaml` configuration file. Essential for defining the model structure or initializing weights. |
|
||||
| `data` | `None` | Path to the dataset configuration file (e.g., `coco128.yaml`). This file contains dataset-specific parameters, including paths to training and validation data, class names, and number of classes. |
|
||||
| `epochs` | `100` | Total number of training epochs. Each epoch represents a full pass over the entire dataset. Adjusting this value can affect training duration and model performance. |
|
||||
| `time` | `None` | Maximum training time in hours. If set, this overrides the `epochs` argument, allowing training to automatically stop after the specified duration. Useful for time-constrained training scenarios. |
|
||||
| `patience` | `100` | Number of epochs to wait without improvement in validation metrics before early stopping the training. Helps prevent overfitting by stopping training when performance plateaus. |
|
||||
| `batch` | `16` | Batch size for training, indicating how many images are processed before the model's internal parameters are updated. AutoBatch (`batch=-1`) dynamically adjusts the batch size based on GPU memory availability. |
|
||||
| `imgsz` | `640` | Target image size for training. All images are resized to this dimension before being fed into the model. Affects model accuracy and computational complexity. |
|
||||
| `save` | `True` | Enables saving of training checkpoints and final model weights. Useful for resuming training or model deployment. |
|
||||
| `save_period` | `-1` | Frequency of saving model checkpoints, specified in epochs. A value of -1 disables this feature. Useful for saving interim models during long training sessions. |
|
||||
| `cache` | `False` | Enables caching of dataset images in memory (`True`/`ram`), on disk (`disk`), or disables it (`False`). Improves training speed by reducing disk I/O at the cost of increased memory usage. |
|
||||
| `device` | `None` | Specifies the computational device(s) for training: a single GPU (`device=0`), multiple GPUs (`device=0,1`), CPU (`device=cpu`), or MPS for Apple silicon (`device=mps`). |
|
||||
| `workers` | `8` | Number of worker threads for data loading (per `RANK` if Multi-GPU training). Influences the speed of data preprocessing and feeding into the model, especially useful in multi-GPU setups. |
|
||||
| `project` | `None` | Name of the project directory where training outputs are saved. Allows for organized storage of different experiments. |
|
||||
| `name` | `None` | Name of the training run. Used for creating a subdirectory within the project folder, where training logs and outputs are stored. |
|
||||
| `exist_ok` | `False` | If True, allows overwriting of an existing project/name directory. Useful for iterative experimentation without needing to manually clear previous outputs. |
|
||||
| `pretrained` | `True` | Determines whether to start training from a pretrained model. Can be a boolean value or a string path to a specific model from which to load weights. Enhances training efficiency and model performance. |
|
||||
| `optimizer` | `'auto'` | Choice of optimizer for training. Options include `SGD`, `Adam`, `AdamW`, `NAdam`, `RAdam`, `RMSProp` etc., or `auto` for automatic selection based on model configuration. Affects convergence speed and stability. |
|
||||
| `verbose` | `False` | Enables verbose output during training, providing detailed logs and progress updates. Useful for debugging and closely monitoring the training process. |
|
||||
| `seed` | `0` | Sets the random seed for training, ensuring reproducibility of results across runs with the same configurations. |
|
||||
| `deterministic` | `True` | Forces deterministic algorithm use, ensuring reproducibility but may affect performance and speed due to the restriction on non-deterministic algorithms. |
|
||||
| `single_cls` | `False` | Treats all classes in multi-class datasets as a single class during training. Useful for binary classification tasks or when focusing on object presence rather than classification. |
|
||||
| `rect` | `False` | Enables rectangular training, optimizing batch composition for minimal padding. Can improve efficiency and speed but may affect model accuracy. |
|
||||
| `cos_lr` | `False` | Utilizes a cosine learning rate scheduler, adjusting the learning rate following a cosine curve over epochs. Helps in managing learning rate for better convergence. |
|
||||
| `close_mosaic` | `10` | Disables mosaic data augmentation in the last N epochs to stabilize training before completion. Setting to 0 disables this feature. |
|
||||
| `resume` | `False` | Resumes training from the last saved checkpoint. Automatically loads model weights, optimizer state, and epoch count, continuing training seamlessly. |
|
||||
| `amp` | `True` | Enables Automatic Mixed Precision (AMP) training, reducing memory usage and possibly speeding up training with minimal impact on accuracy. |
|
||||
| `fraction` | `1.0` | Specifies the fraction of the dataset to use for training. Allows for training on a subset of the full dataset, useful for experiments or when resources are limited. |
|
||||
| `profile` | `False` | Enables profiling of ONNX and TensorRT speeds during training, useful for optimizing model deployment. |
|
||||
| `freeze` | `None` | Freezes the first N layers of the model or specified layers by index, reducing the number of trainable parameters. Useful for fine-tuning or transfer learning. |
|
||||
| `lr0` | `0.01` | Initial learning rate (i.e. `SGD=1E-2`, `Adam=1E-3`) . Adjusting this value is crucial for the optimization process, influencing how rapidly model weights are updated. |
|
||||
| `lrf` | `0.01` | Final learning rate as a fraction of the initial rate = (`lr0 * lrf`), used in conjunction with schedulers to adjust the learning rate over time. |
|
||||
| `momentum` | `0.937` | Momentum factor for SGD or beta1 for Adam optimizers, influencing the incorporation of past gradients in the current update. |
|
||||
| `weight_decay` | `0.0005` | L2 regularization term, penalizing large weights to prevent overfitting. |
|
||||
| `warmup_epochs` | `3.0` | Number of epochs for learning rate warmup, gradually increasing the learning rate from a low value to the initial learning rate to stabilize training early on. |
|
||||
| `warmup_momentum` | `0.8` | Initial momentum for warmup phase, gradually adjusting to the set momentum over the warmup period. |
|
||||
| `warmup_bias_lr` | `0.1` | Learning rate for bias parameters during the warmup phase, helping stabilize model training in the initial epochs. |
|
||||
| `box` | `7.5` | Weight of the box loss component in the loss function, influencing how much emphasis is placed on accurately predicting bounding box coordinates. |
|
||||
| `cls` | `0.5` | Weight of the classification loss in the total loss function, affecting the importance of correct class prediction relative to other components. |
|
||||
| `dfl` | `1.5` | Weight of the distribution focal loss, used in certain YOLO versions for fine-grained classification. |
|
||||
| `pose` | `12.0` | Weight of the pose loss in models trained for pose estimation, influencing the emphasis on accurately predicting pose keypoints. |
|
||||
| `kobj` | `2.0` | Weight of the keypoint objectness loss in pose estimation models, balancing detection confidence with pose accuracy. |
|
||||
| `label_smoothing` | `0.0` | Applies label smoothing, softening hard labels to a mix of the target label and a uniform distribution over labels, can improve generalization. |
|
||||
| `nbs` | `64` | Nominal batch size for normalization of loss. |
|
||||
| `overlap_mask` | `True` | Determines whether segmentation masks should overlap during training, applicable in instance segmentation tasks. |
|
||||
| `mask_ratio` | `4` | Downsample ratio for segmentation masks, affecting the resolution of masks used during training. |
|
||||
| `dropout` | `0.0` | Dropout rate for regularization in classification tasks, preventing overfitting by randomly omitting units during training. |
|
||||
| `val` | `True` | Enables validation during training, allowing for periodic evaluation of model performance on a separate dataset. |
|
||||
| `plots` | `False` | Generates and saves plots of training and validation metrics, as well as prediction examples, providing visual insights into model performance and learning progression. |
|
||||
|
||||
[Train Guide](../modes/train.md){ .md-button }
|
||||
|
||||
## Predict Settings
|
||||
|
||||
The prediction settings for YOLO models encompass a range of hyperparameters and configurations that influence the model's performance, speed, and accuracy during inference on new data. Careful tuning and experimentation with these settings are essential to achieve optimal performance for a specific task. Key settings include the confidence threshold, Non-Maximum Suppression (NMS) threshold, and the number of classes considered. Additional factors affecting the prediction process are input data size and format, the presence of supplementary features such as masks or multiple labels per box, and the particular task the model is employed for.
|
||||
|
||||
Inference arguments:
|
||||
|
||||
| Argument | Type | Default | Description |
|
||||
|-----------------|----------------|------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `source` | `str` | `'ultralytics/assets'` | Specifies the data source for inference. Can be an image path, video file, directory, URL, or device ID for live feeds. Supports a wide range of formats and sources, enabling flexible application across different types of input. |
|
||||
| `conf` | `float` | `0.25` | Sets the minimum confidence threshold for detections. Objects detected with confidence below this threshold will be disregarded. Adjusting this value can help reduce false positives. |
|
||||
| `iou` | `float` | `0.7` | Intersection Over Union (IoU) threshold for Non-Maximum Suppression (NMS). Lower values result in fewer detections by eliminating overlapping boxes, useful for reducing duplicates. |
|
||||
| `imgsz` | `int or tuple` | `640` | Defines the image size for inference. Can be a single integer `640` for square resizing or a (height, width) tuple. Proper sizing can improve detection accuracy and processing speed. |
|
||||
| `half` | `bool` | `False` | Enables half-precision (FP16) inference, which can speed up model inference on supported GPUs with minimal impact on accuracy. |
|
||||
| `device` | `str` | `None` | Specifies the device for inference (e.g., `cpu`, `cuda:0` or `0`). Allows users to select between CPU, a specific GPU, or other compute devices for model execution. |
|
||||
| `max_det` | `int` | `300` | Maximum number of detections allowed per image. Limits the total number of objects the model can detect in a single inference, preventing excessive outputs in dense scenes. |
|
||||
| `vid_stride` | `int` | `1` | Frame stride for video inputs. Allows skipping frames in videos to speed up processing at the cost of temporal resolution. A value of 1 processes every frame, higher values skip frames. |
|
||||
| `stream_buffer` | `bool` | `False` | Determines if all frames should be buffered when processing video streams (`True`), or if the model should return the most recent frame (`False`). Useful for real-time applications. |
|
||||
| `visualize` | `bool` | `False` | Activates visualization of model features during inference, providing insights into what the model is "seeing". Useful for debugging and model interpretation. |
|
||||
| `augment` | `bool` | `False` | Enables test-time augmentation (TTA) for predictions, potentially improving detection robustness at the cost of inference speed. |
|
||||
| `agnostic_nms` | `bool` | `False` | Enables class-agnostic Non-Maximum Suppression (NMS), which merges overlapping boxes of different classes. Useful in multi-class detection scenarios where class overlap is common. |
|
||||
| `classes` | `list[int]` | `None` | Filters predictions to a set of class IDs. Only detections belonging to the specified classes will be returned. Useful for focusing on relevant objects in multi-class detection tasks. |
|
||||
| `retina_masks` | `bool` | `False` | Uses high-resolution segmentation masks if available in the model. This can enhance mask quality for segmentation tasks, providing finer detail. |
|
||||
| `embed` | `list[int]` | `None` | Specifies the layers from which to extract feature vectors or embeddings. Useful for downstream tasks like clustering or similarity search. |
|
||||
|
||||
Visualization arguments:
|
||||
|
||||
| Argument | Type | Default | Description |
|
||||
|---------------|---------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `show` | `bool` | `False` | If `True`, displays the annotated images or videos in a window. Useful for immediate visual feedback during development or testing. |
|
||||
| `save` | `bool` | `False` | Enables saving of the annotated images or videos to file. Useful for documentation, further analysis, or sharing results. |
|
||||
| `save_frames` | `bool` | `False` | When processing videos, saves individual frames as images. Useful for extracting specific frames or for detailed frame-by-frame analysis. |
|
||||
| `save_txt` | `bool` | `False` | Saves detection results in a text file, following the format `[class] [x_center] [y_center] [width] [height] [confidence]`. Useful for integration with other analysis tools. |
|
||||
| `save_conf` | `bool` | `False` | Includes confidence scores in the saved text files. Enhances the detail available for post-processing and analysis. |
|
||||
| `save_crop` | `bool` | `False` | Saves cropped images of detections. Useful for dataset augmentation, analysis, or creating focused datasets for specific objects. |
|
||||
| `show_labels` | `bool` | `True` | Displays labels for each detection in the visual output. Provides immediate understanding of detected objects. |
|
||||
| `show_conf` | `bool` | `True` | Displays the confidence score for each detection alongside the label. Gives insight into the model's certainty for each detection. |
|
||||
| `show_boxes` | `bool` | `True` | Draws bounding boxes around detected objects. Essential for visual identification and location of objects in images or video frames. |
|
||||
| `line_width` | `None or int` | `None` | Specifies the line width of bounding boxes. If `None`, the line width is automatically adjusted based on the image size. Provides visual customization for clarity. |
|
||||
|
||||
[Predict Guide](../modes/predict.md){ .md-button }
|
||||
|
||||
## Validation Settings
|
||||
|
||||
The val (validation) settings for YOLO models involve various hyperparameters and configurations used to evaluate the model's performance on a validation dataset. These settings influence the model's performance, speed, and accuracy. Common YOLO validation settings include batch size, validation frequency during training, and performance evaluation metrics. Other factors affecting the validation process include the validation dataset's size and composition, as well as the specific task the model is employed for.
|
||||
|
||||
| Argument | Type | Default | Description |
|
||||
|---------------|---------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `data` | `str` | `None` | Specifies the path to the dataset configuration file (e.g., `coco128.yaml`). This file includes paths to validation data, class names, and number of classes. |
|
||||
| `imgsz` | `int` | `640` | Defines the size of input images. All images are resized to this dimension before processing. |
|
||||
| `batch` | `int` | `16` | Sets the number of images per batch. Use `-1` for AutoBatch, which automatically adjusts based on GPU memory availability. |
|
||||
| `save_json` | `bool` | `False` | If `True`, saves the results to a JSON file for further analysis or integration with other tools. |
|
||||
| `save_hybrid` | `bool` | `False` | If `True`, saves a hybrid version of labels that combines original annotations with additional model predictions. |
|
||||
| `conf` | `float` | `0.001` | Sets the minimum confidence threshold for detections. Detections with confidence below this threshold are discarded. |
|
||||
| `iou` | `float` | `0.6` | Sets the Intersection Over Union (IoU) threshold for Non-Maximum Suppression (NMS). Helps in reducing duplicate detections. |
|
||||
| `max_det` | `int` | `300` | Limits the maximum number of detections per image. Useful in dense scenes to prevent excessive detections. |
|
||||
| `half` | `bool` | `True` | Enables half-precision (FP16) computation, reducing memory usage and potentially increasing speed with minimal impact on accuracy. |
|
||||
| `device` | `str` | `None` | Specifies the device for validation (`cpu`, `cuda:0`, etc.). Allows flexibility in utilizing CPU or GPU resources. |
|
||||
| `dnn` | `bool` | `False` | If `True`, uses the OpenCV DNN module for ONNX model inference, offering an alternative to PyTorch inference methods. |
|
||||
| `plots` | `bool` | `False` | When set to `True`, generates and saves plots of predictions versus ground truth for visual evaluation of the model's performance. |
|
||||
| `rect` | `bool` | `False` | If `True`, uses rectangular inference for batching, reducing padding and potentially increasing speed and efficiency. |
|
||||
| `split` | `str` | `val` | Determines the dataset split to use for validation (`val`, `test`, or `train`). Allows flexibility in choosing the data segment for performance evaluation. |
|
||||
|
||||
Careful tuning and experimentation with these settings are crucial to ensure optimal performance on the validation dataset and detect and prevent overfitting.
|
||||
|
||||
[Val Guide](../modes/val.md){ .md-button }
|
||||
|
||||
## Export Settings
|
||||
|
||||
Export settings for YOLO models encompass configurations and options related to saving or exporting the model for use in different environments or platforms. These settings can impact the model's performance, size, and compatibility with various systems. Key export settings include the exported model file format (e.g., ONNX, TensorFlow SavedModel), the target device (e.g., CPU, GPU), and additional features such as masks or multiple labels per box. The export process may also be affected by the model's specific task and the requirements or constraints of the destination environment or platform.
|
||||
|
||||
| Argument | Type | Default | Description |
|
||||
|-------------|------------------|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `format` | `str` | `'torchscript'` | Target format for the exported model, such as `'onnx'`, `'torchscript'`, `'tensorflow'`, or others, defining compatibility with various deployment environments. |
|
||||
| `imgsz` | `int` or `tuple` | `640` | Desired image size for the model input. Can be an integer for square images or a tuple `(height, width)` for specific dimensions. |
|
||||
| `keras` | `bool` | `False` | Enables export to Keras format for TensorFlow SavedModel, providing compatibility with TensorFlow serving and APIs. |
|
||||
| `optimize` | `bool` | `False` | Applies optimization for mobile devices when exporting to TorchScript, potentially reducing model size and improving performance. |
|
||||
| `half` | `bool` | `False` | Enables FP16 (half-precision) quantization, reducing model size and potentially speeding up inference on supported hardware. |
|
||||
| `int8` | `bool` | `False` | Activates INT8 quantization, further compressing the model and speeding up inference with minimal accuracy loss, primarily for edge devices. |
|
||||
| `dynamic` | `bool` | `False` | Allows dynamic input sizes for ONNX and TensorRT exports, enhancing flexibility in handling varying image dimensions. |
|
||||
| `simplify` | `bool` | `False` | Simplifies the model graph for ONNX exports, potentially improving performance and compatibility. |
|
||||
| `opset` | `int` | `None` | Specifies the ONNX opset version for compatibility with different ONNX parsers and runtimes. If not set, uses the latest supported version. |
|
||||
| `workspace` | `float` | `4.0` | Sets the maximum workspace size in GB for TensorRT optimizations, balancing memory usage and performance. |
|
||||
| `nms` | `bool` | `False` | Adds Non-Maximum Suppression (NMS) to the CoreML export, essential for accurate and efficient detection post-processing. |
|
||||
|
||||
It is crucial to thoughtfully configure these settings to ensure the exported model is optimized for the intended use case and functions effectively in the target environment.
|
||||
|
||||
[Export Guide](../modes/export.md){ .md-button }
|
||||
|
||||
## Augmentation Settings
|
||||
|
||||
Augmentation techniques are essential for improving the robustness and performance of YOLO models by introducing variability into the training data, helping the model generalize better to unseen data. The following table outlines the purpose and effect of each augmentation argument:
|
||||
|
||||
| Argument | Type | Default | Range | Description |
|
||||
|----------------|---------|---------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `hsv_h` | `float` | `0.015` | `0.0 - 1.0` | Adjusts the hue of the image by a fraction of the color wheel, introducing color variability. Helps the model generalize across different lighting conditions. |
|
||||
| `hsv_s` | `float` | `0.7` | `0.0 - 1.0` | Alters the saturation of the image by a fraction, affecting the intensity of colors. Useful for simulating different environmental conditions. |
|
||||
| `hsv_v` | `float` | `0.4` | `0.0 - 1.0` | Modifies the value (brightness) of the image by a fraction, helping the model to perform well under various lighting conditions. |
|
||||
| `degrees` | `float` | `0.0` | `-180 - +180` | Rotates the image randomly within the specified degree range, improving the model's ability to recognize objects at various orientations. |
|
||||
| `translate` | `float` | `0.1` | `0.0 - 1.0` | Translates the image horizontally and vertically by a fraction of the image size, aiding in learning to detect partially visible objects. |
|
||||
| `scale` | `float` | `0.5` | `>=0.0` | Scales the image by a gain factor, simulating objects at different distances from the camera. |
|
||||
| `shear` | `float` | `0.0` | `-180 - +180` | Shears the image by a specified degree, mimicking the effect of objects being viewed from different angles. |
|
||||
| `perspective` | `float` | `0.0` | `0.0 - 0.001` | Applies a random perspective transformation to the image, enhancing the model's ability to understand objects in 3D space. |
|
||||
| `flipud` | `float` | `0.0` | `0.0 - 1.0` | Flips the image upside down with the specified probability, increasing the data variability without affecting the object's characteristics. |
|
||||
| `fliplr` | `float` | `0.5` | `0.0 - 1.0` | Flips the image left to right with the specified probability, useful for learning symmetrical objects and increasing dataset diversity. |
|
||||
| `bgr` | `float` | `0.0` | `0.0 - 1.0` | Flips the image channels from RGB to BGR with the specified probability, useful for increasing robustness to incorrect channel ordering. |
|
||||
| `mosaic` | `float` | `1.0` | `0.0 - 1.0` | Combines four training images into one, simulating different scene compositions and object interactions. Highly effective for complex scene understanding. |
|
||||
| `mixup` | `float` | `0.0` | `0.0 - 1.0` | Blends two images and their labels, creating a composite image. Enhances the model's ability to generalize by introducing label noise and visual variability. |
|
||||
| `copy_paste` | `float` | `0.0` | `0.0 - 1.0` | Copies objects from one image and pastes them onto another, useful for increasing object instances and learning object occlusion. |
|
||||
| `auto_augment` | `str` | `randaugment` | - | Automatically applies a predefined augmentation policy (`randaugment`, `autoaugment`, `augmix`), optimizing for classification tasks by diversifying the visual features. |
|
||||
| `erasing` | `float` | `0.4` | `0.0 - 1.0` | Randomly erases a portion of the image during classification training, encouraging the model to focus on less obvious features for recognition. |
|
||||
|
||||
These settings can be adjusted to meet the specific requirements of the dataset and task at hand. Experimenting with different values can help find the optimal augmentation strategy that leads to the best model performance.
|
||||
|
||||
## Logging, Checkpoints and Plotting Settings
|
||||
|
||||
Logging, checkpoints, plotting, and file management are important considerations when training a YOLO model.
|
||||
|
||||
- Logging: It is often helpful to log various metrics and statistics during training to track the model's progress and diagnose any issues that may arise. This can be done using a logging library such as TensorBoard or by writing log messages to a file.
|
||||
- Checkpoints: It is a good practice to save checkpoints of the model at regular intervals during training. This allows you to resume training from a previous point if the training process is interrupted or if you want to experiment with different training configurations.
|
||||
- Plotting: Visualizing the model's performance and training progress can be helpful for understanding how the model is behaving and identifying potential issues. This can be done using a plotting library such as matplotlib or by generating plots using a logging library such as TensorBoard.
|
||||
- File management: Managing the various files generated during the training process, such as model checkpoints, log files, and plots, can be challenging. It is important to have a clear and organized file structure to keep track of these files and make it easy to access and analyze them as needed.
|
||||
|
||||
Effective logging, checkpointing, plotting, and file management can help you keep track of the model's progress and make it easier to debug and optimize the training process.
|
||||
|
||||
| Argument | Default | Description |
|
||||
|------------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `project` | `'runs'` | Specifies the root directory for saving training runs. Each run will be saved in a separate subdirectory within this directory. |
|
||||
| `name` | `'exp'` | Defines the name of the experiment. If not specified, YOLO automatically increments this name for each run, e.g., `exp`, `exp2`, etc., to avoid overwriting previous experiments. |
|
||||
| `exist_ok` | `False` | Determines whether to overwrite an existing experiment directory if one with the same name already exists. Setting this to `True` allows overwriting, while `False` prevents it. |
|
||||
| `plots` | `False` | Controls the generation and saving of training and validation plots. Set to `True` to create plots such as loss curves, precision-recall curves, and sample predictions. Useful for visually tracking model performance over time. |
|
||||
| `save` | `False` | Enables the saving of training checkpoints and final model weights. Set to `True` to periodically save model states, allowing training to be resumed from these checkpoints or models to be deployed. |
|
231
docs/en/usage/cli.md
Normal file
231
docs/en/usage/cli.md
Normal file
@ -0,0 +1,231 @@
|
||||
---
|
||||
comments: true
|
||||
description: Learn how to use Ultralytics YOLO through Command Line, train models, run predictions and exports models to different formats easily using terminal commands.
|
||||
keywords: Ultralytics, YOLO, CLI, train, validation, prediction, command line interface, YOLO CLI, YOLO terminal, model training, prediction, exporting
|
||||
---
|
||||
|
||||
# Command Line Interface Usage
|
||||
|
||||
The YOLO command line interface (CLI) allows for simple single-line commands without the need for a Python environment. CLI requires no customization or Python code. You can simply run all tasks from the terminal with the `yolo` command.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/GsXGnb-A4Kc?start=19"
|
||||
title="YouTube video player" frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||
allowfullscreen>
|
||||
</iframe>
|
||||
<br>
|
||||
<strong>Watch:</strong> Mastering Ultralytics YOLOv8: CLI
|
||||
</p>
|
||||
|
||||
!!! Example
|
||||
|
||||
=== "Syntax"
|
||||
|
||||
Ultralytics `yolo` commands use the following syntax:
|
||||
```bash
|
||||
yolo TASK MODE ARGS
|
||||
|
||||
Where TASK (optional) is one of [detect, segment, classify]
|
||||
MODE (required) is one of [train, val, predict, export, track]
|
||||
ARGS (optional) are any number of custom 'arg=value' pairs like 'imgsz=320' that override defaults.
|
||||
```
|
||||
See all ARGS in the full [Configuration Guide](cfg.md) or with `yolo cfg`
|
||||
|
||||
=== "Train"
|
||||
|
||||
Train a detection model for 10 epochs with an initial learning_rate of 0.01
|
||||
```bash
|
||||
yolo train data=coco128.yaml model=yolov8n.pt epochs=10 lr0=0.01
|
||||
```
|
||||
|
||||
=== "Predict"
|
||||
|
||||
Predict a YouTube video using a pretrained segmentation model at image size 320:
|
||||
```bash
|
||||
yolo predict model=yolov8n-seg.pt source='https://youtu.be/LNwODJXcvt4' imgsz=320
|
||||
```
|
||||
|
||||
=== "Val"
|
||||
|
||||
Val a pretrained detection model at batch-size 1 and image size 640:
|
||||
```bash
|
||||
yolo val model=yolov8n.pt data=coco128.yaml batch=1 imgsz=640
|
||||
```
|
||||
|
||||
=== "Export"
|
||||
|
||||
Export a YOLOv8n classification model to ONNX format at image size 224 by 128 (no TASK required)
|
||||
```bash
|
||||
yolo export model=yolov8n-cls.pt format=onnx imgsz=224,128
|
||||
```
|
||||
|
||||
=== "Special"
|
||||
|
||||
Run special commands to see version, view settings, run checks and more:
|
||||
```bash
|
||||
yolo help
|
||||
yolo checks
|
||||
yolo version
|
||||
yolo settings
|
||||
yolo copy-cfg
|
||||
yolo cfg
|
||||
```
|
||||
|
||||
Where:
|
||||
|
||||
- `TASK` (optional) is one of `[detect, segment, classify]`. If it is not passed explicitly YOLOv8 will try to guess the `TASK` from the model type.
|
||||
- `MODE` (required) is one of `[train, val, predict, export, track]`
|
||||
- `ARGS` (optional) are any number of custom `arg=value` pairs like `imgsz=320` that override defaults. For a full list of available `ARGS` see the [Configuration](cfg.md) page and `defaults.yaml`
|
||||
GitHub [source](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/default.yaml).
|
||||
|
||||
!!! Warning "Warning"
|
||||
|
||||
Arguments must be passed as `arg=val` pairs, split by an equals `=` sign and delimited by spaces ` ` between pairs. Do not use `--` argument prefixes or commas `,` between arguments.
|
||||
|
||||
- `yolo predict model=yolov8n.pt imgsz=640 conf=0.25` ✅
|
||||
- `yolo predict model yolov8n.pt imgsz 640 conf 0.25` ❌
|
||||
- `yolo predict --model yolov8n.pt --imgsz 640 --conf 0.25` ❌
|
||||
|
||||
## Train
|
||||
|
||||
Train YOLOv8n on the COCO128 dataset for 100 epochs at image size 640. For a full list of available arguments see the [Configuration](cfg.md) page.
|
||||
|
||||
!!! Example "Example"
|
||||
|
||||
=== "Train"
|
||||
|
||||
Start training YOLOv8n on COCO128 for 100 epochs at image-size 640.
|
||||
```bash
|
||||
yolo detect train data=coco128.yaml model=yolov8n.pt epochs=100 imgsz=640
|
||||
```
|
||||
|
||||
=== "Resume"
|
||||
|
||||
Resume an interrupted training.
|
||||
```bash
|
||||
yolo detect train resume model=last.pt
|
||||
```
|
||||
|
||||
## Val
|
||||
|
||||
Validate trained YOLOv8n model accuracy on the COCO128 dataset. No argument need to passed as the `model` retains it's training `data` and arguments as model attributes.
|
||||
|
||||
!!! Example "Example"
|
||||
|
||||
=== "Official"
|
||||
|
||||
Validate an official YOLOv8n model.
|
||||
```bash
|
||||
yolo detect val model=yolov8n.pt
|
||||
```
|
||||
|
||||
=== "Custom"
|
||||
|
||||
Validate a custom-trained model.
|
||||
```bash
|
||||
yolo detect val model=path/to/best.pt
|
||||
```
|
||||
|
||||
## Predict
|
||||
|
||||
Use a trained YOLOv8n model to run predictions on images.
|
||||
|
||||
!!! Example "Example"
|
||||
|
||||
=== "Official"
|
||||
|
||||
Predict with an official YOLOv8n model.
|
||||
```bash
|
||||
yolo detect predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg'
|
||||
```
|
||||
|
||||
=== "Custom"
|
||||
|
||||
Predict with a custom model.
|
||||
```bash
|
||||
yolo detect predict model=path/to/best.pt source='https://ultralytics.com/images/bus.jpg'
|
||||
```
|
||||
|
||||
## Export
|
||||
|
||||
Export a YOLOv8n model to a different format like ONNX, CoreML, etc.
|
||||
|
||||
!!! Example "Example"
|
||||
|
||||
=== "Official"
|
||||
|
||||
Export an official YOLOv8n model to ONNX format.
|
||||
```bash
|
||||
yolo export model=yolov8n.pt format=onnx
|
||||
```
|
||||
|
||||
=== "Custom"
|
||||
|
||||
Export a custom-trained model to ONNX format.
|
||||
```bash
|
||||
yolo export model=path/to/best.pt format=onnx
|
||||
```
|
||||
|
||||
Available YOLOv8 export formats are in the table below. You can export to any format using the `format` argument, i.e. `format='onnx'` or `format='engine'`.
|
||||
|
||||
| Format | `format` Argument | Model | Metadata | Arguments |
|
||||
|--------------------------------------------------------------------|-------------------|---------------------------|----------|-----------------------------------------------------|
|
||||
| [PyTorch](https://pytorch.org/) | - | `yolov8n.pt` | ✅ | - |
|
||||
| [TorchScript](https://pytorch.org/docs/stable/jit.html) | `torchscript` | `yolov8n.torchscript` | ✅ | `imgsz`, `optimize` |
|
||||
| [ONNX](https://onnx.ai/) | `onnx` | `yolov8n.onnx` | ✅ | `imgsz`, `half`, `dynamic`, `simplify`, `opset` |
|
||||
| [OpenVINO](../integrations/openvino.md) | `openvino` | `yolov8n_openvino_model/` | ✅ | `imgsz`, `half`, `int8` |
|
||||
| [TensorRT](https://developer.nvidia.com/tensorrt) | `engine` | `yolov8n.engine` | ✅ | `imgsz`, `half`, `dynamic`, `simplify`, `workspace` |
|
||||
| [CoreML](https://github.com/apple/coremltools) | `coreml` | `yolov8n.mlpackage` | ✅ | `imgsz`, `half`, `int8`, `nms` |
|
||||
| [TF SavedModel](https://www.tensorflow.org/guide/saved_model) | `saved_model` | `yolov8n_saved_model/` | ✅ | `imgsz`, `keras`, `int8` |
|
||||
| [TF GraphDef](https://www.tensorflow.org/api_docs/python/tf/Graph) | `pb` | `yolov8n.pb` | ❌ | `imgsz` |
|
||||
| [TF Lite](https://www.tensorflow.org/lite) | `tflite` | `yolov8n.tflite` | ✅ | `imgsz`, `half`, `int8` |
|
||||
| [TF Edge TPU](https://coral.ai/docs/edgetpu/models-intro/) | `edgetpu` | `yolov8n_edgetpu.tflite` | ✅ | `imgsz` |
|
||||
| [TF.js](https://www.tensorflow.org/js) | `tfjs` | `yolov8n_web_model/` | ✅ | `imgsz`, `half`, `int8` |
|
||||
| [PaddlePaddle](https://github.com/PaddlePaddle) | `paddle` | `yolov8n_paddle_model/` | ✅ | `imgsz` |
|
||||
| [NCNN](https://github.com/Tencent/ncnn) | `ncnn` | `yolov8n_ncnn_model/` | ✅ | `imgsz`, `half` |
|
||||
|
||||
## Overriding default arguments
|
||||
|
||||
Default arguments can be overridden by simply passing them as arguments in the CLI in `arg=value` pairs.
|
||||
|
||||
!!! Tip ""
|
||||
|
||||
=== "Train"
|
||||
|
||||
Train a detection model for `10 epochs` with `learning_rate` of `0.01`
|
||||
```bash
|
||||
yolo detect train data=coco128.yaml model=yolov8n.pt epochs=10 lr0=0.01
|
||||
```
|
||||
|
||||
=== "Predict"
|
||||
|
||||
Predict a YouTube video using a pretrained segmentation model at image size 320:
|
||||
```bash
|
||||
yolo segment predict model=yolov8n-seg.pt source='https://youtu.be/LNwODJXcvt4' imgsz=320
|
||||
```
|
||||
|
||||
=== "Val"
|
||||
|
||||
Validate a pretrained detection model at batch-size 1 and image size 640:
|
||||
```bash
|
||||
yolo detect val model=yolov8n.pt data=coco128.yaml batch=1 imgsz=640
|
||||
```
|
||||
|
||||
## Overriding default config file
|
||||
|
||||
You can override the `default.yaml` config file entirely by passing a new file with the `cfg` arguments, i.e. `cfg=custom.yaml`.
|
||||
|
||||
To do this first create a copy of `default.yaml` in your current working dir with the `yolo copy-cfg` command.
|
||||
|
||||
This will create `default_copy.yaml`, which you can then pass as `cfg=default_copy.yaml` along with any additional args, like `imgsz=320` in this example:
|
||||
|
||||
!!! Example
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo copy-cfg
|
||||
yolo cfg=default_copy.yaml imgsz=320
|
||||
```
|
91
docs/en/usage/engine.md
Normal file
91
docs/en/usage/engine.md
Normal file
@ -0,0 +1,91 @@
|
||||
---
|
||||
comments: true
|
||||
description: Discover how to customize and extend base Ultralytics YOLO Trainer engines. Support your custom model and dataloader by overriding built-in functions.
|
||||
keywords: Ultralytics, YOLO, trainer engines, BaseTrainer, DetectionTrainer, customizing trainers, extending trainers, custom model, custom dataloader
|
||||
---
|
||||
|
||||
Both the Ultralytics YOLO command-line and Python interfaces are simply a high-level abstraction on the base engine executors. Let's take a look at the Trainer engine.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/GsXGnb-A4Kc?start=104"
|
||||
title="YouTube video player" frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||
allowfullscreen>
|
||||
</iframe>
|
||||
<br>
|
||||
<strong>Watch:</strong> Mastering Ultralytics YOLOv8: Advanced Customization
|
||||
</p>
|
||||
|
||||
## BaseTrainer
|
||||
|
||||
BaseTrainer contains the generic boilerplate training routine. It can be customized for any task based over overriding the required functions or operations as long the as correct formats are followed. For example, you can support your own custom model and dataloader by just overriding these functions:
|
||||
|
||||
- `get_model(cfg, weights)` - The function that builds the model to be trained
|
||||
- `get_dataloader()` - The function that builds the dataloader More details and source code can be found in [`BaseTrainer` Reference](../reference/engine/trainer.md)
|
||||
|
||||
## DetectionTrainer
|
||||
|
||||
Here's how you can use the YOLOv8 `DetectionTrainer` and customize it.
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
trainer = DetectionTrainer(overrides={...})
|
||||
trainer.train()
|
||||
trained_model = trainer.best # get best model
|
||||
```
|
||||
|
||||
### Customizing the DetectionTrainer
|
||||
|
||||
Let's customize the trainer **to train a custom detection model** that is not supported directly. You can do this by simply overloading the existing the `get_model` functionality:
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
|
||||
class CustomTrainer(DetectionTrainer):
|
||||
def get_model(self, cfg, weights):
|
||||
...
|
||||
|
||||
|
||||
trainer = CustomTrainer(overrides={...})
|
||||
trainer.train()
|
||||
```
|
||||
|
||||
You now realize that you need to customize the trainer further to:
|
||||
|
||||
- Customize the `loss function`.
|
||||
- Add `callback` that uploads model to your Google Drive after every 10 `epochs` Here's how you can do it:
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
from ultralytics.nn.tasks import DetectionModel
|
||||
|
||||
|
||||
class MyCustomModel(DetectionModel):
|
||||
def init_criterion(self):
|
||||
...
|
||||
|
||||
|
||||
class CustomTrainer(DetectionTrainer):
|
||||
def get_model(self, cfg, weights):
|
||||
return MyCustomModel(...)
|
||||
|
||||
|
||||
# callback to upload model weights
|
||||
def log_model(trainer):
|
||||
last_weight_path = trainer.last
|
||||
print(last_weight_path)
|
||||
|
||||
|
||||
trainer = CustomTrainer(overrides={...})
|
||||
trainer.add_callback("on_train_epoch_end", log_model) # Adds to existing callback
|
||||
trainer.train()
|
||||
```
|
||||
|
||||
To know more about Callback triggering events and entry point, checkout our [Callbacks Guide](callbacks.md)
|
||||
|
||||
## Other engine components
|
||||
|
||||
There are other components that can be customized similarly like `Validators` and `Predictors`. See Reference section for more information on these.
|
325
docs/en/usage/python.md
Normal file
325
docs/en/usage/python.md
Normal file
@ -0,0 +1,325 @@
|
||||
---
|
||||
comments: true
|
||||
description: Boost your Python projects with object detection, segmentation and classification using YOLOv8. Explore how to load, train, validate, predict, export, track and benchmark models with ease.
|
||||
keywords: YOLOv8, Ultralytics, Python, object detection, segmentation, classification, model training, validation, prediction, model export, benchmark, real-time tracking
|
||||
---
|
||||
|
||||
# Python Usage
|
||||
|
||||
Welcome to the YOLOv8 Python Usage documentation! This guide is designed to help you seamlessly integrate YOLOv8 into your Python projects for object detection, segmentation, and classification. Here, you'll learn how to load and use pretrained models, train new models, and perform predictions on images. The easy-to-use Python interface is a valuable resource for anyone looking to incorporate YOLOv8 into their Python projects, allowing you to quickly implement advanced object detection capabilities. Let's get started!
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/GsXGnb-A4Kc?start=58"
|
||||
title="YouTube video player" frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||
allowfullscreen>
|
||||
</iframe>
|
||||
<br>
|
||||
<strong>Watch:</strong> Mastering Ultralytics YOLOv8: Python
|
||||
</p>
|
||||
|
||||
For example, users can load a model, train it, evaluate its performance on a validation set, and even export it to ONNX format with just a few lines of code.
|
||||
|
||||
!!! Example "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Create a new YOLO model from scratch
|
||||
model = YOLO('yolov8n.yaml')
|
||||
|
||||
# Load a pretrained YOLO model (recommended for training)
|
||||
model = YOLO('yolov8n.pt')
|
||||
|
||||
# Train the model using the 'coco128.yaml' dataset for 3 epochs
|
||||
results = model.train(data='coco128.yaml', epochs=3)
|
||||
|
||||
# Evaluate the model's performance on the validation set
|
||||
results = model.val()
|
||||
|
||||
# Perform object detection on an image using the model
|
||||
results = model('https://ultralytics.com/images/bus.jpg')
|
||||
|
||||
# Export the model to ONNX format
|
||||
success = model.export(format='onnx')
|
||||
```
|
||||
|
||||
## [Train](../modes/train.md)
|
||||
|
||||
Train mode is used for training a YOLOv8 model on a custom dataset. In this mode, the model is trained using the specified dataset and hyperparameters. The training process involves optimizing the model's parameters so that it can accurately predict the classes and locations of objects in an image.
|
||||
|
||||
!!! Example "Train"
|
||||
|
||||
=== "From pretrained(recommended)"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO('yolov8n.pt') # pass any model type
|
||||
results = model.train(epochs=5)
|
||||
```
|
||||
|
||||
=== "From scratch"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO('yolov8n.yaml')
|
||||
results = model.train(data='coco128.yaml', epochs=5)
|
||||
```
|
||||
|
||||
=== "Resume"
|
||||
|
||||
```python
|
||||
model = YOLO("last.pt")
|
||||
results = model.train(resume=True)
|
||||
```
|
||||
|
||||
[Train Examples](../modes/train.md){ .md-button }
|
||||
|
||||
## [Val](../modes/val.md)
|
||||
|
||||
Val mode is used for validating a YOLOv8 model after it has been trained. In this mode, the model is evaluated on a validation set to measure its accuracy and generalization performance. This mode can be used to tune the hyperparameters of the model to improve its performance.
|
||||
|
||||
!!! Example "Val"
|
||||
|
||||
=== "Val after training"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO('yolov8n.yaml')
|
||||
model.train(data='coco128.yaml', epochs=5)
|
||||
model.val() # It'll automatically evaluate the data you trained.
|
||||
```
|
||||
|
||||
=== "Val independently"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("model.pt")
|
||||
# It'll use the data YAML file in model.pt if you don't set data.
|
||||
model.val()
|
||||
# or you can set the data you want to val
|
||||
model.val(data='coco128.yaml')
|
||||
```
|
||||
|
||||
[Val Examples](../modes/val.md){ .md-button }
|
||||
|
||||
## [Predict](../modes/predict.md)
|
||||
|
||||
Predict mode is used for making predictions using a trained YOLOv8 model on new images or videos. In this mode, the model is loaded from a checkpoint file, and the user can provide images or videos to perform inference. The model predicts the classes and locations of objects in the input images or videos.
|
||||
|
||||
!!! Example "Predict"
|
||||
|
||||
=== "From source"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
from PIL import Image
|
||||
import cv2
|
||||
|
||||
model = YOLO("model.pt")
|
||||
# accepts all formats - image/dir/Path/URL/video/PIL/ndarray. 0 for webcam
|
||||
results = model.predict(source="0")
|
||||
results = model.predict(source="folder", show=True) # Display preds. Accepts all YOLO predict arguments
|
||||
|
||||
# from PIL
|
||||
im1 = Image.open("bus.jpg")
|
||||
results = model.predict(source=im1, save=True) # save plotted images
|
||||
|
||||
# from ndarray
|
||||
im2 = cv2.imread("bus.jpg")
|
||||
results = model.predict(source=im2, save=True, save_txt=True) # save predictions as labels
|
||||
|
||||
# from list of PIL/ndarray
|
||||
results = model.predict(source=[im1, im2])
|
||||
```
|
||||
|
||||
=== "Results usage"
|
||||
|
||||
```python
|
||||
# results would be a list of Results object including all the predictions by default
|
||||
# but be careful as it could occupy a lot memory when there're many images,
|
||||
# especially the task is segmentation.
|
||||
# 1. return as a list
|
||||
results = model.predict(source="folder")
|
||||
|
||||
# results would be a generator which is more friendly to memory by setting stream=True
|
||||
# 2. return as a generator
|
||||
results = model.predict(source=0, stream=True)
|
||||
|
||||
for result in results:
|
||||
# Detection
|
||||
result.boxes.xyxy # box with xyxy format, (N, 4)
|
||||
result.boxes.xywh # box with xywh format, (N, 4)
|
||||
result.boxes.xyxyn # box with xyxy format but normalized, (N, 4)
|
||||
result.boxes.xywhn # box with xywh format but normalized, (N, 4)
|
||||
result.boxes.conf # confidence score, (N, 1)
|
||||
result.boxes.cls # cls, (N, 1)
|
||||
|
||||
# Segmentation
|
||||
result.masks.data # masks, (N, H, W)
|
||||
result.masks.xy # x,y segments (pixels), List[segment] * N
|
||||
result.masks.xyn # x,y segments (normalized), List[segment] * N
|
||||
|
||||
# Classification
|
||||
result.probs # cls prob, (num_class, )
|
||||
|
||||
# Each result is composed of torch.Tensor by default,
|
||||
# in which you can easily use following functionality:
|
||||
result = result.cuda()
|
||||
result = result.cpu()
|
||||
result = result.to("cpu")
|
||||
result = result.numpy()
|
||||
```
|
||||
|
||||
[Predict Examples](../modes/predict.md){ .md-button }
|
||||
|
||||
## [Export](../modes/export.md)
|
||||
|
||||
Export mode is used for exporting a YOLOv8 model to a format that can be used for deployment. In this mode, the model is converted to a format that can be used by other software applications or hardware devices. This mode is useful when deploying the model to production environments.
|
||||
|
||||
!!! Example "Export"
|
||||
|
||||
=== "Export to ONNX"
|
||||
|
||||
Export an official YOLOv8n model to ONNX with dynamic batch-size and image-size.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO('yolov8n.pt')
|
||||
model.export(format='onnx', dynamic=True)
|
||||
```
|
||||
|
||||
=== "Export to TensorRT"
|
||||
|
||||
Export an official YOLOv8n model to TensorRT on `device=0` for acceleration on CUDA devices.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO('yolov8n.pt')
|
||||
model.export(format='onnx', device=0)
|
||||
```
|
||||
|
||||
[Export Examples](../modes/export.md){ .md-button }
|
||||
|
||||
## [Track](../modes/track.md)
|
||||
|
||||
Track mode is used for tracking objects in real-time using a YOLOv8 model. In this mode, the model is loaded from a checkpoint file, and the user can provide a live video stream to perform real-time object tracking. This mode is useful for applications such as surveillance systems or self-driving cars.
|
||||
|
||||
!!! Example "Track"
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO('yolov8n.pt') # load an official detection model
|
||||
model = YOLO('yolov8n-seg.pt') # load an official segmentation model
|
||||
model = YOLO('path/to/best.pt') # load a custom model
|
||||
|
||||
# Track with the model
|
||||
results = model.track(source="https://youtu.be/LNwODJXcvt4", show=True)
|
||||
results = model.track(source="https://youtu.be/LNwODJXcvt4", show=True, tracker="bytetrack.yaml")
|
||||
```
|
||||
|
||||
[Track Examples](../modes/track.md){ .md-button }
|
||||
|
||||
## [Benchmark](../modes/benchmark.md)
|
||||
|
||||
Benchmark mode is used to profile the speed and accuracy of various export formats for YOLOv8. The benchmarks provide information on the size of the exported format, its `mAP50-95` metrics (for object detection and segmentation) or `accuracy_top5` metrics (for classification), and the inference time in milliseconds per image across various export formats like ONNX, OpenVINO, TensorRT and others. This information can help users choose the optimal export format for their specific use case based on their requirements for speed and accuracy.
|
||||
|
||||
!!! Example "Benchmark"
|
||||
|
||||
=== "Python"
|
||||
|
||||
Benchmark an official YOLOv8n model across all export formats.
|
||||
```python
|
||||
from ultralytics.utils.benchmarks import benchmark
|
||||
|
||||
# Benchmark
|
||||
benchmark(model='yolov8n.pt', data='coco8.yaml', imgsz=640, half=False, device=0)
|
||||
```
|
||||
|
||||
[Benchmark Examples](../modes/benchmark.md){ .md-button }
|
||||
|
||||
## Explorer
|
||||
|
||||
Explorer API can be used to explore datasets with advanced semantic, vector-similarity and SQL search among other features. It also enabled searching for images based on their content using natural language by utilizing the power of LLMs. The Explorer API allows you to write your own dataset exploration notebooks or scripts to get insights into your datasets.
|
||||
|
||||
!!! Example "Semantic Search Using Explorer"
|
||||
|
||||
=== "Using Images"
|
||||
|
||||
```python
|
||||
from ultralytics import Explorer
|
||||
|
||||
# create an Explorer object
|
||||
exp = Explorer(data='coco128.yaml', model='yolov8n.pt')
|
||||
exp.create_embeddings_table()
|
||||
|
||||
similar = exp.get_similar(img='https://ultralytics.com/images/bus.jpg', limit=10)
|
||||
print(similar.head())
|
||||
|
||||
# Search using multiple indices
|
||||
similar = exp.get_similar(
|
||||
img=['https://ultralytics.com/images/bus.jpg',
|
||||
'https://ultralytics.com/images/bus.jpg'],
|
||||
limit=10
|
||||
)
|
||||
print(similar.head())
|
||||
```
|
||||
|
||||
=== "Using Dataset Indices"
|
||||
|
||||
```python
|
||||
from ultralytics import Explorer
|
||||
|
||||
# create an Explorer object
|
||||
exp = Explorer(data='coco128.yaml', model='yolov8n.pt')
|
||||
exp.create_embeddings_table()
|
||||
|
||||
similar = exp.get_similar(idx=1, limit=10)
|
||||
print(similar.head())
|
||||
|
||||
# Search using multiple indices
|
||||
similar = exp.get_similar(idx=[1,10], limit=10)
|
||||
print(similar.head())
|
||||
```
|
||||
|
||||
[Explorer](../datasets/explorer/index.md){ .md-button }
|
||||
|
||||
## Using Trainers
|
||||
|
||||
`YOLO` model class is a high-level wrapper on the Trainer classes. Each YOLO task has its own trainer that inherits from `BaseTrainer`.
|
||||
|
||||
!!! Tip "Detection Trainer Example"
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo import DetectionTrainer, DetectionValidator, DetectionPredictor
|
||||
|
||||
# trainer
|
||||
trainer = DetectionTrainer(overrides={})
|
||||
trainer.train()
|
||||
trained_model = trainer.best
|
||||
|
||||
# Validator
|
||||
val = DetectionValidator(args=...)
|
||||
val(model=trained_model)
|
||||
|
||||
# predictor
|
||||
pred = DetectionPredictor(overrides={})
|
||||
pred(source=SOURCE, model=trained_model)
|
||||
|
||||
# resume from last weight
|
||||
overrides["resume"] = trainer.last
|
||||
trainer = detect.DetectionTrainer(overrides=overrides)
|
||||
```
|
||||
|
||||
You can easily customize Trainers to support custom tasks or explore R&D ideas. Learn more about Customizing `Trainers`, `Validators` and `Predictors` to suit your project needs in the Customization Section.
|
||||
|
||||
[Customization tutorials](engine.md){ .md-button }
|
429
docs/en/usage/simple-utilities.md
Normal file
429
docs/en/usage/simple-utilities.md
Normal file
@ -0,0 +1,429 @@
|
||||
---
|
||||
comments: true
|
||||
description: Discover how to extend the utility of the Ultralytics package to support your development process.
|
||||
keywords: Ultralytics, YOLO, custom, function, workflow, utility, support,
|
||||
---
|
||||
|
||||
# Simple Utilities
|
||||
|
||||
<p align="center">
|
||||
<img src="https://github.com/ultralytics/ultralytics/assets/62214284/516112de-4567-49f8-b93f-b55a10b79dd7" alt="code with perspective">
|
||||
</p>
|
||||
|
||||
The `ultralytics` package comes with a myriad of utilities that can support, enhance, and speed up your workflows. There are many more available, but here are some that will be useful for most developers. They're also a great reference point to use when learning to program.
|
||||
|
||||
## Data
|
||||
|
||||
### YOLO Data Explorer
|
||||
|
||||
[YOLO Explorer](../datasets/explorer/index.md) was added in the `8.1.0` anniversary update and is a powerful tool you can use to better understand your dataset. One of the key functions that YOLO Explorer provides, is the ability to use text queries to find object instances in your dataset.
|
||||
|
||||
### Auto Labeling / Annotations
|
||||
|
||||
Dataset annotation is a very resource intensive and time-consuming process. If you have a YOLO object detection model trained on a reasonable amount of data, you can use it and [SAM](../models/sam.md) to auto-annotate additional data (segmentation format).
|
||||
|
||||
```{ .py .annotate }
|
||||
from ultralytics.data.annotator import auto_annotate
|
||||
|
||||
auto_annotate(#(1)!
|
||||
data='path/to/new/data',
|
||||
det_model='yolov8n.pt',
|
||||
sam_model='mobile_sam.pt',
|
||||
device="cuda",
|
||||
output_dir="path/to/save_labels",
|
||||
)
|
||||
```
|
||||
|
||||
1. Nothing returns from this function
|
||||
|
||||
- [See the reference section for `annotator.auto_annotate`](../reference/data/annotator.md#ultralytics.data.annotator.auto_annotate) for more insight on how the function operates.
|
||||
|
||||
- Use in combination with the [function `segments2boxes`](#convert-segments-to-bounding-boxes) to generate object detection bounding boxes as well
|
||||
|
||||
### Convert COCO into YOLO Format
|
||||
|
||||
Use to convert COCO JSON annotations into proper YOLO format. For object detection (bounding box) datasets, `use_segments` and `use_keypoints` should both be `False`
|
||||
|
||||
```{ .py .annotate }
|
||||
from ultralytics.data.converter import convert_coco
|
||||
|
||||
convert_coco(#(1)!
|
||||
'../datasets/coco/annotations/',
|
||||
use_segments=False,
|
||||
use_keypoints=False,
|
||||
cls91to80=True,
|
||||
)
|
||||
```
|
||||
|
||||
1. Nothing returns from this function
|
||||
|
||||
For additional information about the `convert_coco` function, [visit the reference page](../reference/data/converter.md#ultralytics.data.converter.convert_coco)
|
||||
|
||||
### Convert Bounding Boxes to Segments
|
||||
|
||||
With existing `x y w h` bounding box data, convert to segments using the `yolo_bbox2segment` function. The files for images and annotations need to be organized like this:
|
||||
|
||||
```
|
||||
data
|
||||
|__ images
|
||||
├─ 001.jpg
|
||||
├─ 002.jpg
|
||||
├─ ..
|
||||
└─ NNN.jpg
|
||||
|__ labels
|
||||
├─ 001.txt
|
||||
├─ 002.txt
|
||||
├─ ..
|
||||
└─ NNN.txt
|
||||
```
|
||||
|
||||
```{ .py .annotate }
|
||||
from ultralytics.data.converter import yolo_bbox2segment
|
||||
|
||||
yolo_bbox2segment(#(1)!
|
||||
im_dir="path/to/images",
|
||||
save_dir=None, # saved to "labels-segment" in images directory
|
||||
sam_model="sam_b.pt"
|
||||
)
|
||||
```
|
||||
|
||||
1. Nothing returns from this function
|
||||
|
||||
[Visit the `yolo_bbox2segment` reference page](../reference/data/converter.md#ultralytics.data.converter.yolo_bbox2segment) for more information regarding the function.
|
||||
|
||||
### Convert Segments to Bounding Boxes
|
||||
|
||||
If you have a dataset that uses the [segmentation dataset format](../datasets/segment/index.md) you can easily convert these into up-right (or horizontal) bounding boxes (`x y w h` format) with this function.
|
||||
|
||||
```python
|
||||
from ultralytics.utils.ops import segments2boxes
|
||||
|
||||
segments = np.array(
|
||||
[[805, 392, 797, 400, ..., 808, 714, 808, 392],
|
||||
[115, 398, 113, 400, ..., 150, 400, 149, 298],
|
||||
[267, 412, 265, 413, ..., 300, 413, 299, 412],
|
||||
]
|
||||
)
|
||||
|
||||
segments2boxes([s.reshape(-1,2) for s in segments])
|
||||
>>> array([[ 741.66, 631.12, 133.31, 479.25],
|
||||
[ 146.81, 649.69, 185.62, 502.88],
|
||||
[ 281.81, 636.19, 118.12, 448.88]],
|
||||
dtype=float32) # xywh bounding boxes
|
||||
```
|
||||
|
||||
To understand how this function works, visit the [reference page](../reference/utils/ops.md#ultralytics.utils.ops.segments2boxes)
|
||||
|
||||
## Utilities
|
||||
|
||||
### Image Compression
|
||||
|
||||
Compresses a single image file to reduced size while preserving its aspect ratio and quality. If the input image is smaller than the maximum dimension, it will not be resized.
|
||||
|
||||
```{ .py .annotate }
|
||||
from pathlib import Path
|
||||
from ultralytics.data.utils import compress_one_image
|
||||
|
||||
for f in Path('path/to/dataset').rglob('*.jpg'):
|
||||
compress_one_image(f)#(1)!
|
||||
```
|
||||
|
||||
1. Nothing returns from this function
|
||||
|
||||
### Auto-split Dataset
|
||||
|
||||
Automatically split a dataset into `train`/`val`/`test` splits and save the resulting splits into `autosplit_*.txt` files. This function will use random sampling, which is not included when using [`fraction` argument for training](../modes/train.md#arguments).
|
||||
|
||||
```{ .py .annotate }
|
||||
from ultralytics.data.utils import autosplit
|
||||
|
||||
autosplit( #(1)!
|
||||
path="path/to/images",
|
||||
weights=(0.9, 0.1, 0.0), # (train, validation, test) fractional splits
|
||||
annotated_only=False # split only images with annotation file when True
|
||||
)
|
||||
```
|
||||
|
||||
1. Nothing returns from this function
|
||||
|
||||
See the [Reference page](../reference/data/utils.md#ultralytics.data.utils.autosplit) for additional details on this function.
|
||||
|
||||
### Segment-polygon to Binary Mask
|
||||
|
||||
Convert a single polygon (as list) to a binary mask of the specified image size. Polygon in the form of `[N, 2]` with `N` as the number of `(x, y)` points defining the polygon contour.
|
||||
|
||||
!!! warning
|
||||
|
||||
`N` <b><u>must always</b></u> be even.
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
from ultralytics.data.utils import polygon2mask
|
||||
|
||||
imgsz = (1080, 810)
|
||||
polygon = np.array(
|
||||
[805, 392, 797, 400, ..., 808, 714, 808, 392], # (238, 2)
|
||||
)
|
||||
|
||||
mask = polygon2mask(
|
||||
imgsz, # tuple
|
||||
[polygon], # input as list
|
||||
color=255, # 8-bit binary
|
||||
downsample_ratio=1
|
||||
)
|
||||
```
|
||||
|
||||
## Bounding Boxes
|
||||
|
||||
### Bounding Box (horizontal) Instances
|
||||
|
||||
To manage bounding box data, the `Bboxes` class will help to convert between box coordinate formatting, scale box dimensions, calculate areas, include offsets, and more!
|
||||
|
||||
```python
|
||||
from ultralytics.utils.instance import Bboxes
|
||||
|
||||
boxes = Bboxes(
|
||||
bboxes=np.array(
|
||||
[[ 22.878, 231.27, 804.98, 756.83,],
|
||||
[ 48.552, 398.56, 245.35, 902.71,],
|
||||
[ 669.47, 392.19, 809.72, 877.04,],
|
||||
[ 221.52, 405.8, 344.98, 857.54,],
|
||||
[ 0, 550.53, 63.01, 873.44,],
|
||||
[ 0.0584, 254.46, 32.561, 324.87,]]
|
||||
),
|
||||
format="xyxy",
|
||||
)
|
||||
|
||||
boxes.areas()
|
||||
>>> array([ 4.1104e+05, 99216, 68000, 55772, 20347, 2288.5])
|
||||
boxes.convert("xywh")
|
||||
boxes.bboxes
|
||||
>>> array(
|
||||
[[ 413.93, 494.05, 782.1, 525.56],
|
||||
[ 146.95, 650.63, 196.8, 504.15],
|
||||
[ 739.6, 634.62, 140.25, 484.85],
|
||||
[ 283.25, 631.67, 123.46, 451.74],
|
||||
[ 31.505, 711.99, 63.01, 322.91],
|
||||
[ 16.31, 289.67, 32.503, 70.41]]
|
||||
)
|
||||
```
|
||||
|
||||
See the [`Bboxes` reference section](../reference/utils/instance.md#ultralytics.utils.instance.Bboxes) for more attributes and methods available.
|
||||
|
||||
!!! tip
|
||||
Many of the following functions (and more) can be accessed using the [`Bboxes` class](#bounding-box-horizontal-instances) but if you prefer to work with the functions directly, see the next subsections on how to import these independently.
|
||||
|
||||
### Scaling Boxes
|
||||
|
||||
When scaling and image up or down, corresponding bounding box coordinates can be appropriately scaled to match using `ultralytics.utils.ops.scale_boxes`.
|
||||
|
||||
```{ .py .annotate }
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
from ultralytics.utils.ops import scale_boxes
|
||||
|
||||
image = cv.imread("ultralytics/assets/bus.jpg")
|
||||
*(h, w), c = image.shape
|
||||
resized = cv.resize(image, None, (), fx=1.2, fy=1.2)
|
||||
*(new_h, new_w), _ = resized.shape
|
||||
|
||||
xyxy_boxes = np.array(
|
||||
[[ 22.878, 231.27, 804.98, 756.83,],
|
||||
[ 48.552, 398.56, 245.35, 902.71,],
|
||||
[ 669.47, 392.19, 809.72, 877.04,],
|
||||
[ 221.52, 405.8, 344.98, 857.54,],
|
||||
[ 0, 550.53, 63.01, 873.44,],
|
||||
[ 0.0584, 254.46, 32.561, 324.87,]]
|
||||
)
|
||||
|
||||
new_boxes = scale_boxes(
|
||||
img1_shape=(h, w), # original image dimensions
|
||||
boxes=xyxy_boxes, # boxes from original image
|
||||
img0_shape=(new_h, new_w), # resized image dimensions (scale to)
|
||||
ratio_pad=None,
|
||||
padding=False,
|
||||
xywh=False,
|
||||
)
|
||||
|
||||
new_boxes#(1)!
|
||||
>>> array(
|
||||
[[ 27.454, 277.52, 965.98, 908.2],
|
||||
[ 58.262, 478.27, 294.42, 1083.3],
|
||||
[ 803.36, 470.63, 971.66, 1052.4],
|
||||
[ 265.82, 486.96, 413.98, 1029],
|
||||
[ 0, 660.64, 75.612, 1048.1],
|
||||
[ 0.0701, 305.35, 39.073, 389.84]]
|
||||
)
|
||||
```
|
||||
|
||||
1. Bounding boxes scaled for the new image size
|
||||
|
||||
### Bounding Box Format Conversions
|
||||
|
||||
#### XYXY → XYWH
|
||||
|
||||
Convert bounding box coordinates from (x1, y1, x2, y2) format to (x, y, width, height) format where (x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner.
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
from ultralytics.utils.ops import xyxy2xywh
|
||||
|
||||
xyxy_boxes = np.array(
|
||||
[[ 22.878, 231.27, 804.98, 756.83,],
|
||||
[ 48.552, 398.56, 245.35, 902.71,],
|
||||
[ 669.47, 392.19, 809.72, 877.04,],
|
||||
[ 221.52, 405.8, 344.98, 857.54,],
|
||||
[ 0, 550.53, 63.01, 873.44,],
|
||||
[ 0.0584, 254.46, 32.561, 324.87,]]
|
||||
)
|
||||
xywh = xyxy2xywh(xyxy_boxes)
|
||||
|
||||
xywh
|
||||
>>> array(
|
||||
[[ 413.93, 494.05, 782.1, 525.56],
|
||||
[ 146.95, 650.63, 196.8, 504.15],
|
||||
[ 739.6, 634.62, 140.25, 484.85],
|
||||
[ 283.25, 631.67, 123.46, 451.74],
|
||||
[ 31.505, 711.99, 63.01, 322.91],
|
||||
[ 16.31, 289.67, 32.503, 70.41]]
|
||||
)
|
||||
```
|
||||
|
||||
### All Bounding Box Conversions
|
||||
|
||||
```python
|
||||
from ultralytics.utils.ops import xywh2xyxy
|
||||
from ultralytics.utils.ops import xywhn2xyxy # normalized → pixel
|
||||
from ultralytics.utils.ops import xyxy2xywhn # pixel → normalized
|
||||
from ultralytics.utils.ops import xywh2ltwh # xywh → top-left corner, w, h
|
||||
from ultralytics.utils.ops import xyxy2ltwh # xyxy → top-left corner, w, h
|
||||
from ultralytics.utils.ops import ltwh2xywh
|
||||
from ultralytics.utils.ops import ltwh2xyxy
|
||||
```
|
||||
|
||||
See docstring for each function or visit the `ultralytics.utils.ops` [reference page](../reference/utils/ops.md) to read more about each function.
|
||||
|
||||
## Plotting
|
||||
|
||||
### Drawing Annotations
|
||||
|
||||
Ultralytics includes an Annotator class that can be used to annotate any kind of data. It's easiest to use with [object detection bounding boxes](../modes/predict.md#boxes), [pose key points](../modes/predict.md#keypoints), and [oriented bounding boxes](../modes/predict.md#obb).
|
||||
|
||||
#### Horizontal Bounding Boxes
|
||||
|
||||
```{ .py .annotate }
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
from ultralytics.utils.plotting import Annotator, colors
|
||||
|
||||
names { #(1)!
|
||||
0: "person",
|
||||
5: "bus",
|
||||
11: "stop sign",
|
||||
}
|
||||
|
||||
image = cv.imread("ultralytics/assets/bus.jpg")
|
||||
ann = Annotator(
|
||||
image,
|
||||
line_width=None, # default auto-size
|
||||
font_size=None, # default auto-size
|
||||
font="Arial.ttf", # must be ImageFont compatible
|
||||
pil=False, # use PIL, otherwise uses OpenCV
|
||||
)
|
||||
|
||||
xyxy_boxes = np.array(
|
||||
[[ 5, 22.878, 231.27, 804.98, 756.83,], # class-idx x1 y1 x2 y2
|
||||
[ 0, 48.552, 398.56, 245.35, 902.71,],
|
||||
[ 0, 669.47, 392.19, 809.72, 877.04,],
|
||||
[ 0, 221.52, 405.8, 344.98, 857.54,],
|
||||
[ 0, 0, 550.53, 63.01, 873.44,],
|
||||
[11, 0.0584, 254.46, 32.561, 324.87,]]
|
||||
)
|
||||
|
||||
for nb, box in enumerate(xyxy_boxes):
|
||||
c_idx, *box = box
|
||||
label = f"{str(nb).zfill(2)}:{names.get(int(c_idx))}"
|
||||
ann.box_label(box, label, color=colors(c_idx, bgr=True))
|
||||
|
||||
image_with_bboxes = ann.result()
|
||||
```
|
||||
|
||||
1. Names can be used from `model.names` when [working with detection results](../modes/predict.md#working-with-results)
|
||||
|
||||
#### Oriented Bounding Boxes (OBB)
|
||||
```python
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
from ultralytics.utils.plotting import Annotator, colors
|
||||
|
||||
obb_names = {10: "small vehicle"}
|
||||
obb_image = cv.imread("datasets/dota8/images/train/P1142__1024__0___824.jpg")
|
||||
obb_boxes = np.array(
|
||||
[[ 0, 635, 560, 919, 719, 1087, 420, 803, 261,], # class-idx x1 y1 x2 y2 x3 y2 x4 y4
|
||||
[ 0, 331, 19, 493, 260, 776, 70, 613, -171,],
|
||||
[ 9, 869, 161, 886, 147, 851, 101, 833, 115,]
|
||||
]
|
||||
)
|
||||
ann = Annotator(
|
||||
obb_image,
|
||||
line_width=None, # default auto-size
|
||||
font_size=None, # default auto-size
|
||||
font="Arial.ttf", # must be ImageFont compatible
|
||||
pil=False, # use PIL, otherwise uses OpenCV
|
||||
)
|
||||
for obb in obb_boxes:
|
||||
c_idx, *obb = obb
|
||||
obb = np.array(obb).reshape(-1, 4, 2).squeeze()
|
||||
label = f"{names.get(int(c_idx))}"
|
||||
ann.box_label(
|
||||
obb,
|
||||
label,
|
||||
color=colors(c_idx, True),
|
||||
rotated=True,
|
||||
)
|
||||
|
||||
image_with_obb = ann.result()
|
||||
```
|
||||
|
||||
See the [`Annotator` Reference Page](../reference/utils/plotting.md#ultralytics.utils.plotting.Annotator) for additional insight.
|
||||
|
||||
## Miscellaneous
|
||||
|
||||
### Code Profiling
|
||||
|
||||
Check duration for code to run/process either using `with` or as a decorator.
|
||||
|
||||
```python
|
||||
from ultralytics.utils.ops import Profile
|
||||
|
||||
with Profile(device=device) as dt:
|
||||
pass # operation to measure
|
||||
|
||||
print(dt)
|
||||
>>> "Elapsed time is 9.5367431640625e-07 s"
|
||||
```
|
||||
|
||||
### Ultralytics Supported Formats
|
||||
|
||||
Want or need to use the formats of [images or videos types supported](../modes/predict.md#image-and-video-formats) by Ultralytics programmatically? Use these constants if you need.
|
||||
|
||||
```python
|
||||
from ultralytics.data.utils import IMG_FORMATS
|
||||
from ultralytics.data.utils import VID_FORMATS
|
||||
|
||||
print(IMG_FORMATS)
|
||||
>>> ('bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp', 'pfm')
|
||||
```
|
||||
|
||||
### Make Divisible
|
||||
|
||||
Calculates the nearest whole number to `x` to make evenly divisible when divided by `y`.
|
||||
|
||||
```python
|
||||
from ultralytics.utils.ops import make_divisible
|
||||
|
||||
make_divisible(7, 3)
|
||||
>>> 9
|
||||
make_divisible(7, 2)
|
||||
>>> 8
|
||||
```
|
Reference in New Issue
Block a user