单目3D初始代码
This commit is contained in:
181
docs/en/modes/benchmark.md
Executable file
181
docs/en/modes/benchmark.md
Executable file
@@ -0,0 +1,181 @@
|
||||
---
|
||||
comments: true
|
||||
description: Learn how to evaluate your YOLO26 model's performance in real-world scenarios using benchmark mode. Optimize speed, accuracy, and resource allocation across export formats.
|
||||
keywords: model benchmarking, YOLO26, Ultralytics, performance evaluation, export formats, ONNX, TensorRT, OpenVINO, CoreML, TensorFlow, optimization, mAP50-95, inference time
|
||||
---
|
||||
|
||||
# Model Benchmarking with Ultralytics YOLO
|
||||
|
||||
<img width="1024" src="https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/ultralytics-yolov8-ecosystem-integrations.avif" alt="Ultralytics YOLO ecosystem and integrations">
|
||||
|
||||
## Benchmark Visualization
|
||||
|
||||
!!! tip "Refresh Browser"
|
||||
|
||||
You may need to refresh the page to view the graphs correctly due to potential cookie issues.
|
||||
|
||||
<script async src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||
<script defer src="../../javascript/benchmark.js"></script>
|
||||
|
||||
<canvas id="modelComparisonChart" width="1024" height="400"></canvas>
|
||||
|
||||
## Introduction
|
||||
|
||||
Once your model is trained and validated, the next logical step is to evaluate its performance in various real-world scenarios. Benchmark mode in Ultralytics YOLO26 serves this purpose by providing a robust framework for assessing the speed and [accuracy](https://www.ultralytics.com/glossary/accuracy) of your model across a range of export formats.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/UF7pYdLSMng"
|
||||
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> Benchmark Ultralytics YOLO26 Models | How to Compare Model Performance on Different Hardware?
|
||||
</p>
|
||||
|
||||
## Why Is Benchmarking Crucial?
|
||||
|
||||
- **Informed Decisions:** Gain insights into the trade-offs between speed and accuracy.
|
||||
- **Resource Allocation:** Understand how different export formats perform on different hardware.
|
||||
- **Optimization:** Learn which export format offers the best performance for your specific use case.
|
||||
- **Cost Efficiency:** Make more efficient use of hardware resources based on benchmark results.
|
||||
|
||||
### Key Metrics in Benchmark Mode
|
||||
|
||||
- **mAP50-95:** For [object detection](https://www.ultralytics.com/glossary/object-detection), segmentation, and pose estimation.
|
||||
- **accuracy_top5:** For [image classification](https://www.ultralytics.com/glossary/image-classification).
|
||||
- **Inference Time:** Time taken for each image in milliseconds.
|
||||
|
||||
### Supported Export Formats
|
||||
|
||||
- **ONNX:** For optimal CPU performance
|
||||
- **TensorRT:** For maximal GPU efficiency
|
||||
- **OpenVINO:** For Intel hardware optimization
|
||||
- **CoreML, TensorFlow SavedModel, and More:** For diverse deployment needs.
|
||||
|
||||
!!! tip
|
||||
|
||||
* Export to ONNX or OpenVINO for up to 3x CPU speedup.
|
||||
* Export to TensorRT for up to 5x GPU speedup.
|
||||
|
||||
## Usage Examples
|
||||
|
||||
Run YOLO26n benchmarks across all supported export formats (ONNX, TensorRT, etc.). See the Arguments section below for a full list of export options.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics.utils.benchmarks import benchmark
|
||||
|
||||
# Benchmark on GPU
|
||||
benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, half=False, device=0)
|
||||
|
||||
# Benchmark specific export format
|
||||
benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, format="onnx")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo benchmark model=yolo26n.pt data='coco8.yaml' imgsz=640 half=False device=0
|
||||
|
||||
# Benchmark specific export format
|
||||
yolo benchmark model=yolo26n.pt data='coco8.yaml' imgsz=640 format=onnx
|
||||
```
|
||||
|
||||
## Arguments
|
||||
|
||||
Arguments such as `model`, `data`, `imgsz`, `half`, `device`, `verbose` and `format` provide users with the flexibility to fine-tune the benchmarks to their specific needs and compare the performance of different export formats with ease.
|
||||
|
||||
| Key | Default Value | Description |
|
||||
| --------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `model` | `None` | Specifies the path to the model file. Accepts both `.pt` and `.yaml` formats, e.g., `"yolo26n.pt"` for pretrained models or configuration files. |
|
||||
| `data` | `None` | Path to a YAML file defining the dataset for benchmarking, typically including paths and settings for [validation data](https://www.ultralytics.com/glossary/validation-data). Example: `"coco8.yaml"`. |
|
||||
| `imgsz` | `640` | The input image size for the model. Can be a single integer for square images or a tuple `(width, height)` for non-square, e.g., `(640, 480)`. |
|
||||
| `half` | `False` | Enables FP16 (half-precision) inference, reducing memory usage and possibly increasing speed on compatible hardware. Use `half=True` to enable. |
|
||||
| `int8` | `False` | Activates INT8 quantization for further optimized performance on supported devices, especially useful for edge devices. Set `int8=True` to use. |
|
||||
| `device` | `None` | Defines the computation device(s) for benchmarking, such as `"cpu"` or `"cuda:0"`. |
|
||||
| `verbose` | `False` | Controls the level of detail in logging output. Set `verbose=True` for detailed logs. |
|
||||
| `format` | `''` | Benchmarks only the specified export format (e.g., `format=onnx`). Leave it blank to test every supported format automatically. |
|
||||
|
||||
## Export Formats
|
||||
|
||||
Benchmarks will attempt to run automatically on all possible export formats listed below. Alternatively, you can run benchmarks for a specific format by using the `format` argument, which accepts any of the formats mentioned below.
|
||||
|
||||
{% include "macros/export-table.md" %}
|
||||
|
||||
See full `export` details in the [Export](../modes/export.md) page.
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I benchmark my YOLO26 model's performance using Ultralytics?
|
||||
|
||||
Ultralytics YOLO26 offers a Benchmark mode to assess your model's performance across different export formats. This mode provides insights into key metrics such as [mean Average Precision](https://www.ultralytics.com/glossary/mean-average-precision-map) (mAP50-95), accuracy, and inference time in milliseconds. To run benchmarks, you can use either Python or CLI commands. For example, to benchmark on a GPU:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics.utils.benchmarks import benchmark
|
||||
|
||||
# Benchmark on GPU
|
||||
benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, half=False, device=0)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo benchmark model=yolo26n.pt data='coco8.yaml' imgsz=640 half=False device=0
|
||||
```
|
||||
|
||||
For more details on benchmark arguments, visit the [Arguments](#arguments) section.
|
||||
|
||||
### What are the benefits of exporting YOLO26 models to different formats?
|
||||
|
||||
Exporting YOLO26 models to different formats such as [ONNX](https://docs.ultralytics.com/integrations/onnx/), [TensorRT](https://docs.ultralytics.com/integrations/tensorrt/), and [OpenVINO](https://docs.ultralytics.com/integrations/openvino/) allows you to optimize performance based on your deployment environment. For instance:
|
||||
|
||||
- **ONNX:** Provides up to 3x CPU speedup.
|
||||
- **TensorRT:** Offers up to 5x GPU speedup.
|
||||
- **OpenVINO:** Specifically optimized for Intel hardware.
|
||||
|
||||
These formats enhance both the speed and accuracy of your models, making them more efficient for various real-world applications. Visit the [Export](../modes/export.md) page for complete details.
|
||||
|
||||
### Why is benchmarking crucial in evaluating YOLO26 models?
|
||||
|
||||
Benchmarking your YOLO26 models is essential for several reasons:
|
||||
|
||||
- **Informed Decisions:** Understand the trade-offs between speed and accuracy.
|
||||
- **Resource Allocation:** Gauge the performance across different hardware options.
|
||||
- **Optimization:** Determine which export format offers the best performance for specific use cases.
|
||||
- **Cost Efficiency:** Optimize hardware usage based on benchmark results.
|
||||
|
||||
Key metrics such as mAP50-95, Top-5 accuracy, and inference time help in making these evaluations. Refer to the [Key Metrics](#key-metrics-in-benchmark-mode) section for more information.
|
||||
|
||||
### Which export formats are supported by YOLO26, and what are their advantages?
|
||||
|
||||
YOLO26 supports a variety of export formats, each tailored for specific hardware and use cases:
|
||||
|
||||
- **ONNX:** Best for CPU performance.
|
||||
- **TensorRT:** Ideal for GPU efficiency.
|
||||
- **OpenVINO:** Optimized for Intel hardware.
|
||||
- **CoreML & [TensorFlow](https://www.ultralytics.com/glossary/tensorflow):** Useful for iOS and general ML applications.
|
||||
|
||||
For a complete list of supported formats and their respective advantages, check out the [Supported Export Formats](#supported-export-formats) section.
|
||||
|
||||
### What arguments can I use to fine-tune my YOLO26 benchmarks?
|
||||
|
||||
When running benchmarks, several arguments can be customized to suit specific needs:
|
||||
|
||||
- **model:** Path to the model file (e.g., "yolo26n.pt").
|
||||
- **data:** Path to a YAML file defining the dataset (e.g., "coco8.yaml").
|
||||
- **imgsz:** The input image size, either as a single integer or a tuple.
|
||||
- **half:** Enable FP16 inference for better performance.
|
||||
- **int8:** Activate INT8 quantization for edge devices.
|
||||
- **device:** Specify the computation device (e.g., "cpu", "cuda:0").
|
||||
- **verbose:** Control the level of logging detail.
|
||||
|
||||
For a full list of arguments, refer to the [Arguments](#arguments) section.
|
||||
197
docs/en/modes/export.md
Executable file
197
docs/en/modes/export.md
Executable file
@@ -0,0 +1,197 @@
|
||||
---
|
||||
comments: true
|
||||
description: Learn how to export your YOLO26 model to various formats like ONNX, TensorRT, and CoreML. Achieve maximum compatibility and performance.
|
||||
keywords: YOLO26, Model Export, ONNX, TensorRT, CoreML, Ultralytics, AI, Machine Learning, Inference, Deployment
|
||||
---
|
||||
|
||||
# Model Export with Ultralytics YOLO
|
||||
|
||||
<img width="1024" src="https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/ultralytics-yolov8-ecosystem-integrations.avif" alt="Ultralytics YOLO ecosystem and integrations">
|
||||
|
||||
## Introduction
|
||||
|
||||
The ultimate goal of training a model is to deploy it for real-world applications. Export mode in Ultralytics YOLO26 offers a versatile range of options for exporting your trained model to different formats, making it deployable across various platforms and devices. This comprehensive guide aims to walk you through the nuances of model exporting, showcasing how to achieve maximum compatibility and performance.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/KGHYU-MKYeE"
|
||||
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> How to Export Ultralytics YOLO26 in different formats for Deployment | ONNX, TensorRT, CoreML 🚀
|
||||
</p>
|
||||
|
||||
## Why Choose YOLO26's Export Mode?
|
||||
|
||||
- **Versatility:** Export to multiple formats including [ONNX](../integrations/onnx.md), [TensorRT](../integrations/tensorrt.md), [CoreML](../integrations/coreml.md), and more.
|
||||
- **Performance:** Gain up to 5x GPU speedup with TensorRT and 3x CPU speedup with ONNX or [OpenVINO](../integrations/openvino.md).
|
||||
- **Compatibility:** Make your model universally deployable across numerous hardware and software environments.
|
||||
- **Ease of Use:** Simple CLI and Python API for quick and straightforward model exporting.
|
||||
|
||||
### Key Features of Export Mode
|
||||
|
||||
Here are some of the standout functionalities:
|
||||
|
||||
- **One-Click Export:** Simple commands for exporting to different formats.
|
||||
- **Batch Export:** Export batched-inference capable models.
|
||||
- **Optimized Inference:** Exported models are optimized for quicker inference times.
|
||||
- **Tutorial Videos:** In-depth guides and tutorials for a smooth exporting experience.
|
||||
|
||||
!!! tip
|
||||
|
||||
* Export to [ONNX](../integrations/onnx.md) or [OpenVINO](../integrations/openvino.md) for up to 3x CPU speedup.
|
||||
* Export to [TensorRT](../integrations/tensorrt.md) for up to 5x GPU speedup.
|
||||
|
||||
## Usage Examples
|
||||
|
||||
Export a YOLO26n model to a different format like ONNX or TensorRT. See the Arguments section below for a full list of export arguments.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt") # load an official model
|
||||
model = YOLO("path/to/best.pt") # load a custom-trained model
|
||||
|
||||
# Export the model
|
||||
model.export(format="onnx")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo export model=yolo26n.pt format=onnx # export official model
|
||||
yolo export model=path/to/best.pt format=onnx # export custom-trained model
|
||||
```
|
||||
|
||||
## Arguments
|
||||
|
||||
This table details the configurations and options available for exporting YOLO models to different formats. These settings are critical for optimizing the exported model's performance, size, and compatibility across various platforms and environments. Proper configuration ensures that the model is ready for deployment in the intended application with optimal efficiency.
|
||||
|
||||
{% include "macros/export-args.md" %}
|
||||
|
||||
Adjusting these parameters allows for customization of the export process to fit specific requirements, such as deployment environment, hardware constraints, and performance targets. Selecting the appropriate format and settings is essential for achieving the best balance between model size, speed, and [accuracy](https://www.ultralytics.com/glossary/accuracy).
|
||||
|
||||
## Export Formats
|
||||
|
||||
Available YOLO26 export formats are in the table below. You can export to any format using the `format` argument, i.e., `format='onnx'` or `format='engine'`. You can predict or validate directly on exported models, i.e., `yolo predict model=yolo26n.onnx`. Usage examples are shown for your model after export completes.
|
||||
|
||||
{% include "macros/export-table.md" %}
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I export a YOLO26 model to ONNX format?
|
||||
|
||||
Exporting a YOLO26 model to ONNX format is straightforward with Ultralytics. It provides both Python and CLI methods for exporting models.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt") # load an official model
|
||||
model = YOLO("path/to/best.pt") # load a custom-trained model
|
||||
|
||||
# Export the model
|
||||
model.export(format="onnx")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo export model=yolo26n.pt format=onnx # export official model
|
||||
yolo export model=path/to/best.pt format=onnx # export custom-trained model
|
||||
```
|
||||
|
||||
For more details on the process, including advanced options like handling different input sizes, refer to the [ONNX integration guide](../integrations/onnx.md).
|
||||
|
||||
### What are the benefits of using TensorRT for model export?
|
||||
|
||||
Using TensorRT for model export offers significant performance improvements. YOLO26 models exported to TensorRT can achieve up to a 5x GPU speedup, making it ideal for real-time inference applications.
|
||||
|
||||
- **Versatility:** Optimize models for a specific hardware setup.
|
||||
- **Speed:** Achieve faster inference through advanced optimizations.
|
||||
- **Compatibility:** Integrate smoothly with NVIDIA hardware.
|
||||
|
||||
To learn more about integrating TensorRT, see the [TensorRT integration guide](../integrations/tensorrt.md).
|
||||
|
||||
### How do I enable INT8 quantization when exporting my YOLO26 model?
|
||||
|
||||
INT8 quantization is an excellent way to compress the model and speed up inference, especially on edge devices. Here's how you can enable INT8 quantization:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("yolo26n.pt") # Load a model
|
||||
model.export(format="engine", int8=True)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo export model=yolo26n.pt format=engine int8=True # export TensorRT model with INT8 quantization
|
||||
```
|
||||
|
||||
INT8 quantization can be applied to various formats, such as [TensorRT](../integrations/tensorrt.md), [OpenVINO](../integrations/openvino.md), and [CoreML](../integrations/coreml.md). For optimal quantization results, provide a representative [dataset](https://docs.ultralytics.com/datasets/) using the `data` parameter.
|
||||
|
||||
### Why is dynamic input size important when exporting models?
|
||||
|
||||
Dynamic input size allows the exported model to handle varying image dimensions, providing flexibility and optimizing processing efficiency for different use cases. When exporting to formats like [ONNX](../integrations/onnx.md) or [TensorRT](../integrations/tensorrt.md), enabling dynamic input size ensures that the model can adapt to different input shapes seamlessly.
|
||||
|
||||
To enable this feature, use the `dynamic=True` flag during export:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
model.export(format="onnx", dynamic=True)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo export model=yolo26n.pt format=onnx dynamic=True
|
||||
```
|
||||
|
||||
Dynamic input sizing is particularly useful for applications where input dimensions may vary, such as video processing or when handling images from different sources.
|
||||
|
||||
### What are the key export arguments to consider for optimizing model performance?
|
||||
|
||||
Understanding and configuring export arguments is crucial for optimizing model performance:
|
||||
|
||||
- **`format:`** The target format for the exported model (e.g., `onnx`, `torchscript`, `tensorflow`).
|
||||
- **`imgsz:`** Desired image size for the model input (e.g., `640` or `(height, width)`).
|
||||
- **`half:`** Enables FP16 quantization, reducing model size and potentially speeding up inference.
|
||||
- **`optimize:`** Applies specific optimizations for mobile or constrained environments.
|
||||
- **`int8:`** Enables INT8 quantization, highly beneficial for [edge AI](https://www.ultralytics.com/blog/deploying-computer-vision-applications-on-edge-ai-devices) deployments.
|
||||
|
||||
For deployment on specific hardware platforms, consider using specialized export formats like [TensorRT](../integrations/tensorrt.md) for NVIDIA GPUs, [CoreML](../integrations/coreml.md) for Apple devices, or [Edge TPU](../integrations/edge-tpu.md) for Google Coral devices.
|
||||
|
||||
### What do the output tensors represent in exported YOLO models?
|
||||
|
||||
When you export a YOLO model to formats like ONNX or TensorRT, the output tensor structure depends on the model task. Understanding these outputs is important for custom inference implementations.
|
||||
|
||||
For **detection models** (e.g., `yolo26n.pt`), the output is typically a single tensor shaped like `(batch_size, 4 + num_classes, num_predictions)` where the channels represent box coordinates plus per-class scores, and `num_predictions` depends on the export input resolution (and can be dynamic).
|
||||
|
||||
For **segmentation models** (e.g., `yolo26n-seg.pt`), you'll typically get two outputs: the first tensor shaped like `(batch_size, 4 + num_classes + mask_dim, num_predictions)` (boxes, class scores, and mask coefficients), and the second tensor shaped like `(batch_size, mask_dim, proto_h, proto_w)` containing mask prototypes used with the coefficients to generate instance masks. Sizes depend on the export input resolution (and can be dynamic).
|
||||
|
||||
For **pose models** (e.g., `yolo26n-pose.pt`), the output tensor is typically shaped like `(batch_size, 4 + num_classes + keypoint_dims, num_predictions)`, where `keypoint_dims` depends on the pose specification (e.g., number of keypoints and whether confidence is included), and `num_predictions` depends on the export input resolution (and can be dynamic).
|
||||
|
||||
The examples in the [ONNX inference examples](https://github.com/ultralytics/ultralytics/tree/main/examples) demonstrate how to process these outputs for each model type.
|
||||
217
docs/en/modes/index.md
Executable file
217
docs/en/modes/index.md
Executable file
@@ -0,0 +1,217 @@
|
||||
---
|
||||
comments: true
|
||||
description: Discover the diverse modes of Ultralytics YOLO26, including training, validation, prediction, export, tracking, and benchmarking. Maximize model performance and efficiency.
|
||||
keywords: Ultralytics, YOLO26, machine learning, model training, validation, prediction, export, tracking, benchmarking, object detection
|
||||
---
|
||||
|
||||
# Ultralytics YOLO26 Modes
|
||||
|
||||
<img width="1024" src="https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/ultralytics-yolov8-ecosystem-integrations.avif" alt="Ultralytics YOLO ecosystem and integrations">
|
||||
|
||||
## Introduction
|
||||
|
||||
Ultralytics YOLO26 is not just another object detection model; it's a versatile framework designed to cover the entire lifecycle of [machine learning](https://www.ultralytics.com/glossary/machine-learning-ml) models—from data ingestion and model training to validation, deployment, and real-world tracking. Each mode serves a specific purpose and is engineered to offer you the flexibility and efficiency required for different tasks and use cases.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/j8uQc0qB91s?si=dhnGKgqvs7nPgeaM"
|
||||
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 Modes Tutorial: Train, Validate, Predict, Export & Benchmark.
|
||||
</p>
|
||||
|
||||
### Modes at a Glance
|
||||
|
||||
Understanding the different **modes** that Ultralytics YOLO26 supports is critical to getting the most out of your models:
|
||||
|
||||
- **Train** mode: Fine-tune your model on custom or preloaded datasets.
|
||||
- **Val** mode: A post-training checkpoint to validate model performance.
|
||||
- **Predict** mode: Unleash the predictive power of your model on real-world data.
|
||||
- **Export** mode: Make your [model deployment](https://www.ultralytics.com/glossary/model-deployment)-ready in various formats.
|
||||
- **Track** mode: Extend your object detection model into real-time tracking applications.
|
||||
- **Benchmark** mode: Analyze the speed and accuracy of your model in diverse deployment environments.
|
||||
|
||||
This comprehensive guide aims to give you an overview and practical insights into each mode, helping you harness the full potential of YOLO26.
|
||||
|
||||
## [Train](train.md)
|
||||
|
||||
Train mode is used for training a YOLO26 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. Training is essential for creating models that can recognize specific objects relevant to your application.
|
||||
|
||||
[Train Examples](train.md){ .md-button }
|
||||
|
||||
## [Val](val.md)
|
||||
|
||||
Val mode is used for validating a YOLO26 model after it has been trained. In this mode, the model is evaluated on a validation set to measure its accuracy and generalization performance. Validation helps identify potential issues like [overfitting](https://www.ultralytics.com/glossary/overfitting) and provides metrics such as [mean Average Precision](https://www.ultralytics.com/glossary/mean-average-precision-map) (mAP) to quantify model performance. This mode is crucial for tuning hyperparameters and improving overall model effectiveness.
|
||||
|
||||
[Val Examples](val.md){ .md-button }
|
||||
|
||||
## [Predict](predict.md)
|
||||
|
||||
Predict mode is used for making predictions using a trained YOLO26 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 identifies and localizes objects in the input media, making it ready for real-world applications. Predict mode is the gateway to applying your trained model to solve practical problems.
|
||||
|
||||
[Predict Examples](predict.md){ .md-button }
|
||||
|
||||
## [Export](export.md)
|
||||
|
||||
Export mode is used for converting a YOLO26 model to formats suitable for deployment across different platforms and devices. This mode transforms your PyTorch model into optimized formats like ONNX, TensorRT, or CoreML, enabling deployment in production environments. Exporting is essential for integrating your model with various software applications or hardware devices, often resulting in significant performance improvements.
|
||||
|
||||
[Export Examples](export.md){ .md-button }
|
||||
|
||||
## [Track](track.md)
|
||||
|
||||
Track mode extends YOLO26's object detection capabilities to track objects across video frames or live streams. This mode is particularly valuable for applications requiring persistent object identification, such as [surveillance systems](https://www.ultralytics.com/blog/shattering-the-surveillance-status-quo-with-vision-ai) or [self-driving cars](https://www.ultralytics.com/solutions/ai-in-automotive). Track mode implements sophisticated algorithms like ByteTrack to maintain object identity across frames, even when objects temporarily disappear from view.
|
||||
|
||||
[Track Examples](track.md){ .md-button }
|
||||
|
||||
## [Benchmark](benchmark.md)
|
||||
|
||||
Benchmark mode profiles the speed and accuracy of various export formats for YOLO26. This mode provides comprehensive metrics on model size, accuracy (mAP50-95 for detection tasks or accuracy_top5 for classification), and inference time across different formats like ONNX, [OpenVINO](https://docs.ultralytics.com/integrations/openvino/), and TensorRT. Benchmarking helps you select the optimal export format based on your specific requirements for speed and accuracy in your deployment environment.
|
||||
|
||||
[Benchmark Examples](benchmark.md){ .md-button }
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I train a custom [object detection](https://www.ultralytics.com/glossary/object-detection) model with Ultralytics YOLO26?
|
||||
|
||||
Training a custom object detection model with Ultralytics YOLO26 involves using the train mode. You need a dataset formatted in [YOLO format](../datasets/detect/index.md#ultralytics-yolo-format), containing images and corresponding annotation files. Use the following command to start the training process:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO model (you can choose n, s, m, l, or x versions)
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Start training on your custom dataset
|
||||
model.train(data="path/to/dataset.yaml", epochs=100, imgsz=640)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Train a YOLO model from the command line
|
||||
yolo detect train data=path/to/dataset.yaml model=yolo26n.pt epochs=100 imgsz=640
|
||||
```
|
||||
|
||||
For more detailed instructions, you can refer to the [Ultralytics Train Guide](../modes/train.md).
|
||||
|
||||
### What metrics does Ultralytics YOLO26 use to validate the model's performance?
|
||||
|
||||
Ultralytics YOLO26 uses various metrics during the validation process to assess model performance. These include:
|
||||
|
||||
- **mAP (mean Average Precision)**: This evaluates the accuracy of object detection.
|
||||
- **IOU (Intersection over Union)**: Measures the overlap between predicted and ground truth bounding boxes.
|
||||
- **[Precision](https://www.ultralytics.com/glossary/precision) and [Recall](https://www.ultralytics.com/glossary/recall)**: Precision measures the ratio of true positive detections to the total detected positives, while recall measures the ratio of true positive detections to the total actual positives.
|
||||
|
||||
You can run the following command to start the validation:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained or custom YOLO model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Run validation on your dataset
|
||||
model.val(data="path/to/validation.yaml")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Validate a YOLO model from the command line
|
||||
yolo val model=yolo26n.pt data=path/to/validation.yaml
|
||||
```
|
||||
|
||||
Refer to the [Validation Guide](../modes/val.md) for further details.
|
||||
|
||||
### How can I export my YOLO26 model for deployment?
|
||||
|
||||
Ultralytics YOLO26 offers export functionality to convert your trained model into various deployment formats such as ONNX, TensorRT, CoreML, and more. Use the following example to export your model:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load your trained YOLO model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Export the model to ONNX format (you can specify other formats as needed)
|
||||
model.export(format="onnx")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Export a YOLO model to ONNX format from the command line
|
||||
yolo export model=yolo26n.pt format=onnx
|
||||
```
|
||||
|
||||
Detailed steps for each export format can be found in the [Export Guide](../modes/export.md).
|
||||
|
||||
### What is the purpose of the benchmark mode in Ultralytics YOLO26?
|
||||
|
||||
Benchmark mode in Ultralytics YOLO26 is used to analyze the speed and [accuracy](https://www.ultralytics.com/glossary/accuracy) of various export formats such as ONNX, TensorRT, and OpenVINO. It provides metrics like model size, `mAP50-95` for object detection, and inference time across different hardware setups, helping you choose the most suitable format for your deployment needs.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics.utils.benchmarks import benchmark
|
||||
|
||||
# Run benchmark on GPU (device 0)
|
||||
# You can adjust parameters like model, dataset, image size, and precision as needed
|
||||
benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, half=False, device=0)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Benchmark a YOLO model from the command line
|
||||
# Adjust parameters as needed for your specific use case
|
||||
yolo benchmark model=yolo26n.pt data='coco8.yaml' imgsz=640 half=False device=0
|
||||
```
|
||||
|
||||
For more details, refer to the [Benchmark Guide](../modes/benchmark.md).
|
||||
|
||||
### How can I perform real-time object tracking using Ultralytics YOLO26?
|
||||
|
||||
Real-time object tracking can be achieved using the track mode in Ultralytics YOLO26. This mode extends object detection capabilities to track objects across video frames or live feeds. Use the following example to enable tracking:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Start tracking objects in a video
|
||||
# You can also use live video streams or webcam input
|
||||
model.track(source="path/to/video.mp4")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Perform object tracking on a video from the command line
|
||||
# You can specify different sources like webcam (0) or RTSP streams
|
||||
yolo track model=yolo26n.pt source=path/to/video.mp4
|
||||
```
|
||||
|
||||
For in-depth instructions, visit the [Track Guide](../modes/track.md).
|
||||
876
docs/en/modes/predict.md
Executable file
876
docs/en/modes/predict.md
Executable file
@@ -0,0 +1,876 @@
|
||||
---
|
||||
comments: true
|
||||
description: Harness the power of Ultralytics YOLO26 for real-time, high-speed inference on various data sources. Learn about predict mode, key features, and practical applications.
|
||||
keywords: Ultralytics, YOLO26, model prediction, inference, predict mode, real-time inference, computer vision, machine learning, streaming, high performance
|
||||
---
|
||||
|
||||
# Model Prediction with Ultralytics YOLO
|
||||
|
||||
<img width="1024" src="https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/ultralytics-yolov8-ecosystem-integrations.avif" alt="Ultralytics YOLO ecosystem and integrations">
|
||||
|
||||
## Introduction
|
||||
|
||||
In the world of [machine learning](https://www.ultralytics.com/glossary/machine-learning-ml) and [computer vision](https://www.ultralytics.com/glossary/computer-vision-cv), the process of making sense of visual data is often called inference or prediction. Ultralytics YOLO26 offers a powerful feature known as **predict mode**, tailored for high-performance, real-time inference across a wide range of data sources.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/YKbBXWBJloY"
|
||||
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> How to Extract Results from Ultralytics YOLO26 Tasks for Custom Projects 🚀
|
||||
</p>
|
||||
|
||||
## Real-world Applications
|
||||
|
||||
| Manufacturing | Sports | Safety |
|
||||
| :-----------------------------------------------: | :--------------------------------------------------: | :-----------------------------------------: |
|
||||
| ![Vehicle Spare Parts Detection][car spare parts] | ![Football Player Detection][football player detect] | ![People Fall Detection][human fall detect] |
|
||||
| Vehicle Spare Parts Detection | Football Player Detection | People Fall Detection |
|
||||
|
||||
## Why Use Ultralytics YOLO for Inference?
|
||||
|
||||
Here's why you should consider YOLO26's predict mode for your various inference needs:
|
||||
|
||||
- **Versatility:** Capable of running inference on images, videos, and even live streams.
|
||||
- **Performance:** Engineered for real-time, high-speed processing without sacrificing [accuracy](https://www.ultralytics.com/glossary/accuracy).
|
||||
- **Ease of Use:** Intuitive Python and CLI interfaces for rapid deployment and testing.
|
||||
- **Highly Customizable:** Various settings and parameters to tune the model's inference behavior according to your specific requirements.
|
||||
|
||||
### Key Features of Predict Mode
|
||||
|
||||
YOLO26's predict mode is designed to be robust and versatile, featuring:
|
||||
|
||||
- **Multiple Data Source Compatibility:** Whether your data is in the form of individual images, a collection of images, video files, or real-time video streams, predict mode has you covered.
|
||||
- **Streaming Mode:** Use the streaming feature to generate a memory-efficient generator of `Results` objects. Enable this by setting `stream=True` in the predictor's call method.
|
||||
- **Batch Processing:** Process multiple images or video frames in a single batch, further reducing total inference time.
|
||||
- **Integration Friendly:** Easily integrate with existing data pipelines and other software components, thanks to its flexible API.
|
||||
|
||||
Ultralytics YOLO models return either a Python list of `Results` objects or a memory-efficient generator of `Results` objects when `stream=True` is passed to the model during inference:
|
||||
|
||||
!!! example "Predict"
|
||||
|
||||
=== "Return a list with `stream=False`"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt") # pretrained YOLO26n model
|
||||
|
||||
# Run batched inference on a list of images
|
||||
results = model(["image1.jpg", "image2.jpg"]) # return a list of Results objects
|
||||
|
||||
# Process results list
|
||||
for result in results:
|
||||
boxes = result.boxes # Boxes object for bounding box outputs
|
||||
masks = result.masks # Masks object for segmentation masks outputs
|
||||
keypoints = result.keypoints # Keypoints object for pose outputs
|
||||
probs = result.probs # Probs object for classification outputs
|
||||
obb = result.obb # Oriented boxes object for OBB outputs
|
||||
result.show() # display to screen
|
||||
result.save(filename="result.jpg") # save to disk
|
||||
```
|
||||
|
||||
=== "Return a generator with `stream=True`"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt") # pretrained YOLO26n model
|
||||
|
||||
# Run batched inference on a list of images
|
||||
results = model(["image1.jpg", "image2.jpg"], stream=True) # return a generator of Results objects
|
||||
|
||||
# Process results generator
|
||||
for result in results:
|
||||
boxes = result.boxes # Boxes object for bounding box outputs
|
||||
masks = result.masks # Masks object for segmentation masks outputs
|
||||
keypoints = result.keypoints # Keypoints object for pose outputs
|
||||
probs = result.probs # Probs object for classification outputs
|
||||
obb = result.obb # Oriented boxes object for OBB outputs
|
||||
result.show() # display to screen
|
||||
result.save(filename="result.jpg") # save to disk
|
||||
```
|
||||
|
||||
## Inference Sources
|
||||
|
||||
YOLO26 can process different types of input sources for inference, as shown in the table below. The sources include static images, video streams, and various data formats. The table also indicates whether each source can be used in streaming mode with the argument `stream=True` ✅. Streaming mode is beneficial for processing videos or live streams as it creates a generator of results instead of loading all frames into memory.
|
||||
|
||||
!!! tip
|
||||
|
||||
Use `stream=True` for processing long videos or large datasets to efficiently manage memory. When `stream=False`, the results for all frames or data points are stored in memory, which can quickly add up and cause out-of-memory errors for large inputs. In contrast, `stream=True` utilizes a generator, which only keeps the results of the current frame or data point in memory, significantly reducing memory consumption and preventing out-of-memory issues.
|
||||
|
||||
| Source | Example | Type | Notes |
|
||||
| ----------------------------------------------------- | ------------------------------------------ | --------------- | -------------------------------------------------------------------------------------------- |
|
||||
| image | `'image.jpg'` | `str` or `Path` | Single image file. |
|
||||
| URL | `'https://ultralytics.com/images/bus.jpg'` | `str` | URL to an image. |
|
||||
| screenshot | `'screen'` | `str` | Capture a screenshot. |
|
||||
| PIL | `Image.open('image.jpg')` | `PIL.Image` | HWC format with RGB channels. |
|
||||
| [OpenCV](https://www.ultralytics.com/glossary/opencv) | `cv2.imread('image.jpg')` | `np.ndarray` | HWC format with BGR channels `uint8 (0-255)`. |
|
||||
| numpy | `np.zeros((640,1280,3))` | `np.ndarray` | HWC format with BGR channels `uint8 (0-255)`. |
|
||||
| torch | `torch.zeros(16,3,320,640)` | `torch.Tensor` | BCHW format with RGB channels `float32 (0.0-1.0)`. |
|
||||
| CSV | `'sources.csv'` | `str` or `Path` | CSV file containing paths to images, videos, or directories. |
|
||||
| video ✅ | `'video.mp4'` | `str` or `Path` | Video file in formats like MP4, AVI, etc. |
|
||||
| directory ✅ | `'path/'` | `str` or `Path` | Path to a directory containing images or videos. |
|
||||
| glob ✅ | `'path/*.jpg'` | `str` | Glob pattern to match multiple files. Use the `*` character as a wildcard. |
|
||||
| YouTube ✅ | `'https://youtu.be/LNwODJXcvt4'` | `str` | URL to a YouTube video. |
|
||||
| stream ✅ | `'rtsp://example.com/media.mp4'` | `str` | URL for streaming protocols such as RTSP, RTMP, TCP, or an IP address. |
|
||||
| multi-stream ✅ | `'list.streams'` | `str` or `Path` | `*.streams` text file with one stream URL per row, i.e., 8 streams will run at batch-size 8. |
|
||||
| webcam ✅ | `0` | `int` | Index of the connected camera device to run inference on. |
|
||||
|
||||
Below are code examples for using each source type:
|
||||
|
||||
!!! example "Prediction sources"
|
||||
|
||||
=== "image"
|
||||
|
||||
Run inference on an image file.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Define path to the image file
|
||||
source = "path/to/image.jpg"
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source) # list of Results objects
|
||||
```
|
||||
|
||||
=== "screenshot"
|
||||
|
||||
Run inference on the current screen content as a screenshot.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Define current screenshot as source
|
||||
source = "screen"
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source) # list of Results objects
|
||||
```
|
||||
|
||||
=== "URL"
|
||||
|
||||
Run inference on an image or video hosted remotely via URL.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Define remote image or video URL
|
||||
source = "https://ultralytics.com/images/bus.jpg"
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source) # list of Results objects
|
||||
```
|
||||
|
||||
=== "PIL"
|
||||
|
||||
Run inference on an image opened with Python Imaging Library (PIL).
|
||||
```python
|
||||
from PIL import Image
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Open an image using PIL
|
||||
source = Image.open("path/to/image.jpg")
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source) # list of Results objects
|
||||
```
|
||||
|
||||
=== "OpenCV"
|
||||
|
||||
Run inference on an image read with OpenCV.
|
||||
```python
|
||||
import cv2
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Read an image using OpenCV
|
||||
source = cv2.imread("path/to/image.jpg")
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source) # list of Results objects
|
||||
```
|
||||
|
||||
=== "numpy"
|
||||
|
||||
Run inference on an image represented as a numpy array.
|
||||
```python
|
||||
import numpy as np
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Create a random numpy array of HWC shape (640, 640, 3) with values in range [0, 255] and type uint8
|
||||
source = np.random.randint(low=0, high=255, size=(640, 640, 3), dtype="uint8")
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source) # list of Results objects
|
||||
```
|
||||
|
||||
=== "torch"
|
||||
|
||||
Run inference on an image represented as a [PyTorch](https://www.ultralytics.com/glossary/pytorch) tensor.
|
||||
```python
|
||||
import torch
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Create a random torch tensor of BCHW shape (1, 3, 640, 640) with values in range [0, 1] and type float32
|
||||
source = torch.rand(1, 3, 640, 640, dtype=torch.float32)
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source) # list of Results objects
|
||||
```
|
||||
|
||||
=== "CSV"
|
||||
|
||||
Run inference on a collection of images, URLs, videos, and directories listed in a CSV file.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Define a path to a CSV file with images, URLs, videos and directories
|
||||
source = "path/to/file.csv"
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source) # list of Results objects
|
||||
```
|
||||
|
||||
=== "video"
|
||||
|
||||
Run inference on a video file. By using `stream=True`, you can create a generator of Results objects to reduce memory usage.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Define path to video file
|
||||
source = "path/to/video.mp4"
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source, stream=True) # generator of Results objects
|
||||
```
|
||||
|
||||
=== "directory"
|
||||
|
||||
Run inference on all images and videos in a directory. To include assets in subdirectories, use a glob pattern such as `path/to/dir/**/*`.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Define path to directory containing images and videos for inference
|
||||
source = "path/to/dir"
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source, stream=True) # generator of Results objects
|
||||
```
|
||||
|
||||
=== "glob"
|
||||
|
||||
Run inference on all images and videos that match a glob expression with `*` characters.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Define a glob search for all JPG files in a directory
|
||||
source = "path/to/dir/*.jpg"
|
||||
|
||||
# OR define a recursive glob search for all JPG files including subdirectories
|
||||
source = "path/to/dir/**/*.jpg"
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source, stream=True) # generator of Results objects
|
||||
```
|
||||
|
||||
=== "YouTube"
|
||||
|
||||
Run inference on a YouTube video. By using `stream=True`, you can create a generator of Results objects to reduce memory usage for long videos.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Define source as YouTube video URL
|
||||
source = "https://youtu.be/LNwODJXcvt4"
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source, stream=True) # generator of Results objects
|
||||
```
|
||||
|
||||
=== "Stream"
|
||||
|
||||
Use the stream mode to run inference on live video streams using RTSP, RTMP, TCP, or IP address protocols. If a single stream is provided, the model runs inference with a [batch size](https://www.ultralytics.com/glossary/batch-size) of 1. For multiple streams, a `.streams` text file can be used to perform batched inference, where the batch size is determined by the number of streams provided (e.g., batch-size 8 for 8 streams).
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Single stream with batch-size 1 inference
|
||||
source = "rtsp://example.com/media.mp4" # RTSP, RTMP, TCP, or IP streaming address
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source, stream=True) # generator of Results objects
|
||||
```
|
||||
|
||||
For single stream usage, the batch size is set to 1 by default, allowing efficient real-time processing of the video feed.
|
||||
|
||||
=== "Multi-Stream"
|
||||
|
||||
To handle multiple video streams simultaneously, use a `.streams` text file containing one source per line. The model will run batched inference where the batch size equals the number of streams. This setup enables efficient processing of multiple feeds concurrently.
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Multiple streams with batched inference (e.g., batch-size 8 for 8 streams)
|
||||
source = "path/to/list.streams" # *.streams text file with one streaming address per line
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source, stream=True) # generator of Results objects
|
||||
```
|
||||
|
||||
Example `.streams` text file:
|
||||
|
||||
```text
|
||||
rtsp://example.com/media1.mp4
|
||||
rtsp://example.com/media2.mp4
|
||||
rtmp://example2.com/live
|
||||
tcp://192.168.1.100:554
|
||||
...
|
||||
```
|
||||
|
||||
Each row in the file represents a streaming source, allowing you to monitor and perform inference on several video streams at once.
|
||||
|
||||
=== "Webcam"
|
||||
|
||||
You can run inference on a connected camera device by passing the index of that particular camera to `source`.
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Run inference on the source
|
||||
results = model(source=0, stream=True) # generator of Results objects
|
||||
```
|
||||
|
||||
## Inference Arguments
|
||||
|
||||
`model.predict()` accepts multiple arguments that can be passed at inference time to override defaults:
|
||||
|
||||
!!! note
|
||||
|
||||
Ultralytics uses minimal padding during inference by default (`rect=True`). In this mode, the shorter side of each image is padded only as much as needed to make it divisible by the model's maximum stride, rather than padding it all the way to the full `imgsz`. When running inference on a batch of images, minimal padding only works if all images have identical size. Otherwise, images are uniformly padded to a square shape with both sides equal to `imgsz`.
|
||||
|
||||
- `batch=1`, using `rect` padding by default.
|
||||
- `batch>1`, using `rect` padding only if all the images in one batch have identical size, otherwise using square padding to `imgsz`.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Run inference on 'bus.jpg' with arguments
|
||||
model.predict("https://ultralytics.com/images/bus.jpg", save=True, imgsz=320, conf=0.25)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Run inference on 'bus.jpg'
|
||||
yolo predict model=yolo26n.pt source='https://ultralytics.com/images/bus.jpg'
|
||||
```
|
||||
|
||||
Inference arguments:
|
||||
|
||||
{% include "macros/predict-args.md" %}
|
||||
|
||||
Visualization arguments:
|
||||
|
||||
{% from "macros/visualization-args.md" import param_table %}
|
||||
{{ param_table() }}
|
||||
|
||||
## Image and Video Formats
|
||||
|
||||
YOLO26 supports various image and video formats, as specified in [ultralytics/data/utils.py](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/data/utils.py). See the tables below for the valid suffixes and example predict commands.
|
||||
|
||||
### Images
|
||||
|
||||
The below table contains valid Ultralytics image formats.
|
||||
|
||||
!!! note
|
||||
|
||||
HEIC/HEIF formats require `pi-heif`, which is installed automatically on first use. AVIF is supported natively by Pillow.
|
||||
|
||||
| Image Suffixes | Example Predict Command | Reference |
|
||||
| -------------- | -------------------------------- | -------------------------------------------------------------------------- |
|
||||
| `.avif` | `yolo predict source=image.avif` | [AV1 Image File Format](https://en.wikipedia.org/wiki/AVIF) |
|
||||
| `.bmp` | `yolo predict source=image.bmp` | [Microsoft BMP File Format](https://en.wikipedia.org/wiki/BMP_file_format) |
|
||||
| `.dng` | `yolo predict source=image.dng` | [Adobe DNG](https://en.wikipedia.org/wiki/Digital_Negative) |
|
||||
| `.heic` | `yolo predict source=image.heic` | [High Efficiency Image Format](https://en.wikipedia.org/wiki/HEIF) |
|
||||
| `.heif` | `yolo predict source=image.heif` | [High Efficiency Image Format](https://en.wikipedia.org/wiki/HEIF) |
|
||||
| `.jp2` | `yolo predict source=image.jp2` | [JPEG 2000](https://en.wikipedia.org/wiki/JPEG_2000) |
|
||||
| `.jpeg` | `yolo predict source=image.jpeg` | [JPEG](https://en.wikipedia.org/wiki/JPEG) |
|
||||
| `.jpg` | `yolo predict source=image.jpg` | [JPEG](https://en.wikipedia.org/wiki/JPEG) |
|
||||
| `.mpo` | `yolo predict source=image.mpo` | [Multi Picture Object](https://fileinfo.com/extension/mpo) |
|
||||
| `.png` | `yolo predict source=image.png` | [Portable Network Graphics](https://en.wikipedia.org/wiki/PNG) |
|
||||
| `.tif` | `yolo predict source=image.tif` | [Tag Image File Format](https://en.wikipedia.org/wiki/TIFF) |
|
||||
| `.tiff` | `yolo predict source=image.tiff` | [Tag Image File Format](https://en.wikipedia.org/wiki/TIFF) |
|
||||
| `.webp` | `yolo predict source=image.webp` | [WebP](https://en.wikipedia.org/wiki/WebP) |
|
||||
|
||||
### Videos
|
||||
|
||||
The below table contains valid Ultralytics video formats.
|
||||
|
||||
| Video Suffixes | Example Predict Command | Reference |
|
||||
| -------------- | -------------------------------- | -------------------------------------------------------------------------------- |
|
||||
| `.asf` | `yolo predict source=video.asf` | [Advanced Systems Format](https://en.wikipedia.org/wiki/Advanced_Systems_Format) |
|
||||
| `.avi` | `yolo predict source=video.avi` | [Audio Video Interleave](https://en.wikipedia.org/wiki/Audio_Video_Interleave) |
|
||||
| `.gif` | `yolo predict source=video.gif` | [Graphics Interchange Format](https://en.wikipedia.org/wiki/GIF) |
|
||||
| `.m4v` | `yolo predict source=video.m4v` | [MPEG-4 Part 14](https://en.wikipedia.org/wiki/M4V) |
|
||||
| `.mkv` | `yolo predict source=video.mkv` | [Matroska](https://en.wikipedia.org/wiki/Matroska) |
|
||||
| `.mov` | `yolo predict source=video.mov` | [QuickTime File Format](https://en.wikipedia.org/wiki/QuickTime_File_Format) |
|
||||
| `.mp4` | `yolo predict source=video.mp4` | [MPEG-4 Part 14 - Wikipedia](https://en.wikipedia.org/wiki/MPEG-4_Part_14) |
|
||||
| `.mpeg` | `yolo predict source=video.mpeg` | [MPEG-1 Part 2](https://en.wikipedia.org/wiki/MPEG-1) |
|
||||
| `.mpg` | `yolo predict source=video.mpg` | [MPEG-1 Part 2](https://en.wikipedia.org/wiki/MPEG-1) |
|
||||
| `.ts` | `yolo predict source=video.ts` | [MPEG Transport Stream](https://en.wikipedia.org/wiki/MPEG_transport_stream) |
|
||||
| `.wmv` | `yolo predict source=video.wmv` | [Windows Media Video](https://en.wikipedia.org/wiki/Windows_Media_Video) |
|
||||
| `.webm` | `yolo predict source=video.webm` | [WebM Project](https://en.wikipedia.org/wiki/WebM) |
|
||||
|
||||
## Working with Results
|
||||
|
||||
All Ultralytics `predict()` calls will return a list of `Results` objects:
|
||||
|
||||
!!! example "Results"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Run inference on an image
|
||||
results = model("https://ultralytics.com/images/bus.jpg")
|
||||
results = model(
|
||||
[
|
||||
"https://ultralytics.com/images/bus.jpg",
|
||||
"https://ultralytics.com/images/zidane.jpg",
|
||||
]
|
||||
) # batch inference
|
||||
```
|
||||
|
||||
`Results` objects have the following attributes:
|
||||
|
||||
| Attribute | Type | Description |
|
||||
| ------------ | --------------------- | ---------------------------------------------------------------------------------------- |
|
||||
| `orig_img` | `np.ndarray` | The original image as a numpy array. |
|
||||
| `orig_shape` | `tuple` | The original image shape in (height, width) format. |
|
||||
| `boxes` | `Boxes, optional` | A Boxes object containing the detection bounding boxes. |
|
||||
| `masks` | `Masks, optional` | A Masks object containing the detection masks. |
|
||||
| `probs` | `Probs, optional` | A Probs object containing probabilities of each class for classification task. |
|
||||
| `keypoints` | `Keypoints, optional` | A Keypoints object containing detected keypoints for each object. |
|
||||
| `obb` | `OBB, optional` | An OBB object containing oriented bounding boxes. |
|
||||
| `speed` | `dict` | A dictionary of preprocess, inference, and postprocess speeds in milliseconds per image. |
|
||||
| `names` | `dict` | A dictionary mapping class indices to class names. |
|
||||
| `path` | `str` | The path to the image file. |
|
||||
| `save_dir` | `str, optional` | Directory to save results. |
|
||||
|
||||
`Results` objects have the following methods:
|
||||
|
||||
| Method | Return Type | Description |
|
||||
| ------------- | ---------------------- | ----------------------------------------------------------------------------------------- |
|
||||
| `update()` | `None` | Updates the Results object with new detection data (boxes, masks, probs, obb, keypoints). |
|
||||
| `cpu()` | `Results` | Returns a copy of the Results object with all tensors moved to CPU memory. |
|
||||
| `numpy()` | `Results` | Returns a copy of the Results object with all tensors converted to numpy arrays. |
|
||||
| `cuda()` | `Results` | Returns a copy of the Results object with all tensors moved to GPU memory. |
|
||||
| `to()` | `Results` | Returns a copy of the Results object with tensors moved to specified device and dtype. |
|
||||
| `new()` | `Results` | Creates a new Results object with the same image, path, names, and speed attributes. |
|
||||
| `plot()` | `np.ndarray` | Plots detection results on an input RGB image and returns the annotated image. |
|
||||
| `show()` | `None` | Displays the image with annotated inference results. |
|
||||
| `save()` | `str` | Saves annotated inference results image to file and returns the filename. |
|
||||
| `verbose()` | `str` | Returns a log string for each task, detailing detection and classification outcomes. |
|
||||
| `save_txt()` | `str` | Saves detection results to a text file and returns the path to the saved file. |
|
||||
| `save_crop()` | `None` | Saves cropped detection images to specified directory. |
|
||||
| `summary()` | `List[Dict[str, Any]]` | Converts inference results to a summarized dictionary with optional normalization. |
|
||||
| `to_df()` | `DataFrame` | Converts detection results to a Polars DataFrame. |
|
||||
| `to_csv()` | `str` | Converts detection results to CSV format. |
|
||||
| `to_json()` | `str` | Converts detection results to JSON format. |
|
||||
|
||||
For more details see the [`Results` class documentation](../reference/engine/results.md).
|
||||
|
||||
### Boxes
|
||||
|
||||
`Boxes` object can be used to index, manipulate, and convert bounding boxes to different formats.
|
||||
|
||||
!!! example "Boxes"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Run inference on an image
|
||||
results = model("https://ultralytics.com/images/bus.jpg") # results list
|
||||
|
||||
# View results
|
||||
for r in results:
|
||||
print(r.boxes) # print the Boxes object containing the detection bounding boxes
|
||||
```
|
||||
|
||||
Here is a table for the `Boxes` class methods and properties, including their name, type, and description:
|
||||
|
||||
| Name | Type | Description |
|
||||
| --------- | ------------------------- | ------------------------------------------------------------------ |
|
||||
| `cpu()` | Method | Move the object to CPU memory. |
|
||||
| `numpy()` | Method | Convert the object to a numpy array. |
|
||||
| `cuda()` | Method | Move the object to CUDA memory. |
|
||||
| `to()` | Method | Move the object to the specified device. |
|
||||
| `xyxy` | Property (`torch.Tensor`) | Return the boxes in xyxy format. |
|
||||
| `conf` | Property (`torch.Tensor`) | Return the confidence values of the boxes. |
|
||||
| `cls` | Property (`torch.Tensor`) | Return the class values of the boxes. |
|
||||
| `id` | Property (`torch.Tensor`) | Return the track IDs of the boxes (if available). |
|
||||
| `xywh` | Property (`torch.Tensor`) | Return the boxes in xywh format. |
|
||||
| `xyxyn` | Property (`torch.Tensor`) | Return the boxes in xyxy format normalized by original image size. |
|
||||
| `xywhn` | Property (`torch.Tensor`) | Return the boxes in xywh format normalized by original image size. |
|
||||
|
||||
For more details see the [`Boxes` class documentation](../reference/engine/results.md#ultralytics.engine.results.Boxes).
|
||||
|
||||
### Masks
|
||||
|
||||
`Masks` object can be used to index, manipulate and convert masks to segments.
|
||||
|
||||
!!! example "Masks"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n-seg Segment model
|
||||
model = YOLO("yolo26n-seg.pt")
|
||||
|
||||
# Run inference on an image
|
||||
results = model("https://ultralytics.com/images/bus.jpg") # results list
|
||||
|
||||
# View results
|
||||
for r in results:
|
||||
print(r.masks) # print the Masks object containing the detected instance masks
|
||||
```
|
||||
|
||||
Here is a table for the `Masks` class methods and properties, including their name, type, and description:
|
||||
|
||||
| Name | Type | Description |
|
||||
| --------- | ------------------------- | --------------------------------------------------------------- |
|
||||
| `cpu()` | Method | Returns the masks tensor on CPU memory. |
|
||||
| `numpy()` | Method | Returns the masks tensor as a numpy array. |
|
||||
| `cuda()` | Method | Returns the masks tensor on GPU memory. |
|
||||
| `to()` | Method | Returns the masks tensor with the specified device and dtype. |
|
||||
| `xyn` | Property (`torch.Tensor`) | A list of normalized segments represented as tensors. |
|
||||
| `xy` | Property (`torch.Tensor`) | A list of segments in pixel coordinates represented as tensors. |
|
||||
|
||||
For more details see the [`Masks` class documentation](../reference/engine/results.md#ultralytics.engine.results.Masks).
|
||||
|
||||
### Keypoints
|
||||
|
||||
`Keypoints` object can be used to index, manipulate and normalize coordinates.
|
||||
|
||||
!!! example "Keypoints"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n-pose Pose model
|
||||
model = YOLO("yolo26n-pose.pt")
|
||||
|
||||
# Run inference on an image
|
||||
results = model("https://ultralytics.com/images/bus.jpg") # results list
|
||||
|
||||
# View results
|
||||
for r in results:
|
||||
print(r.keypoints) # print the Keypoints object containing the detected keypoints
|
||||
```
|
||||
|
||||
Here is a table for the `Keypoints` class methods and properties, including their name, type, and description:
|
||||
|
||||
| Name | Type | Description |
|
||||
| --------- | ------------------------- | ----------------------------------------------------------------- |
|
||||
| `cpu()` | Method | Returns the keypoints tensor on CPU memory. |
|
||||
| `numpy()` | Method | Returns the keypoints tensor as a numpy array. |
|
||||
| `cuda()` | Method | Returns the keypoints tensor on GPU memory. |
|
||||
| `to()` | Method | Returns the keypoints tensor with the specified device and dtype. |
|
||||
| `xyn` | Property (`torch.Tensor`) | A list of normalized keypoints represented as tensors. |
|
||||
| `xy` | Property (`torch.Tensor`) | A list of keypoints in pixel coordinates represented as tensors. |
|
||||
| `conf` | Property (`torch.Tensor`) | Returns confidence values of keypoints if available, else None. |
|
||||
|
||||
For more details see the [`Keypoints` class documentation](../reference/engine/results.md#ultralytics.engine.results.Keypoints).
|
||||
|
||||
### Probs
|
||||
|
||||
`Probs` object can be used index, get `top1` and `top5` indices and scores of classification.
|
||||
|
||||
!!! example "Probs"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n-cls Classify model
|
||||
model = YOLO("yolo26n-cls.pt")
|
||||
|
||||
# Run inference on an image
|
||||
results = model("https://ultralytics.com/images/bus.jpg") # results list
|
||||
|
||||
# View results
|
||||
for r in results:
|
||||
print(r.probs) # print the Probs object containing the detected class probabilities
|
||||
```
|
||||
|
||||
Here's a table summarizing the methods and properties for the `Probs` class:
|
||||
|
||||
| Name | Type | Description |
|
||||
| ---------- | ------------------------- | ----------------------------------------------------------------------- |
|
||||
| `cpu()` | Method | Returns a copy of the probs tensor on CPU memory. |
|
||||
| `numpy()` | Method | Returns a copy of the probs tensor as a numpy array. |
|
||||
| `cuda()` | Method | Returns a copy of the probs tensor on GPU memory. |
|
||||
| `to()` | Method | Returns a copy of the probs tensor with the specified device and dtype. |
|
||||
| `top1` | Property (`int`) | Index of the top 1 class. |
|
||||
| `top5` | Property (`list[int]`) | Indices of the top 5 classes. |
|
||||
| `top1conf` | Property (`torch.Tensor`) | Confidence of the top 1 class. |
|
||||
| `top5conf` | Property (`torch.Tensor`) | Confidences of the top 5 classes. |
|
||||
|
||||
For more details see the [`Probs` class documentation](../reference/engine/results.md#ultralytics.engine.results.Probs).
|
||||
|
||||
### OBB
|
||||
|
||||
`OBB` object can be used to index, manipulate, and convert oriented bounding boxes to different formats.
|
||||
|
||||
!!! example "OBB"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n-obb.pt")
|
||||
|
||||
# Run inference on an image
|
||||
results = model("https://ultralytics.com/images/boats.jpg") # results list
|
||||
|
||||
# View results
|
||||
for r in results:
|
||||
print(r.obb) # print the OBB object containing the oriented detection bounding boxes
|
||||
```
|
||||
|
||||
Here is a table for the `OBB` class methods and properties, including their name, type, and description:
|
||||
|
||||
| Name | Type | Description |
|
||||
| ----------- | ------------------------- | --------------------------------------------------------------------- |
|
||||
| `cpu()` | Method | Move the object to CPU memory. |
|
||||
| `numpy()` | Method | Convert the object to a numpy array. |
|
||||
| `cuda()` | Method | Move the object to CUDA memory. |
|
||||
| `to()` | Method | Move the object to the specified device. |
|
||||
| `conf` | Property (`torch.Tensor`) | Return the confidence values of the boxes. |
|
||||
| `cls` | Property (`torch.Tensor`) | Return the class values of the boxes. |
|
||||
| `id` | Property (`torch.Tensor`) | Return the track IDs of the boxes (if available). |
|
||||
| `xyxy` | Property (`torch.Tensor`) | Return the horizontal boxes in xyxy format. |
|
||||
| `xywhr` | Property (`torch.Tensor`) | Return the rotated boxes in xywhr format. |
|
||||
| `xyxyxyxy` | Property (`torch.Tensor`) | Return the rotated boxes in xyxyxyxy format. |
|
||||
| `xyxyxyxyn` | Property (`torch.Tensor`) | Return the rotated boxes in xyxyxyxy format normalized by image size. |
|
||||
|
||||
For more details see the [`OBB` class documentation](../reference/engine/results.md#ultralytics.engine.results.OBB).
|
||||
|
||||
## Plotting Results
|
||||
|
||||
The `plot()` method in `Results` objects facilitates visualization of predictions by overlaying detected objects (such as bounding boxes, masks, keypoints, and probabilities) onto the original image. This method returns the annotated image as a NumPy array, allowing for easy display or saving.
|
||||
|
||||
!!! example "Plotting"
|
||||
|
||||
```python
|
||||
from PIL import Image
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO26n model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Run inference on 'bus.jpg'
|
||||
results = model(["https://ultralytics.com/images/bus.jpg", "https://ultralytics.com/images/zidane.jpg"]) # results list
|
||||
|
||||
# Visualize the results
|
||||
for i, r in enumerate(results):
|
||||
# Plot results image
|
||||
im_bgr = r.plot() # BGR-order numpy array
|
||||
im_rgb = Image.fromarray(im_bgr[..., ::-1]) # RGB-order PIL image
|
||||
|
||||
# Show results to screen (in supported environments)
|
||||
r.show()
|
||||
|
||||
# Save results to disk
|
||||
r.save(filename=f"results{i}.jpg")
|
||||
```
|
||||
|
||||
### `plot()` Method Parameters
|
||||
|
||||
The `plot()` method supports various arguments to customize the output:
|
||||
|
||||
| Argument | Type | Description | Default |
|
||||
| ------------ | ---------------------- | -------------------------------------------------------------------------- | ----------------- |
|
||||
| `conf` | `bool` | Include detection confidence scores. | `True` |
|
||||
| `line_width` | `float` | Line width of bounding boxes. Scales with image size if `None`. | `None` |
|
||||
| `font_size` | `float` | Text font size. Scales with image size if `None`. | `None` |
|
||||
| `font` | `str` | Font name for text annotations. | `'Arial.ttf'` |
|
||||
| `pil` | `bool` | Return image as a PIL Image object. | `False` |
|
||||
| `img` | `np.ndarray` | Alternative image for plotting. Uses the original image if `None`. | `None` |
|
||||
| `im_gpu` | `torch.Tensor` | GPU-accelerated image for faster mask plotting. Shape: (1, 3, 640, 640). | `None` |
|
||||
| `kpt_radius` | `int` | Radius for drawn keypoints. | `5` |
|
||||
| `kpt_line` | `bool` | Connect keypoints with lines. | `True` |
|
||||
| `labels` | `bool` | Include class labels in annotations. | `True` |
|
||||
| `boxes` | `bool` | Overlay bounding boxes on the image. | `True` |
|
||||
| `masks` | `bool` | Overlay masks on the image. | `True` |
|
||||
| `probs` | `bool` | Include classification probabilities. | `True` |
|
||||
| `show` | `bool` | Display the annotated image directly using the default image viewer. | `False` |
|
||||
| `save` | `bool` | Save the annotated image to a file specified by `filename`. | `False` |
|
||||
| `filename` | `str` | Path and name of the file to save the annotated image if `save` is `True`. | `None` |
|
||||
| `color_mode` | `str` | Specify the color mode, e.g., 'instance' or 'class'. | `'class'` |
|
||||
| `txt_color` | `tuple[int, int, int]` | RGB text color for bounding box and image classification label. | `(255, 255, 255)` |
|
||||
|
||||
## Thread-Safe Inference
|
||||
|
||||
Ensuring thread safety during inference is crucial when you are running multiple YOLO models in parallel across different threads. Thread-safe inference guarantees that each thread's predictions are isolated and do not interfere with one another, avoiding race conditions and ensuring consistent and reliable outputs.
|
||||
|
||||
When using YOLO models in a multi-threaded application, it's important to instantiate separate model objects for each thread or employ thread-local storage to prevent conflicts:
|
||||
|
||||
!!! example "Thread-Safe Inference"
|
||||
|
||||
Instantiate a single model inside each thread for thread-safe inference:
|
||||
```python
|
||||
from threading import Thread
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def thread_safe_predict(model, image_path):
|
||||
"""Performs thread-safe prediction on an image using a locally instantiated YOLO model."""
|
||||
model = YOLO(model)
|
||||
results = model.predict(image_path)
|
||||
# Process results
|
||||
|
||||
|
||||
# Starting threads that each have their own model instance
|
||||
Thread(target=thread_safe_predict, args=("yolo26n.pt", "image1.jpg")).start()
|
||||
Thread(target=thread_safe_predict, args=("yolo26n.pt", "image2.jpg")).start()
|
||||
```
|
||||
|
||||
For an in-depth look at thread-safe inference with YOLO models and step-by-step instructions, please refer to our [YOLO Thread-Safe Inference Guide](../guides/yolo-thread-safe-inference.md). This guide will provide you with all the necessary information to avoid common pitfalls and ensure that your multi-threaded inference runs smoothly.
|
||||
|
||||
## Streaming Source `for`-loop
|
||||
|
||||
Here's a Python script using OpenCV (`cv2`) and YOLO to run inference on video frames. This script assumes you have already installed the necessary packages (`opencv-python` and `ultralytics`).
|
||||
|
||||
!!! example "Streaming for-loop"
|
||||
|
||||
```python
|
||||
import cv2
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load the YOLO model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Open the video file
|
||||
video_path = "path/to/your/video/file.mp4"
|
||||
cap = cv2.VideoCapture(video_path)
|
||||
|
||||
# Loop through the video frames
|
||||
while cap.isOpened():
|
||||
# Read a frame from the video
|
||||
success, frame = cap.read()
|
||||
|
||||
if success:
|
||||
# Run YOLO inference on the frame
|
||||
results = model(frame)
|
||||
|
||||
# Visualize the results on the frame
|
||||
annotated_frame = results[0].plot()
|
||||
|
||||
# Display the annotated frame
|
||||
cv2.imshow("YOLO Inference", annotated_frame)
|
||||
|
||||
# Break the loop if 'q' is pressed
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
else:
|
||||
# Break the loop if the end of the video is reached
|
||||
break
|
||||
|
||||
# Release the video capture object and close the display window
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
This script will run predictions on each frame of the video, visualize the results, and display them in a window. The loop can be exited by pressing 'q'.
|
||||
|
||||
[car spare parts]: https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/car-parts-detection-for-predict.avif
|
||||
[football player detect]: https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/football-players-detection.avif
|
||||
[human fall detect]: https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/person-fall-detection.avif
|
||||
|
||||
## FAQ
|
||||
|
||||
### What is Ultralytics YOLO and its predict mode for real-time inference?
|
||||
|
||||
Ultralytics YOLO is a state-of-the-art model for real-time [object detection](https://www.ultralytics.com/glossary/object-detection), segmentation, and classification. Its **predict mode** allows users to perform high-speed inference on various data sources such as images, videos, and live streams. Designed for performance and versatility, it also offers batch processing and streaming modes. For more details on its features, check out the [Ultralytics YOLO predict mode](#key-features-of-predict-mode).
|
||||
|
||||
### How can I run inference using Ultralytics YOLO on different data sources?
|
||||
|
||||
Ultralytics YOLO can process a wide range of data sources, including individual images, videos, directories, URLs, and streams. You can specify the data source in the `model.predict()` call. For example, use `'image.jpg'` for a local image or `'https://ultralytics.com/images/bus.jpg'` for a URL. Check out the detailed examples for various [inference sources](#inference-sources) in the documentation.
|
||||
|
||||
### How do I optimize YOLO inference speed and memory usage?
|
||||
|
||||
To optimize inference speed and manage memory efficiently, you can use the streaming mode by setting `stream=True` in the predictor's call method. The streaming mode generates a memory-efficient generator of `Results` objects instead of loading all frames into memory. For processing long videos or large datasets, streaming mode is particularly useful. Learn more about [streaming mode](#key-features-of-predict-mode).
|
||||
|
||||
### What inference arguments does Ultralytics YOLO support?
|
||||
|
||||
The `model.predict()` method in YOLO supports various arguments such as `conf`, `iou`, `imgsz`, `device`, and more. These arguments allow you to customize the inference process, setting parameters like confidence thresholds, image size, and the device used for computation. Detailed descriptions of these arguments can be found in the [inference arguments](#inference-arguments) section.
|
||||
|
||||
### How can I visualize and save the results of YOLO predictions?
|
||||
|
||||
After running inference with YOLO, the `Results` objects contain methods for displaying and saving annotated images. You can use methods like `result.show()` and `result.save(filename="result.jpg")` to visualize and save the results. Any missing parent directories in the filename path are created automatically (e.g., `result.save("path/to/result.jpg")`). For a comprehensive list of these methods, refer to the [working with results](#working-with-results) section.
|
||||
533
docs/en/modes/track.md
Executable file
533
docs/en/modes/track.md
Executable file
@@ -0,0 +1,533 @@
|
||||
---
|
||||
comments: true
|
||||
description: Discover efficient, flexible, and customizable multi-object tracking with Ultralytics YOLO. Learn to track real-time video streams with ease.
|
||||
keywords: multi-object tracking, Ultralytics YOLO, video analytics, real-time tracking, object detection, AI, machine learning
|
||||
---
|
||||
|
||||
# Multi-Object Tracking with Ultralytics YOLO
|
||||
|
||||
<img width="1024" src="https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/multi-object-tracking-examples.avif" alt="YOLO multi-object tracking with trajectory paths">
|
||||
|
||||
Object tracking in the realm of video analytics is a critical task that not only identifies the location and class of objects within the frame but also maintains a unique ID for each detected object as the video progresses. The applications are limitless—ranging from surveillance and security to real-time sports analytics.
|
||||
|
||||
## Why Choose Ultralytics YOLO for Object Tracking?
|
||||
|
||||
The output from Ultralytics trackers is consistent with standard [object detection](https://www.ultralytics.com/glossary/object-detection) but has the added value of object IDs. This makes it easy to track objects in video streams and perform subsequent analytics. Here's why you should consider using Ultralytics YOLO for your object tracking needs:
|
||||
|
||||
- **Efficiency:** Process video streams in real-time without compromising [accuracy](https://www.ultralytics.com/glossary/accuracy).
|
||||
- **Flexibility:** Supports multiple tracking algorithms and configurations.
|
||||
- **Ease of Use:** Simple Python API and CLI options for quick integration and deployment.
|
||||
- **Customizability:** Easy to use with custom-trained YOLO models, allowing integration into domain-specific applications.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/qQkzKISt5GE"
|
||||
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> How to Run Multi-Object Tracking with Ultralytics YOLO26 | BoT-SORT & ByteTrack | VisionAI 🚀
|
||||
</p>
|
||||
|
||||
## Real-world Applications
|
||||
|
||||
| Transportation | Retail | Aquaculture |
|
||||
| :--------------------------------: | :------------------------------: | :--------------------------: |
|
||||
| ![Vehicle Tracking][vehicle track] | ![People Tracking][people track] | ![Fish Tracking][fish track] |
|
||||
| Vehicle Tracking | People Tracking | Fish Tracking |
|
||||
|
||||
## Features at a Glance
|
||||
|
||||
Ultralytics YOLO extends its object detection features to provide robust and versatile object tracking:
|
||||
|
||||
- **Real-Time Tracking:** Seamlessly track objects in high-frame-rate videos.
|
||||
- **Multiple Tracker Support:** Choose from a variety of established tracking algorithms.
|
||||
- **Customizable Tracker Configurations:** Tailor the tracking algorithm to meet specific requirements by adjusting various parameters.
|
||||
|
||||
## Available Trackers
|
||||
|
||||
Ultralytics YOLO supports the following tracking algorithms. They can be enabled by passing the relevant YAML configuration file such as `tracker=tracker_type.yaml`:
|
||||
|
||||
- [BoT-SORT](https://github.com/NirAharon/BoT-SORT) - Use `botsort.yaml` to enable this tracker.
|
||||
- [ByteTrack](https://github.com/FoundationVision/ByteTrack) - Use `bytetrack.yaml` to enable this tracker.
|
||||
|
||||
The default tracker is BoT-SORT.
|
||||
|
||||
## Tracking
|
||||
|
||||
To run the tracker on video streams, use a trained Detect, Segment, or Pose model such as YOLO26n, YOLO26n-seg, or YOLO26n-pose.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load an official or custom model
|
||||
model = YOLO("yolo26n.pt") # Load an official Detect model
|
||||
model = YOLO("yolo26n-seg.pt") # Load an official Segment model
|
||||
model = YOLO("yolo26n-pose.pt") # Load an official Pose model
|
||||
model = YOLO("path/to/best.pt") # Load a custom-trained model
|
||||
|
||||
# Perform tracking with the model
|
||||
results = model.track("https://youtu.be/LNwODJXcvt4", show=True) # Tracking with default tracker
|
||||
results = model.track("https://youtu.be/LNwODJXcvt4", show=True, tracker="bytetrack.yaml") # with ByteTrack
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Perform tracking with various models using the command line interface
|
||||
yolo track model=yolo26n.pt source="https://youtu.be/LNwODJXcvt4" # Official Detect model
|
||||
yolo track model=yolo26n-seg.pt source="https://youtu.be/LNwODJXcvt4" # Official Segment model
|
||||
yolo track model=yolo26n-pose.pt source="https://youtu.be/LNwODJXcvt4" # Official Pose model
|
||||
yolo track model=path/to/best.pt source="https://youtu.be/LNwODJXcvt4" # Custom trained model
|
||||
|
||||
# Track using ByteTrack tracker
|
||||
yolo track model=path/to/best.pt source="https://youtu.be/LNwODJXcvt4" tracker="bytetrack.yaml"
|
||||
```
|
||||
|
||||
As can be seen in the above usage, tracking is available for all Detect, Segment, and Pose models run on videos or streaming sources.
|
||||
|
||||
## Configuration
|
||||
|
||||
### Tracking Arguments
|
||||
|
||||
Tracking configuration shares properties with Predict mode, such as `conf`, `iou`, and `show`. For further configurations, refer to the [Predict](../modes/predict.md#inference-arguments) model page.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Configure the tracking parameters and run the tracker
|
||||
model = YOLO("yolo26n.pt")
|
||||
results = model.track(source="https://youtu.be/LNwODJXcvt4", conf=0.1, iou=0.7, show=True)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Configure tracking parameters and run the tracker using the command line interface
|
||||
yolo track model=yolo26n.pt source="https://youtu.be/LNwODJXcvt4" conf=0.1 iou=0.7 show
|
||||
```
|
||||
|
||||
### Tracker Selection
|
||||
|
||||
Ultralytics also allows you to use a modified tracker configuration file. To do this, simply make a copy of a tracker config file (for example, `custom_tracker.yaml`) from [ultralytics/cfg/trackers](https://github.com/ultralytics/ultralytics/tree/main/ultralytics/cfg/trackers) and modify any configurations (except the `tracker_type`) as per your needs.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load the model and run the tracker with a custom configuration file
|
||||
model = YOLO("yolo26n.pt")
|
||||
results = model.track(source="https://youtu.be/LNwODJXcvt4", tracker="custom_tracker.yaml")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Load the model and run the tracker with a custom configuration file using the command line interface
|
||||
yolo track model=yolo26n.pt source="https://youtu.be/LNwODJXcvt4" tracker='custom_tracker.yaml'
|
||||
```
|
||||
|
||||
Refer to [Tracker Arguments](#tracker-arguments) section for a detailed description of each parameter.
|
||||
|
||||
### Tracker Arguments
|
||||
|
||||
Some tracking behaviors can be fine-tuned by editing the YAML configuration files specific to each tracking algorithm. These files define parameters like thresholds, buffers, and matching logic:
|
||||
|
||||
- [`botsort.yaml`](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/trackers/botsort.yaml)
|
||||
- [`bytetrack.yaml`](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/trackers/bytetrack.yaml)
|
||||
|
||||
The following table provides a description of each parameter:
|
||||
|
||||
!!! warning "Tracker Threshold Information"
|
||||
|
||||
If a detection's confidence score falls below [`track_high_thresh`](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/trackers/bytetrack.yaml#L5), the tracker will not update that object, resulting in no active tracks.
|
||||
|
||||
| **Parameter** | **Valid Values or Ranges** | **Description** |
|
||||
| ------------------- | --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `tracker_type` | `botsort`, `bytetrack` | Specifies the tracker type. Options are `botsort` or `bytetrack`. |
|
||||
| `track_high_thresh` | `0.0-1.0` | Threshold used for the first association during tracking. Affects how confidently a detection is matched to an existing track. |
|
||||
| `track_low_thresh` | `0.0-1.0` | Threshold for the second association during tracking. Used when the first association fails, with more lenient criteria. |
|
||||
| `new_track_thresh` | `0.0-1.0` | Threshold to initialize a new track if the detection does not match any existing tracks. Controls when a new object is considered to appear. |
|
||||
| `track_buffer` | `>=0` | Buffer used to indicate the number of frames lost tracks should be kept alive before getting removed. Higher value means more tolerance for occlusion. |
|
||||
| `match_thresh` | `0.0-1.0` | Threshold for matching tracks. Higher values make the matching more lenient. |
|
||||
| `fuse_score` | `True`, `False` | Determines whether to fuse confidence scores with IoU distances before matching. Helps balance spatial and confidence information when associating. |
|
||||
| `gmc_method` | `orb`, `sift`, `ecc`, `sparseOptFlow`, `None` | Method used for global motion compensation. Helps account for camera movement to improve tracking. |
|
||||
| `proximity_thresh` | `0.0-1.0` | Minimum IoU required for a valid match with ReID (Re-identification). Ensures spatial closeness before using appearance cues. |
|
||||
| `appearance_thresh` | `0.0-1.0` | Minimum appearance similarity required for ReID. Sets how visually similar two detections must be to be linked. |
|
||||
| `with_reid` | `True`, `False` | Indicates whether to use ReID. Enables appearance-based matching for better tracking across occlusions. Only supported by BoTSORT. |
|
||||
| `model` | `auto`, `yolo26[nsmlx]-cls.pt` | Specifies the model to use. Defaults to `auto`, which uses native features if the detector is YOLO, otherwise uses `yolo26n-cls.pt`. |
|
||||
|
||||
### Enabling Re-Identification (ReID)
|
||||
|
||||
By default, ReID is turned off to minimize performance overhead. Enabling it is simple—just set `with_reid: True` in the [tracker configuration](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/trackers/botsort.yaml). You can also customize the `model` used for ReID, allowing you to trade off accuracy and speed depending on your use case:
|
||||
|
||||
- **Native features (`model: auto`)**: This leverages features directly from the YOLO detector for ReID, adding minimal overhead. It's ideal when you need some level of ReID without significantly impacting performance. If the detector doesn't support native features, it automatically falls back to using `yolo26n-cls.pt`.
|
||||
- **YOLO classification models**: You can explicitly set a classification model (e.g. `yolo26n-cls.pt`) for ReID feature extraction. This provides more discriminative embeddings, but introduces additional latency due to the extra inference step.
|
||||
|
||||
For better performance, especially when using a separate classification model for ReID, you can export it to a faster backend like TensorRT:
|
||||
|
||||
!!! example "Exporting a ReID model to TensorRT"
|
||||
|
||||
```python
|
||||
from torch import nn
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load the classification model
|
||||
model = YOLO("yolo26n-cls.pt")
|
||||
|
||||
# Add average pooling layer
|
||||
head = model.model.model[-1]
|
||||
pool = nn.Sequential(nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(start_dim=1))
|
||||
pool.f, pool.i = head.f, head.i
|
||||
model.model.model[-1] = pool
|
||||
|
||||
# Export to TensorRT
|
||||
model.export(format="engine", half=True, dynamic=True, batch=32)
|
||||
```
|
||||
|
||||
Once exported, you can point to the TensorRT model path in your tracker config, and it will be used for ReID during tracking.
|
||||
|
||||
## Python Examples
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/leOPZhE0ckg"
|
||||
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> How to Build Interactive Object Tracking with Ultralytics YOLO | Click to Crop & Display ⚡
|
||||
</p>
|
||||
|
||||
### Persisting Tracks Loop
|
||||
|
||||
Here is a Python script using [OpenCV](https://www.ultralytics.com/glossary/opencv) (`cv2`) and YOLO26 to run object tracking on video frames. This script assumes the necessary packages (`opencv-python` and `ultralytics`) are already installed. The `persist=True` argument tells the tracker that the current image or frame is the next in a sequence and to expect tracks from the previous image in the current image.
|
||||
|
||||
!!! example "Streaming for-loop with tracking"
|
||||
|
||||
```python
|
||||
import cv2
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load the YOLO26 model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Open the video file
|
||||
video_path = "path/to/video.mp4"
|
||||
cap = cv2.VideoCapture(video_path)
|
||||
|
||||
# Loop through the video frames
|
||||
while cap.isOpened():
|
||||
# Read a frame from the video
|
||||
success, frame = cap.read()
|
||||
|
||||
if success:
|
||||
# Run YOLO26 tracking on the frame, persisting tracks between frames
|
||||
results = model.track(frame, persist=True)
|
||||
|
||||
# Visualize the results on the frame
|
||||
annotated_frame = results[0].plot()
|
||||
|
||||
# Display the annotated frame
|
||||
cv2.imshow("YOLO26 Tracking", annotated_frame)
|
||||
|
||||
# Break the loop if 'q' is pressed
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
else:
|
||||
# Break the loop if the end of the video is reached
|
||||
break
|
||||
|
||||
# Release the video capture object and close the display window
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
Please note the change from `model(frame)` to `model.track(frame)`, which enables object tracking instead of simple detection. This modified script will run the tracker on each frame of the video, visualize the results, and display them in a window. The loop can be exited by pressing 'q'.
|
||||
|
||||
### Plotting Tracks Over Time
|
||||
|
||||
Visualizing object tracks over consecutive frames can provide valuable insights into the movement patterns and behavior of detected objects within a video. With Ultralytics YOLO26, plotting these tracks is a seamless and efficient process.
|
||||
|
||||
In the following example, we demonstrate how to utilize YOLO26's tracking capabilities to plot the movement of detected objects across multiple video frames. This script involves opening a video file, reading it frame by frame, and utilizing the YOLO model to identify and track various objects. By retaining the center points of the detected bounding boxes and connecting them, we can draw lines that represent the paths followed by the tracked objects.
|
||||
|
||||
!!! example "Plotting tracks over multiple video frames"
|
||||
|
||||
```python
|
||||
from collections import defaultdict
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load the YOLO26 model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Open the video file
|
||||
video_path = "path/to/video.mp4"
|
||||
cap = cv2.VideoCapture(video_path)
|
||||
|
||||
# Store the track history
|
||||
track_history = defaultdict(lambda: [])
|
||||
|
||||
# Loop through the video frames
|
||||
while cap.isOpened():
|
||||
# Read a frame from the video
|
||||
success, frame = cap.read()
|
||||
|
||||
if success:
|
||||
# Run YOLO26 tracking on the frame, persisting tracks between frames
|
||||
result = model.track(frame, persist=True)[0]
|
||||
|
||||
# Get the boxes and track IDs
|
||||
if result.boxes and result.boxes.is_track:
|
||||
boxes = result.boxes.xywh.cpu()
|
||||
track_ids = result.boxes.id.int().cpu().tolist()
|
||||
|
||||
# Visualize the result on the frame
|
||||
frame = result.plot()
|
||||
|
||||
# Plot the tracks
|
||||
for box, track_id in zip(boxes, track_ids):
|
||||
x, y, w, h = box
|
||||
track = track_history[track_id]
|
||||
track.append((float(x), float(y))) # x, y center point
|
||||
if len(track) > 30: # retain 30 tracks for 30 frames
|
||||
track.pop(0)
|
||||
|
||||
# Draw the tracking lines
|
||||
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
|
||||
cv2.polylines(frame, [points], isClosed=False, color=(230, 230, 230), thickness=10)
|
||||
|
||||
# Display the annotated frame
|
||||
cv2.imshow("YOLO26 Tracking", frame)
|
||||
|
||||
# Break the loop if 'q' is pressed
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
else:
|
||||
# Break the loop if the end of the video is reached
|
||||
break
|
||||
|
||||
# Release the video capture object and close the display window
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
### Multithreaded Tracking
|
||||
|
||||
Multithreaded tracking provides the capability to run object tracking on multiple video streams simultaneously. This is particularly useful when handling multiple video inputs, such as from multiple surveillance cameras, where concurrent processing can greatly enhance efficiency and performance.
|
||||
|
||||
In the provided Python script, we make use of Python's `threading` module to run multiple instances of the tracker concurrently. Each thread is responsible for running the tracker on one video file, and all the threads run simultaneously in the background.
|
||||
|
||||
To ensure that each thread receives the correct parameters (the video file, the model to use and the file index), we define a function `run_tracker_in_thread` that accepts these parameters and contains the main tracking loop. This function reads the video frame by frame, runs the tracker, and displays the results.
|
||||
|
||||
Two different models are used in this example: `yolo26n.pt` and `yolo26n-seg.pt`, each tracking objects in a different video file. The video files are specified in `SOURCES`.
|
||||
|
||||
The `daemon=True` parameter in `threading.Thread` means that these threads will be closed as soon as the main program finishes. We then start the threads with `start()` and use `join()` to make the main thread wait until both tracker threads have finished.
|
||||
|
||||
Finally, after all threads have completed their task, the windows displaying the results are closed using `cv2.destroyAllWindows()`.
|
||||
|
||||
!!! example "Multithreaded tracking implementation"
|
||||
|
||||
```python
|
||||
import threading
|
||||
|
||||
import cv2
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Define model names and video sources
|
||||
MODEL_NAMES = ["yolo26n.pt", "yolo26n-seg.pt"]
|
||||
SOURCES = ["path/to/video.mp4", "0"] # local video, 0 for webcam
|
||||
|
||||
|
||||
def run_tracker_in_thread(model_name, filename):
|
||||
"""Run YOLO tracker in its own thread for concurrent processing.
|
||||
|
||||
Args:
|
||||
model_name (str): The YOLO26 model object.
|
||||
filename (str): The path to the video file or the identifier for the webcam/external camera source.
|
||||
"""
|
||||
model = YOLO(model_name)
|
||||
results = model.track(filename, save=True, stream=True)
|
||||
for r in results:
|
||||
pass
|
||||
|
||||
|
||||
# Create and start tracker threads using a for loop
|
||||
tracker_threads = []
|
||||
for video_file, model_name in zip(SOURCES, MODEL_NAMES):
|
||||
thread = threading.Thread(target=run_tracker_in_thread, args=(model_name, video_file), daemon=True)
|
||||
tracker_threads.append(thread)
|
||||
thread.start()
|
||||
|
||||
# Wait for all tracker threads to finish
|
||||
for thread in tracker_threads:
|
||||
thread.join()
|
||||
|
||||
# Clean up and close windows
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
This example can easily be extended to handle more video files and models by creating more threads and applying the same methodology.
|
||||
|
||||
## Contribute New Trackers
|
||||
|
||||
Are you proficient in multi-object tracking and have successfully implemented or adapted a tracking algorithm with Ultralytics YOLO? We invite you to contribute to our Trackers section in [ultralytics/cfg/trackers](https://github.com/ultralytics/ultralytics/tree/main/ultralytics/cfg/trackers)! Your real-world applications and solutions could be invaluable for users working on tracking tasks.
|
||||
|
||||
By contributing to this section, you help expand the scope of tracking solutions available within the Ultralytics YOLO framework, adding another layer of functionality and utility for the community.
|
||||
|
||||
To initiate your contribution, please refer to our [Contributing Guide](../help/contributing.md) for comprehensive instructions on submitting a Pull Request (PR) 🛠️. We are excited to see what you bring to the table!
|
||||
|
||||
Together, let's enhance the tracking capabilities of the Ultralytics YOLO ecosystem 🙏!
|
||||
|
||||
[fish track]: https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/fish-tracking.avif
|
||||
[people track]: https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/people-tracking.avif
|
||||
[vehicle track]: https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/vehicle-tracking.avif
|
||||
|
||||
## FAQ
|
||||
|
||||
### What is Multi-Object Tracking and how does Ultralytics YOLO support it?
|
||||
|
||||
Multi-object tracking in video analytics involves both identifying objects and maintaining a unique ID for each detected object across video frames. Ultralytics YOLO supports this by providing real-time tracking along with object IDs, facilitating tasks such as security surveillance and sports analytics. The system uses trackers like [BoT-SORT](https://github.com/NirAharon/BoT-SORT) and [ByteTrack](https://github.com/FoundationVision/ByteTrack), which can be configured via YAML files.
|
||||
|
||||
### How do I configure a custom tracker for Ultralytics YOLO?
|
||||
|
||||
You can configure a custom tracker by copying an existing tracker configuration file (e.g., `custom_tracker.yaml`) from the [Ultralytics tracker configuration directory](https://github.com/ultralytics/ultralytics/tree/main/ultralytics/cfg/trackers) and modifying parameters as needed, except for the `tracker_type`. Use this file in your tracking model like so:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
results = model.track(source="https://youtu.be/LNwODJXcvt4", tracker="custom_tracker.yaml")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo track model=yolo26n.pt source="https://youtu.be/LNwODJXcvt4" tracker='custom_tracker.yaml'
|
||||
```
|
||||
|
||||
### How can I run object tracking on multiple video streams simultaneously?
|
||||
|
||||
To run object tracking on multiple video streams simultaneously, you can use Python's `threading` module. Each thread will handle a separate video stream. Here's an example of how you can set this up:
|
||||
|
||||
!!! example "Multithreaded Tracking"
|
||||
|
||||
```python
|
||||
import threading
|
||||
|
||||
import cv2
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Define model names and video sources
|
||||
MODEL_NAMES = ["yolo26n.pt", "yolo26n-seg.pt"]
|
||||
SOURCES = ["path/to/video.mp4", "0"] # local video, 0 for webcam
|
||||
|
||||
|
||||
def run_tracker_in_thread(model_name, filename):
|
||||
"""Run YOLO tracker in its own thread for concurrent processing.
|
||||
|
||||
Args:
|
||||
model_name (str): The YOLO26 model object.
|
||||
filename (str): The path to the video file or the identifier for the webcam/external camera source.
|
||||
"""
|
||||
model = YOLO(model_name)
|
||||
results = model.track(filename, save=True, stream=True)
|
||||
for r in results:
|
||||
pass
|
||||
|
||||
|
||||
# Create and start tracker threads using a for loop
|
||||
tracker_threads = []
|
||||
for video_file, model_name in zip(SOURCES, MODEL_NAMES):
|
||||
thread = threading.Thread(target=run_tracker_in_thread, args=(model_name, video_file), daemon=True)
|
||||
tracker_threads.append(thread)
|
||||
thread.start()
|
||||
|
||||
# Wait for all tracker threads to finish
|
||||
for thread in tracker_threads:
|
||||
thread.join()
|
||||
|
||||
# Clean up and close windows
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
### What are the real-world applications of multi-object tracking with Ultralytics YOLO?
|
||||
|
||||
Multi-object tracking with Ultralytics YOLO has numerous applications, including:
|
||||
|
||||
- **Transportation:** Vehicle tracking for traffic management and [autonomous driving](https://www.ultralytics.com/blog/ai-in-self-driving-cars).
|
||||
- **Retail:** People tracking for in-store analytics and security.
|
||||
- **Aquaculture:** Fish tracking for monitoring aquatic environments.
|
||||
- **Sports Analytics:** Tracking players and equipment for performance analysis.
|
||||
- **Security Systems:** [Monitoring suspicious activities](https://www.ultralytics.com/blog/security-alarm-system-projects-with-ultralytics-yolov8) and creating [security alarms](https://docs.ultralytics.com/guides/security-alarm-system/).
|
||||
|
||||
These applications benefit from Ultralytics YOLO's ability to process high-frame-rate videos in real time with exceptional accuracy.
|
||||
|
||||
### How can I visualize object tracks over multiple video frames with Ultralytics YOLO?
|
||||
|
||||
To visualize object tracks over multiple video frames, you can use the YOLO model's tracking features along with OpenCV to draw the paths of detected objects. Here's an example script that demonstrates this:
|
||||
|
||||
!!! example "Plotting tracks over multiple video frames"
|
||||
|
||||
```python
|
||||
from collections import defaultdict
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
video_path = "path/to/video.mp4"
|
||||
cap = cv2.VideoCapture(video_path)
|
||||
track_history = defaultdict(lambda: [])
|
||||
|
||||
while cap.isOpened():
|
||||
success, frame = cap.read()
|
||||
if success:
|
||||
results = model.track(frame, persist=True)
|
||||
boxes = results[0].boxes.xywh.cpu()
|
||||
track_ids = results[0].boxes.id.int().cpu().tolist()
|
||||
annotated_frame = results[0].plot()
|
||||
for box, track_id in zip(boxes, track_ids):
|
||||
x, y, w, h = box
|
||||
track = track_history[track_id]
|
||||
track.append((float(x), float(y)))
|
||||
if len(track) > 30:
|
||||
track.pop(0)
|
||||
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
|
||||
cv2.polylines(annotated_frame, [points], isClosed=False, color=(230, 230, 230), thickness=10)
|
||||
cv2.imshow("YOLO26 Tracking", annotated_frame)
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
else:
|
||||
break
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
This script will plot the tracking lines showing the movement paths of the tracked objects over time, providing valuable insights into object behavior and patterns.
|
||||
430
docs/en/modes/train.md
Executable file
430
docs/en/modes/train.md
Executable file
@@ -0,0 +1,430 @@
|
||||
---
|
||||
comments: true
|
||||
description: Learn how to efficiently train object detection models using YOLO26 with comprehensive instructions on settings, augmentation, and hardware utilization.
|
||||
keywords: Ultralytics, YOLO26, model training, deep learning, object detection, GPU training, dataset augmentation, hyperparameter tuning, model performance, apple silicon training
|
||||
---
|
||||
|
||||
# Model Training with Ultralytics YOLO
|
||||
|
||||
<img width="1024" src="https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/ultralytics-yolov8-ecosystem-integrations.avif" alt="Ultralytics YOLO ecosystem and integrations">
|
||||
|
||||
## Introduction
|
||||
|
||||
Training a [deep learning](https://www.ultralytics.com/glossary/deep-learning-dl) model involves feeding it data and adjusting its parameters so that it can make accurate predictions. Train mode in Ultralytics YOLO26 is engineered for effective and efficient training of object detection models, fully utilizing modern hardware capabilities. This guide aims to cover all the details you need to get started with training your own models using YOLO26's robust set of features.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/LNwODJXcvt4?si=7n1UvGRLSd9p5wKs"
|
||||
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> How to Train a YOLO model on Your Custom Dataset in Google Colab.
|
||||
</p>
|
||||
|
||||
## Why Choose Ultralytics YOLO for Training?
|
||||
|
||||
Here are some compelling reasons to opt for YOLO26's Train mode:
|
||||
|
||||
- **Efficiency:** Make the most out of your hardware, whether you're on a single-GPU setup or scaling across multiple GPUs.
|
||||
- **Versatility:** Train on custom datasets in addition to readily available ones like COCO, VOC, and ImageNet.
|
||||
- **User-Friendly:** Simple yet powerful CLI and Python interfaces for a straightforward training experience.
|
||||
- **Hyperparameter Flexibility:** A broad range of customizable hyperparameters to fine-tune model performance. For deeper control, you can [customize the trainer](../guides/custom-trainer.md) itself.
|
||||
|
||||
### Key Features of Train Mode
|
||||
|
||||
The following are some notable features of YOLO26's Train mode:
|
||||
|
||||
- **Automatic Dataset Download:** Standard datasets like COCO, VOC, and ImageNet are downloaded automatically on first use.
|
||||
- **Multi-GPU Support:** Scale your training efforts seamlessly across multiple GPUs to expedite the process.
|
||||
- **Hyperparameter Configuration:** The option to modify hyperparameters through YAML configuration files or CLI arguments.
|
||||
- **Visualization and Monitoring:** Real-time tracking of training metrics and visualization of the learning process for better insights.
|
||||
|
||||
!!! tip
|
||||
|
||||
* YOLO26 datasets like COCO, VOC, ImageNet, and many others automatically download on first use, i.e., `yolo train data=coco.yaml`
|
||||
|
||||
## Usage Examples
|
||||
|
||||
Train YOLO26n on the COCO8 dataset for 100 [epochs](https://www.ultralytics.com/glossary/epoch) at image size 640. The training device can be specified using the `device` argument. If no argument is passed, GPU `device=0` will be used when available; otherwise `device='cpu'` will be used. See the Arguments section below for a full list of training arguments.
|
||||
|
||||
!!! warning "Windows Multi-Processing Error"
|
||||
|
||||
On Windows, you may receive a `RuntimeError` when launching the training as a script. Add an `if __name__ == "__main__":` block before your training code to resolve it.
|
||||
|
||||
!!! example "Single-GPU and CPU Training Example"
|
||||
|
||||
Device is determined automatically. If a GPU is available, it will be used (default CUDA device 0); otherwise training will start on CPU.
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.yaml") # build a new model from YAML
|
||||
model = YOLO("yolo26n.pt") # load a pretrained model (recommended for training)
|
||||
model = YOLO("yolo26n.yaml").load("yolo26n.pt") # build from YAML and transfer weights
|
||||
|
||||
# Train the model
|
||||
results = model.train(data="coco8.yaml", epochs=100, imgsz=640)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Build a new model from YAML and start training from scratch
|
||||
yolo detect train data=coco8.yaml model=yolo26n.yaml epochs=100 imgsz=640
|
||||
|
||||
# Start training from a pretrained *.pt model
|
||||
yolo detect train data=coco8.yaml model=yolo26n.pt epochs=100 imgsz=640
|
||||
|
||||
# Build a new model from YAML, transfer pretrained weights to it and start training
|
||||
yolo detect train data=coco8.yaml model=yolo26n.yaml pretrained=yolo26n.pt epochs=100 imgsz=640
|
||||
```
|
||||
|
||||
### Multi-GPU Training
|
||||
|
||||
Multi-GPU training allows for more efficient utilization of available hardware resources by distributing the training load across multiple GPUs. This feature is available through both the Python API and the command-line interface. To enable multi-GPU training, specify the GPU device IDs you wish to use.
|
||||
|
||||
!!! example "Multi-GPU Training Example"
|
||||
|
||||
To train with 2 GPUs, CUDA devices 0 and 1 use the following commands. Expand to additional GPUs as required.
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt") # load a pretrained model (recommended for training)
|
||||
|
||||
# Train the model with 2 GPUs
|
||||
results = model.train(data="coco8.yaml", epochs=100, imgsz=640, device=[0, 1])
|
||||
|
||||
# Train the model with the two most idle GPUs
|
||||
results = model.train(data="coco8.yaml", epochs=100, imgsz=640, device=[-1, -1])
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Start training from a pretrained *.pt model using GPUs 0 and 1
|
||||
yolo detect train data=coco8.yaml model=yolo26n.pt epochs=100 imgsz=640 device=0,1
|
||||
|
||||
# Use the two most idle GPUs
|
||||
yolo detect train data=coco8.yaml model=yolo26n.pt epochs=100 imgsz=640 device=-1,-1
|
||||
```
|
||||
|
||||
### Idle GPU Training
|
||||
|
||||
Idle GPU Training enables automatic selection of the least utilized GPUs in multi-GPU systems, optimizing resource usage without manual GPU selection. This feature identifies available GPUs based on utilization metrics and VRAM availability.
|
||||
|
||||
!!! example "Idle GPU Training Example"
|
||||
|
||||
To automatically select and use the most idle GPU(s) for training, use the `-1` device parameter. This is particularly useful in shared computing environments or servers with multiple users.
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt") # load a pretrained model (recommended for training)
|
||||
|
||||
# Train using the single most idle GPU
|
||||
results = model.train(data="coco8.yaml", epochs=100, imgsz=640, device=-1)
|
||||
|
||||
# Train using the two most idle GPUs
|
||||
results = model.train(data="coco8.yaml", epochs=100, imgsz=640, device=[-1, -1])
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Start training using the single most idle GPU
|
||||
yolo detect train data=coco8.yaml model=yolo26n.pt epochs=100 imgsz=640 device=-1
|
||||
|
||||
# Start training using the two most idle GPUs
|
||||
yolo detect train data=coco8.yaml model=yolo26n.pt epochs=100 imgsz=640 device=-1,-1
|
||||
```
|
||||
|
||||
The auto-selection algorithm prioritizes GPUs with:
|
||||
|
||||
1. Lower current utilization percentages
|
||||
2. Higher available memory (free VRAM)
|
||||
3. Lower temperature and power consumption
|
||||
|
||||
This feature is especially valuable in shared computing environments or when running multiple training jobs across different models. It automatically adapts to changing system conditions, ensuring optimal resource allocation without manual intervention.
|
||||
|
||||
### Apple Silicon MPS Training
|
||||
|
||||
With the support for Apple silicon chips integrated in the Ultralytics YOLO models, it's now possible to train your models on devices utilizing the powerful Metal Performance Shaders (MPS) framework. The MPS offers a high-performance way of executing computation and image processing tasks on Apple's custom silicon.
|
||||
|
||||
To enable training on Apple silicon chips, you should specify 'mps' as your device when initiating the training process. Below is an example of how you could do this in Python and via the command line:
|
||||
|
||||
!!! example "MPS Training Example"
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt") # load a pretrained model (recommended for training)
|
||||
|
||||
# Train the model with MPS
|
||||
results = model.train(data="coco8.yaml", epochs=100, imgsz=640, device="mps")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Start training from a pretrained *.pt model using MPS
|
||||
yolo detect train data=coco8.yaml model=yolo26n.pt epochs=100 imgsz=640 device=mps
|
||||
```
|
||||
|
||||
While leveraging the computational power of the Apple silicon chips, this enables more efficient processing of the training tasks. For more detailed guidance and advanced configuration options, please refer to the [PyTorch MPS documentation](https://docs.pytorch.org/docs/stable/notes/mps.html).
|
||||
|
||||
### Resuming Interrupted Trainings
|
||||
|
||||
Resuming training from a previously saved state is a crucial feature when working with deep learning models. This can come in handy in various scenarios, like when the training process has been unexpectedly interrupted, or when you wish to continue training a model with new data or for more epochs.
|
||||
|
||||
When training is resumed, Ultralytics YOLO loads the weights from the last saved model and also restores the optimizer state, [learning rate](https://www.ultralytics.com/glossary/learning-rate) scheduler, and the epoch number. This allows you to continue the training process seamlessly from where it was left off.
|
||||
|
||||
You can easily resume training in Ultralytics YOLO by setting the `resume` argument to `True` when calling the `train` method, and specifying the path to the `.pt` file containing the partially trained model weights.
|
||||
|
||||
Below is an example of how to resume an interrupted training using Python and via the command line:
|
||||
|
||||
!!! example "Resume Training Example"
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("path/to/last.pt") # load a partially trained model
|
||||
|
||||
# Resume training
|
||||
results = model.train(resume=True)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
# Resume an interrupted training
|
||||
yolo train resume model=path/to/last.pt
|
||||
```
|
||||
|
||||
By setting `resume=True`, the `train` function will continue training from where it left off, using the state stored in the 'path/to/last.pt' file. If the `resume` argument is omitted or set to `False`, the `train` function will start a new training session.
|
||||
|
||||
Remember that checkpoints are saved at the end of every epoch by default, or at fixed intervals using the `save_period` argument, so you must complete at least 1 epoch to resume a training run.
|
||||
|
||||
## 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](https://www.ultralytics.com/glossary/accuracy). Key training settings include batch size, learning rate, momentum, and weight decay. Additionally, the choice of optimizer, [loss function](https://www.ultralytics.com/glossary/loss-function), and training dataset composition can impact the training process. Careful tuning and experimentation with these settings are crucial for optimizing performance.
|
||||
|
||||
{% include "macros/train-args.md" %}
|
||||
|
||||
!!! info "Note on Batch-size Settings"
|
||||
|
||||
The `batch` argument can be configured in three ways:
|
||||
|
||||
- **Fixed [Batch Size](https://www.ultralytics.com/glossary/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.
|
||||
- **OOM Auto-Retry**: If a CUDA out-of-memory error occurs during the first epoch, the trainer automatically halves the batch size and retries (up to 3 times). This only applies to single-GPU training; multi-GPU (DDP) training will raise the error immediately.
|
||||
|
||||
## Augmentation Settings and Hyperparameters
|
||||
|
||||
Augmentation techniques are essential for improving the robustness and performance of YOLO models by introducing variability into the [training data](https://www.ultralytics.com/glossary/training-data), helping the model generalize better to unseen data. The following table outlines the purpose and effect of each augmentation argument:
|
||||
|
||||
{% include "macros/augmentation-args.md" %}
|
||||
|
||||
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.
|
||||
|
||||
!!! info
|
||||
|
||||
For more information about training augmentation operations, see the [reference section](../reference/data/augment.md).
|
||||
|
||||
## Logging
|
||||
|
||||
In training a YOLO26 model, you might find it valuable to keep track of the model's performance over time. This is where logging comes into play. Ultralytics YOLO provides support for three types of loggers - [Comet](../integrations/comet.md), [ClearML](../integrations/clearml.md), and [TensorBoard](../integrations/tensorboard.md).
|
||||
|
||||
To use a logger, select it from the dropdown menu in the code snippet above and run it. The chosen logger will be installed and initialized.
|
||||
|
||||
### Comet
|
||||
|
||||
[Comet](../integrations/comet.md) is a platform that allows data scientists and developers to track, compare, explain and optimize experiments and models. It provides functionalities such as real-time metrics, code diffs, and hyperparameters tracking.
|
||||
|
||||
To use Comet:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
# pip install comet_ml
|
||||
import comet_ml
|
||||
|
||||
comet_ml.init()
|
||||
```
|
||||
|
||||
Remember to sign in to your Comet account on their website and get your API key. You will need to add this to your environment variables or your script to log your experiments.
|
||||
|
||||
### ClearML
|
||||
|
||||
[ClearML](https://clear.ml/) is an open-source platform that automates tracking of experiments and helps with efficient sharing of resources. It is designed to help teams manage, execute, and reproduce their ML work more efficiently.
|
||||
|
||||
To use ClearML:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
# pip install clearml
|
||||
import clearml
|
||||
|
||||
clearml.browser_login()
|
||||
```
|
||||
|
||||
After running this script, you will need to sign in to your ClearML account on the browser and authenticate your session.
|
||||
|
||||
### TensorBoard
|
||||
|
||||
[TensorBoard](https://www.tensorflow.org/tensorboard) is a visualization toolkit for [TensorFlow](https://www.ultralytics.com/glossary/tensorflow). It allows you to visualize your TensorFlow graph, plot quantitative metrics about the execution of your graph, and show additional data like images that pass through it.
|
||||
|
||||
To use TensorBoard in [Google Colab](https://colab.research.google.com/github/ultralytics/ultralytics/blob/main/examples/tutorial.ipynb):
|
||||
|
||||
!!! example
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
load_ext tensorboard
|
||||
tensorboard --logdir ultralytics/runs # replace with 'runs' directory
|
||||
```
|
||||
|
||||
To use TensorBoard locally run the below command and view results at `http://localhost:6006/`.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
tensorboard --logdir ultralytics/runs # replace with 'runs' directory
|
||||
```
|
||||
|
||||
This will load TensorBoard and direct it to the directory where your training logs are saved.
|
||||
|
||||
After setting up your logger, you can then proceed with your model training. All training metrics will be automatically logged in your chosen platform, and you can access these logs to monitor your model's performance over time, compare different models, and identify areas for improvement.
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I train an [object detection](https://www.ultralytics.com/glossary/object-detection) model using Ultralytics YOLO26?
|
||||
|
||||
To train an object detection model using Ultralytics YOLO26, you can either use the Python API or the CLI. Below is an example for both:
|
||||
|
||||
!!! example "Single-GPU and CPU Training Example"
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt") # load a pretrained model (recommended for training)
|
||||
|
||||
# Train the model
|
||||
results = model.train(data="coco8.yaml", epochs=100, imgsz=640)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo detect train data=coco8.yaml model=yolo26n.pt epochs=100 imgsz=640
|
||||
```
|
||||
|
||||
For more details, refer to the [Train Settings](#train-settings) section.
|
||||
|
||||
### What are the key features of Ultralytics YOLO26's Train mode?
|
||||
|
||||
The key features of Ultralytics YOLO26's Train mode include:
|
||||
|
||||
- **Automatic Dataset Download:** Automatically downloads standard datasets like COCO, VOC, and ImageNet.
|
||||
- **Multi-GPU Support:** Scale training across multiple GPUs for faster processing.
|
||||
- **Hyperparameter Configuration:** Customize hyperparameters through YAML files or CLI arguments.
|
||||
- **Visualization and Monitoring:** Real-time tracking of training metrics for better insights.
|
||||
|
||||
These features make training efficient and customizable to your needs. For more details, see the [Key Features of Train Mode](#key-features-of-train-mode) section.
|
||||
|
||||
### How do I resume training from an interrupted session in Ultralytics YOLO26?
|
||||
|
||||
To resume training from an interrupted session, set the `resume` argument to `True` and specify the path to the last saved checkpoint.
|
||||
|
||||
!!! example "Resume Training Example"
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load the partially trained model
|
||||
model = YOLO("path/to/last.pt")
|
||||
|
||||
# Resume training
|
||||
results = model.train(resume=True)
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo train resume model=path/to/last.pt
|
||||
```
|
||||
|
||||
Check the section on [Resuming Interrupted Trainings](#resuming-interrupted-trainings) for more information.
|
||||
|
||||
### Can I train YOLO26 models on Apple silicon chips?
|
||||
|
||||
Yes, Ultralytics YOLO26 supports training on Apple silicon chips utilizing the Metal Performance Shaders (MPS) framework. Specify 'mps' as your training device.
|
||||
|
||||
!!! example "MPS Training Example"
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Train the model on Apple silicon chip (M1/M2/M3/M4)
|
||||
results = model.train(data="coco8.yaml", epochs=100, imgsz=640, device="mps")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo detect train data=coco8.yaml model=yolo26n.pt epochs=100 imgsz=640 device=mps
|
||||
```
|
||||
|
||||
For more details, refer to the [Apple Silicon MPS Training](#apple-silicon-mps-training) section.
|
||||
|
||||
### What are the common training settings, and how do I configure them?
|
||||
|
||||
Ultralytics YOLO26 allows you to configure a variety of training settings such as batch size, learning rate, epochs, and more through arguments. Here's a brief overview:
|
||||
|
||||
| Argument | Default | Description |
|
||||
| -------- | ------- | ---------------------------------------------------------------------- |
|
||||
| `model` | `None` | Path to the model file for training. |
|
||||
| `data` | `None` | Path to the dataset configuration file (e.g., `coco8.yaml`). |
|
||||
| `epochs` | `100` | Total number of training epochs. |
|
||||
| `batch` | `16` | Batch size, adjustable as integer or auto mode. |
|
||||
| `imgsz` | `640` | Target image size for training. |
|
||||
| `device` | `None` | Computational device(s) for training like `cpu`, `0`, `0,1`, or `mps`. |
|
||||
| `save` | `True` | Enables saving of training checkpoints and final model weights. |
|
||||
|
||||
For an in-depth guide on training settings, check the [Train Settings](#train-settings) section.
|
||||
256
docs/en/modes/val.md
Executable file
256
docs/en/modes/val.md
Executable file
@@ -0,0 +1,256 @@
|
||||
---
|
||||
comments: true
|
||||
description: Learn how to validate your YOLO26 model with precise metrics, easy-to-use tools, and custom settings for optimal performance.
|
||||
keywords: Ultralytics, YOLO26, model validation, machine learning, object detection, mAP metrics, Python API, CLI
|
||||
---
|
||||
|
||||
# Model Validation with Ultralytics YOLO
|
||||
|
||||
<img width="1024" src="https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/ultralytics-yolov8-ecosystem-integrations.avif" alt="Ultralytics YOLO ecosystem and integrations">
|
||||
|
||||
## Introduction
|
||||
|
||||
Validation is a critical step in the [machine learning](https://www.ultralytics.com/glossary/machine-learning-ml) pipeline, allowing you to assess the quality of your trained models. Val mode in Ultralytics YOLO26 provides a robust suite of tools and metrics for evaluating the performance of your [object detection](https://www.ultralytics.com/glossary/object-detection) models. This guide serves as a complete resource for understanding how to effectively use the Val mode to ensure that your models are both accurate and reliable.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/j8uQc0qB91s?start=47"
|
||||
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 Modes Tutorial: Validation
|
||||
</p>
|
||||
|
||||
## Why Validate with Ultralytics YOLO?
|
||||
|
||||
Here's why using YOLO26's Val mode is advantageous:
|
||||
|
||||
- **Precision:** Get accurate metrics like mAP50, mAP75, and mAP50-95 to comprehensively evaluate your model.
|
||||
- **Convenience:** Utilize built-in features that remember training settings, simplifying the validation process.
|
||||
- **Flexibility:** Validate your model with the same or different datasets and image sizes.
|
||||
- **[Hyperparameter Tuning](https://www.ultralytics.com/glossary/hyperparameter-tuning):** Use validation metrics to fine-tune your model for better performance.
|
||||
|
||||
### Key Features of Val Mode
|
||||
|
||||
These are the notable functionalities offered by YOLO26's Val mode:
|
||||
|
||||
- **Automated Settings:** Models remember their training configurations for straightforward validation.
|
||||
- **Multi-Metric Support:** Evaluate your model based on a range of accuracy metrics.
|
||||
- **CLI and Python API:** Choose from command-line interface or Python API based on your preference for validation.
|
||||
- **Data Compatibility:** Works seamlessly with datasets used during the training phase as well as custom datasets.
|
||||
|
||||
!!! tip
|
||||
|
||||
* YOLO26 models automatically remember their training settings, so you can validate a model at the same image size and on the original dataset easily with just `yolo val model=yolo26n.pt` or `YOLO("yolo26n.pt").val()`
|
||||
|
||||
## Usage Examples
|
||||
|
||||
Validate a trained YOLO26n model [accuracy](https://www.ultralytics.com/glossary/accuracy) on the COCO8 dataset. No arguments are needed as the `model` retains its training `data` and arguments as model attributes. See the Arguments section below for a full list of validation arguments.
|
||||
|
||||
!!! warning "Windows Multi-Processing Error"
|
||||
|
||||
On Windows, you may receive a `RuntimeError` when launching the validation as a script. Add an `if __name__ == "__main__":` block before your validation code to resolve it.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt") # load an official model
|
||||
model = YOLO("path/to/best.pt") # load a custom model
|
||||
|
||||
# Validate the model
|
||||
metrics = model.val() # no arguments needed, dataset and settings remembered
|
||||
metrics.box.map # map50-95
|
||||
metrics.box.map50 # map50
|
||||
metrics.box.map75 # map75
|
||||
metrics.box.maps # a list containing mAP50-95 for each category
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo detect val model=yolo26n.pt # val official model
|
||||
yolo detect val model=path/to/best.pt # val custom model
|
||||
```
|
||||
|
||||
## Arguments for YOLO Model Validation
|
||||
|
||||
When validating YOLO models, several arguments can be fine-tuned to optimize the evaluation process. These arguments control aspects such as input image size, batch processing, and performance thresholds. Below is a detailed breakdown of each argument to help you customize your validation settings effectively.
|
||||
|
||||
{% include "macros/validation-args.md" %}
|
||||
|
||||
Each of these settings plays a vital role in the validation process, allowing for a customizable and efficient evaluation of YOLO models. Adjusting these parameters according to your specific needs and resources can help achieve the best balance between accuracy and performance.
|
||||
|
||||
### Example Validation with Arguments
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/zHxwDkYShNc"
|
||||
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> How to Export Model Validation Results in CSV, JSON, SQL, Polars DataFrame & More
|
||||
</p>
|
||||
|
||||
<a href="https://github.com/ultralytics/notebooks/blob/main/notebooks/how-to-export-the-validation-results-into-dataframe-csv-sql-and-other-formats.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Explore model validation and different export methods in Google Colab"></a>
|
||||
|
||||
The below examples showcase YOLO model validation with custom arguments in Python and CLI.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Customize validation settings
|
||||
metrics = model.val(data="coco8.yaml", imgsz=640, batch=16, conf=0.25, iou=0.7, device="0")
|
||||
```
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo val model=yolo26n.pt data=coco8.yaml imgsz=640 batch=16 conf=0.25 iou=0.7 device=0
|
||||
```
|
||||
|
||||
!!! tip "Export ConfusionMatrix"
|
||||
|
||||
You can also save the ConfusionMatrix results in different formats using the provided code.
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
results = model.val(data="coco8.yaml", plots=True)
|
||||
print(results.confusion_matrix.to_df())
|
||||
```
|
||||
|
||||
| Method | Return Type | Description |
|
||||
| ----------- | ---------------------- | -------------------------------------------------------------------------- |
|
||||
| `summary()` | `List[Dict[str, Any]]` | Converts validation results to a summarized dictionary. |
|
||||
| `to_df()` | `DataFrame` | Returns the validation results as a structured Polars DataFrame. |
|
||||
| `to_csv()` | `str` | Exports the validation results in CSV format and returns the CSV string. |
|
||||
| `to_json()` | `str` | Exports the validation results in JSON format and returns the JSON string. |
|
||||
|
||||
For more details see the [`DataExportMixin` class documentation](../reference/utils/__init__.md/#ultralytics.utils.DataExportMixin).
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I validate my YOLO26 model with Ultralytics?
|
||||
|
||||
To validate your YOLO26 model, you can use the Val mode provided by Ultralytics. For example, using the Python API, you can load a model and run validation with:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Validate the model
|
||||
metrics = model.val()
|
||||
print(metrics.box.map) # map50-95
|
||||
```
|
||||
|
||||
Alternatively, you can use the command-line interface (CLI):
|
||||
|
||||
```bash
|
||||
yolo val model=yolo26n.pt
|
||||
```
|
||||
|
||||
For further customization, you can adjust various arguments like `imgsz`, `batch`, and `conf` in both Python and CLI modes. Check the [Arguments for YOLO Model Validation](#arguments-for-yolo-model-validation) section for the full list of parameters.
|
||||
|
||||
### What metrics can I get from YOLO26 model validation?
|
||||
|
||||
YOLO26 model validation provides several key metrics to assess model performance. These include:
|
||||
|
||||
- mAP50 (mean Average Precision at IoU threshold 0.5)
|
||||
- mAP75 (mean Average Precision at IoU threshold 0.75)
|
||||
- mAP50-95 (mean Average Precision across multiple IoU thresholds from 0.5 to 0.95)
|
||||
|
||||
Using the Python API, you can access these metrics as follows:
|
||||
|
||||
```python
|
||||
metrics = model.val() # assumes `model` has been loaded
|
||||
print(metrics.box.map) # mAP50-95
|
||||
print(metrics.box.map50) # mAP50
|
||||
print(metrics.box.map75) # mAP75
|
||||
print(metrics.box.maps) # list of mAP50-95 for each category
|
||||
```
|
||||
|
||||
For a complete performance evaluation, it's crucial to review all these metrics. For more details, refer to the [Key Features of Val Mode](#key-features-of-val-mode).
|
||||
|
||||
### What are the advantages of using Ultralytics YOLO for validation?
|
||||
|
||||
Using Ultralytics YOLO for validation provides several advantages:
|
||||
|
||||
- **[Precision](https://www.ultralytics.com/glossary/precision):** YOLO26 offers accurate performance metrics including mAP50, mAP75, and mAP50-95.
|
||||
- **Convenience:** The models remember their training settings, making validation straightforward.
|
||||
- **Flexibility:** You can validate against the same or different datasets and image sizes.
|
||||
- **Hyperparameter Tuning:** Validation metrics help in fine-tuning models for better performance.
|
||||
|
||||
These benefits ensure that your models are evaluated thoroughly and can be optimized for superior results. Learn more about these advantages in the [Why Validate with Ultralytics YOLO](#why-validate-with-ultralytics-yolo) section.
|
||||
|
||||
### Can I validate my YOLO26 model using a custom dataset?
|
||||
|
||||
Yes, you can validate your YOLO26 model using a [custom dataset](https://docs.ultralytics.com/datasets/). Specify the `data` argument with the path to your dataset configuration file. This file should include the path to the [validation data](https://www.ultralytics.com/glossary/validation-data).
|
||||
|
||||
!!! note
|
||||
|
||||
Validation is performed using the model's own class names, which you can view using `model.names`, and which may be different to those specified in the dataset configuration file.
|
||||
|
||||
Example in Python:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Validate with a custom dataset
|
||||
metrics = model.val(data="path/to/your/custom_dataset.yaml")
|
||||
print(metrics.box.map) # map50-95
|
||||
```
|
||||
|
||||
Example using CLI:
|
||||
|
||||
```bash
|
||||
yolo val model=yolo26n.pt data=path/to/your/custom_dataset.yaml
|
||||
```
|
||||
|
||||
For more customizable options during validation, see the [Example Validation with Arguments](#example-validation-with-arguments) section.
|
||||
|
||||
### How do I save validation results to a JSON file in YOLO26?
|
||||
|
||||
To save the validation results to a JSON file, you can set the `save_json` argument to `True` when running validation. This can be done in both the Python API and CLI.
|
||||
|
||||
Example in Python:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Save validation results to JSON
|
||||
metrics = model.val(save_json=True)
|
||||
```
|
||||
|
||||
Example using CLI:
|
||||
|
||||
```bash
|
||||
yolo val model=yolo26n.pt save_json=True
|
||||
```
|
||||
|
||||
This functionality is particularly useful for further analysis or integration with other tools. Check the [Arguments for YOLO Model Validation](#arguments-for-yolo-model-validation) for more details.
|
||||
Reference in New Issue
Block a user