添加状态分类和液面分割
This commit is contained in:
224
ultralytics_yolov8-main/docs/en/usage/callbacks.md
Normal file
224
ultralytics_yolov8-main/docs/en/usage/callbacks.md
Normal file
@ -0,0 +1,224 @@
|
||||
---
|
||||
comments: true
|
||||
description: Explore Ultralytics callbacks for training, validation, exporting, and prediction. Learn how to use and customize them for your ML models.
|
||||
keywords: Ultralytics, callbacks, training, validation, export, prediction, ML models, YOLOv8, Python, machine learning
|
||||
---
|
||||
|
||||
## 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):
|
||||
"""Handle prediction batch end by combining results with corresponding frames; modifies predictor results."""
|
||||
_, 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("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 |
|
||||
|
||||
## FAQ
|
||||
|
||||
### What are Ultralytics callbacks and how can I use them?
|
||||
|
||||
**Ultralytics callbacks** are specialized entry points triggered during key stages of model operations like training, validation, exporting, and prediction. These callbacks allow for custom functionality at specific points in the process, enabling enhancements and modifications to the workflow. Each callback accepts a `Trainer`, `Validator`, or `Predictor` object, depending on the operation type. For detailed properties of these objects, refer to the [Reference section](../reference/cfg/__init__.md).
|
||||
|
||||
To use a callback, you can define a function and then add it to the model with the `add_callback` method. Here's an example of how to return additional information during prediction:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def on_predict_batch_end(predictor):
|
||||
"""Handle prediction batch end by combining results with corresponding frames; modifies predictor results."""
|
||||
_, image, _, _ = predictor.batch
|
||||
image = image if isinstance(image, list) else [image]
|
||||
predictor.results = zip(predictor.results, image)
|
||||
|
||||
|
||||
model = YOLO("yolov8n.pt")
|
||||
model.add_callback("on_predict_batch_end", on_predict_batch_end)
|
||||
for result, frame in model.predict():
|
||||
pass
|
||||
```
|
||||
|
||||
### How can I customize Ultralytics training routine using callbacks?
|
||||
|
||||
To customize your Ultralytics training routine using callbacks, you can inject your logic at specific stages of the training process. Ultralytics YOLO provides a variety of training callbacks such as `on_train_start`, `on_train_end`, and `on_train_batch_end`. These allow you to add custom metrics, processing, or logging.
|
||||
|
||||
Here's an example of how to log additional metrics at the end of each training epoch:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def on_train_epoch_end(trainer):
|
||||
"""Custom logic for additional metrics logging at the end of each training epoch."""
|
||||
additional_metric = compute_additional_metric(trainer)
|
||||
trainer.log({"additional_metric": additional_metric})
|
||||
|
||||
|
||||
model = YOLO("yolov8n.pt")
|
||||
model.add_callback("on_train_epoch_end", on_train_epoch_end)
|
||||
model.train(data="coco.yaml", epochs=10)
|
||||
```
|
||||
|
||||
Refer to the [Training Guide](../modes/train.md) for more details on how to effectively use training callbacks.
|
||||
|
||||
### Why should I use callbacks during validation in Ultralytics YOLO?
|
||||
|
||||
Using **callbacks during validation** in Ultralytics YOLO can enhance model evaluation by allowing custom processing, logging, or metrics calculation. Callbacks such as `on_val_start`, `on_val_batch_end`, and `on_val_end` provide entry points to inject custom logic, ensuring detailed and comprehensive validation processes.
|
||||
|
||||
For instance, you might want to log additional validation metrics or save intermediate results for further analysis. Here's an example of how to log custom metrics at the end of validation:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def on_val_end(validator):
|
||||
"""Log custom metrics at end of validation."""
|
||||
custom_metric = compute_custom_metric(validator)
|
||||
validator.log({"custom_metric": custom_metric})
|
||||
|
||||
|
||||
model = YOLO("yolov8n.pt")
|
||||
model.add_callback("on_val_end", on_val_end)
|
||||
model.val(data="coco.yaml")
|
||||
```
|
||||
|
||||
Check out the [Validation Guide](../modes/val.md) for further insights on incorporating callbacks into your validation process.
|
||||
|
||||
### How do I attach a custom callback for the prediction mode in Ultralytics YOLO?
|
||||
|
||||
To attach a custom callback for the **prediction mode** in Ultralytics YOLO, you define a callback function and register it with the prediction process. Common prediction callbacks include `on_predict_start`, `on_predict_batch_end`, and `on_predict_end`. These allow for modification of prediction outputs and integration of additional functionalities like data logging or result transformation.
|
||||
|
||||
Here is an example where a custom callback is used to log predictions:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def on_predict_end(predictor):
|
||||
"""Log predictions at the end of prediction."""
|
||||
for result in predictor.results:
|
||||
log_prediction(result)
|
||||
|
||||
|
||||
model = YOLO("yolov8n.pt")
|
||||
model.add_callback("on_predict_end", on_predict_end)
|
||||
results = model.predict(source="image.jpg")
|
||||
```
|
||||
|
||||
For more comprehensive usage, refer to the [Prediction Guide](../modes/predict.md) which includes detailed instructions and additional customization options.
|
||||
|
||||
### What are some practical examples of using callbacks in Ultralytics YOLO?
|
||||
|
||||
Ultralytics YOLO supports various practical implementations of callbacks to enhance and customize different phases like training, validation, and prediction. Some practical examples include:
|
||||
|
||||
1. **Logging Custom Metrics**: Log additional metrics at different stages, such as the end of training or validation epochs.
|
||||
2. **Data Augmentation**: Implement custom data transformations or augmentations during prediction or training batches.
|
||||
3. **Intermediate Results**: Save intermediate results such as predictions or frames for further analysis or visualization.
|
||||
|
||||
Example: Combining frames with prediction results during prediction using `on_predict_batch_end`:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def on_predict_batch_end(predictor):
|
||||
"""Combine prediction results with frames."""
|
||||
_, image, _, _ = predictor.batch
|
||||
image = image if isinstance(image, list) else [image]
|
||||
predictor.results = zip(predictor.results, image)
|
||||
|
||||
|
||||
model = YOLO("yolov8n.pt")
|
||||
model.add_callback("on_predict_batch_end", on_predict_batch_end)
|
||||
for result, frame in model.predict():
|
||||
pass
|
||||
```
|
||||
|
||||
Explore the [Complete Callback Reference](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/callbacks/base.py) to find more options and examples.
|
||||
302
ultralytics_yolov8-main/docs/en/usage/cfg.md
Normal file
302
ultralytics_yolov8-main/docs/en/usage/cfg.md
Normal file
@ -0,0 +1,302 @@
|
||||
---
|
||||
comments: true
|
||||
description: Optimize your YOLO model's performance with the right settings and hyperparameters. Learn about training, validation, and prediction configurations.
|
||||
keywords: YOLO, hyperparameters, configuration, training, validation, prediction, model settings, Ultralytics, performance optimization, machine learning
|
||||
---
|
||||
|
||||
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., `coco8.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, with three modes: set as an integer (e.g., `batch=16`), auto mode for 60% GPU memory utilization (`batch=-1`), or auto mode with specified utilization fraction (`batch=0.70`). |
|
||||
| `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. |
|
||||
|
||||
!!! info "Note on Batch-size Settings"
|
||||
|
||||
The `batch` argument can be configured in three ways:
|
||||
|
||||
- **Fixed Batch Size**: Set an integer value (e.g., `batch=16`), specifying the number of images per batch directly.
|
||||
- **Auto Mode (60% GPU Memory)**: Use `batch=-1` to automatically adjust batch size for approximately 60% CUDA memory utilization.
|
||||
- **Auto Mode with Utilization Fraction**: Set a fraction value (e.g., `batch=0.70`) to adjust batch size based on the specified fraction of GPU memory usage.
|
||||
|
||||
[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., `coco8.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.
|
||||
|
||||
{% include "macros/export-args.md" %}
|
||||
|
||||
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 - 0.9` | Randomly erases a portion of the image during classification training, encouraging the model to focus on less obvious features for recognition. |
|
||||
| `crop_fraction` | `float` | `1.0` | `0.1 - 1.0` | Crops the classification image to a fraction of its size to emphasize central features and adapt to object scales, reducing background distractions. |
|
||||
|
||||
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. |
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I improve the performance of my YOLO model during training?
|
||||
|
||||
Improving YOLO model performance involves tuning hyperparameters like batch size, learning rate, momentum, and weight decay. Adjusting augmentation settings, selecting the right optimizer, and employing techniques like early stopping or mixed precision can also help. For detailed guidance on training settings, refer to the [Train Guide](../modes/train.md).
|
||||
|
||||
### What are the key hyperparameters to consider for YOLO model accuracy?
|
||||
|
||||
Key hyperparameters affecting YOLO model accuracy include:
|
||||
|
||||
- **Batch Size (`batch`)**: Larger batch sizes can stabilize training but may require more memory.
|
||||
- **Learning Rate (`lr0`)**: Controls the step size for weight updates; smaller rates offer fine adjustments but slow convergence.
|
||||
- **Momentum (`momentum`)**: Helps accelerate gradient vectors in the right directions, dampening oscillations.
|
||||
- **Image Size (`imgsz`)**: Larger image sizes can improve accuracy but increase computational load.
|
||||
|
||||
Adjust these values based on your dataset and hardware capabilities. Explore more in the [Train Settings](#train-settings) section.
|
||||
|
||||
### How do I set the learning rate for training a YOLO model?
|
||||
|
||||
The learning rate (`lr0`) is crucial for optimization. A common starting point is `0.01` for SGD or `0.001` for Adam. It's essential to monitor training metrics and adjust if necessary. Use cosine learning rate schedulers (`cos_lr`) or warmup techniques (`warmup_epochs`, `warmup_momentum`) to dynamically modify the rate during training. Find more details in the [Train Guide](../modes/train.md).
|
||||
|
||||
### What are the default inference settings for YOLO models?
|
||||
|
||||
Default inference settings include:
|
||||
|
||||
- **Confidence Threshold (`conf=0.25`)**: Minimum confidence for detections.
|
||||
- **IoU Threshold (`iou=0.7`)**: For Non-Maximum Suppression (NMS).
|
||||
- **Image Size (`imgsz=640`)**: Resizes input images prior to inference.
|
||||
- **Device (`device=None`)**: Selects CPU or GPU for inference.
|
||||
For a comprehensive overview, visit the [Predict Settings](#predict-settings) section and the [Predict Guide](../modes/predict.md).
|
||||
|
||||
### Why should I use mixed precision training with YOLO models?
|
||||
|
||||
Mixed precision training, enabled with `amp=True`, helps reduce memory usage and can speed up training by utilizing the advantages of both FP16 and FP32. This is beneficial for modern GPUs, which support mixed precision natively, allowing more models to fit in memory and enable faster computations without significant loss in accuracy. Learn more about this in the [Train Guide](../modes/train.md).
|
||||
270
ultralytics_yolov8-main/docs/en/usage/cli.md
Normal file
270
ultralytics_yolov8-main/docs/en/usage/cli.md
Normal file
@ -0,0 +1,270 @@
|
||||
---
|
||||
comments: true
|
||||
description: Explore the YOLOv8 command line interface (CLI) for easy execution of detection tasks without needing a Python environment.
|
||||
keywords: YOLOv8 CLI, command line interface, YOLOv8 commands, detection tasks, Ultralytics, model training, model prediction
|
||||
---
|
||||
|
||||
# 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, pose, obb]
|
||||
MODE (required) is one of [train, val, predict, export, track, benchmark]
|
||||
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=coco8.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=coco8.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, pose, obb]`. 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, benchmark]`
|
||||
- `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`
|
||||
|
||||
!!! 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 COCO8 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 COCO8 for 100 epochs at image-size 640.
|
||||
```bash
|
||||
yolo detect train data=coco8.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 COCO8 dataset. No argument need to passed as the `model` retains its 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'`.
|
||||
|
||||
{% include "macros/export-table.md" %}
|
||||
|
||||
See full `export` details in the [Export](../modes/export.md) page.
|
||||
|
||||
## 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=coco8.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=coco8.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
|
||||
```
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I use the Ultralytics YOLOv8 command line interface (CLI) for model training?
|
||||
|
||||
To train a YOLOv8 model using the CLI, you can execute a simple one-line command in the terminal. For example, to train a detection model for 10 epochs with a learning rate of 0.01, you would run:
|
||||
|
||||
```bash
|
||||
yolo train data=coco8.yaml model=yolov8n.pt epochs=10 lr0=0.01
|
||||
```
|
||||
|
||||
This command uses the `train` mode with specific arguments. Refer to the full list of available arguments in the [Configuration Guide](cfg.md).
|
||||
|
||||
### What tasks can I perform with the Ultralytics YOLOv8 CLI?
|
||||
|
||||
The Ultralytics YOLOv8 CLI supports a variety of tasks including detection, segmentation, classification, validation, prediction, export, and tracking. For instance:
|
||||
|
||||
- **Train a Model**: Run `yolo train data=<data.yaml> model=<model.pt> epochs=<num>`.
|
||||
- **Run Predictions**: Use `yolo predict model=<model.pt> source=<data_source> imgsz=<image_size>`.
|
||||
- **Export a Model**: Execute `yolo export model=<model.pt> format=<export_format>`.
|
||||
|
||||
Each task can be customized with various arguments. For detailed syntax and examples, see the respective sections like [Train](#train), [Predict](#predict), and [Export](#export).
|
||||
|
||||
### How can I validate the accuracy of a trained YOLOv8 model using the CLI?
|
||||
|
||||
To validate a YOLOv8 model's accuracy, use the `val` mode. For example, to validate a pretrained detection model with a batch size of 1 and image size of 640, run:
|
||||
|
||||
```bash
|
||||
yolo val model=yolov8n.pt data=coco8.yaml batch=1 imgsz=640
|
||||
```
|
||||
|
||||
This command evaluates the model on the specified dataset and provides performance metrics. For more details, refer to the [Val](#val) section.
|
||||
|
||||
### What formats can I export my YOLOv8 models to using the CLI?
|
||||
|
||||
YOLOv8 models can be exported to various formats such as ONNX, CoreML, TensorRT, and more. For instance, to export a model to ONNX format, run:
|
||||
|
||||
```bash
|
||||
yolo export model=yolov8n.pt format=onnx
|
||||
```
|
||||
|
||||
For complete details, visit the [Export](../modes/export.md) page.
|
||||
|
||||
### How do I customize YOLOv8 CLI commands to override default arguments?
|
||||
|
||||
To override default arguments in YOLOv8 CLI commands, pass them as `arg=value` pairs. For example, to train a model with custom arguments, use:
|
||||
|
||||
```bash
|
||||
yolo train data=coco8.yaml model=yolov8n.pt epochs=10 lr0=0.01
|
||||
```
|
||||
|
||||
For a full list of available arguments and their descriptions, refer to the [Configuration Guide](cfg.md). Ensure arguments are formatted correctly, as shown in the [Overriding default arguments](#overriding-default-arguments) section.
|
||||
179
ultralytics_yolov8-main/docs/en/usage/engine.md
Normal file
179
ultralytics_yolov8-main/docs/en/usage/engine.md
Normal file
@ -0,0 +1,179 @@
|
||||
---
|
||||
comments: true
|
||||
description: Learn to customize the YOLOv8 Trainer for specific tasks. Step-by-step instructions with Python examples for maximum model performance.
|
||||
keywords: Ultralytics, YOLOv8, Trainer Customization, Python, Machine Learning, AI, Model Training, DetectionTrainer, Custom Models
|
||||
---
|
||||
|
||||
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):
|
||||
"""Loads a custom detection model given configuration and weight files."""
|
||||
...
|
||||
|
||||
|
||||
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):
|
||||
"""Initializes the loss function and adds a callback for uploading the model to Google Drive every 10 epochs."""
|
||||
...
|
||||
|
||||
|
||||
class CustomTrainer(DetectionTrainer):
|
||||
def get_model(self, cfg, weights):
|
||||
"""Returns a customized detection model instance configured with specified config and weights."""
|
||||
return MyCustomModel(...)
|
||||
|
||||
|
||||
# callback to upload model weights
|
||||
def log_model(trainer):
|
||||
"""Logs the path of the last model weight used by the 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.
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I customize the Ultralytics YOLOv8 DetectionTrainer for specific tasks?
|
||||
|
||||
To customize the Ultralytics YOLOv8 `DetectionTrainer` for a specific task, you can override its methods to adapt to your custom model and dataloader. Start by inheriting from `DetectionTrainer` and then redefine methods like `get_model` to implement your custom functionalities. Here's an example:
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
|
||||
class CustomTrainer(DetectionTrainer):
|
||||
def get_model(self, cfg, weights):
|
||||
"""Loads a custom detection model given configuration and weight files."""
|
||||
...
|
||||
|
||||
|
||||
trainer = CustomTrainer(overrides={...})
|
||||
trainer.train()
|
||||
trained_model = trainer.best # get best model
|
||||
```
|
||||
|
||||
For further customization like changing the `loss function` or adding a `callback`, you can reference our [Callbacks Guide](../usage/callbacks.md).
|
||||
|
||||
### What are the key components of the BaseTrainer in Ultralytics YOLOv8?
|
||||
|
||||
The `BaseTrainer` in Ultralytics YOLOv8 serves as the foundation for training routines and can be customized for various tasks by overriding its generic methods. Key components include:
|
||||
|
||||
- `get_model(cfg, weights)` to build the model to be trained.
|
||||
- `get_dataloader()` to build the dataloader.
|
||||
|
||||
For more details on the customization and source code, see the [`BaseTrainer` Reference](../reference/engine/trainer.md).
|
||||
|
||||
### How can I add a callback to the Ultralytics YOLOv8 DetectionTrainer?
|
||||
|
||||
You can add callbacks to monitor and modify the training process in Ultralytics YOLOv8 `DetectionTrainer`. For instance, here's how you can add a callback to log model weights after every training epoch:
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
|
||||
# callback to upload model weights
|
||||
def log_model(trainer):
|
||||
"""Logs the path of the last model weight used by the trainer."""
|
||||
last_weight_path = trainer.last
|
||||
print(last_weight_path)
|
||||
|
||||
|
||||
trainer = DetectionTrainer(overrides={...})
|
||||
trainer.add_callback("on_train_epoch_end", log_model) # Adds to existing callbacks
|
||||
trainer.train()
|
||||
```
|
||||
|
||||
For further details on callback events and entry points, refer to our [Callbacks Guide](../usage/callbacks.md).
|
||||
|
||||
### Why should I use Ultralytics YOLOv8 for model training?
|
||||
|
||||
Ultralytics YOLOv8 offers a high-level abstraction on powerful engine executors, making it ideal for rapid development and customization. Key benefits include:
|
||||
|
||||
- **Ease of Use**: Both command-line and Python interfaces simplify complex tasks.
|
||||
- **Performance**: Optimized for real-time object detection and various vision AI applications.
|
||||
- **Customization**: Easily extendable for custom models, loss functions, and dataloaders.
|
||||
|
||||
Learn more about YOLOv8's capabilities by visiting [Ultralytics YOLO](https://www.ultralytics.com/yolo).
|
||||
|
||||
### Can I use the Ultralytics YOLOv8 DetectionTrainer for non-standard models?
|
||||
|
||||
Yes, Ultralytics YOLOv8 `DetectionTrainer` is highly flexible and can be customized for non-standard models. By inheriting from `DetectionTrainer`, you can overload different methods to support your specific model's needs. Here's a simple example:
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
|
||||
class CustomDetectionTrainer(DetectionTrainer):
|
||||
def get_model(self, cfg, weights):
|
||||
"""Loads a custom detection model."""
|
||||
...
|
||||
|
||||
|
||||
trainer = CustomDetectionTrainer(overrides={...})
|
||||
trainer.train()
|
||||
```
|
||||
|
||||
For more comprehensive instructions and examples, review the [DetectionTrainer](../reference/engine/trainer.md) documentation.
|
||||
418
ultralytics_yolov8-main/docs/en/usage/python.md
Normal file
418
ultralytics_yolov8-main/docs/en/usage/python.md
Normal file
@ -0,0 +1,418 @@
|
||||
---
|
||||
comments: true
|
||||
description: Learn to integrate YOLOv8 in Python for object detection, segmentation, and classification. Load, train models, and make predictions easily with our comprehensive guide.
|
||||
keywords: YOLOv8, Python, object detection, segmentation, classification, machine learning, AI, pretrained models, train models, make predictions
|
||||
---
|
||||
|
||||
# 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 'coco8.yaml' dataset for 3 epochs
|
||||
results = model.train(data="coco8.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="coco8.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
|
||||
|
||||
# Load a YOLOv8 model
|
||||
model = YOLO("yolov8n.yaml")
|
||||
|
||||
# Train the model
|
||||
model.train(data="coco8.yaml", epochs=5)
|
||||
|
||||
# Validate on training data
|
||||
model.val()
|
||||
```
|
||||
|
||||
=== "Val on another dataset"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a YOLOv8 model
|
||||
model = YOLO("yolov8n.yaml")
|
||||
|
||||
# Train the model
|
||||
model.train(data="coco8.yaml", epochs=5)
|
||||
|
||||
# Validate on separate data
|
||||
model.val(data="path/to/separate/data.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
|
||||
import cv2
|
||||
from PIL import Image
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
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="coco8.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="coco8.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 DetectionPredictor, DetectionTrainer, DetectionValidator
|
||||
|
||||
# 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 }
|
||||
|
||||
## FAQ
|
||||
|
||||
### How can I integrate YOLOv8 into my Python project for object detection?
|
||||
|
||||
Integrating Ultralytics YOLOv8 into your Python projects is simple. You can load a pre-trained model or train a new model from scratch. Here's how to get started:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO model
|
||||
model = YOLO("yolov8n.pt")
|
||||
|
||||
# Perform object detection on an image
|
||||
results = model("https://ultralytics.com/images/bus.jpg")
|
||||
|
||||
# Visualize the results
|
||||
for result in results:
|
||||
result.show()
|
||||
```
|
||||
|
||||
See more detailed examples in our [Predict Mode](../modes/predict.md) section.
|
||||
|
||||
### What are the different modes available in YOLOv8?
|
||||
|
||||
Ultralytics YOLOv8 provides various modes to cater to different machine learning workflows. These include:
|
||||
|
||||
- **[Train](../modes/train.md)**: Train a model using custom datasets.
|
||||
- **[Val](../modes/val.md)**: Validate model performance on a validation set.
|
||||
- **[Predict](../modes/predict.md)**: Make predictions on new images or video streams.
|
||||
- **[Export](../modes/export.md)**: Export models to various formats like ONNX, TensorRT.
|
||||
- **[Track](../modes/track.md)**: Real-time object tracking in video streams.
|
||||
- **[Benchmark](../modes/benchmark.md)**: Benchmark model performance across different configurations.
|
||||
|
||||
Each mode is designed to provide comprehensive functionalities for different stages of model development and deployment.
|
||||
|
||||
### How do I train a custom YOLOv8 model using my dataset?
|
||||
|
||||
To train a custom YOLOv8 model, you need to specify your dataset and other hyperparameters. Here's a quick example:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load the YOLO model
|
||||
model = YOLO("yolov8n.yaml")
|
||||
|
||||
# Train the model with custom dataset
|
||||
model.train(data="path/to/your/dataset.yaml", epochs=10)
|
||||
```
|
||||
|
||||
For more details on training and hyperlinks to example usage, visit our [Train Mode](../modes/train.md) page.
|
||||
|
||||
### How do I export YOLOv8 models for deployment?
|
||||
|
||||
Exporting YOLOv8 models in a format suitable for deployment is straightforward with the `export` function. For example, you can export a model to ONNX format:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load the YOLO model
|
||||
model = YOLO("yolov8n.pt")
|
||||
|
||||
# Export the model to ONNX format
|
||||
model.export(format="onnx")
|
||||
```
|
||||
|
||||
For various export options, refer to the [Export Mode](../modes/export.md) documentation.
|
||||
|
||||
### Can I validate my YOLOv8 model on different datasets?
|
||||
|
||||
Yes, validating YOLOv8 models on different datasets is possible. After training, you can use the validation mode to evaluate the performance:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a YOLOv8 model
|
||||
model = YOLO("yolov8n.yaml")
|
||||
|
||||
# Train the model
|
||||
model.train(data="coco8.yaml", epochs=5)
|
||||
|
||||
# Validate the model on a different dataset
|
||||
model.val(data="path/to/separate/data.yaml")
|
||||
```
|
||||
|
||||
Check the [Val Mode](../modes/val.md) page for detailed examples and usage.
|
||||
647
ultralytics_yolov8-main/docs/en/usage/simple-utilities.md
Normal file
647
ultralytics_yolov8-main/docs/en/usage/simple-utilities.md
Normal file
@ -0,0 +1,647 @@
|
||||
---
|
||||
comments: true
|
||||
description: Explore essential utilities in the Ultralytics package to speed up and enhance your workflows. Learn about data processing, annotations, conversions, and more.
|
||||
keywords: Ultralytics, utilities, data processing, auto annotation, YOLO, dataset conversion, bounding boxes, image compression, machine learning tools
|
||||
---
|
||||
|
||||
# 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.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/1bPY2LRG590"
|
||||
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> Ultralytics Utilities | Auto Annotation, Explorer API and Dataset Conversion
|
||||
</p>
|
||||
|
||||
## 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 Segmentation Masks into YOLO Format
|
||||
|
||||

|
||||
|
||||
Use to convert a dataset of segmentation mask images to the `YOLO` segmentation format.
|
||||
This function takes the directory containing the binary format mask images and converts them into YOLO segmentation format.
|
||||
|
||||
The converted masks will be saved in the specified output directory.
|
||||
|
||||
```python
|
||||
from ultralytics.data.converter import convert_segment_masks_to_yolo_seg
|
||||
|
||||
# The classes here is the total classes in the dataset, for COCO dataset we have 80 classes
|
||||
convert_segment_masks_to_yolo_seg(masks_dir="path/to/masks_dir", output_dir="path/to/output_dir", classes=80)
|
||||
```
|
||||
|
||||
### 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)
|
||||
|
||||
### Get Bounding Box Dimensions
|
||||
|
||||
```{.py .annotate }
|
||||
from ultralytics.utils.plotting import Annotator
|
||||
from ultralytics import YOLO
|
||||
import cv2
|
||||
|
||||
model = YOLO('yolov8n.pt') # Load pretrain or fine-tune model
|
||||
|
||||
# Process the image
|
||||
source = cv2.imread('path/to/image.jpg')
|
||||
results = model(source)
|
||||
|
||||
# Extract results
|
||||
annotator = Annotator(source, example=model.names)
|
||||
|
||||
for box in results[0].boxes.xyxy.cpu():
|
||||
width, height, area = annotator.get_bbox_dimension(box)
|
||||
print("Bounding Box Width {}, Height {}, Area {}".format(
|
||||
width.item(), height.item(), area.item()))
|
||||
```
|
||||
|
||||
### 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
|
||||
import numpy as np
|
||||
|
||||
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#train-settings).
|
||||
|
||||
```{ .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
|
||||
import numpy as np
|
||||
|
||||
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")
|
||||
print(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,
|
||||
)
|
||||
|
||||
print(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)
|
||||
|
||||
print(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 (
|
||||
ltwh2xywh,
|
||||
ltwh2xyxy,
|
||||
xywh2ltwh, # xywh → top-left corner, w, h
|
||||
xywh2xyxy,
|
||||
xywhn2xyxy, # normalized → pixel
|
||||
xyxy2ltwh, # xyxy → top-left corner, w, h
|
||||
xyxy2xywhn, # pixel → normalized
|
||||
)
|
||||
|
||||
for func in (ltwh2xywh, ltwh2xyxy, xywh2ltwh, xywh2xyxy, xywhn2xyxy, xyxy2ltwh, xyxy2xywhn):
|
||||
print(help(func)) # print function docstrings
|
||||
```
|
||||
|
||||
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"{obb_names.get(int(c_idx))}"
|
||||
ann.box_label(
|
||||
obb,
|
||||
label,
|
||||
color=colors(c_idx, True),
|
||||
rotated=True,
|
||||
)
|
||||
|
||||
image_with_obb = ann.result()
|
||||
```
|
||||
|
||||
#### Bounding Boxes Circle Annotation ([Circle Label](https://docs.ultralytics.com/reference/utils/plotting/#ultralytics.utils.plotting.Annotator.circle_label))
|
||||
|
||||
```python
|
||||
import cv2
|
||||
|
||||
from ultralytics import YOLO
|
||||
from ultralytics.utils.plotting import Annotator, colors
|
||||
|
||||
model = YOLO("yolov8s.pt")
|
||||
cap = cv2.VideoCapture("path/to/video/file.mp4")
|
||||
|
||||
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
|
||||
writer = cv2.VideoWriter("Ultralytics circle annotation.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
|
||||
|
||||
while True:
|
||||
ret, im0 = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
annotator = Annotator(im0, line_width=2)
|
||||
|
||||
results = model.predict(im0)
|
||||
boxes = results[0].boxes.xyxy.cpu()
|
||||
clss = results[0].boxes.cls.cpu().tolist()
|
||||
|
||||
for box, cls in zip(boxes, clss):
|
||||
x1, y1 = int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)
|
||||
annotator.circle_label(box, label=model.names[int(cls)], color=colors(int(cls), True))
|
||||
|
||||
writer.write(im0)
|
||||
cv2.imshow("Ultralytics circle annotation", im0)
|
||||
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
|
||||
writer.release()
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
#### Bounding Boxes Text Annotation ([Text Label](https://docs.ultralytics.com/reference/utils/plotting/#ultralytics.utils.plotting.Annotator.text_label))
|
||||
|
||||
```python
|
||||
import cv2
|
||||
|
||||
from ultralytics import YOLO
|
||||
from ultralytics.utils.plotting import Annotator, colors
|
||||
|
||||
model = YOLO("yolov8s.pt")
|
||||
cap = cv2.VideoCapture("path/to/video/file.mp4")
|
||||
|
||||
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
|
||||
writer = cv2.VideoWriter("Ultralytics text annotation.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
|
||||
|
||||
while True:
|
||||
ret, im0 = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
annotator = Annotator(im0, line_width=2)
|
||||
|
||||
results = model.predict(im0)
|
||||
boxes = results[0].boxes.xyxy.cpu()
|
||||
clss = results[0].boxes.cls.cpu().tolist()
|
||||
|
||||
for box, cls in zip(boxes, clss):
|
||||
x1, y1 = int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)
|
||||
annotator.text_label(box, label=model.names[int(cls)], color=colors(int(cls), True))
|
||||
|
||||
writer.write(im0)
|
||||
cv2.imshow("Ultralytics text annotation", im0)
|
||||
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
|
||||
writer.release()
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
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="cuda:0") 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, VID_FORMATS
|
||||
|
||||
print(IMG_FORMATS)
|
||||
# {'tiff', 'pfm', 'bmp', 'mpo', 'dng', 'jpeg', 'png', 'webp', 'tif', 'jpg'}
|
||||
|
||||
print(VID_FORMATS)
|
||||
# {'avi', 'mpg', 'wmv', 'mpeg', 'm4v', 'mov', 'mp4', 'asf', 'mkv', 'ts', 'gif', 'webm'}
|
||||
```
|
||||
|
||||
### 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
|
||||
```
|
||||
|
||||
## FAQ
|
||||
|
||||
### What utilities are included in the Ultralytics package to enhance machine learning workflows?
|
||||
|
||||
The Ultralytics package includes a variety of utilities designed to streamline and optimize machine learning workflows. Key utilities include [auto-annotation](../reference/data/annotator.md#ultralytics.data.annotator.auto_annotate) for labeling datasets, converting COCO to YOLO format with [convert_coco](../reference/data/converter.md#ultralytics.data.converter.convert_coco), compressing images, and dataset auto-splitting. These tools aim to reduce manual effort, ensure consistency, and enhance data processing efficiency.
|
||||
|
||||
### How can I use Ultralytics to auto-label my dataset?
|
||||
|
||||
If you have a pre-trained Ultralytics YOLO object detection model, you can use it with the [SAM](../models/sam.md) model to auto-annotate your dataset in segmentation format. Here's an example:
|
||||
|
||||
```python
|
||||
from ultralytics.data.annotator import auto_annotate
|
||||
|
||||
auto_annotate(
|
||||
data="path/to/new/data",
|
||||
det_model="yolov8n.pt",
|
||||
sam_model="mobile_sam.pt",
|
||||
device="cuda",
|
||||
output_dir="path/to/save_labels",
|
||||
)
|
||||
```
|
||||
|
||||
For more details, check the [auto_annotate reference section](../reference/data/annotator.md#ultralytics.data.annotator.auto_annotate).
|
||||
|
||||
### How do I convert COCO dataset annotations to YOLO format in Ultralytics?
|
||||
|
||||
To convert COCO JSON annotations into YOLO format for object detection, you can use the `convert_coco` utility. Here's a sample code snippet:
|
||||
|
||||
```python
|
||||
from ultralytics.data.converter import convert_coco
|
||||
|
||||
convert_coco(
|
||||
"../datasets/coco/annotations/",
|
||||
use_segments=False,
|
||||
use_keypoints=False,
|
||||
cls91to80=True,
|
||||
)
|
||||
```
|
||||
|
||||
For additional information, visit the [convert_coco reference page](../reference/data/converter.md#ultralytics.data.converter.convert_coco).
|
||||
|
||||
### What is the purpose of the YOLO Data Explorer in the Ultralytics package?
|
||||
|
||||
The [YOLO Explorer](../datasets/explorer/index.md) is a powerful tool introduced in the `8.1.0` update to enhance dataset understanding. It allows you to use text queries to find object instances in your dataset, making it easier to analyze and manage your data. This tool provides valuable insights into dataset composition and distribution, helping to improve model training and performance.
|
||||
|
||||
### How can I convert bounding boxes to segments in Ultralytics?
|
||||
|
||||
To convert existing bounding box data (in `x y w h` format) to segments, you can use the `yolo_bbox2segment` function. Ensure your files are organized with separate directories for images and labels.
|
||||
|
||||
```python
|
||||
from ultralytics.data.converter import yolo_bbox2segment
|
||||
|
||||
yolo_bbox2segment(
|
||||
im_dir="path/to/images",
|
||||
save_dir=None, # saved to "labels-segment" in the images directory
|
||||
sam_model="sam_b.pt",
|
||||
)
|
||||
```
|
||||
|
||||
For more information, visit the [yolo_bbox2segment reference page](../reference/data/converter.md#ultralytics.data.converter.yolo_bbox2segment).
|
||||
Reference in New Issue
Block a user