feat: HSAP platform v2 — modular navigation, quality review, audit log, world model simulation
Major changes: - New frontend (platform/web/): Vite + React 18 + TypeScript + Tailwind - 4-module navigation: 数据送标 / 模型管理 / 车队管理 / 系统管理 - Data catalog with charts (DMS/ADAS/Lane 3-tab view) - Quality review workflow (标注质检): Good/Fine/Bad scoring with auto-advance - Audit enhancements: batch operations, rejection categories, Feishu notifications - Operation audit log (操作日志) - World model simulation studio (仿真工坊) - Dataset version management with snapshots and diff - ADAS 7-class dataset integration (138K images organized + compressed) - User management with Feishu integration and pagination - CRUD/search/filter on all pages, card layout redesign - PIL-optimized image overlay rendering - Auto-snapshot on build, in_review workflow stage - Removed embedded algorithm code (now in workspace)
This commit is contained in:
271
algorithms/dms_yolo/code.embedded.bak/docs/en/usage/callbacks.md
Normal file
271
algorithms/dms_yolo/code.embedded.bak/docs/en/usage/callbacks.md
Normal file
@@ -0,0 +1,271 @@
|
||||
---
|
||||
comments: true
|
||||
description: Explore Ultralytics callbacks for training, validation, exporting, and prediction. Learn how to use and customize them for your ML models.
|
||||
keywords: Ultralytics, callbacks, training, validation, export, prediction, ML models, YOLO, Python, machine learning
|
||||
---
|
||||
|
||||
# Callbacks
|
||||
|
||||
Ultralytics framework supports callbacks, which serve as entry points at strategic stages during the `train`, `val`, `export`, and `predict` modes. Each callback accepts a `Trainer`, `Validator`, or `Predictor` object, depending on the operation type. All properties of these objects are detailed in the [Reference section](../reference/cfg/__init__.md) of the documentation.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/ENQXiK7HF5o"
|
||||
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 use Ultralytics Callbacks | Predict, Train, Validate and Export Callbacks | Ultralytics YOLO🚀
|
||||
</p>
|
||||
|
||||
## Examples
|
||||
|
||||
### Returning Additional Information with Prediction
|
||||
|
||||
In this example, we demonstrate how to return the original frame along with each result object:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def on_predict_batch_end(predictor):
|
||||
"""Combine prediction results with corresponding frames."""
|
||||
_, image, _, _ = predictor.batch
|
||||
|
||||
# Ensure that image is a list
|
||||
image = image if isinstance(image, list) else [image]
|
||||
|
||||
# Combine the prediction results with the corresponding frames
|
||||
predictor.results = zip(predictor.results, image)
|
||||
|
||||
|
||||
# Create a YOLO model instance
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Add the custom callback to the model
|
||||
model.add_callback("on_predict_batch_end", on_predict_batch_end)
|
||||
|
||||
# Iterate through the results and frames
|
||||
for result, frame in model.predict(): # or model.track()
|
||||
pass
|
||||
```
|
||||
|
||||
### Access Model metrics using the `on_model_save` callback
|
||||
|
||||
This example shows how to retrieve training details, such as the best_fitness score, total_loss, and other metrics after a checkpoint is saved using the `on_model_save` callback.
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a YOLO model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
|
||||
def print_checkpoint_metrics(trainer):
|
||||
"""Print trainer metrics and loss details after each checkpoint is saved."""
|
||||
print(
|
||||
f"Model details\n"
|
||||
f"Best fitness: {trainer.best_fitness}, "
|
||||
f"Loss names: {trainer.loss_names}, " # List of loss names
|
||||
f"Metrics: {trainer.metrics}, "
|
||||
f"Total loss: {trainer.tloss}" # Total loss value
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Add on_model_save callback.
|
||||
model.add_callback("on_model_save", print_checkpoint_metrics)
|
||||
|
||||
# Run model training on custom dataset.
|
||||
results = model.train(data="coco8.yaml", epochs=3)
|
||||
```
|
||||
|
||||
## All Callbacks
|
||||
|
||||
Below are all the supported callbacks. For more details, refer to the callbacks [source code](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/callbacks/base.py).
|
||||
|
||||
### Trainer Callbacks
|
||||
|
||||
| Callback | Description |
|
||||
| --------------------------- | -------------------------------------------------------------------------------------------- |
|
||||
| `on_pretrain_routine_start` | Triggered at the beginning of the pre-training routine. |
|
||||
| `on_pretrain_routine_end` | Triggered at the end of the pre-training routine. |
|
||||
| `on_train_start` | Triggered when the training starts. |
|
||||
| `on_train_epoch_start` | Triggered at the start of each training [epoch](https://www.ultralytics.com/glossary/epoch). |
|
||||
| `on_train_batch_start` | Triggered at the start of each training batch. |
|
||||
| `optimizer_step` | Triggered during the optimizer step. |
|
||||
| `on_before_zero_grad` | Triggered before gradients are zeroed. |
|
||||
| `on_train_batch_end` | Triggered at the end of each training batch. |
|
||||
| `on_train_epoch_end` | Triggered at the end of each training epoch. |
|
||||
| `on_fit_epoch_end` | Triggered at the end of each fit epoch. |
|
||||
| `on_model_save` | Triggered when the model is saved. |
|
||||
| `on_train_end` | Triggered when the training process ends. |
|
||||
| `on_params_update` | Triggered when model parameters are updated. |
|
||||
| `teardown` | Triggered when the training process is being cleaned up. |
|
||||
|
||||
### Validator Callbacks
|
||||
|
||||
| Callback | Description |
|
||||
| -------------------- | ------------------------------------------------ |
|
||||
| `on_val_start` | Triggered when validation starts. |
|
||||
| `on_val_batch_start` | Triggered at the start of each validation batch. |
|
||||
| `on_val_batch_end` | Triggered at the end of each validation batch. |
|
||||
| `on_val_end` | Triggered when validation ends. |
|
||||
|
||||
### Predictor Callbacks
|
||||
|
||||
| Callback | Description |
|
||||
| ---------------------------- | --------------------------------------------------- |
|
||||
| `on_predict_start` | Triggered when the prediction process starts. |
|
||||
| `on_predict_batch_start` | Triggered at the start of each prediction batch. |
|
||||
| `on_predict_postprocess_end` | Triggered at the end of prediction post-processing. |
|
||||
| `on_predict_batch_end` | Triggered at the end of each prediction batch. |
|
||||
| `on_predict_end` | Triggered when the prediction process ends. |
|
||||
|
||||
### Exporter Callbacks
|
||||
|
||||
| Callback | Description |
|
||||
| ----------------- | ----------------------------------------- |
|
||||
| `on_export_start` | Triggered when the export process starts. |
|
||||
| `on_export_end` | Triggered when the export process ends. |
|
||||
|
||||
## FAQ
|
||||
|
||||
### What are Ultralytics callbacks and how can I use them?
|
||||
|
||||
Ultralytics callbacks are specialized entry points that are triggered during key stages of model operations such as training, validation, exporting, and prediction. These callbacks enable custom functionality at specific points in the process, allowing for enhancements and modifications to the workflow. Each callback accepts a `Trainer`, `Validator`, or `Predictor` object, depending on the operation type. For detailed properties of these objects, refer to the [Reference section](../reference/cfg/__init__.md).
|
||||
|
||||
To use a callback, define a function and add it to the model using the [`model.add_callback()`](../reference/engine/model.md#ultralytics.engine.model.Model.add_callback) method. Here is an example of returning additional information during prediction:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def on_predict_batch_end(predictor):
|
||||
"""Handle prediction batch end by combining results with corresponding frames; modifies predictor results."""
|
||||
_, image, _, _ = predictor.batch
|
||||
image = image if isinstance(image, list) else [image]
|
||||
predictor.results = zip(predictor.results, image)
|
||||
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
model.add_callback("on_predict_batch_end", on_predict_batch_end)
|
||||
for result, frame in model.predict():
|
||||
pass
|
||||
```
|
||||
|
||||
### How can I customize the Ultralytics training routine using callbacks?
|
||||
|
||||
Customize your Ultralytics training routine by injecting logic at specific stages of the training process. Ultralytics YOLO provides a variety of training callbacks, such as `on_train_start`, `on_train_end`, and `on_train_batch_end`, which allow you to add custom metrics, processing, or logging.
|
||||
|
||||
Here's how to freeze BatchNorm statistics when freezing layers with callbacks:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
# Add a callback to put the frozen layers in eval mode to prevent BN values from changing
|
||||
def put_in_eval_mode(trainer):
|
||||
n_layers = trainer.args.freeze
|
||||
if not isinstance(n_layers, int):
|
||||
return
|
||||
|
||||
for i, (name, module) in enumerate(trainer.model.named_modules()):
|
||||
if name.endswith("bn") and int(name.split(".")[1]) < n_layers:
|
||||
module.eval()
|
||||
module.track_running_stats = False
|
||||
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
model.add_callback("on_train_epoch_start", put_in_eval_mode)
|
||||
model.train(data="coco.yaml", epochs=10)
|
||||
```
|
||||
|
||||
For more details on effectively using training callbacks, see the [Training Guide](../modes/train.md).
|
||||
|
||||
### Why should I use callbacks during validation in Ultralytics YOLO?
|
||||
|
||||
Using callbacks during validation in Ultralytics YOLO enhances model evaluation by enabling custom processing, logging, or metrics calculation. Callbacks like `on_val_start`, `on_val_batch_end`, and `on_val_end` provide entry points to inject custom logic, ensuring detailed and comprehensive validation processes.
|
||||
|
||||
For example, to plot all validation batches instead of just the first three:
|
||||
|
||||
```python
|
||||
import inspect
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def plot_samples(validator):
|
||||
frame = inspect.currentframe().f_back.f_back
|
||||
v = frame.f_locals
|
||||
validator.plot_val_samples(v["batch"], v["batch_i"])
|
||||
validator.plot_predictions(v["batch"], v["preds"], v["batch_i"])
|
||||
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
model.add_callback("on_val_batch_end", plot_samples)
|
||||
model.val(data="coco.yaml")
|
||||
```
|
||||
|
||||
For more insights on incorporating callbacks into your validation process, see the [Validation Guide](../modes/val.md).
|
||||
|
||||
### How do I attach a custom callback for the prediction mode in Ultralytics YOLO?
|
||||
|
||||
To attach a custom callback for prediction mode in Ultralytics YOLO, define a callback function and register it with the prediction process. Common prediction callbacks include `on_predict_start`, `on_predict_batch_end`, and `on_predict_end`. These allow for the modification of prediction outputs and the integration of additional functionalities, like data logging or result transformation.
|
||||
|
||||
Here is an example where a custom callback saves predictions based on whether an object of a particular class is present:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
class_id = 2
|
||||
|
||||
|
||||
def save_on_object(predictor):
|
||||
r = predictor.results[0]
|
||||
if class_id in r.boxes.cls:
|
||||
predictor.args.save = True
|
||||
else:
|
||||
predictor.args.save = False
|
||||
|
||||
|
||||
model.add_callback("on_predict_postprocess_end", save_on_object)
|
||||
results = model("pedestrians.mp4", stream=True, save=True)
|
||||
|
||||
for results in results:
|
||||
pass
|
||||
```
|
||||
|
||||
For more comprehensive usage, refer to the [Prediction Guide](../modes/predict.md), which includes detailed instructions and additional customization options.
|
||||
|
||||
### What are some practical examples of using callbacks in Ultralytics YOLO?
|
||||
|
||||
Ultralytics YOLO supports various practical implementations of callbacks to enhance and customize different phases like training, validation, and prediction. Some practical examples include:
|
||||
|
||||
- **Logging Custom Metrics**: Log additional metrics at different stages, such as at the end of training or validation [epochs](https://www.ultralytics.com/glossary/epoch).
|
||||
- **[Data Augmentation](https://www.ultralytics.com/glossary/data-augmentation)**: Implement custom data transformations or augmentations during prediction or training batches.
|
||||
- **Intermediate Results**: Save intermediate results, such as predictions or frames, for further analysis or visualization.
|
||||
|
||||
Example: Combining frames with prediction results during prediction using `on_predict_batch_end`:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def on_predict_batch_end(predictor):
|
||||
"""Combine prediction results with frames."""
|
||||
_, image, _, _ = predictor.batch
|
||||
image = image if isinstance(image, list) else [image]
|
||||
predictor.results = zip(predictor.results, image)
|
||||
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
model.add_callback("on_predict_batch_end", on_predict_batch_end)
|
||||
for result, frame in model.predict():
|
||||
pass
|
||||
```
|
||||
|
||||
Explore the [callback source code](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/callbacks/base.py) for more options and examples.
|
||||
210
algorithms/dms_yolo/code.embedded.bak/docs/en/usage/cfg.md
Normal file
210
algorithms/dms_yolo/code.embedded.bak/docs/en/usage/cfg.md
Normal file
@@ -0,0 +1,210 @@
|
||||
---
|
||||
comments: true
|
||||
description: Optimize your Ultralytics YOLO model's performance with the right settings and hyperparameters. Learn about training, validation, and prediction configurations.
|
||||
keywords: YOLO, hyperparameters, configuration, training, validation, prediction, model settings, Ultralytics, performance optimization, machine learning
|
||||
---
|
||||
|
||||
# Configuration
|
||||
|
||||
YOLO settings and hyperparameters play a critical role in the model's performance, speed, and [accuracy](https://www.ultralytics.com/glossary/accuracy). These settings can affect the model's behavior at various stages, including training, validation, and prediction.
|
||||
|
||||
**Watch:** Mastering Ultralytics YOLO: Configuration
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/GsXGnb-A4Kc?start=87"
|
||||
title="YouTube video player" frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||
allowfullscreen>
|
||||
</iframe>
|
||||
<br>
|
||||
<strong>Watch:</strong> Mastering Ultralytics YOLO: Configuration
|
||||
</p>
|
||||
|
||||
Ultralytics commands use the following syntax:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo TASK MODE ARGS
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a YOLO model from a pretrained weights file
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Run the model in MODE using custom ARGS
|
||||
MODE = "predict"
|
||||
ARGS = {"source": "image.jpg", "imgsz": 640}
|
||||
getattr(model, MODE)(**ARGS)
|
||||
```
|
||||
|
||||
Where:
|
||||
|
||||
- `TASK` (optional) is one of ([detect](../tasks/detect.md), [segment](../tasks/segment.md), [classify](../tasks/classify.md), [pose](../tasks/pose.md), [obb](../tasks/obb.md))
|
||||
- `MODE` (required) is one of ([train](../modes/train.md), [val](../modes/val.md), [predict](../modes/predict.md), [export](../modes/export.md), [track](../modes/track.md), [benchmark](../modes/benchmark.md))
|
||||
- `ARGS` (optional) are `arg=value` pairs like `imgsz=640` that override defaults.
|
||||
|
||||
Default `ARG` values are defined on this page and come from the `cfg/default.yaml` [file](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/default.yaml).
|
||||
|
||||
## Tasks
|
||||
|
||||
Ultralytics YOLO models can perform a variety of computer vision tasks, including:
|
||||
|
||||
- **Detect**: [Object detection](https://docs.ultralytics.com/tasks/detect/) identifies and localizes objects within an image or video.
|
||||
- **Segment**: [Instance segmentation](https://docs.ultralytics.com/tasks/segment/) divides an image or video into regions corresponding to different objects or classes.
|
||||
- **Classify**: [Image classification](https://docs.ultralytics.com/tasks/classify/) predicts the class label of an input image.
|
||||
- **Pose**: [Pose estimation](https://docs.ultralytics.com/tasks/pose/) identifies objects and estimates their keypoints in an image or video.
|
||||
- **OBB**: [Oriented Bounding Boxes](https://docs.ultralytics.com/tasks/obb/) uses rotated bounding boxes, suitable for satellite or medical imagery.
|
||||
|
||||
| Argument | Default | Description |
|
||||
| -------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `task` | `'detect'` | Specifies the YOLO task: `detect` for [object detection](https://www.ultralytics.com/glossary/object-detection), `segment` for segmentation, `classify` for classification, `pose` for pose estimation, and `obb` for oriented bounding boxes. Each task is tailored to specific outputs and problems in image and video analysis. |
|
||||
|
||||
[Tasks Guide](../tasks/index.md){ .md-button }
|
||||
|
||||
## Modes
|
||||
|
||||
Ultralytics YOLO models operate in different modes, each designed for a specific stage of the model lifecycle:
|
||||
|
||||
- **Train**: Train a YOLO model on a custom dataset.
|
||||
- **Val**: Validate a trained YOLO model.
|
||||
- **Predict**: Use a trained YOLO model to make predictions on new images or videos.
|
||||
- **Export**: Export a YOLO model for deployment.
|
||||
- **Track**: Track objects in real-time using a YOLO model.
|
||||
- **Benchmark**: Benchmark the speed and accuracy of YOLO exports (ONNX, TensorRT, etc.).
|
||||
|
||||
| Argument | Default | Description |
|
||||
| -------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `mode` | `'train'` | Specifies the YOLO model's operating mode: `train` for model training, `val` for validation, `predict` for inference, `export` for converting to deployment formats, `track` for object tracking, and `benchmark` for performance evaluation. Each mode supports different stages, from development to deployment. |
|
||||
|
||||
[Modes Guide](../modes/index.md){ .md-button }
|
||||
|
||||
## Train Settings
|
||||
|
||||
Training settings for YOLO models include hyperparameters and configurations that affect the model's performance, speed, and [accuracy](https://www.ultralytics.com/glossary/accuracy). Key settings include [batch size](https://www.ultralytics.com/glossary/batch-size), [learning rate](https://www.ultralytics.com/glossary/learning-rate), momentum, and weight decay. The choice of optimizer, [loss function](https://www.ultralytics.com/glossary/loss-function), and dataset composition also impact training. Tuning and experimentation are crucial for optimal performance. For more details, see the [Ultralytics entrypoint function](../reference/cfg/__init__.md).
|
||||
|
||||
{% include "macros/train-args.md" %}
|
||||
|
||||
!!! info "Note on Batch-size Settings"
|
||||
|
||||
The `batch` argument offers three configuration options:
|
||||
|
||||
- **Fixed Batch Size**: Specify the number of images per batch with an integer (e.g., `batch=16`).
|
||||
- **Auto Mode (60% GPU Memory)**: Use `batch=-1` for automatic adjustment to approximately 60% CUDA memory utilization.
|
||||
- **Auto Mode with Utilization Fraction**: Set a fraction (e.g., `batch=0.70`) to adjust based on a specified GPU memory usage.
|
||||
|
||||
[Train Guide](../modes/train.md){ .md-button }
|
||||
|
||||
## Predict Settings
|
||||
|
||||
Prediction settings for YOLO models include hyperparameters and configurations that influence performance, speed, and [accuracy](https://www.ultralytics.com/glossary/accuracy) during inference. Key settings include the confidence threshold, [Non-Maximum Suppression (NMS)](https://www.ultralytics.com/glossary/non-maximum-suppression-nms) threshold, and the number of classes. Input data size, format, and supplementary features like masks also affect predictions. Tuning these settings is essential for optimal performance.
|
||||
|
||||
Inference arguments:
|
||||
|
||||
{% include "macros/predict-args.md" %}
|
||||
|
||||
Visualization arguments:
|
||||
|
||||
{% from "macros/visualization-args.md" import param_table %} {{ param_table() }}
|
||||
|
||||
[Predict Guide](../modes/predict.md){ .md-button }
|
||||
|
||||
## Validation Settings
|
||||
|
||||
Validation settings for YOLO models involve hyperparameters and configurations to evaluate performance on a [validation dataset](https://www.ultralytics.com/glossary/validation-data). These settings influence performance, speed, and [accuracy](https://www.ultralytics.com/glossary/accuracy). Common settings include batch size, validation frequency, and performance metrics. The validation dataset's size and composition, along with the specific task, also affect the process.
|
||||
|
||||
{% include "macros/validation-args.md" %}
|
||||
|
||||
Careful tuning and experimentation are crucial to ensure optimal performance and to detect and prevent [overfitting](https://www.ultralytics.com/glossary/overfitting).
|
||||
|
||||
[Val Guide](../modes/val.md){ .md-button }
|
||||
|
||||
## Export Settings
|
||||
|
||||
Export settings for YOLO models include configurations for saving or exporting the model for use in different environments. These settings impact performance, size, and compatibility. Key settings include the exported file format (e.g., ONNX, TensorFlow SavedModel), the target device (e.g., CPU, GPU), and features like masks. The model's task and the destination environment's constraints also affect the export process.
|
||||
|
||||
{% include "macros/export-args.md" %}
|
||||
|
||||
Thoughtful configuration ensures the exported model is optimized for its use case and functions effectively in the target environment.
|
||||
|
||||
[Export Guide](../modes/export.md){ .md-button }
|
||||
|
||||
## Solutions Settings
|
||||
|
||||
Ultralytics Solutions configuration settings offer flexibility to customize models for tasks like object counting, heatmap creation, workout tracking, data analysis, zone tracking, queue management, and region-based counting. These options allow easy adjustments for accurate and useful results tailored to specific needs.
|
||||
|
||||
{% from "macros/solutions-args.md" import param_table %} {{ param_table() }}
|
||||
|
||||
[Solutions Guide](../solutions/index.md){ .md-button }
|
||||
|
||||
## Augmentation Settings
|
||||
|
||||
[Data augmentation](https://www.ultralytics.com/glossary/data-augmentation) techniques are essential for improving YOLO model robustness and performance 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 each augmentation argument's purpose and effect:
|
||||
|
||||
{% include "macros/augmentation-args.md" %}
|
||||
|
||||
Adjust these settings to meet dataset and task requirements. Experimenting with different values can help find the optimal augmentation strategy for the best model performance.
|
||||
|
||||
[Augmentation Guide](../guides/yolo-data-augmentation.md){ .md-button }
|
||||
|
||||
## Logging, Checkpoints and Plotting Settings
|
||||
|
||||
Logging, checkpoints, plotting, and file management are important when training a YOLO model:
|
||||
|
||||
- **Logging**: Track the model's progress and diagnose issues using libraries like [TensorBoard](https://docs.ultralytics.com/integrations/tensorboard/) or by writing to a file.
|
||||
- **Checkpoints**: Save the model at regular intervals to resume training or experiment with different configurations.
|
||||
- **Plotting**: Visualize performance and training progress using libraries like matplotlib or TensorBoard.
|
||||
- **File management**: Organize files generated during training, such as checkpoints, log files, and plots, for easy access and analysis.
|
||||
|
||||
Effective management of these aspects helps track progress and makes debugging and optimization easier.
|
||||
|
||||
| Argument | Default | Description |
|
||||
| ---------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `project` | `'runs'` | Specifies the root directory for saving training runs. Each run is saved in a separate subdirectory. |
|
||||
| `name` | `'exp'` | Defines the experiment name. If unspecified, YOLO increments this name for each run (e.g., `exp`, `exp2`) to avoid overwriting. |
|
||||
| `exist_ok` | `False` | Determines whether to overwrite an existing experiment directory. `True` allows overwriting; `False` prevents it. |
|
||||
| `plots` | `False` | Controls the generation and saving of training and validation plots. Set to `True` to create plots like loss curves, [precision](https://www.ultralytics.com/glossary/precision)-[recall](https://www.ultralytics.com/glossary/recall) curves, and sample predictions for visual tracking of performance. |
|
||||
| `save` | `False` | Enables saving training checkpoints and final model weights. Set to `True` to save model states periodically, allowing training resumption or model deployment. |
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I improve my YOLO model's performance during training?
|
||||
|
||||
Improve performance by tuning hyperparameters like [batch size](https://www.ultralytics.com/glossary/batch-size), [learning rate](https://www.ultralytics.com/glossary/learning-rate), momentum, and weight decay. Adjust [data augmentation](https://www.ultralytics.com/glossary/data-augmentation) settings, select the right optimizer, and use techniques like early stopping or [mixed precision](https://www.ultralytics.com/glossary/mixed-precision). For details, see the [Train Guide](../modes/train.md).
|
||||
|
||||
### What are the key hyperparameters for YOLO model accuracy?
|
||||
|
||||
Key hyperparameters affecting accuracy include:
|
||||
|
||||
- **Batch Size (`batch`)**: Larger sizes can stabilize training but need more memory.
|
||||
- **Learning Rate (`lr0`)**: Smaller rates offer fine adjustments but slower convergence.
|
||||
- **Momentum (`momentum`)**: Accelerates gradient vectors, dampening oscillations.
|
||||
- **Image Size (`imgsz`)**: Larger sizes improve accuracy but increase computational load.
|
||||
|
||||
Adjust these based on your dataset and hardware. Learn more in [Train Settings](#train-settings).
|
||||
|
||||
### How do I set the learning rate for training a YOLO model?
|
||||
|
||||
The learning rate (`lr0`) is crucial; start with `0.01` for SGD or `0.001` for [Adam optimizer](https://www.ultralytics.com/glossary/adam-optimizer). Monitor metrics and adjust as needed. Use cosine learning rate schedulers (`cos_lr`) or warmup (`warmup_epochs`, `warmup_momentum`). Details are in the [Train Guide](../modes/train.md).
|
||||
|
||||
### What are the default inference settings for YOLO models?
|
||||
|
||||
Default settings include:
|
||||
|
||||
- **Confidence Threshold (`conf=0.25`)**: Minimum confidence for detections.
|
||||
- **IoU Threshold (`iou=0.7`)**: For [Non-Maximum Suppression (NMS)](https://www.ultralytics.com/glossary/non-maximum-suppression-nms).
|
||||
- **Image Size (`imgsz=640`)**: Resizes input images.
|
||||
- **Device (`device=None`)**: Selects CPU or GPU.
|
||||
|
||||
For a full overview, see [Predict Settings](#predict-settings) and the [Predict Guide](../modes/predict.md).
|
||||
|
||||
### Why use mixed precision training with YOLO models?
|
||||
|
||||
[Mixed precision](https://www.ultralytics.com/glossary/mixed-precision) training (`amp=True`) reduces memory usage and speeds up training using FP16 and FP32. It's beneficial for modern GPUs, allowing larger models and faster computations without significant accuracy loss. Learn more in the [Train Guide](../modes/train.md).
|
||||
346
algorithms/dms_yolo/code.embedded.bak/docs/en/usage/cli.md
Normal file
346
algorithms/dms_yolo/code.embedded.bak/docs/en/usage/cli.md
Normal file
@@ -0,0 +1,346 @@
|
||||
---
|
||||
comments: true
|
||||
description: Explore the YOLO command line interface (CLI) for easy execution of detection tasks without needing a Python environment.
|
||||
keywords: YOLO CLI, command line interface, YOLO commands, detection tasks, Ultralytics, model training, model prediction
|
||||
---
|
||||
|
||||
# Command Line Interface
|
||||
|
||||
The Ultralytics command line interface (CLI) provides a straightforward way to use Ultralytics YOLO models without needing a Python environment. The CLI supports running various tasks directly from the terminal using the `yolo` command, requiring no customization or Python code.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/GsXGnb-A4Kc?start=19"
|
||||
title="YouTube video player" frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||
allowfullscreen>
|
||||
</iframe>
|
||||
<br>
|
||||
<strong>Watch:</strong> Mastering Ultralytics YOLO: CLI
|
||||
</p>
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Syntax"
|
||||
|
||||
Ultralytics `yolo` commands use the following syntax:
|
||||
```bash
|
||||
yolo TASK MODE ARGS
|
||||
```
|
||||
|
||||
Where:
|
||||
- `TASK` (optional) is one of [detect, segment, classify, pose, obb]
|
||||
- `MODE` (required) is one of [train, val, predict, export, track, benchmark]
|
||||
- `ARGS` (optional) are any number of custom `arg=value` pairs like `imgsz=320` that override defaults.
|
||||
|
||||
See all ARGS in the full [Configuration Guide](cfg.md) or with `yolo cfg`.
|
||||
|
||||
=== "Train"
|
||||
|
||||
Train a detection model for 10 [epochs](https://www.ultralytics.com/glossary/epoch) with an initial [learning rate](https://www.ultralytics.com/glossary/learning-rate) of 0.01:
|
||||
|
||||
```bash
|
||||
yolo train data=coco8.yaml model=yolo26n.pt epochs=10 lr0=0.01
|
||||
```
|
||||
|
||||
=== "Predict"
|
||||
|
||||
Predict using a pretrained segmentation model on a YouTube video at image size 320:
|
||||
|
||||
```bash
|
||||
yolo predict model=yolo26n-seg.pt source='https://youtu.be/LNwODJXcvt4' imgsz=320
|
||||
```
|
||||
|
||||
=== "Val"
|
||||
|
||||
Validate a pretrained detection model with a [batch size](https://www.ultralytics.com/glossary/batch-size) of 1 and image size 640:
|
||||
|
||||
```bash
|
||||
yolo val model=yolo26n.pt data=coco8.yaml batch=1 imgsz=640
|
||||
```
|
||||
|
||||
=== "Export"
|
||||
|
||||
Export a YOLO classification model to ONNX format with image size 224x128 (no TASK required):
|
||||
|
||||
```bash
|
||||
yolo export model=yolo26n-cls.pt format=onnx imgsz=224,128
|
||||
```
|
||||
|
||||
=== "Special"
|
||||
|
||||
Run special commands to view version, settings, run checks, and more:
|
||||
|
||||
```bash
|
||||
yolo help
|
||||
yolo checks
|
||||
yolo version
|
||||
yolo settings
|
||||
yolo copy-cfg
|
||||
yolo cfg
|
||||
```
|
||||
|
||||
Where:
|
||||
|
||||
- `TASK` (optional) is one of `[detect, segment, classify, pose, obb]`. If not explicitly passed, YOLO will attempt to infer the `TASK` from the model type.
|
||||
- `MODE` (required) is one of `[train, val, predict, export, track, benchmark]`
|
||||
- `ARGS` (optional) are any number of custom `arg=value` pairs like `imgsz=320` that override defaults. For a full list of available `ARGS`, see the [Configuration](cfg.md) page and `default.yaml`.
|
||||
|
||||
!!! warning
|
||||
|
||||
Arguments must be passed as `arg=val` pairs, separated by an equals `=` sign and delimited by spaces between pairs. Do not use `--` argument prefixes or commas `,` between arguments.
|
||||
|
||||
- `yolo predict model=yolo26n.pt imgsz=640 conf=0.25` ✅
|
||||
- `yolo predict model yolo26n.pt imgsz 640 conf 0.25` ❌
|
||||
- `yolo predict --model yolo26n.pt --imgsz 640 --conf 0.25` ❌
|
||||
|
||||
## Train
|
||||
|
||||
Train YOLO on the COCO8 dataset for 100 epochs at image size 640. For a full list of available arguments, see the [Configuration](cfg.md) page.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Train"
|
||||
|
||||
Start training YOLO26n on COCO8 for 100 epochs at image size 640:
|
||||
|
||||
```bash
|
||||
yolo detect train data=coco8.yaml model=yolo26n.pt epochs=100 imgsz=640
|
||||
```
|
||||
|
||||
=== "Resume"
|
||||
|
||||
Resume an interrupted training session:
|
||||
|
||||
```bash
|
||||
yolo detect train resume model=last.pt
|
||||
```
|
||||
|
||||
## Val
|
||||
|
||||
Validate the [accuracy](https://www.ultralytics.com/glossary/accuracy) of the trained model on the COCO8 dataset. No arguments are needed as the `model` retains its training `data` and arguments as model attributes.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Official"
|
||||
|
||||
Validate an official YOLO26n model:
|
||||
|
||||
```bash
|
||||
yolo detect val model=yolo26n.pt
|
||||
```
|
||||
|
||||
=== "Custom"
|
||||
|
||||
Validate a custom-trained model:
|
||||
|
||||
```bash
|
||||
yolo detect val model=path/to/best.pt
|
||||
```
|
||||
|
||||
## Predict
|
||||
|
||||
Use a trained model to run predictions on images.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Official"
|
||||
|
||||
Predict with an official YOLO26n model:
|
||||
|
||||
```bash
|
||||
yolo detect predict model=yolo26n.pt source='https://ultralytics.com/images/bus.jpg'
|
||||
```
|
||||
|
||||
=== "Custom"
|
||||
|
||||
Predict with a custom model:
|
||||
|
||||
```bash
|
||||
yolo detect predict model=path/to/best.pt source='https://ultralytics.com/images/bus.jpg'
|
||||
```
|
||||
|
||||
## Export
|
||||
|
||||
Export a model to a different format like ONNX or CoreML.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Official"
|
||||
|
||||
Export an official YOLO26n model to ONNX format:
|
||||
|
||||
```bash
|
||||
yolo export model=yolo26n.pt format=onnx
|
||||
```
|
||||
|
||||
=== "Custom"
|
||||
|
||||
Export a custom-trained model to ONNX format:
|
||||
|
||||
```bash
|
||||
yolo export model=path/to/best.pt format=onnx
|
||||
```
|
||||
|
||||
Available Ultralytics export formats are in the table below. You can export to any format using the `format` argument, i.e., `format='onnx'` or `format='engine'`.
|
||||
|
||||
{% include "macros/export-table.md" %}
|
||||
|
||||
See full `export` details on the [Export](../modes/export.md) page.
|
||||
|
||||
## Overriding Default Arguments
|
||||
|
||||
Override default arguments by passing them in the CLI as `arg=value` pairs.
|
||||
|
||||
!!! tip
|
||||
|
||||
=== "Train"
|
||||
|
||||
Train a detection model for 10 epochs with a learning rate of 0.01:
|
||||
|
||||
```bash
|
||||
yolo detect train data=coco8.yaml model=yolo26n.pt epochs=10 lr0=0.01
|
||||
```
|
||||
|
||||
=== "Predict"
|
||||
|
||||
Predict using a pretrained segmentation model on a YouTube video at image size 320:
|
||||
|
||||
```bash
|
||||
yolo segment predict model=yolo26n-seg.pt source='https://youtu.be/LNwODJXcvt4' imgsz=320
|
||||
```
|
||||
|
||||
=== "Val"
|
||||
|
||||
Validate a pretrained detection model with a batch size of 1 and image size 640:
|
||||
|
||||
```bash
|
||||
yolo detect val model=yolo26n.pt data=coco8.yaml batch=1 imgsz=640
|
||||
```
|
||||
|
||||
## Overriding Default Config File
|
||||
|
||||
Override the `default.yaml` configuration file entirely by passing a new file with the `cfg` argument, such as `cfg=custom.yaml`.
|
||||
|
||||
To do this, first create a copy of `default.yaml` in your current working directory with the `yolo copy-cfg` command, which creates a `default_copy.yaml` file.
|
||||
|
||||
You can then pass this file as `cfg=default_copy.yaml` along with any additional arguments, like `imgsz=320` in this example:
|
||||
|
||||
!!! example
|
||||
|
||||
=== "CLI"
|
||||
|
||||
```bash
|
||||
yolo copy-cfg
|
||||
yolo cfg=default_copy.yaml imgsz=320
|
||||
```
|
||||
|
||||
## Solutions Commands
|
||||
|
||||
Ultralytics provides ready-to-use solutions for common computer vision applications through the CLI. These solutions simplify the implementation of complex tasks like object counting, workout monitoring, and queue management.
|
||||
|
||||
!!! example
|
||||
|
||||
=== "Count"
|
||||
|
||||
Count objects in a video or live stream:
|
||||
|
||||
```bash
|
||||
yolo solutions count show=True
|
||||
yolo solutions count source="path/to/video.mp4" # specify video file path
|
||||
```
|
||||
|
||||
=== "Workout"
|
||||
|
||||
Monitor workout exercises using a pose model:
|
||||
|
||||
```bash
|
||||
yolo solutions workout show=True
|
||||
yolo solutions workout source="path/to/video.mp4" # specify video file path
|
||||
|
||||
# Use keypoints for ab-workouts
|
||||
yolo solutions workout kpts=[5, 11, 13] # left side
|
||||
yolo solutions workout kpts=[6, 12, 14] # right side
|
||||
```
|
||||
|
||||
=== "Queue"
|
||||
|
||||
Count objects in a designated queue or region:
|
||||
|
||||
```bash
|
||||
yolo solutions queue show=True
|
||||
yolo solutions queue source="path/to/video.mp4" # specify video file path
|
||||
yolo solutions queue region="[(20, 400), (1080, 400), (1080, 360), (20, 360)]" # configure queue coordinates
|
||||
```
|
||||
|
||||
=== "Inference"
|
||||
|
||||
Perform object detection, instance segmentation, or pose estimation in a web browser using Streamlit:
|
||||
|
||||
```bash
|
||||
yolo solutions inference
|
||||
yolo solutions inference model="path/to/model.pt" # use custom model
|
||||
```
|
||||
|
||||
=== "Help"
|
||||
|
||||
View available solutions and their options:
|
||||
|
||||
```bash
|
||||
yolo solutions help
|
||||
```
|
||||
|
||||
For more information on Ultralytics solutions, visit the [Solutions](../solutions/index.md) page.
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I use the Ultralytics YOLO command line interface (CLI) for model training?
|
||||
|
||||
To train a model using the CLI, execute a single-line command in the terminal. For example, to train a detection model for 10 epochs with a [learning rate](https://www.ultralytics.com/glossary/learning-rate) of 0.01, run:
|
||||
|
||||
```bash
|
||||
yolo train data=coco8.yaml model=yolo26n.pt epochs=10 lr0=0.01
|
||||
```
|
||||
|
||||
This command uses the `train` mode with specific arguments. For a full list of available arguments, refer to the [Configuration Guide](cfg.md).
|
||||
|
||||
### What tasks can I perform with the Ultralytics YOLO CLI?
|
||||
|
||||
The Ultralytics YOLO CLI supports various tasks, including [detection](../tasks/detect.md), [segmentation](../tasks/segment.md), [classification](../tasks/classify.md), [pose estimation](../tasks/pose.md), and [oriented bounding box detection](../tasks/obb.md). You can also perform operations like:
|
||||
|
||||
- **Train a Model**: Run `yolo train data=<data.yaml> model=<model.pt> epochs=<num>`.
|
||||
- **Run Predictions**: Use `yolo predict model=<model.pt> source=<data_source> imgsz=<image_size>`.
|
||||
- **Export a Model**: Execute `yolo export model=<model.pt> format=<export_format>`.
|
||||
- **Use Solutions**: Run `yolo solutions <solution_name>` for ready-made applications.
|
||||
|
||||
Customize each task with various arguments. For detailed syntax and examples, see the respective sections like [Train](#train), [Predict](#predict), and [Export](#export).
|
||||
|
||||
### How can I validate the accuracy of a trained YOLO model using the CLI?
|
||||
|
||||
To validate a model's [accuracy](https://www.ultralytics.com/glossary/accuracy), use the `val` mode. For example, to validate a pretrained detection model with a [batch size](https://www.ultralytics.com/glossary/batch-size) of 1 and an image size of 640, run:
|
||||
|
||||
```bash
|
||||
yolo val model=yolo26n.pt data=coco8.yaml batch=1 imgsz=640
|
||||
```
|
||||
|
||||
This command evaluates the model on the specified dataset and provides performance metrics like [mAP](https://www.ultralytics.com/glossary/mean-average-precision-map), [precision](https://www.ultralytics.com/glossary/precision), and [recall](https://www.ultralytics.com/glossary/recall). For more details, refer to the [Val](#val) section.
|
||||
|
||||
### What formats can I export my YOLO models to using the CLI?
|
||||
|
||||
You can export YOLO models to various formats including ONNX, TensorRT, CoreML, TensorFlow, and more. For instance, to export a model to ONNX format, run:
|
||||
|
||||
```bash
|
||||
yolo export model=yolo26n.pt format=onnx
|
||||
```
|
||||
|
||||
The export command supports numerous options to optimize your model for specific deployment environments. For complete details on all available export formats and their specific parameters, visit the [Export](../modes/export.md) page.
|
||||
|
||||
### How do I use the pre-built solutions in the Ultralytics CLI?
|
||||
|
||||
Ultralytics provides ready-to-use solutions through the `solutions` command. For example, to count objects in a video:
|
||||
|
||||
```bash
|
||||
yolo solutions count source="path/to/video.mp4"
|
||||
```
|
||||
|
||||
These solutions require minimal configuration and provide immediate functionality for common computer vision tasks. To see all available solutions, run `yolo solutions help`. Each solution has specific parameters that can be customized to fit your needs.
|
||||
210
algorithms/dms_yolo/code.embedded.bak/docs/en/usage/engine.md
Normal file
210
algorithms/dms_yolo/code.embedded.bak/docs/en/usage/engine.md
Normal file
@@ -0,0 +1,210 @@
|
||||
---
|
||||
comments: true
|
||||
description: Learn to customize the Ultralytics YOLO Trainer for specific tasks. Step-by-step instructions with Python examples for maximum model performance.
|
||||
keywords: Ultralytics, YOLO, Trainer Customization, Python, Machine Learning, AI, Model Training, DetectionTrainer, Custom Models
|
||||
---
|
||||
|
||||
# Advanced Customization
|
||||
|
||||
Both the Ultralytics YOLO command-line and Python interfaces are high-level abstractions built upon base engine executors. This guide focuses on the `Trainer` engine, explaining how to customize it for your specific needs.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/GsXGnb-A4Kc?start=104"
|
||||
title="YouTube video player" frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||
allowfullscreen>
|
||||
</iframe>
|
||||
<br>
|
||||
<strong>Watch:</strong> Mastering Ultralytics YOLO: Advanced Customization
|
||||
</p>
|
||||
|
||||
## BaseTrainer
|
||||
|
||||
The `BaseTrainer` class provides a generic training routine adaptable for various tasks. Customize it by overriding specific functions or operations while adhering to the required formats. For example, integrate your own custom model and dataloader by overriding these functions:
|
||||
|
||||
- `get_model(cfg, weights)`: Builds the model to be trained.
|
||||
- `get_dataloader()`: Builds the dataloader.
|
||||
|
||||
For more details and source code, see the [`BaseTrainer` Reference](../reference/engine/trainer.md).
|
||||
|
||||
## DetectionTrainer
|
||||
|
||||
Here's how to use and customize the Ultralytics YOLO `DetectionTrainer`:
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
trainer = DetectionTrainer(overrides={...})
|
||||
trainer.train()
|
||||
trained_model = trainer.best # Get the best model
|
||||
```
|
||||
|
||||
### Customizing the DetectionTrainer
|
||||
|
||||
To train a custom detection model not directly supported, overload the existing `get_model` functionality:
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
|
||||
class CustomTrainer(DetectionTrainer):
|
||||
def get_model(self, cfg, weights):
|
||||
"""Loads a custom detection model given configuration and weight files."""
|
||||
...
|
||||
|
||||
|
||||
trainer = CustomTrainer(overrides={...})
|
||||
trainer.train()
|
||||
```
|
||||
|
||||
Further customize the trainer by modifying the [loss function](https://www.ultralytics.com/glossary/loss-function) or adding a [callback](callbacks.md) to upload the model to Google Drive every 10 [epochs](https://www.ultralytics.com/glossary/epoch). Here's an example:
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
from ultralytics.nn.tasks import DetectionModel
|
||||
|
||||
|
||||
class MyCustomModel(DetectionModel):
|
||||
def init_criterion(self):
|
||||
"""Initializes the loss function and adds a callback for uploading the model to Google Drive every 10 epochs."""
|
||||
...
|
||||
|
||||
|
||||
class CustomTrainer(DetectionTrainer):
|
||||
def get_model(self, cfg, weights):
|
||||
"""Returns a customized detection model instance configured with specified config and weights."""
|
||||
return MyCustomModel(...)
|
||||
|
||||
|
||||
# Callback to upload model weights
|
||||
def log_model(trainer):
|
||||
"""Logs the path of the last model weight used by the trainer."""
|
||||
last_weight_path = trainer.last
|
||||
print(last_weight_path)
|
||||
|
||||
|
||||
trainer = CustomTrainer(overrides={...})
|
||||
trainer.add_callback("on_train_epoch_end", log_model) # Adds to existing callbacks
|
||||
trainer.train()
|
||||
```
|
||||
|
||||
For more information on callback triggering events and entry points, see the [Callbacks Guide](../usage/callbacks.md).
|
||||
|
||||
## Other Engine Components
|
||||
|
||||
Customize other components like `Validators` and `Predictors` similarly. For more information, refer to the documentation for [Validators](../reference/engine/validator.md) and [Predictors](../reference/engine/predictor.md).
|
||||
|
||||
## Using YOLO with Custom Trainers
|
||||
|
||||
The `YOLO` model class provides a high-level wrapper for the Trainer classes. You can leverage this architecture for greater flexibility in your machine learning workflows:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
|
||||
# Create a custom trainer
|
||||
class MyCustomTrainer(DetectionTrainer):
|
||||
def get_model(self, cfg, weights):
|
||||
"""Custom code implementation."""
|
||||
...
|
||||
|
||||
|
||||
# Initialize YOLO model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Train with custom trainer
|
||||
results = model.train(trainer=MyCustomTrainer, data="coco8.yaml", epochs=3)
|
||||
```
|
||||
|
||||
This approach allows you to maintain the simplicity of the YOLO interface while customizing the underlying training process to suit your specific requirements.
|
||||
|
||||
## FAQ
|
||||
|
||||
### How do I customize the Ultralytics YOLO DetectionTrainer for specific tasks?
|
||||
|
||||
Customize the `DetectionTrainer` for specific tasks by overriding its methods to adapt to your custom model and dataloader. Start by inheriting from `DetectionTrainer` and redefine methods like `get_model` to implement custom functionalities. Here's an example:
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
|
||||
class CustomTrainer(DetectionTrainer):
|
||||
def get_model(self, cfg, weights):
|
||||
"""Loads a custom detection model given configuration and weight files."""
|
||||
...
|
||||
|
||||
|
||||
trainer = CustomTrainer(overrides={...})
|
||||
trainer.train()
|
||||
trained_model = trainer.best # Get the best model
|
||||
```
|
||||
|
||||
For further customization, such as changing the [loss function](https://www.ultralytics.com/glossary/loss-function) or adding a [callback](https://www.ultralytics.com/glossary/callback), refer to the [Callbacks Guide](../usage/callbacks.md).
|
||||
|
||||
### What are the key components of the BaseTrainer in Ultralytics YOLO?
|
||||
|
||||
The `BaseTrainer` serves as the foundation for training routines, customizable for various tasks by overriding its generic methods. Key components include:
|
||||
|
||||
- `get_model(cfg, weights)`: Builds the model to be trained.
|
||||
- `get_dataloader()`: Builds the dataloader.
|
||||
- `preprocess_batch()`: Handles batch preprocessing before model forward pass.
|
||||
- `set_model_attributes()`: Sets model attributes based on dataset information.
|
||||
- `get_validator()`: Returns a validator for model evaluation.
|
||||
|
||||
For more details on customization and source code, see the [`BaseTrainer` Reference](../reference/engine/trainer.md).
|
||||
|
||||
### How can I add a callback to the Ultralytics YOLO DetectionTrainer?
|
||||
|
||||
Add callbacks to monitor and modify the training process in `DetectionTrainer`. Here's how to add a callback to log model weights after every training [epoch](https://www.ultralytics.com/glossary/epoch):
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
|
||||
# Callback to upload model weights
|
||||
def log_model(trainer):
|
||||
"""Logs the path of the last model weight used by the trainer."""
|
||||
last_weight_path = trainer.last
|
||||
print(last_weight_path)
|
||||
|
||||
|
||||
trainer = DetectionTrainer(overrides={...})
|
||||
trainer.add_callback("on_train_epoch_end", log_model) # Adds to existing callbacks
|
||||
trainer.train()
|
||||
```
|
||||
|
||||
For more details on callback events and entry points, refer to the [Callbacks Guide](../usage/callbacks.md).
|
||||
|
||||
### Why should I use Ultralytics YOLO for model training?
|
||||
|
||||
Ultralytics YOLO provides a high-level abstraction over powerful engine executors, making it ideal for rapid development and customization. Key benefits include:
|
||||
|
||||
- **Ease of Use**: Both command-line and Python interfaces simplify complex tasks.
|
||||
- **Performance**: Optimized for real-time [object detection](https://www.ultralytics.com/glossary/object-detection) and various vision AI applications.
|
||||
- **Customization**: Easily extendable for custom models, [loss functions](https://www.ultralytics.com/glossary/loss-function), and dataloaders.
|
||||
- **Modularity**: Components can be modified independently without affecting the entire pipeline.
|
||||
- **Integration**: Seamlessly works with popular frameworks and tools in the ML ecosystem.
|
||||
|
||||
Learn more about YOLO's capabilities by exploring the main [Ultralytics YOLO](https://www.ultralytics.com/yolo) page.
|
||||
|
||||
### Can I use the Ultralytics YOLO DetectionTrainer for non-standard models?
|
||||
|
||||
Yes, the `DetectionTrainer` is highly flexible and customizable for non-standard models. Inherit from `DetectionTrainer` and overload methods to support your specific model's needs. Here's a simple example:
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionTrainer
|
||||
|
||||
|
||||
class CustomDetectionTrainer(DetectionTrainer):
|
||||
def get_model(self, cfg, weights):
|
||||
"""Loads a custom detection model."""
|
||||
...
|
||||
|
||||
|
||||
trainer = CustomDetectionTrainer(overrides={...})
|
||||
trainer.train()
|
||||
```
|
||||
|
||||
For comprehensive instructions and examples, review the [`DetectionTrainer` Reference](../reference/models/yolo/detect/train.md).
|
||||
374
algorithms/dms_yolo/code.embedded.bak/docs/en/usage/python.md
Normal file
374
algorithms/dms_yolo/code.embedded.bak/docs/en/usage/python.md
Normal file
@@ -0,0 +1,374 @@
|
||||
---
|
||||
comments: true
|
||||
description: Learn to integrate Ultralytics YOLO in Python for object detection, segmentation, and classification. Load and train models, and make predictions easily with our comprehensive guide.
|
||||
keywords: YOLO, Python, object detection, segmentation, classification, machine learning, AI, pretrained models, train models, make predictions
|
||||
---
|
||||
|
||||
# Python Usage
|
||||
|
||||
Welcome to the Ultralytics YOLO Python Usage documentation! This guide is designed to help you seamlessly integrate Ultralytics YOLO into your Python projects for [object detection](https://www.ultralytics.com/glossary/object-detection), [segmentation](https://docs.ultralytics.com/tasks/segment/), and [classification](https://docs.ultralytics.com/tasks/classify/). Here, you'll learn how to load and use pretrained models, train new models, and perform predictions on images. The easy-to-use Python interface is a valuable resource for anyone looking to incorporate YOLO into their Python projects, allowing you to quickly implement advanced object detection capabilities. Let's get started!
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/GsXGnb-A4Kc?start=58"
|
||||
title="YouTube video player" frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||
allowfullscreen>
|
||||
</iframe>
|
||||
<br>
|
||||
<strong>Watch:</strong> Mastering Ultralytics YOLO: Python
|
||||
</p>
|
||||
|
||||
For example, users can load a model, train it, evaluate its performance on a validation set, and even [export it to ONNX format](../modes/export.md) with just a few lines of code.
|
||||
|
||||
!!! example "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Create a new YOLO model from scratch
|
||||
model = YOLO("yolo26n.yaml")
|
||||
|
||||
# Load a pretrained YOLO model (recommended for training)
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Train the model using the 'coco8.yaml' dataset for 3 epochs
|
||||
results = model.train(data="coco8.yaml", epochs=3)
|
||||
|
||||
# Evaluate the model's performance on the validation set
|
||||
results = model.val()
|
||||
|
||||
# Perform object detection on an image using the model
|
||||
results = model("https://ultralytics.com/images/bus.jpg")
|
||||
|
||||
# Export the model to ONNX format
|
||||
success = model.export(format="onnx")
|
||||
```
|
||||
|
||||
## Train
|
||||
|
||||
[Train mode](../modes/train.md) is used for training a YOLO model on a custom dataset. In this mode, the model is trained using the specified dataset and hyperparameters. The training process involves optimizing the model's parameters so that it can accurately predict the classes and locations of objects in an image.
|
||||
|
||||
!!! example "Train"
|
||||
|
||||
=== "From pretrained (recommended)"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("yolo26n.pt") # pass any model type
|
||||
results = model.train(epochs=5)
|
||||
```
|
||||
|
||||
=== "From scratch"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("yolo26n.yaml")
|
||||
results = model.train(data="coco8.yaml", epochs=5)
|
||||
```
|
||||
|
||||
=== "Resume"
|
||||
|
||||
```python
|
||||
model = YOLO("last.pt")
|
||||
results = model.train(resume=True)
|
||||
```
|
||||
|
||||
[Train Examples](../modes/train.md){ .md-button }
|
||||
|
||||
## Val
|
||||
|
||||
[Val mode](../modes/val.md) is used for validating a YOLO model after it has been trained. In this mode, the model is evaluated on a validation set to measure its [accuracy](https://www.ultralytics.com/glossary/accuracy) and generalization performance. This mode can be used to tune the hyperparameters of the model to improve its performance.
|
||||
|
||||
!!! example "Val"
|
||||
|
||||
=== "Val after training"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a YOLO model
|
||||
model = YOLO("yolo26n.yaml")
|
||||
|
||||
# Train the model
|
||||
model.train(data="coco8.yaml", epochs=5)
|
||||
|
||||
# Validate on training data
|
||||
model.val()
|
||||
```
|
||||
|
||||
=== "Val on another dataset"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a YOLO model
|
||||
model = YOLO("yolo26n.yaml")
|
||||
|
||||
# Train the model
|
||||
model.train(data="coco8.yaml", epochs=5)
|
||||
|
||||
# Validate on separate data
|
||||
model.val(data="path/to/separate/data.yaml")
|
||||
```
|
||||
|
||||
[Val Examples](../modes/val.md){ .md-button }
|
||||
|
||||
## Predict
|
||||
|
||||
[Predict mode](../modes/predict.md) is used for making predictions using a trained YOLO model on new images or videos. In this mode, the model is loaded from a checkpoint file, and the user can provide images or videos to perform inference. The model predicts the classes and locations of objects in the input images or videos.
|
||||
|
||||
!!! example "Predict"
|
||||
|
||||
=== "From source"
|
||||
|
||||
```python
|
||||
import cv2
|
||||
from PIL import Image
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("model.pt")
|
||||
# accepts all formats - image/dir/Path/URL/video/PIL/ndarray. 0 for webcam
|
||||
results = model.predict(source="0")
|
||||
results = model.predict(source="folder", show=True) # Display preds. Accepts all YOLO predict arguments
|
||||
|
||||
# from PIL
|
||||
im1 = Image.open("bus.jpg")
|
||||
results = model.predict(source=im1, save=True) # save plotted images
|
||||
|
||||
# from ndarray
|
||||
im2 = cv2.imread("bus.jpg")
|
||||
results = model.predict(source=im2, save=True, save_txt=True) # save predictions as labels
|
||||
|
||||
# from list of PIL/ndarray
|
||||
results = model.predict(source=[im1, im2])
|
||||
```
|
||||
|
||||
=== "Results usage"
|
||||
|
||||
```python
|
||||
# results would be a list of Results object including all the predictions by default
|
||||
# but be careful as it could occupy a lot memory when there're many images,
|
||||
# especially the task is segmentation.
|
||||
# 1. return as a list
|
||||
results = model.predict(source="folder")
|
||||
|
||||
# results would be a generator which is more friendly to memory by setting stream=True
|
||||
# 2. return as a generator
|
||||
results = model.predict(source=0, stream=True)
|
||||
|
||||
for result in results:
|
||||
# Detection
|
||||
result.boxes.xyxy # box with xyxy format, (N, 4)
|
||||
result.boxes.xywh # box with xywh format, (N, 4)
|
||||
result.boxes.xyxyn # box with xyxy format but normalized, (N, 4)
|
||||
result.boxes.xywhn # box with xywh format but normalized, (N, 4)
|
||||
result.boxes.conf # confidence score, (N, 1)
|
||||
result.boxes.cls # cls, (N, 1)
|
||||
|
||||
# Segmentation
|
||||
result.masks.data # masks, (N, H, W)
|
||||
result.masks.xy # x,y segments (pixels), List[segment] * N
|
||||
result.masks.xyn # x,y segments (normalized), List[segment] * N
|
||||
|
||||
# Classification
|
||||
result.probs # cls prob, (num_class, )
|
||||
|
||||
# Each result is composed of torch.Tensor by default,
|
||||
# in which you can easily use following functionality:
|
||||
result = result.cuda()
|
||||
result = result.cpu()
|
||||
result = result.to("cpu")
|
||||
result = result.numpy()
|
||||
```
|
||||
|
||||
[Predict Examples](../modes/predict.md){ .md-button }
|
||||
|
||||
## Export
|
||||
|
||||
[Export mode](../modes/export.md) is used for exporting a YOLO model to a format that can be used for deployment. In this mode, the model is converted to a format that can be used by other software applications or hardware devices. This mode is useful when deploying the model to production environments.
|
||||
|
||||
!!! example "Export"
|
||||
|
||||
=== "Export to ONNX"
|
||||
|
||||
Export an official YOLO model to [ONNX](https://www.ultralytics.com/glossary/onnx-open-neural-network-exchange) with dynamic batch-size and image-size.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
model.export(format="onnx", dynamic=True)
|
||||
```
|
||||
|
||||
=== "Export to TensorRT"
|
||||
|
||||
Export an official YOLO model to [TensorRT](https://www.ultralytics.com/glossary/tensorrt) on `device=0` for acceleration on CUDA devices.
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
model = YOLO("yolo26n.pt")
|
||||
model.export(format="engine", device=0)
|
||||
```
|
||||
|
||||
[Export Examples](../modes/export.md){ .md-button }
|
||||
|
||||
## Track
|
||||
|
||||
[Track mode](../modes/track.md) is used for tracking objects in real-time using a YOLO model. In this mode, the model is loaded from a checkpoint file, and the user can provide a live video stream to perform real-time object tracking. This mode is useful for applications such as surveillance systems or [self-driving cars](https://www.ultralytics.com/solutions/ai-in-automotive).
|
||||
|
||||
!!! example "Track"
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a model
|
||||
model = YOLO("yolo26n.pt") # load an official detection model
|
||||
model = YOLO("yolo26n-seg.pt") # load an official segmentation model
|
||||
model = YOLO("path/to/best.pt") # load a custom model
|
||||
|
||||
# Track with the model
|
||||
results = model.track(source="https://youtu.be/LNwODJXcvt4", show=True)
|
||||
results = model.track(source="https://youtu.be/LNwODJXcvt4", show=True, tracker="bytetrack.yaml")
|
||||
```
|
||||
|
||||
[Track Examples](../modes/track.md){ .md-button }
|
||||
|
||||
## Benchmark
|
||||
|
||||
[Benchmark mode](../modes/benchmark.md) is used to profile the speed and accuracy of various export formats for YOLO. The benchmarks provide information on the size of the exported format, its `mAP50-95` metrics (for object detection and segmentation) or `accuracy_top5` metrics (for classification), and the inference time in milliseconds per image across various export formats like ONNX, [OpenVINO](https://docs.ultralytics.com/integrations/openvino/), TensorRT and others. This information can help users choose the optimal export format for their specific use case based on their requirements for speed and accuracy.
|
||||
|
||||
!!! example "Benchmark"
|
||||
|
||||
=== "Python"
|
||||
|
||||
Benchmark an official YOLO model across all export formats.
|
||||
```python
|
||||
from ultralytics.utils.benchmarks import benchmark
|
||||
|
||||
# Benchmark
|
||||
benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, half=False, device=0)
|
||||
```
|
||||
|
||||
[Benchmark Examples](../modes/benchmark.md){ .md-button }
|
||||
|
||||
## Using Trainers
|
||||
|
||||
The `YOLO` model class serves as a high-level wrapper for the Trainer classes. Each YOLO task has its own trainer, which inherits from `BaseTrainer`. This architecture allows for greater flexibility and customization in your [machine learning workflows](https://docs.ultralytics.com/guides/model-training-tips/).
|
||||
|
||||
!!! tip "Detection Trainer Example"
|
||||
|
||||
```python
|
||||
from ultralytics.models.yolo.detect import DetectionPredictor, DetectionTrainer, DetectionValidator
|
||||
|
||||
# trainer
|
||||
trainer = DetectionTrainer(overrides={})
|
||||
trainer.train()
|
||||
trained_model = trainer.best
|
||||
|
||||
# Validator
|
||||
val = DetectionValidator(args=...)
|
||||
val(model=trained_model)
|
||||
|
||||
# predictor
|
||||
pred = DetectionPredictor(overrides={})
|
||||
pred(source=SOURCE, model=trained_model)
|
||||
|
||||
# resume from last weight
|
||||
overrides["resume"] = trainer.last
|
||||
trainer = DetectionTrainer(overrides=overrides)
|
||||
```
|
||||
|
||||
You can easily customize Trainers to support custom tasks or explore research and development ideas. The modular design of Ultralytics YOLO allows you to adapt the framework to your specific needs, whether you're working on a novel [computer vision](https://www.ultralytics.com/glossary/computer-vision-cv) task or fine-tuning existing models for better performance.
|
||||
|
||||
[Customization tutorials](engine.md){ .md-button }
|
||||
|
||||
## FAQ
|
||||
|
||||
### How can I integrate YOLO into my Python project for object detection?
|
||||
|
||||
Integrating Ultralytics YOLO into your Python projects is simple. You can load a pretrained model or train a new model from scratch. Here's how to get started:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a pretrained YOLO model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Perform object detection on an image
|
||||
results = model("https://ultralytics.com/images/bus.jpg")
|
||||
|
||||
# Visualize the results
|
||||
for result in results:
|
||||
result.show()
|
||||
```
|
||||
|
||||
See more detailed examples in our [Predict Mode](../modes/predict.md) section.
|
||||
|
||||
### What are the different modes available in YOLO?
|
||||
|
||||
Ultralytics YOLO provides various modes to cater to different [machine learning](https://www.ultralytics.com/glossary/machine-learning-ml) workflows. These include:
|
||||
|
||||
- **[Train](../modes/train.md)**: Train a model using custom datasets.
|
||||
- **[Val](../modes/val.md)**: Validate model performance on a validation set.
|
||||
- **[Predict](../modes/predict.md)**: Make predictions on new images or video streams.
|
||||
- **[Export](../modes/export.md)**: Export models to various formats like ONNX and TensorRT.
|
||||
- **[Track](../modes/track.md)**: Real-time object tracking in video streams.
|
||||
- **[Benchmark](../modes/benchmark.md)**: Benchmark model performance across different configurations.
|
||||
|
||||
Each mode is designed to provide comprehensive functionalities for different stages of [model development and deployment](https://docs.ultralytics.com/guides/model-deployment-options/).
|
||||
|
||||
### How do I train a custom YOLO model using my dataset?
|
||||
|
||||
To train a custom YOLO model, you need to specify your dataset and other [hyperparameters](https://www.ultralytics.com/glossary/hyperparameter-tuning). Here's a quick example:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load the YOLO model
|
||||
model = YOLO("yolo26n.yaml")
|
||||
|
||||
# Train the model with custom dataset
|
||||
model.train(data="path/to/your/dataset.yaml", epochs=10)
|
||||
```
|
||||
|
||||
For more details on training and hyperlinks to example usage, visit our [Train Mode](../modes/train.md) page.
|
||||
|
||||
### How do I export YOLO models for deployment?
|
||||
|
||||
Exporting YOLO models in a format suitable for deployment is straightforward with the `export` function. For example, you can export a model to ONNX format:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load the YOLO model
|
||||
model = YOLO("yolo26n.pt")
|
||||
|
||||
# Export the model to ONNX format
|
||||
model.export(format="onnx")
|
||||
```
|
||||
|
||||
For various export options, refer to the [Export Mode](../modes/export.md) documentation.
|
||||
|
||||
### Can I validate my YOLO model on different datasets?
|
||||
|
||||
Yes, validating YOLO models on different datasets is possible. After training, you can use the validation mode to evaluate the performance:
|
||||
|
||||
```python
|
||||
from ultralytics import YOLO
|
||||
|
||||
# Load a YOLO model
|
||||
model = YOLO("yolo26n.yaml")
|
||||
|
||||
# Train the model
|
||||
model.train(data="coco8.yaml", epochs=5)
|
||||
|
||||
# Validate the model on a different dataset
|
||||
model.val(data="path/to/separate/data.yaml")
|
||||
```
|
||||
|
||||
Check the [Val Mode](../modes/val.md) page for detailed examples and usage.
|
||||
@@ -0,0 +1,771 @@
|
||||
---
|
||||
comments: true
|
||||
description: Explore essential utilities in the Ultralytics package to speed up and enhance your workflows. Learn about data processing, annotations, conversions, and more.
|
||||
keywords: Ultralytics, utilities, data processing, auto annotation, YOLO, dataset conversion, bounding boxes, image compression, machine learning tools
|
||||
---
|
||||
|
||||
# Simple Utilities
|
||||
|
||||
<p align="center">
|
||||
<img src="https://cdn.jsdelivr.net/gh/ultralytics/assets@main/docs/code-with-perspective.avif" alt="YOLO model code with 3D perspective visualization">
|
||||
</p>
|
||||
|
||||
The `ultralytics` package provides a variety of utilities to support, enhance, and accelerate your workflows. While there are many more available, this guide highlights some of the most useful ones for developers, serving as a practical reference for programming with Ultralytics tools.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/1bPY2LRG590"
|
||||
title="YouTube video player" frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||
allowfullscreen>
|
||||
</iframe>
|
||||
<br>
|
||||
<strong>Watch:</strong> Ultralytics Utilities | Auto Annotation, Explorer API and Dataset Conversion
|
||||
</p>
|
||||
|
||||
## Data
|
||||
|
||||
### Auto Labeling / Annotations
|
||||
|
||||
Dataset annotation is a resource-intensive and time-consuming process. If you have an Ultralytics YOLO [object detection](https://www.ultralytics.com/glossary/object-detection) model trained on a reasonable amount of data, you can use it with [SAM](../models/sam.md) to auto-annotate additional data in segmentation format.
|
||||
|
||||
```python
|
||||
from ultralytics.data.annotator import auto_annotate
|
||||
|
||||
auto_annotate(
|
||||
data="path/to/new/data",
|
||||
det_model="yolo26n.pt",
|
||||
sam_model="mobile_sam.pt",
|
||||
device="cuda",
|
||||
output_dir="path/to/save_labels",
|
||||
)
|
||||
```
|
||||
|
||||
This function does not return any value. For further details:
|
||||
|
||||
- See the [reference section for `annotator.auto_annotate`](../reference/data/annotator.md#ultralytics.data.annotator.auto_annotate) for more insight on how the function operates.
|
||||
- Use in combination with the [function `segments2boxes`](#convert-segments-to-bounding-boxes) to generate object detection bounding boxes as well.
|
||||
|
||||
### Visualize Dataset Annotations
|
||||
|
||||
This function visualizes YOLO annotations on an image before training, helping to identify and correct any wrong annotations that could lead to incorrect detection results. It draws bounding boxes, labels objects with class names, and adjusts text color based on the background's luminance for better readability.
|
||||
|
||||
```python
|
||||
from ultralytics.data.utils import visualize_image_annotations
|
||||
|
||||
label_map = { # Define the label map with all annotated class labels.
|
||||
0: "person",
|
||||
1: "car",
|
||||
}
|
||||
|
||||
# Visualize
|
||||
visualize_image_annotations(
|
||||
"path/to/image.jpg", # Input image path.
|
||||
"path/to/annotations.txt", # Annotation file path for the image.
|
||||
label_map,
|
||||
)
|
||||
```
|
||||
|
||||
### Convert Segmentation Masks into YOLO Format
|
||||
|
||||

|
||||
|
||||
Use this to convert a dataset of segmentation mask images to the [Ultralytics YOLO](../models/yolo26.md) segmentation format. This function takes the directory containing the binary format mask images and converts them into YOLO segmentation format.
|
||||
|
||||
The converted masks will be saved in the specified output directory.
|
||||
|
||||
```python
|
||||
from ultralytics.data.converter import convert_segment_masks_to_yolo_seg
|
||||
|
||||
# The classes here is the total classes in the dataset.
|
||||
# for COCO dataset we have 80 classes.
|
||||
convert_segment_masks_to_yolo_seg(masks_dir="path/to/masks_dir", output_dir="path/to/output_dir", classes=80)
|
||||
```
|
||||
|
||||
### Convert COCO into YOLO Format
|
||||
|
||||
Use this to convert [COCO](https://docs.ultralytics.com/datasets/detect/coco/) JSON annotations into the YOLO format. For object detection (bounding box) datasets, set both `use_segments` and `use_keypoints` to `False`.
|
||||
|
||||
```python
|
||||
from ultralytics.data.converter import convert_coco
|
||||
|
||||
convert_coco(
|
||||
"coco/annotations/",
|
||||
use_segments=False,
|
||||
use_keypoints=False,
|
||||
cls91to80=True,
|
||||
)
|
||||
```
|
||||
|
||||
For additional information about the `convert_coco` function, [visit the reference page](../reference/data/converter.md#ultralytics.data.converter.convert_coco).
|
||||
|
||||
### Get Bounding Box Dimensions
|
||||
|
||||
```python
|
||||
import cv2
|
||||
|
||||
from ultralytics import YOLO
|
||||
from ultralytics.utils.plotting import Annotator
|
||||
|
||||
model = YOLO("yolo26n.pt") # Load pretrain or fine-tune model
|
||||
|
||||
# Process the image
|
||||
source = cv2.imread("path/to/image.jpg")
|
||||
results = model(source)
|
||||
|
||||
# Extract results
|
||||
annotator = Annotator(source, example=model.names)
|
||||
|
||||
for box in results[0].boxes.xyxy.cpu():
|
||||
width, height, area = annotator.get_bbox_dimension(box)
|
||||
print(f"Bounding Box Width {width.item()}, Height {height.item()}, Area {area.item()}")
|
||||
```
|
||||
|
||||
### Convert Bounding Boxes to Segments
|
||||
|
||||
With existing `x y w h` bounding box data, convert to segments using the `yolo_bbox2segment` function. Organize the files for images and annotations as follows:
|
||||
|
||||
```
|
||||
data
|
||||
|__ images
|
||||
├─ 001.jpg
|
||||
├─ 002.jpg
|
||||
├─ ..
|
||||
└─ NNN.jpg
|
||||
|__ labels
|
||||
├─ 001.txt
|
||||
├─ 002.txt
|
||||
├─ ..
|
||||
└─ NNN.txt
|
||||
```
|
||||
|
||||
```python
|
||||
from ultralytics.data.converter import yolo_bbox2segment
|
||||
|
||||
yolo_bbox2segment(
|
||||
im_dir="path/to/images",
|
||||
save_dir=None, # saved to "labels-segment" in images directory
|
||||
sam_model="sam_b.pt",
|
||||
)
|
||||
```
|
||||
|
||||
[Visit the `yolo_bbox2segment` reference page](../reference/data/converter.md#ultralytics.data.converter.yolo_bbox2segment) for more information regarding the function.
|
||||
|
||||
### Convert Segments to Bounding Boxes
|
||||
|
||||
If you have a dataset that uses the [segmentation dataset format](../datasets/segment/index.md), you can easily convert these into upright (or horizontal) bounding boxes (`x y w h` format) with this function.
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
|
||||
from ultralytics.utils.ops import segments2boxes
|
||||
|
||||
segments = np.array(
|
||||
[
|
||||
[805, 392, 797, 400, ..., 808, 714, 808, 392],
|
||||
[115, 398, 113, 400, ..., 150, 400, 149, 298],
|
||||
[267, 412, 265, 413, ..., 300, 413, 299, 412],
|
||||
]
|
||||
)
|
||||
|
||||
segments2boxes([s.reshape(-1, 2) for s in segments])
|
||||
# >>> array([[ 741.66, 631.12, 133.31, 479.25],
|
||||
# [ 146.81, 649.69, 185.62, 502.88],
|
||||
# [ 281.81, 636.19, 118.12, 448.88]],
|
||||
# dtype=float32) # xywh bounding boxes
|
||||
```
|
||||
|
||||
To understand how this function works, visit the [reference page](../reference/utils/ops.md#ultralytics.utils.ops.segments2boxes).
|
||||
|
||||
## Utilities
|
||||
|
||||
### Image Compression
|
||||
|
||||
Compress a single image file to a reduced size while preserving its aspect ratio and quality. If the input image is smaller than the maximum dimension, it will not be resized.
|
||||
|
||||
```python
|
||||
from pathlib import Path
|
||||
|
||||
from ultralytics.data.utils import compress_one_image
|
||||
|
||||
for f in Path("path/to/dataset").rglob("*.jpg"):
|
||||
compress_one_image(f)
|
||||
```
|
||||
|
||||
### Auto-split Dataset
|
||||
|
||||
Automatically split a dataset into `train`/`val`/`test` splits and save the resulting splits into `autosplit_*.txt` files. This function uses random sampling, which is excluded when using the [`fraction` argument for training](../modes/train.md#train-settings).
|
||||
|
||||
```python
|
||||
from ultralytics.data.split import autosplit
|
||||
|
||||
autosplit(
|
||||
path="path/to/images",
|
||||
weights=(0.9, 0.1, 0.0), # (train, validation, test) fractional splits
|
||||
annotated_only=False, # split only images with annotation file when True
|
||||
)
|
||||
```
|
||||
|
||||
See the [Reference page](../reference/data/split.md#ultralytics.data.split.autosplit) for additional details on this function.
|
||||
|
||||
### Segment-polygon to Binary Mask
|
||||
|
||||
Convert a single polygon (as a list) to a binary mask of the specified image size. The polygon should be in the form of `[N, 2]`, where `N` is the number of `(x, y)` points defining the polygon contour.
|
||||
|
||||
!!! warning
|
||||
|
||||
`N` **must always** be even.
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
|
||||
from ultralytics.data.utils import polygon2mask
|
||||
|
||||
imgsz = (1080, 810)
|
||||
polygon = np.array([805, 392, 797, 400, ..., 808, 714, 808, 392]) # (238, 2)
|
||||
|
||||
mask = polygon2mask(
|
||||
imgsz, # tuple
|
||||
[polygon], # input as list
|
||||
color=255, # 8-bit binary
|
||||
downsample_ratio=1,
|
||||
)
|
||||
```
|
||||
|
||||
## Bounding Boxes
|
||||
|
||||
### Bounding Box (Horizontal) Instances
|
||||
|
||||
To manage bounding box data, the `Bboxes` class helps convert between box coordinate formats, scale box dimensions, calculate areas, include offsets, and more.
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
|
||||
from ultralytics.utils.instance import Bboxes
|
||||
|
||||
boxes = Bboxes(
|
||||
bboxes=np.array(
|
||||
[
|
||||
[22.878, 231.27, 804.98, 756.83],
|
||||
[48.552, 398.56, 245.35, 902.71],
|
||||
[669.47, 392.19, 809.72, 877.04],
|
||||
[221.52, 405.8, 344.98, 857.54],
|
||||
[0, 550.53, 63.01, 873.44],
|
||||
[0.0584, 254.46, 32.561, 324.87],
|
||||
]
|
||||
),
|
||||
format="xyxy",
|
||||
)
|
||||
|
||||
boxes.areas()
|
||||
# >>> array([ 4.1104e+05, 99216, 68000, 55772, 20347, 2288.5])
|
||||
|
||||
boxes.convert("xywh")
|
||||
print(boxes.bboxes)
|
||||
# >>> array(
|
||||
# [[ 413.93, 494.05, 782.1, 525.56],
|
||||
# [ 146.95, 650.63, 196.8, 504.15],
|
||||
# [ 739.6, 634.62, 140.25, 484.85],
|
||||
# [ 283.25, 631.67, 123.46, 451.74],
|
||||
# [ 31.505, 711.99, 63.01, 322.91],
|
||||
# [ 16.31, 289.67, 32.503, 70.41]]
|
||||
# )
|
||||
```
|
||||
|
||||
See the [`Bboxes` reference section](../reference/utils/instance.md#ultralytics.utils.instance.Bboxes) for more attributes and methods.
|
||||
|
||||
!!! tip
|
||||
|
||||
Many of the following functions (and more) can be accessed using the [`Bboxes` class](#bounding-box-horizontal-instances), but if you prefer to work with the functions directly, see the next subsections for how to import them independently.
|
||||
|
||||
### Scaling Boxes
|
||||
|
||||
When scaling an image up or down, you can appropriately scale corresponding bounding box coordinates to match using `ultralytics.utils.ops.scale_boxes`.
|
||||
|
||||
```python
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
from ultralytics.utils.ops import scale_boxes
|
||||
|
||||
image = cv.imread("ultralytics/assets/bus.jpg")
|
||||
h, w, c = image.shape
|
||||
resized = cv.resize(image, None, (), fx=1.2, fy=1.2)
|
||||
new_h, new_w, _ = resized.shape
|
||||
|
||||
xyxy_boxes = np.array(
|
||||
[
|
||||
[22.878, 231.27, 804.98, 756.83],
|
||||
[48.552, 398.56, 245.35, 902.71],
|
||||
[669.47, 392.19, 809.72, 877.04],
|
||||
[221.52, 405.8, 344.98, 857.54],
|
||||
[0, 550.53, 63.01, 873.44],
|
||||
[0.0584, 254.46, 32.561, 324.87],
|
||||
]
|
||||
)
|
||||
|
||||
new_boxes = scale_boxes(
|
||||
img1_shape=(h, w), # original image dimensions
|
||||
boxes=xyxy_boxes, # boxes from original image
|
||||
img0_shape=(new_h, new_w), # resized image dimensions (scale to)
|
||||
ratio_pad=None,
|
||||
padding=False,
|
||||
xywh=False,
|
||||
)
|
||||
|
||||
print(new_boxes)
|
||||
# >>> array(
|
||||
# [[ 27.454, 277.52, 965.98, 908.2],
|
||||
# [ 58.262, 478.27, 294.42, 1083.3],
|
||||
# [ 803.36, 470.63, 971.66, 1052.4],
|
||||
# [ 265.82, 486.96, 413.98, 1029],
|
||||
# [ 0, 660.64, 75.612, 1048.1],
|
||||
# [ 0.0701, 305.35, 39.073, 389.84]]
|
||||
# )
|
||||
```
|
||||
|
||||
### Bounding Box Format Conversions
|
||||
|
||||
#### XYXY → XYWH
|
||||
|
||||
Convert bounding box coordinates from (x1, y1, x2, y2) format to (x, y, width, height) format, where (x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner.
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
|
||||
from ultralytics.utils.ops import xyxy2xywh
|
||||
|
||||
xyxy_boxes = np.array(
|
||||
[
|
||||
[22.878, 231.27, 804.98, 756.83],
|
||||
[48.552, 398.56, 245.35, 902.71],
|
||||
[669.47, 392.19, 809.72, 877.04],
|
||||
[221.52, 405.8, 344.98, 857.54],
|
||||
[0, 550.53, 63.01, 873.44],
|
||||
[0.0584, 254.46, 32.561, 324.87],
|
||||
]
|
||||
)
|
||||
xywh = xyxy2xywh(xyxy_boxes)
|
||||
|
||||
print(xywh)
|
||||
# >>> array(
|
||||
# [[ 413.93, 494.05, 782.1, 525.56],
|
||||
# [ 146.95, 650.63, 196.8, 504.15],
|
||||
# [ 739.6, 634.62, 140.25, 484.85],
|
||||
# [ 283.25, 631.67, 123.46, 451.74],
|
||||
# [ 31.505, 711.99, 63.01, 322.91],
|
||||
# [ 16.31, 289.67, 32.503, 70.41]]
|
||||
# )
|
||||
```
|
||||
|
||||
### All Bounding Box Conversions
|
||||
|
||||
```python
|
||||
from ultralytics.utils.ops import (
|
||||
ltwh2xywh,
|
||||
ltwh2xyxy,
|
||||
xywh2ltwh, # xywh → top-left corner, w, h
|
||||
xywh2xyxy,
|
||||
xywhn2xyxy, # normalized → pixel
|
||||
xyxy2ltwh, # xyxy → top-left corner, w, h
|
||||
xyxy2xywhn, # pixel → normalized
|
||||
)
|
||||
|
||||
for func in (ltwh2xywh, ltwh2xyxy, xywh2ltwh, xywh2xyxy, xywhn2xyxy, xyxy2ltwh, xyxy2xywhn):
|
||||
print(help(func)) # print function docstrings
|
||||
```
|
||||
|
||||
See the docstring for each function or visit the `ultralytics.utils.ops` [reference page](../reference/utils/ops.md) to read more.
|
||||
|
||||
## Plotting
|
||||
|
||||
### Annotation utilities
|
||||
|
||||
Ultralytics includes an `Annotator` class for annotating various data types. It's best used with [object detection bounding boxes](../modes/predict.md#boxes), [pose keypoints](../modes/predict.md#keypoints), and [oriented bounding boxes](../modes/predict.md#obb).
|
||||
|
||||
#### Box Annotation
|
||||
|
||||
!!! example "Python Examples using Ultralytics YOLO 🚀"
|
||||
|
||||
=== "Horizontal Bounding Boxes"
|
||||
|
||||
```python
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
from ultralytics.utils.plotting import Annotator, colors
|
||||
|
||||
names = {
|
||||
0: "person",
|
||||
5: "bus",
|
||||
11: "stop sign",
|
||||
}
|
||||
|
||||
image = cv.imread("ultralytics/assets/bus.jpg")
|
||||
ann = Annotator(
|
||||
image,
|
||||
line_width=None, # default auto-size
|
||||
font_size=None, # default auto-size
|
||||
font="Arial.ttf", # must be ImageFont compatible
|
||||
pil=False, # use PIL, otherwise uses OpenCV
|
||||
)
|
||||
|
||||
xyxy_boxes = np.array(
|
||||
[
|
||||
[5, 22.878, 231.27, 804.98, 756.83], # class-idx x1 y1 x2 y2
|
||||
[0, 48.552, 398.56, 245.35, 902.71],
|
||||
[0, 669.47, 392.19, 809.72, 877.04],
|
||||
[0, 221.52, 405.8, 344.98, 857.54],
|
||||
[0, 0, 550.53, 63.01, 873.44],
|
||||
[11, 0.0584, 254.46, 32.561, 324.87],
|
||||
]
|
||||
)
|
||||
|
||||
for nb, box in enumerate(xyxy_boxes):
|
||||
c_idx, *box = box
|
||||
label = f"{str(nb).zfill(2)}:{names.get(int(c_idx))}"
|
||||
ann.box_label(box, label, color=colors(c_idx, bgr=True))
|
||||
|
||||
image_with_bboxes = ann.result()
|
||||
```
|
||||
|
||||
=== "Oriented Bounding Boxes (OBB)"
|
||||
|
||||
```python
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
from ultralytics.utils.plotting import Annotator, colors
|
||||
|
||||
obb_names = {10: "small vehicle"}
|
||||
obb_image = cv.imread("datasets/dota8/images/train/P1142__1024__0___824.jpg")
|
||||
obb_boxes = np.array(
|
||||
[
|
||||
[0, 635, 560, 919, 719, 1087, 420, 803, 261], # class-idx x1 y1 x2 y2 x3 y2 x4 y4
|
||||
[0, 331, 19, 493, 260, 776, 70, 613, -171],
|
||||
[9, 869, 161, 886, 147, 851, 101, 833, 115],
|
||||
]
|
||||
)
|
||||
ann = Annotator(
|
||||
obb_image,
|
||||
line_width=None, # default auto-size
|
||||
font_size=None, # default auto-size
|
||||
font="Arial.ttf", # must be ImageFont compatible
|
||||
pil=False, # use PIL, otherwise uses OpenCV
|
||||
)
|
||||
for obb in obb_boxes:
|
||||
c_idx, *obb = obb
|
||||
obb = np.array(obb).reshape(-1, 4, 2).squeeze()
|
||||
label = f"{obb_names.get(int(c_idx))}"
|
||||
ann.box_label(
|
||||
obb,
|
||||
label,
|
||||
color=colors(c_idx, True),
|
||||
)
|
||||
|
||||
image_with_obb = ann.result()
|
||||
```
|
||||
|
||||
Names can be used from `model.names` when [working with detection results](../modes/predict.md#working-with-results).
|
||||
Also see the [`Annotator` Reference Page](../reference/utils/plotting.md/#ultralytics.utils.plotting.Annotator) for additional insight.
|
||||
|
||||
#### Ultralytics Sweep Annotation
|
||||
|
||||
!!! example "Sweep Annotation using Ultralytics Utilities"
|
||||
|
||||
```python
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from ultralytics import YOLO
|
||||
from ultralytics.solutions.solutions import SolutionAnnotator
|
||||
from ultralytics.utils.plotting import colors
|
||||
|
||||
# User defined video path and model file
|
||||
cap = cv2.VideoCapture("path/to/video.mp4")
|
||||
model = YOLO(model="yolo26s-seg.pt") # Model file, e.g., yolo26s.pt or yolo26m-seg.pt
|
||||
|
||||
if not cap.isOpened():
|
||||
print("Error: Could not open video.")
|
||||
exit()
|
||||
|
||||
# Initialize the video writer object.
|
||||
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
|
||||
video_writer = cv2.VideoWriter("ultralytics.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
|
||||
|
||||
masks = None # Initialize variable to store masks data
|
||||
f = 0 # Initialize frame count variable for enabling mouse event.
|
||||
line_x = w # Store width of line.
|
||||
dragging = False # Initialize bool variable for line dragging.
|
||||
classes = model.names # Store model classes names for plotting.
|
||||
window_name = "Ultralytics Sweep Annotator"
|
||||
|
||||
|
||||
def drag_line(event, x, _, flags, param):
|
||||
"""Mouse callback function to enable dragging a vertical sweep line across the video frame."""
|
||||
global line_x, dragging
|
||||
if event == cv2.EVENT_LBUTTONDOWN or (flags & cv2.EVENT_FLAG_LBUTTON):
|
||||
line_x = max(0, min(x, w))
|
||||
dragging = True
|
||||
|
||||
|
||||
while cap.isOpened(): # Loop over the video capture object.
|
||||
ret, im0 = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
f = f + 1 # Increment frame count.
|
||||
count = 0 # Re-initialize count variable on every frame for precise counts.
|
||||
results = model.track(im0, persist=True)[0]
|
||||
|
||||
if f == 1:
|
||||
cv2.namedWindow(window_name)
|
||||
cv2.setMouseCallback(window_name, drag_line)
|
||||
|
||||
annotator = SolutionAnnotator(im0)
|
||||
|
||||
if results.boxes.is_track:
|
||||
if results.masks is not None:
|
||||
masks = [np.array(m, dtype=np.int32) for m in results.masks.xy]
|
||||
|
||||
boxes = results.boxes.xyxy.tolist()
|
||||
track_ids = results.boxes.id.int().cpu().tolist()
|
||||
clss = results.boxes.cls.cpu().tolist()
|
||||
|
||||
for mask, box, cls, t_id in zip(masks or [None] * len(boxes), boxes, clss, track_ids):
|
||||
color = colors(t_id, True) # Assign different color to each tracked object.
|
||||
label = f"{classes[cls]}:{t_id}"
|
||||
if mask is not None and mask.size > 0:
|
||||
if box[0] > line_x:
|
||||
count += 1
|
||||
cv2.polylines(im0, [mask], True, color, 2)
|
||||
x, y = mask.min(axis=0)
|
||||
(w_m, _), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
|
||||
cv2.rectangle(im0, (x, y - 20), (x + w_m, y), color, -1)
|
||||
cv2.putText(im0, label, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
|
||||
else:
|
||||
if box[0] > line_x:
|
||||
count += 1
|
||||
annotator.box_label(box=box, color=color, label=label)
|
||||
|
||||
# Generate draggable sweep line
|
||||
annotator.sweep_annotator(line_x=line_x, line_y=h, label=f"COUNT:{count}")
|
||||
|
||||
cv2.imshow(window_name, im0)
|
||||
video_writer.write(im0)
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
|
||||
# Release the resources
|
||||
cap.release()
|
||||
video_writer.release()
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
Find additional details about the `sweep_annotator` method in our reference section [here](../reference/solutions/solutions.md/#ultralytics.solutions.solutions.SolutionAnnotator.sweep_annotator).
|
||||
|
||||
#### Adaptive label Annotation
|
||||
|
||||
!!! warning
|
||||
|
||||
Starting from **Ultralytics v8.3.167**, `circle_label` and `text_label` have been replaced by a unified `adaptive_label` function. You can now specify the annotation type using the `shape` argument:
|
||||
|
||||
* **Rectangle**: `annotator.adaptive_label(box, label=names[int(cls)], color=colors(cls, True), shape="rect")`
|
||||
* **Circle**: `annotator.adaptive_label(box, label=names[int(cls)], color=colors(cls, True), shape="circle")`
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/c-S5M36XWmg"
|
||||
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> In-Depth Guide to Text & Circle Annotations with Python Live Demos | Ultralytics Annotations 🚀
|
||||
</p>
|
||||
|
||||
!!! example "Adaptive label Annotation using Ultralytics Utilities"
|
||||
|
||||
=== "[Circle Annotation](https://docs.ultralytics.com/reference/utils/plotting/#ultralytics.utils.plotting.Annotator.adaptive_label)"
|
||||
|
||||
```python
|
||||
import cv2
|
||||
|
||||
from ultralytics import YOLO
|
||||
from ultralytics.solutions.solutions import SolutionAnnotator
|
||||
from ultralytics.utils.plotting import colors
|
||||
|
||||
model = YOLO("yolo26s.pt")
|
||||
names = model.names
|
||||
cap = cv2.VideoCapture("path/to/video.mp4")
|
||||
|
||||
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
|
||||
writer = cv2.VideoWriter("Ultralytics circle annotation.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
|
||||
|
||||
while True:
|
||||
ret, im0 = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
annotator = SolutionAnnotator(im0)
|
||||
results = model.predict(im0)[0]
|
||||
boxes = results.boxes.xyxy.cpu()
|
||||
clss = results.boxes.cls.cpu().tolist()
|
||||
|
||||
for box, cls in zip(boxes, clss):
|
||||
annotator.adaptive_label(box, label=names[int(cls)], color=colors(cls, True), shape="circle")
|
||||
writer.write(im0)
|
||||
cv2.imshow("Ultralytics circle annotation", im0)
|
||||
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
|
||||
writer.release()
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
=== "[Text Annotation](https://docs.ultralytics.com/reference/utils/plotting/#ultralytics.utils.plotting.Annotator.adaptive_label)"
|
||||
|
||||
```python
|
||||
import cv2
|
||||
|
||||
from ultralytics import YOLO
|
||||
from ultralytics.solutions.solutions import SolutionAnnotator
|
||||
from ultralytics.utils.plotting import colors
|
||||
|
||||
model = YOLO("yolo26s.pt")
|
||||
names = model.names
|
||||
cap = cv2.VideoCapture("path/to/video.mp4")
|
||||
|
||||
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
|
||||
writer = cv2.VideoWriter("Ultralytics text annotation.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
|
||||
|
||||
while True:
|
||||
ret, im0 = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
annotator = SolutionAnnotator(im0)
|
||||
results = model.predict(im0)[0]
|
||||
boxes = results.boxes.xyxy.cpu()
|
||||
clss = results.boxes.cls.cpu().tolist()
|
||||
|
||||
for box, cls in zip(boxes, clss):
|
||||
annotator.adaptive_label(box, label=names[int(cls)], color=colors(cls, True), shape="rect")
|
||||
|
||||
writer.write(im0)
|
||||
cv2.imshow("Ultralytics text annotation", im0)
|
||||
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
|
||||
writer.release()
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
See the [`SolutionAnnotator` Reference Page](../reference/solutions/solutions.md/#ultralytics.solutions.solutions.SolutionAnnotator.adaptive_label) for additional insight.
|
||||
|
||||
## Miscellaneous
|
||||
|
||||
### Code Profiling
|
||||
|
||||
Check the duration for code to run/process either using `with` or as a decorator.
|
||||
|
||||
```python
|
||||
from ultralytics.utils.ops import Profile
|
||||
|
||||
with Profile(device="cuda:0") as dt:
|
||||
pass # operation to measure
|
||||
|
||||
print(dt)
|
||||
# >>> "Elapsed time is 9.5367431640625e-07 s"
|
||||
```
|
||||
|
||||
### Ultralytics Supported Formats
|
||||
|
||||
Need to programmatically use the supported [image or video formats](../modes/predict.md#image-and-video-formats) in Ultralytics? Use these constants if needed:
|
||||
|
||||
```python
|
||||
from ultralytics.data.utils import IMG_FORMATS, VID_FORMATS
|
||||
|
||||
print(IMG_FORMATS)
|
||||
# {'avif', 'bmp', 'dng', 'heic', 'jp2', 'jpeg', 'jpeg2000', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp'}
|
||||
|
||||
print(VID_FORMATS)
|
||||
# {'asf', 'avi', 'gif', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ts', 'wmv', 'webm'}
|
||||
```
|
||||
|
||||
### Make Divisible
|
||||
|
||||
Calculate the nearest whole number to `x` that is evenly divisible by `y`.
|
||||
|
||||
```python
|
||||
from ultralytics.utils.ops import make_divisible
|
||||
|
||||
make_divisible(7, 3)
|
||||
# >>> 9
|
||||
make_divisible(7, 2)
|
||||
# >>> 8
|
||||
```
|
||||
|
||||
## FAQ
|
||||
|
||||
### What utilities are included in the Ultralytics package to enhance machine learning workflows?
|
||||
|
||||
The Ultralytics package includes utilities designed to streamline and optimize machine learning workflows. Key utilities include [auto-annotation](../reference/data/annotator.md#ultralytics.data.annotator.auto_annotate) for labeling datasets, converting [COCO](https://docs.ultralytics.com/datasets/detect/coco/) to YOLO format with [convert_coco](../reference/data/converter.md#ultralytics.data.converter.convert_coco), compressing images, and dataset auto-splitting. These tools reduce manual effort, ensure consistency, and enhance data processing efficiency.
|
||||
|
||||
### How can I use Ultralytics to auto-label my dataset?
|
||||
|
||||
If you have a pretrained Ultralytics YOLO object detection model, you can use it with the [SAM](../models/sam.md) model to auto-annotate your dataset in segmentation format. Here's an example:
|
||||
|
||||
```python
|
||||
from ultralytics.data.annotator import auto_annotate
|
||||
|
||||
auto_annotate(
|
||||
data="path/to/new/data",
|
||||
det_model="yolo26n.pt",
|
||||
sam_model="mobile_sam.pt",
|
||||
device="cuda",
|
||||
output_dir="path/to/save_labels",
|
||||
)
|
||||
```
|
||||
|
||||
For more details, check the [auto_annotate reference section](../reference/data/annotator.md#ultralytics.data.annotator.auto_annotate).
|
||||
|
||||
### How do I convert COCO dataset annotations to YOLO format in Ultralytics?
|
||||
|
||||
To convert COCO JSON annotations into YOLO format for object detection, you can use the `convert_coco` utility. Here's a sample code snippet:
|
||||
|
||||
```python
|
||||
from ultralytics.data.converter import convert_coco
|
||||
|
||||
convert_coco(
|
||||
"coco/annotations/",
|
||||
use_segments=False,
|
||||
use_keypoints=False,
|
||||
cls91to80=True,
|
||||
)
|
||||
```
|
||||
|
||||
For additional information, visit the [convert_coco reference page](../reference/data/converter.md#ultralytics.data.converter.convert_coco).
|
||||
|
||||
### What is the purpose of the YOLO Data Explorer in the Ultralytics package?
|
||||
|
||||
The [YOLO Explorer](../datasets/explorer/index.md) is a powerful tool introduced in the `8.1.0` update to enhance dataset understanding. It allows you to use text queries to find object instances in your dataset, making it easier to analyze and manage your data. This tool provides valuable insights into dataset composition and distribution, helping to improve model training and performance.
|
||||
|
||||
### How can I convert bounding boxes to segments in Ultralytics?
|
||||
|
||||
To convert existing bounding box data (in `x y w h` format) to segments, you can use the `yolo_bbox2segment` function. Ensure your files are organized with separate directories for images and labels.
|
||||
|
||||
```python
|
||||
from ultralytics.data.converter import yolo_bbox2segment
|
||||
|
||||
yolo_bbox2segment(
|
||||
im_dir="path/to/images",
|
||||
save_dir=None, # saved to "labels-segment" in the images directory
|
||||
sam_model="sam_b.pt",
|
||||
)
|
||||
```
|
||||
|
||||
For more information, visit the [yolo_bbox2segment reference page](../reference/data/converter.md#ultralytics.data.converter.yolo_bbox2segment).
|
||||
Reference in New Issue
Block a user