Merge branch 'main' into types

types
Ultralytics Assistant 3 months ago committed by GitHub
commit c396718385
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 20
      docs/en/datasets/classify/cifar10.md
  2. 29
      docs/en/datasets/classify/imagewoof.md
  3. 2
      docs/en/datasets/detect/roboflow-100.md
  4. 29
      docs/en/datasets/detect/visdrone.md
  5. 4
      docs/en/datasets/explorer/explorer.ipynb
  6. 32
      docs/en/datasets/segment/crack-seg.md
  7. 30
      docs/en/datasets/segment/package-seg.md
  8. 6
      docs/en/guides/kfold-cross-validation.md
  9. 11
      docs/en/guides/parking-management.md
  10. 2
      docs/en/guides/sahi-tiled-inference.md
  11. 12
      docs/en/hub/datasets.md
  12. 1
      docs/en/index.md
  13. 2
      docs/en/integrations/ibm-watsonx.md
  14. 5
      docs/en/integrations/jupyterlab.md
  15. 139
      docs/en/integrations/tensorboard.md
  16. 2
      docs/en/robots.txt
  17. 3
      examples/YOLOv8-ONNXRuntime-CPP/CMakeLists.txt
  18. 13
      examples/YOLOv8-ONNXRuntime-CPP/README.md
  19. 2
      examples/hub.ipynb
  20. 6
      pyproject.toml
  21. 2
      ultralytics/__init__.py
  22. 22
      ultralytics/cfg/__init__.py
  23. 2
      ultralytics/data/annotator.py
  24. 114
      ultralytics/data/augment.py
  25. 6
      ultralytics/data/base.py
  26. 12
      ultralytics/data/converter.py
  27. 12
      ultralytics/data/explorer/explorer.py
  28. 12
      ultralytics/data/utils.py
  29. 105
      ultralytics/engine/model.py
  30. 60
      ultralytics/engine/results.py
  31. 15
      ultralytics/engine/trainer.py
  32. 10
      ultralytics/engine/tuner.py
  33. 10
      ultralytics/hub/__init__.py
  34. 48
      ultralytics/hub/session.py
  35. 4
      ultralytics/models/fastsam/model.py
  36. 4
      ultralytics/models/fastsam/predict.py
  37. 8
      ultralytics/models/nas/model.py
  38. 2
      ultralytics/models/nas/predict.py
  39. 2
      ultralytics/models/nas/val.py
  40. 2
      ultralytics/models/rtdetr/predict.py
  41. 2
      ultralytics/models/rtdetr/train.py
  42. 2
      ultralytics/models/rtdetr/val.py
  43. 22
      ultralytics/models/sam/model.py
  44. 11
      ultralytics/models/sam/modules/decoders.py
  45. 10
      ultralytics/models/sam/modules/sam.py
  46. 2
      ultralytics/models/sam/modules/tiny_encoder.py
  47. 2
      ultralytics/models/sam/modules/utils.py
  48. 20
      ultralytics/models/sam/predict.py
  49. 2
      ultralytics/models/yolo/classify/predict.py
  50. 2
      ultralytics/models/yolo/classify/train.py
  51. 2
      ultralytics/models/yolo/classify/val.py
  52. 2
      ultralytics/models/yolo/detect/predict.py
  53. 2
      ultralytics/models/yolo/detect/train.py
  54. 2
      ultralytics/models/yolo/detect/val.py
  55. 2
      ultralytics/models/yolo/obb/predict.py
  56. 2
      ultralytics/models/yolo/obb/train.py
  57. 4
      ultralytics/models/yolo/obb/val.py
  58. 2
      ultralytics/models/yolo/pose/predict.py
  59. 2
      ultralytics/models/yolo/pose/train.py
  60. 2
      ultralytics/models/yolo/pose/val.py
  61. 2
      ultralytics/models/yolo/segment/predict.py
  62. 2
      ultralytics/models/yolo/segment/train.py
  63. 2
      ultralytics/models/yolo/segment/val.py
  64. 2
      ultralytics/models/yolo/world/train.py
  65. 4
      ultralytics/nn/modules/__init__.py
  66. 2
      ultralytics/nn/tasks.py
  67. 1
      ultralytics/solutions/__init__.py
  68. 4
      ultralytics/trackers/byte_tracker.py
  69. 10
      ultralytics/trackers/utils/gmc.py
  70. 2
      ultralytics/trackers/utils/kalman_filter.py
  71. 2
      ultralytics/trackers/utils/matching.py
  72. 29
      ultralytics/utils/__init__.py
  73. 4
      ultralytics/utils/benchmarks.py
  74. 22
      ultralytics/utils/checks.py
  75. 12
      ultralytics/utils/downloads.py
  76. 6
      ultralytics/utils/files.py
  77. 2
      ultralytics/utils/instance.py
  78. 14
      ultralytics/utils/metrics.py
  79. 8
      ultralytics/utils/plotting.py
  80. 18
      ultralytics/utils/torch_utils.py
  81. 4
      ultralytics/utils/tuner.py

@ -153,14 +153,18 @@ Each subset comprises images categorized into 10 classes, with their annotations
If you use the CIFAR-10 dataset in your research or development projects, make sure to cite the following paper: If you use the CIFAR-10 dataset in your research or development projects, make sure to cite the following paper:
```bibtex !!! Quote ""
@TECHREPORT{Krizhevsky09learningmultiple,
author={Alex Krizhevsky}, === "BibTeX"
title={Learning multiple layers of features from tiny images},
institution={}, ```bibtex
year={2009} @TECHREPORT{Krizhevsky09learningmultiple,
} author={Alex Krizhevsky},
``` title={Learning multiple layers of features from tiny images},
institution={},
year={2009}
}
```
Acknowledging the dataset's creators helps support continued research and development in the field. For more details, see the [citations and acknowledgments](#citations-and-acknowledgments) section. Acknowledging the dataset's creators helps support continued research and development in the field. For more details, see the [citations and acknowledgments](#citations-and-acknowledgments) section.

@ -59,18 +59,29 @@ ImageWoof dataset comes in three different sizes to accommodate various research
To use these variants in your training, simply replace 'imagewoof' in the dataset argument with 'imagewoof320' or 'imagewoof160'. For example: To use these variants in your training, simply replace 'imagewoof' in the dataset argument with 'imagewoof320' or 'imagewoof160'. For example:
```python !!! Example "Example"
from ultralytics import YOLO
# Load a model === "Python"
model = YOLO("yolov8n-cls.pt") # load a pretrained model (recommended for training)
```python
from ultralytics import YOLO
# Load a model
model = YOLO("yolov8n-cls.pt") # load a pretrained model (recommended for training)
# For medium-sized dataset # For medium-sized dataset
model.train(data="imagewoof320", epochs=100, imgsz=224) model.train(data="imagewoof320", epochs=100, imgsz=224)
# For small-sized dataset # For small-sized dataset
model.train(data="imagewoof160", epochs=100, imgsz=224) model.train(data="imagewoof160", epochs=100, imgsz=224)
``` ```
=== "CLI"
```bash
# Load a pretrained model and train on the small-sized dataset
yolo classify train model=yolov8n-cls.pt data=imagewoof320 epochs=100 imgsz=224
```
It's important to note that using smaller images will likely yield lower performance in terms of classification accuracy. However, it's an excellent way to iterate quickly in the early stages of model development and prototyping. It's important to note that using smaller images will likely yield lower performance in terms of classification accuracy. However, it's an excellent way to iterate quickly in the early stages of model development and prototyping.

@ -203,7 +203,7 @@ The **Roboflow 100** dataset is accessible on [GitHub](https://github.com/robofl
When using the Roboflow 100 dataset in your research, ensure to properly cite it. Here is the recommended citation: When using the Roboflow 100 dataset in your research, ensure to properly cite it. Here is the recommended citation:
!!! Quote !!! Quote ""
=== "BibTeX" === "BibTeX"

@ -159,16 +159,19 @@ The configuration file for the VisDrone dataset, `VisDrone.yaml`, can be found i
If you use the VisDrone dataset in your research or development work, please cite the following paper: If you use the VisDrone dataset in your research or development work, please cite the following paper:
!!! Quote "BibTeX" !!! Quote ""
```bibtex === "BibTeX"
@ARTICLE{9573394,
author={Zhu, Pengfei and Wen, Longyin and Du, Dawei and Bian, Xiao and Fan, Heng and Hu, Qinghua and Ling, Haibin}, ```bibtex
journal={IEEE Transactions on Pattern Analysis and Machine Intelligence}, @ARTICLE{9573394,
title={Detection and Tracking Meet Drones Challenge}, author={Zhu, Pengfei and Wen, Longyin and Du, Dawei and Bian, Xiao and Fan, Heng and Hu, Qinghua and Ling, Haibin},
year={2021}, journal={IEEE Transactions on Pattern Analysis and Machine Intelligence},
volume={}, title={Detection and Tracking Meet Drones Challenge},
number={}, year={2021},
pages={1-1}, volume={},
doi={10.1109/TPAMI.2021.3119563}} number={},
``` pages={1-1},
doi={10.1109/TPAMI.2021.3119563}
}
```

@ -13,7 +13,7 @@
" <a href=\"https://ultralytics.com/yolov8\" target=\"_blank\">\n", " <a href=\"https://ultralytics.com/yolov8\" target=\"_blank\">\n",
" <img width=\"1024\", src=\"https://raw.githubusercontent.com/ultralytics/assets/main/yolov8/banner-yolov8.png\"></a>\n", " <img width=\"1024\", src=\"https://raw.githubusercontent.com/ultralytics/assets/main/yolov8/banner-yolov8.png\"></a>\n",
"\n", "\n",
" [中文](https://docs.ultralytics.com/zh/) | [한국어](https://docs.ultralytics.com/ko/) | [日本語](https://docs.ultralytics.com/ja/) | [Русский](https://docs.ultralytics.com/ru/) | [Deutsch](https://docs.ultralytics.com/de/) | [Français](https://docs.ultralytics.com/fr/) | [Español](https://docs.ultralytics.com/es/) | [Português](https://docs.ultralytics.com/pt/) | [ि](https://docs.ultralytics.com/hi/) | [العربية](https://docs.ultralytics.com/ar/)\n", " [中文](https://docs.ultralytics.com/zh/) | [한국어](https://docs.ultralytics.com/ko/) | [日本語](https://docs.ultralytics.com/ja/) | [Русский](https://docs.ultralytics.com/ru/) | [Deutsch](https://docs.ultralytics.com/de/) | [Français](https://docs.ultralytics.com/fr/) | [Español](https://docs.ultralytics.com/es/) | [Português](https://docs.ultralytics.com/pt/) | [Türkçe](https://docs.ultralytics.com/tr/) | [Tiếng Việt](https://docs.ultralytics.com/vi/) | [العربية](https://docs.ultralytics.com/ar/)\n",
"\n", "\n",
" <a href=\"https://console.paperspace.com/github/ultralytics/ultralytics\"><img src=\"https://assets.paperspace.io/img/gradient-badge.svg\" alt=\"Run on Gradient\"/></a>\n", " <a href=\"https://console.paperspace.com/github/ultralytics/ultralytics\"><img src=\"https://assets.paperspace.io/img/gradient-badge.svg\" alt=\"Run on Gradient\"/></a>\n",
" <a href=\"https://colab.research.google.com/github/ultralytics/ultralytics/blob/main/examples/tutorial.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n", " <a href=\"https://colab.research.google.com/github/ultralytics/ultralytics/blob/main/examples/tutorial.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
@ -598,4 +598,4 @@
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 5 "nbformat_minor": 5
} }

@ -135,19 +135,23 @@ Ultralytics YOLO offers advanced real-time object detection, segmentation, and c
If you incorporate the Crack Segmentation Dataset into your research, please use the following BibTeX reference: If you incorporate the Crack Segmentation Dataset into your research, please use the following BibTeX reference:
```bibtex !!! Quote ""
@misc{ crack-bphdr_dataset,
title = { crack Dataset }, === "BibTeX"
type = { Open Source Dataset },
author = { University }, ```bibtex
howpublished = { \url{ https://universe.roboflow.com/university-bswxt/crack-bphdr } }, @misc{ crack-bphdr_dataset,
url = { https://universe.roboflow.com/university-bswxt/crack-bphdr }, title = { crack Dataset },
journal = { Roboflow Universe }, type = { Open Source Dataset },
publisher = { Roboflow }, author = { University },
year = { 2022 }, howpublished = { \url{ https://universe.roboflow.com/university-bswxt/crack-bphdr } },
month = { dec }, url = { https://universe.roboflow.com/university-bswxt/crack-bphdr },
note = { visited on 2024-01-23 }, journal = { Roboflow Universe },
} publisher = { Roboflow },
``` year = { 2022 },
month = { dec },
note = { visited on 2024-01-23 },
}
```
This citation format ensures proper accreditation to the creators of the dataset and acknowledges its use in your research. This citation format ensures proper accreditation to the creators of the dataset and acknowledges its use in your research.

@ -99,24 +99,28 @@ The [Roboflow Package Segmentation Dataset](https://universe.roboflow.com/factor
### How do I train an Ultralytics YOLOv8 model on the Package Segmentation Dataset? ### How do I train an Ultralytics YOLOv8 model on the Package Segmentation Dataset?
You can train an Ultralytics YOLOv8n model using both Python and CLI methods. For Python, use the snippet below: You can train an Ultralytics YOLOv8n model using both Python and CLI methods. Use the snippets below:
```python !!! Example "Train Example"
from ultralytics import YOLO
# Load a model === "Python"
model = YOLO("yolov8n-seg.pt") # load a pretrained model
```python
from ultralytics import YOLO
# Train the model # Load a model
results = model.train(data="package-seg.yaml", epochs=100, imgsz=640) model = YOLO("yolov8n-seg.pt") # load a pretrained model
```
For CLI: # Train the model
results = model.train(data="package-seg.yaml", epochs=100, imgsz=640)
```
```bash === "CLI"
# Start training from a pretrained *.pt model
yolo segment train data=package-seg.yaml model=yolov8n-seg.pt epochs=100 imgsz=640 ```bash
``` # Start training from a pretrained *.pt model
yolo segment train data=package-seg.yaml model=yolov8n-seg.pt epochs=100 imgsz=640
```
Refer to the model [Training](../../modes/train.md) page for more details. Refer to the model [Training](../../modes/train.md) page for more details.

@ -82,7 +82,7 @@ Without further ado, let's dive in!
```python ```python
import pandas as pd import pandas as pd
indx = [l.stem for l in labels] # uses base filename as ID (no extension) indx = [label.stem for label in labels] # uses base filename as ID (no extension)
labels_df = pd.DataFrame([], columns=cls_idx, index=indx) labels_df = pd.DataFrame([], columns=cls_idx, index=indx)
``` ```
@ -97,9 +97,9 @@ Without further ado, let's dive in!
with open(label, "r") as lf: with open(label, "r") as lf:
lines = lf.readlines() lines = lf.readlines()
for l in lines: for line in lines:
# classes for YOLO label uses integer at first position of each line # classes for YOLO label uses integer at first position of each line
lbl_counter[int(l.split(" ")[0])] += 1 lbl_counter[int(line.split(" ")[0])] += 1
labels_df.loc[label.stem] = lbl_counter labels_df.loc[label.stem] = lbl_counter

@ -10,6 +10,17 @@ keywords: parking management, YOLOv8, Ultralytics, vehicle detection, real-time
Parking management with [Ultralytics YOLOv8](https://github.com/ultralytics/ultralytics/) ensures efficient and safe parking by organizing spaces and monitoring availability. YOLOv8 can improve parking lot management through real-time vehicle detection, and insights into parking occupancy. Parking management with [Ultralytics YOLOv8](https://github.com/ultralytics/ultralytics/) ensures efficient and safe parking by organizing spaces and monitoring availability. YOLOv8 can improve parking lot management through real-time vehicle detection, and insights into parking occupancy.
<p align="center">
<br>
<iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/WwXnljc7ZUM"
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 Implement Parking Management Using Ultralytics YOLOv8 🚀
</p>
## Advantages of Parking Management System? ## Advantages of Parking Management System?
- **Efficiency**: Parking lot management optimizes the use of parking spaces and reduces congestion. - **Efficiency**: Parking lot management optimizes the use of parking spaces and reduces congestion.

@ -248,9 +248,9 @@ Learn more about the [benefits of sliced inference](#benefits-of-sliced-inferenc
Yes, you can visualize prediction results when using YOLOv8 with SAHI. Here's how you can export and visualize the results: Yes, you can visualize prediction results when using YOLOv8 with SAHI. Here's how you can export and visualize the results:
```python ```python
result.export_visuals(export_dir="demo_data/")
from IPython.display import Image from IPython.display import Image
result.export_visuals(export_dir="demo_data/")
Image("demo_data/prediction_visual.png") Image("demo_data/prediction_visual.png")
``` ```

@ -98,6 +98,18 @@ Next, [train a model](./models.md#train-model) on your dataset.
![Ultralytics HUB screenshot of the Dataset page with an arrow pointing to the Train Model button](https://raw.githubusercontent.com/ultralytics/assets/main/docs/hub/datasets/hub_upload_dataset_9.jpg) ![Ultralytics HUB screenshot of the Dataset page with an arrow pointing to the Train Model button](https://raw.githubusercontent.com/ultralytics/assets/main/docs/hub/datasets/hub_upload_dataset_9.jpg)
## Download Dataset
Navigate to the Dataset page of the dataset you want to download, open the dataset actions dropdown and click on the **Download** option. This action will start downloading your dataset.
![Ultralytics HUB screenshot of the Dataset page with an arrow pointing to the Download option](https://raw.githubusercontent.com/ultralytics/assets/main/docs/hub/datasets/hub_download_dataset_1.jpg)
??? tip "Tip"
You can download a dataset directly from the [Datasets](https://hub.ultralytics.com/datasets) page.
![Ultralytics HUB screenshot of the Datasets page with an arrow pointing to the Download option of one of the datasets](https://raw.githubusercontent.com/ultralytics/assets/main/docs/hub/datasets/hub_download_dataset_2.jpg)
## Share Dataset ## Share Dataset
!!! info "Info" !!! info "Info"

@ -16,7 +16,6 @@ keywords: Ultralytics, YOLOv8, object detection, image segmentation, deep learni
<a href="https://docs.ultralytics.com/pt/">Português</a> | <a href="https://docs.ultralytics.com/pt/">Português</a> |
<a href="https://docs.ultralytics.com/tr/">Türkçe</a> | <a href="https://docs.ultralytics.com/tr/">Türkçe</a> |
<a href="https://docs.ultralytics.com/vi/">Tiếng Việt</a> | <a href="https://docs.ultralytics.com/vi/">Tiếng Việt</a> |
<a href="https://docs.ultralytics.com/hi/">ि</a> |
<a href="https://docs.ultralytics.com/ar/">العربية</a> <a href="https://docs.ultralytics.com/ar/">العربية</a>
<br> <br>
<br> <br>

@ -114,7 +114,7 @@ After installing Kaggle, we can load the dataset into Watsonx.
os.environ["KAGGLE_KEY"] = "apiKey" os.environ["KAGGLE_KEY"] = "apiKey"
# Load dataset # Load dataset
!kaggle datasets download atiqishrak/trash-dataset-icra19 --unzip os.system("kaggle datasets download atiqishrak/trash-dataset-icra19 --unzip")
# Store working directory path as work_dir # Store working directory path as work_dir
work_dir = os.getcwd() work_dir = os.getcwd()

@ -117,7 +117,7 @@ To train a YOLOv8 model using JupyterLab:
1. Install JupyterLab and the Ultralytics package: 1. Install JupyterLab and the Ultralytics package:
```python ```bash
pip install jupyterlab ultralytics pip install jupyterlab ultralytics
``` ```
@ -138,7 +138,8 @@ To train a YOLOv8 model using JupyterLab:
``` ```
5. Visualize training results using JupyterLab's built-in plotting capabilities: 5. Visualize training results using JupyterLab's built-in plotting capabilities:
```python
```ipython
%matplotlib inline %matplotlib inline
from ultralytics.utils.plotting import plot_results from ultralytics.utils.plotting import plot_results
plot_results(results) plot_results(results)

@ -61,125 +61,98 @@ Before diving into the usage instructions, be sure to check out the range of [YO
=== "Python" === "Python"
```python ```python
rom ultralytics import YOLO from ultralytics import YOLO
Load a pre-trained model # Load a pre-trained model
odel = YOLO('yolov8n.pt') model = YOLO("yolov8n.pt")
Train the model # Train the model
esults = model.train(data='coco8.yaml', epochs=100, imgsz=640) results = model.train(data="coco8.yaml", epochs=100, imgsz=640)
``
ning the usage code snippet above, you can expect the following output:
text
ard: Start with 'tensorboard --logdir path_to_your_tensorboard_logs', view at http://localhost:6006/
``` ```
put indicates that TensorBoard is now actively monitoring your YOLOv8 training session. You can access the TensorBoard dashboard by visiting the provided URL (http://localhost:6006/) to view real-time training metrics and model performance. For users working in Google Colab, the TensorBoard will be displayed in the same cell where you executed the TensorBoard configuration commands. Upon running the usage code snippet above, you can expect the following output:
information related to the model training process, be sure to check our [YOLOv8 Model Training guide](../modes/train.md). If you are interested in learning more about logging, checkpoints, plotting, and file management, read our [usage guide on configuration](../usage/cfg.md).
standing Your TensorBoard for YOLOv8 Training
's focus on understanding the various features and components of TensorBoard in the context of YOLOv8 training. The three key sections of the TensorBoard are Time Series, Scalars, and Graphs.
Series
Series feature in the TensorBoard offers a dynamic and detailed perspective of various training metrics over time for YOLOv8 models. It focuses on the progression and trends of metrics across training epochs. Here's an example of what you can expect to see. ```bash
TensorBoard: Start with 'tensorboard --logdir path_to_your_tensorboard_logs', view at http://localhost:6006/
(https://github.com/ultralytics/ultralytics/assets/25847604/20b3e038-0356-465e-a37e-1ea232c68354) ```
Features of Time Series in TensorBoard
er Tags and Pinned Cards**: This functionality allows users to filter specific metrics and pin cards for quick comparison and access. It's particularly useful for focusing on specific aspects of the training process.
iled Metric Cards**: Time Series divides metrics into different categories like learning rate (lr), training (train), and validation (val) metrics, each represented by individual cards.
hical Display**: Each card in the Time Series section shows a detailed graph of a specific metric over the course of training. This visual representation aids in identifying trends, patterns, or anomalies in the training process. This output indicates that TensorBoard is now actively monitoring your YOLOv8 training session. You can access the TensorBoard dashboard by visiting the provided URL (http://localhost:6006/) to view real-time training metrics and model performance. For users working in Google Colab, the TensorBoard will be displayed in the same cell where you executed the TensorBoard configuration commands.
epth Analysis**: Time Series provides an in-depth analysis of each metric. For instance, different learning rate segments are shown, offering insights into how adjustments in learning rate impact the model's learning curve. For more information related to the model training process, be sure to check our [YOLOv8 Model Training guide](../modes/train.md). If you are interested in learning more about logging, checkpoints, plotting, and file management, read our [usage guide on configuration](../usage/cfg.md).
ortance of Time Series in YOLOv8 Training ## Understanding Your TensorBoard for YOLOv8 Training
Series section is essential for a thorough analysis of the YOLOv8 model's training progress. It lets you track the metrics in real time to promptly identify and solve issues. It also offers a detailed view of each metrics progression, which is crucial for fine-tuning the model and enhancing its performance. Now, let's focus on understanding the various features and components of TensorBoard in the context of YOLOv8 training. The three key sections of the TensorBoard are Time Series, Scalars, and Graphs.
ars ### Time Series
in the TensorBoard are crucial for plotting and analyzing simple metrics like loss and accuracy during the training of YOLOv8 models. They offer a clear and concise view of how these metrics evolve with each training epoch, providing insights into the model's learning effectiveness and stability. Here's an example of what you can expect to see. The Time Series feature in the TensorBoard offers a dynamic and detailed perspective of various training metrics over time for YOLOv8 models. It focuses on the progression and trends of metrics across training epochs. Here's an example of what you can expect to see.
(https://github.com/ultralytics/ultralytics/assets/25847604/f9228193-13e9-4768-9edf-8fa15ecd24fa) ![image](https://github.com/ultralytics/ultralytics/assets/25847604/20b3e038-0356-465e-a37e-1ea232c68354)
Features of Scalars in TensorBoard #### Key Features of Time Series in TensorBoard
ning Rate (lr) Tags**: These tags show the variations in the learning rate across different segments (e.g., `pg0`, `pg1`, `pg2`). This helps us understand the impact of learning rate adjustments on the training process. - **Filter Tags and Pinned Cards**: This functionality allows users to filter specific metrics and pin cards for quick comparison and access. It's particularly useful for focusing on specific aspects of the training process.
ics Tags**: Scalars include performance indicators such as: - **Detailed Metric Cards**: Time Series divides metrics into different categories like learning rate (lr), training (train), and validation (val) metrics, each represented by individual cards.
AP50 (B)`: Mean Average Precision at 50% Intersection over Union (IoU), crucial for assessing object detection accuracy. - **Graphical Display**: Each card in the Time Series section shows a detailed graph of a specific metric over the course of training. This visual representation aids in identifying trends, patterns, or anomalies in the training process.
AP50-95 (B)`: Mean Average Precision calculated over a range of IoU thresholds, offering a more comprehensive evaluation of accuracy. - **In-Depth Analysis**: Time Series provides an in-depth analysis of each metric. For instance, different learning rate segments are shown, offering insights into how adjustments in learning rate impact the model's learning curve.
recision (B)`: Indicates the ratio of correctly predicted positive observations, key to understanding prediction accuracy. #### Importance of Time Series in YOLOv8 Training
ecall (B)`: Important for models where missing a detection is significant, this metric measures the ability to detect all relevant instances. The Time Series section is essential for a thorough analysis of the YOLOv8 model's training progress. It lets you track the metrics in real time to promptly identify and solve issues. It also offers a detailed view of each metrics progression, which is crucial for fine-tuning the model and enhancing its performance.
learn more about the different metrics, read our guide on [performance metrics](../guides/yolo-performance-metrics.md). ### Scalars
ning and Validation Tags (`train`, `val`)**: These tags display metrics specifically for the training and validation datasets, allowing for a comparative analysis of model performance across different data sets. Scalars in the TensorBoard are crucial for plotting and analyzing simple metrics like loss and accuracy during the training of YOLOv8 models. They offer a clear and concise view of how these metrics evolve with each training epoch, providing insights into the model's learning effectiveness and stability. Here's an example of what you can expect to see.
ortance of Monitoring Scalars ![image](https://github.com/ultralytics/ultralytics/assets/25847604/f9228193-13e9-4768-9edf-8fa15ecd24fa)
g scalar metrics is crucial for fine-tuning the YOLOv8 model. Variations in these metrics, such as spikes or irregular patterns in loss graphs, can highlight potential issues such as overfitting, underfitting, or inappropriate learning rate settings. By closely monitoring these scalars, you can make informed decisions to optimize the training process, ensuring that the model learns effectively and achieves the desired performance. #### Key Features of Scalars in TensorBoard
erence Between Scalars and Time Series - **Learning Rate (lr) Tags**: These tags show the variations in the learning rate across different segments (e.g., `pg0`, `pg1`, `pg2`). This helps us understand the impact of learning rate adjustments on the training process.
th Scalars and Time Series in TensorBoard are used for tracking metrics, they serve slightly different purposes. Scalars focus on plotting simple metrics such as loss and accuracy as scalar values. They provide a high-level overview of how these metrics change with each training epoch. While, the time-series section of the TensorBoard offers a more detailed timeline view of various metrics. It is particularly useful for monitoring the progression and trends of metrics over time, providing a deeper dive into the specifics of the training process. - **Metrics Tags**: Scalars include performance indicators such as:
hs - `mAP50 (B)`: Mean Average Precision at 50% Intersection over Union (IoU), crucial for assessing object detection accuracy.
hs section of the TensorBoard visualizes the computational graph of the YOLOv8 model, showing how operations and data flow within the model. It's a powerful tool for understanding the model's structure, ensuring that all layers are connected correctly, and for identifying any potential bottlenecks in data flow. Here's an example of what you can expect to see. - `mAP50-95 (B)`: Mean Average Precision calculated over a range of IoU thresholds, offering a more comprehensive evaluation of accuracy.
(https://github.com/ultralytics/ultralytics/assets/25847604/039028e0-4ab3-4170-bfa8-f93ce483f615) - `Precision (B)`: Indicates the ratio of correctly predicted positive observations, key to understanding prediction accuracy.
re particularly useful for debugging the model, especially in complex architectures typical in deep learning models like YOLOv8. They help in verifying layer connections and the overall design of the model. - `Recall (B)`: Important for models where missing a detection is significant, this metric measures the ability to detect all relevant instances.
ry - To learn more about the different metrics, read our guide on [performance metrics](../guides/yolo-performance-metrics.md).
de aims to help you use TensorBoard with YOLOv8 for visualization and analysis of machine learning model training. It focuses on explaining how key TensorBoard features can provide insights into training metrics and model performance during YOLOv8 training sessions. - **Training and Validation Tags (`train`, `val`)**: These tags display metrics specifically for the training and validation datasets, allowing for a comparative analysis of model performance across different data sets.
re detailed exploration of these features and effective utilization strategies, you can refer to TensorFlow's official [TensorBoard documentation](https://www.tensorflow.org/tensorboard/get_started) and their [GitHub repository](https://github.com/tensorflow/tensorboard). #### Importance of Monitoring Scalars
learn more about the various integrations of Ultralytics? Check out the [Ultralytics integrations guide page](../integrations/index.md) to see what other exciting capabilities are waiting to be discovered! Observing scalar metrics is crucial for fine-tuning the YOLOv8 model. Variations in these metrics, such as spikes or irregular patterns in loss graphs, can highlight potential issues such as overfitting, underfitting, or inappropriate learning rate settings. By closely monitoring these scalars, you can make informed decisions to optimize the training process, ensuring that the model learns effectively and achieves the desired performance.
## FAQ ### Difference Between Scalars and Time Series
do I integrate YOLOv8 with TensorBoard for real-time visualization? While both Scalars and Time Series in TensorBoard are used for tracking metrics, they serve slightly different purposes. Scalars focus on plotting simple metrics such as loss and accuracy as scalar values. They provide a high-level overview of how these metrics change with each training epoch. While, the time-series section of the TensorBoard offers a more detailed timeline view of various metrics. It is particularly useful for monitoring the progression and trends of metrics over time, providing a deeper dive into the specifics of the training process.
ing YOLOv8 with TensorBoard allows for real-time visual insights during model training. First, install the necessary package: ### Graphs
ple "Installation" The Graphs section of the TensorBoard visualizes the computational graph of the YOLOv8 model, showing how operations and data flow within the model. It's a powerful tool for understanding the model's structure, ensuring that all layers are connected correctly, and for identifying any potential bottlenecks in data flow. Here's an example of what you can expect to see.
"CLI" ![image](https://github.com/ultralytics/ultralytics/assets/25847604/039028e0-4ab3-4170-bfa8-f93ce483f615)
```bash
# Install the required package for YOLOv8 and Tensorboard
pip install ultralytics
```
Next, configure TensorBoard to log your training runs, then start TensorBoard: Graphs are particularly useful for debugging the model, especially in complex architectures typical in deep learning models like YOLOv8. They help in verifying layer connections and the overall design of the model.
!!! Example "Configure TensorBoard for Google Colab" ## Summary
=== "Python" This guide aims to help you use TensorBoard with YOLOv8 for visualization and analysis of machine learning model training. It focuses on explaining how key TensorBoard features can provide insights into training metrics and model performance during YOLOv8 training sessions.
```ipython For a more detailed exploration of these features and effective utilization strategies, you can refer to TensorFlow's official [TensorBoard documentation](https://www.tensorflow.org/tensorboard/get_started) and their [GitHub repository](https://github.com/tensorflow/tensorboard).
%load_ext tensorboard
%tensorboard --logdir path/to/runs
```
Finally, during training, YOLOv8 automatically logs metrics like loss and accuracy to TensorBoard. You can monitor these metrics by visiting [http://localhost:6006/](http://localhost:6006/). Want to learn more about the various integrations of Ultralytics? Check out the [Ultralytics integrations guide page](../integrations/index.md) to see what other exciting capabilities are waiting to be discovered!
For a comprehensive guide, refer to our [YOLOv8 Model Training guide](../modes/train.md). ## FAQ
### What benefits does using TensorBoard with YOLOv8 offer? ### What benefits does using TensorBoard with YOLOv8 offer?
@ -225,16 +198,16 @@ Yes, you can use TensorBoard in a Google Colab environment to train YOLOv8 model
%tensorboard --logdir path/to/runs %tensorboard --logdir path/to/runs
``` ```
Then, run the YOLOv8 training script: Then, run the YOLOv8 training script:
```python ```python
from ultralytics import YOLO from ultralytics import YOLO
# Load a pre-trained model # Load a pre-trained model
model = YOLO("yolov8n.pt") model = YOLO("yolov8n.pt")
# Train the model # Train the model
results = model.train(data="coco8.yaml", epochs=100, imgsz=640) results = model.train(data="coco8.yaml", epochs=100, imgsz=640)
``` ```
TensorBoard will visualize the training progress within Colab, providing real-time insights into metrics like loss and accuracy. For additional details on configuring YOLOv8 training, see our detailed [YOLOv8 Installation guide](../quickstart.md). TensorBoard will visualize the training progress within Colab, providing real-time insights into metrics like loss and accuracy. For additional details on configuring YOLOv8 training, see our detailed [YOLOv8 Installation guide](../quickstart.md).

@ -4,11 +4,9 @@ Sitemap: https://docs.ultralytics.com/ar/sitemap.xml
Sitemap: https://docs.ultralytics.com/de/sitemap.xml Sitemap: https://docs.ultralytics.com/de/sitemap.xml
Sitemap: https://docs.ultralytics.com/es/sitemap.xml Sitemap: https://docs.ultralytics.com/es/sitemap.xml
Sitemap: https://docs.ultralytics.com/fr/sitemap.xml Sitemap: https://docs.ultralytics.com/fr/sitemap.xml
Sitemap: https://docs.ultralytics.com/hi/sitemap.xml
Sitemap: https://docs.ultralytics.com/it/sitemap.xml Sitemap: https://docs.ultralytics.com/it/sitemap.xml
Sitemap: https://docs.ultralytics.com/ja/sitemap.xml Sitemap: https://docs.ultralytics.com/ja/sitemap.xml
Sitemap: https://docs.ultralytics.com/ko/sitemap.xml Sitemap: https://docs.ultralytics.com/ko/sitemap.xml
Sitemap: https://docs.ultralytics.com/nl/sitemap.xml
Sitemap: https://docs.ultralytics.com/pt/sitemap.xml Sitemap: https://docs.ultralytics.com/pt/sitemap.xml
Sitemap: https://docs.ultralytics.com/ru/sitemap.xml Sitemap: https://docs.ultralytics.com/ru/sitemap.xml
Sitemap: https://docs.ultralytics.com/tr/sitemap.xml Sitemap: https://docs.ultralytics.com/tr/sitemap.xml

@ -49,6 +49,9 @@ elseif (APPLE)
# set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-osx-x64-${ONNXRUNTIME_VERSION}") # set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-osx-x64-${ONNXRUNTIME_VERSION}")
# Apple Universal binary # Apple Universal binary
# set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-osx-universal2-${ONNXRUNTIME_VERSION}") # set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-osx-universal2-${ONNXRUNTIME_VERSION}")
else ()
message(SEND_ERROR "Variable ONNXRUNTIME_ROOT is not set properly. Please check if your cmake project \
is not compiled with `-D WIN32=TRUE`, `-D LINUX=TRUE`, or `-D APPLE=TRUE`!")
endif () endif ()
include_directories(${PROJECT_NAME} ${ONNXRUNTIME_ROOT}/include) include_directories(${PROJECT_NAME} ${ONNXRUNTIME_ROOT}/include)

@ -82,6 +82,19 @@ Note (2): Due to ONNX Runtime, we need to use CUDA 11 and cuDNN 8. Keep in mind
cmake .. cmake ..
``` ```
**Notice**:
If you encounter an error indicating that the `ONNXRUNTIME_ROOT` variable is not set correctly, you can resolve this by building the project using the appropriate command tailored to your system.
```console
# compiled in a win32 system
cmake -D WIN32=TRUE ..
# compiled in a linux system
cmake -D LINUX=TRUE ..
# compiled in an apple system
cmake -D APPLE=TRUE ..
```
5. Build the project: 5. Build the project:
```console ```console

@ -11,7 +11,7 @@
"\n", "\n",
"<div align=\"center\">\n", "<div align=\"center\">\n",
"\n", "\n",
"[中文](https://docs.ultralytics.com/zh/hub/) | [한국어](https://docs.ultralytics.com/ko/hub/) | [日本語](https://docs.ultralytics.com/ja/hub/) | [Русский](https://docs.ultralytics.com/ru/hub/) | [Deutsch](https://docs.ultralytics.com/de/hub/) | [Français](https://docs.ultralytics.com/fr/hub/) | [Español](https://docs.ultralytics.com/es/hub/) | [Português](https://docs.ultralytics.com/pt/hub/) | [Türkçe](https://docs.ultralytics.com/tr/hub/) | [Tiếng Việt](https://docs.ultralytics.com/vi/hub/) | [ि](https://docs.ultralytics.com/hi/hub/) | [العربية](https://docs.ultralytics.com/ar/hub/)\n", "[中文](https://docs.ultralytics.com/zh/hub/) | [한국어](https://docs.ultralytics.com/ko/hub/) | [日本語](https://docs.ultralytics.com/ja/hub/) | [Русский](https://docs.ultralytics.com/ru/hub/) | [Deutsch](https://docs.ultralytics.com/de/hub/) | [Français](https://docs.ultralytics.com/fr/hub/) | [Español](https://docs.ultralytics.com/es/hub/) | [Português](https://docs.ultralytics.com/pt/hub/) | [Türkçe](https://docs.ultralytics.com/tr/hub/) | [Tiếng Việt](https://docs.ultralytics.com/vi/hub/) | [العربية](https://docs.ultralytics.com/ar/hub/)\n",
"\n", "\n",
" <a href=\"https://github.com/ultralytics/hub/actions/workflows/ci.yaml\">\n", " <a href=\"https://github.com/ultralytics/hub/actions/workflows/ci.yaml\">\n",
" <img src=\"https://github.com/ultralytics/hub/actions/workflows/ci.yaml/badge.svg\" alt=\"CI CPU\"></a>\n", " <img src=\"https://github.com/ultralytics/hub/actions/workflows/ci.yaml/badge.svg\" alt=\"CI CPU\"></a>\n",

@ -171,6 +171,12 @@ split_before_first_argument = false
[tool.ruff] [tool.ruff]
line-length = 120 line-length = 120
[tool.ruff.format]
docstring-code-format = true
[tool.ruff.lint.pydocstyle]
convention = "google"
[tool.docformatter] [tool.docformatter]
wrap-summaries = 120 wrap-summaries = 120
wrap-descriptions = 120 wrap-descriptions = 120

@ -1,6 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license # Ultralytics YOLO 🚀, AGPL-3.0 license
__version__ = "8.2.79" __version__ = "8.2.81"
import os import os

@ -198,15 +198,15 @@ def cfg2dict(cfg):
Examples: Examples:
Convert a YAML file path to a dictionary: Convert a YAML file path to a dictionary:
>>> config_dict = cfg2dict('config.yaml') >>> config_dict = cfg2dict("config.yaml")
Convert a SimpleNamespace to a dictionary: Convert a SimpleNamespace to a dictionary:
>>> from types import SimpleNamespace >>> from types import SimpleNamespace
>>> config_sn = SimpleNamespace(param1='value1', param2='value2') >>> config_sn = SimpleNamespace(param1="value1", param2="value2")
>>> config_dict = cfg2dict(config_sn) >>> config_dict = cfg2dict(config_sn)
Pass through an already existing dictionary: Pass through an already existing dictionary:
>>> config_dict = cfg2dict({'param1': 'value1', 'param2': 'value2'}) >>> config_dict = cfg2dict({"param1": "value1", "param2": "value2"})
Notes: Notes:
- If cfg is a path or string, it's loaded as YAML and converted to a dictionary. - If cfg is a path or string, it's loaded as YAML and converted to a dictionary.
@ -235,7 +235,7 @@ def get_cfg(cfg: Union[str, Path, Dict, SimpleNamespace] = DEFAULT_CFG_DICT, ove
Examples: Examples:
>>> from ultralytics.cfg import get_cfg >>> from ultralytics.cfg import get_cfg
>>> config = get_cfg() # Load default configuration >>> config = get_cfg() # Load default configuration
>>> config = get_cfg('path/to/config.yaml', overrides={'epochs': 50, 'batch_size': 16}) >>> config = get_cfg("path/to/config.yaml", overrides={"epochs": 50, "batch_size": 16})
Notes: Notes:
- If both `cfg` and `overrides` are provided, the values in `overrides` will take precedence. - If both `cfg` and `overrides` are provided, the values in `overrides` will take precedence.
@ -282,10 +282,10 @@ def check_cfg(cfg, hard=True):
Examples: Examples:
>>> config = { >>> config = {
... 'epochs': 50, # valid integer ... "epochs": 50, # valid integer
... 'lr0': 0.01, # valid float ... "lr0": 0.01, # valid float
... 'momentum': 1.2, # invalid float (out of 0.0-1.0 range) ... "momentum": 1.2, # invalid float (out of 0.0-1.0 range)
... 'save': 'true', # invalid bool ... "save": "true", # invalid bool
... } ... }
>>> check_cfg(config, hard=False) >>> check_cfg(config, hard=False)
>>> print(config) >>> print(config)
@ -345,7 +345,7 @@ def get_save_dir(args, name=None):
Examples: Examples:
>>> from types import SimpleNamespace >>> from types import SimpleNamespace
>>> args = SimpleNamespace(project='my_project', task='detect', mode='train', exist_ok=True) >>> args = SimpleNamespace(project="my_project", task="detect", mode="train", exist_ok=True)
>>> save_dir = get_save_dir(args) >>> save_dir = get_save_dir(args)
>>> print(save_dir) >>> print(save_dir)
my_project/detect/train my_project/detect/train
@ -413,8 +413,8 @@ def check_dict_alignment(base: Dict, custom: Dict, e=None):
SystemExit: If mismatched keys are found between the custom and base dictionaries. SystemExit: If mismatched keys are found between the custom and base dictionaries.
Examples: Examples:
>>> base_cfg = {'epochs': 50, 'lr0': 0.01, 'batch_size': 16} >>> base_cfg = {"epochs": 50, "lr0": 0.01, "batch_size": 16}
>>> custom_cfg = {'epoch': 100, 'lr': 0.02, 'batch_size': 32} >>> custom_cfg = {"epoch": 100, "lr": 0.02, "batch_size": 32}
>>> try: >>> try:
... check_dict_alignment(base_cfg, custom_cfg) ... check_dict_alignment(base_cfg, custom_cfg)
... except SystemExit: ... except SystemExit:

@ -21,7 +21,7 @@ def auto_annotate(data, det_model="yolov8x.pt", sam_model="sam_b.pt", device="",
Examples: Examples:
>>> from ultralytics.data.annotator import auto_annotate >>> from ultralytics.data.annotator import auto_annotate
>>> auto_annotate(data='ultralytics/assets', det_model='yolov8n.pt', sam_model='mobile_sam.pt') >>> auto_annotate(data="ultralytics/assets", det_model="yolov8n.pt", sam_model="mobile_sam.pt")
Notes: Notes:
- The function creates a new directory for output if not specified. - The function creates a new directory for output if not specified.

@ -38,7 +38,7 @@ class BaseTransform:
Examples: Examples:
>>> transform = BaseTransform() >>> transform = BaseTransform()
>>> labels = {'image': np.array(...), 'instances': [...], 'semantic': np.array(...)} >>> labels = {"image": np.array(...), "instances": [...], "semantic": np.array(...)}
>>> transformed_labels = transform(labels) >>> transformed_labels = transform(labels)
""" """
@ -93,7 +93,7 @@ class BaseTransform:
Examples: Examples:
>>> transform = BaseTransform() >>> transform = BaseTransform()
>>> labels = {'instances': Instances(xyxy=torch.rand(5, 4), cls=torch.randint(0, 80, (5,)))} >>> labels = {"instances": Instances(xyxy=torch.rand(5, 4), cls=torch.randint(0, 80, (5,)))}
>>> transformed_labels = transform.apply_instances(labels) >>> transformed_labels = transform.apply_instances(labels)
""" """
pass pass
@ -135,7 +135,7 @@ class BaseTransform:
Examples: Examples:
>>> transform = BaseTransform() >>> transform = BaseTransform()
>>> labels = {'img': np.random.rand(640, 640, 3), 'instances': []} >>> labels = {"img": np.random.rand(640, 640, 3), "instances": []}
>>> transformed_labels = transform(labels) >>> transformed_labels = transform(labels)
""" """
self.apply_image(labels) self.apply_image(labels)
@ -338,6 +338,7 @@ class BaseMixTransform:
... def _mix_transform(self, labels): ... def _mix_transform(self, labels):
... # Implement custom mix logic here ... # Implement custom mix logic here
... return labels ... return labels
...
... def get_indexes(self): ... def get_indexes(self):
... return [random.randint(0, len(self.dataset) - 1) for _ in range(3)] ... return [random.randint(0, len(self.dataset) - 1) for _ in range(3)]
>>> dataset = YourDataset() >>> dataset = YourDataset()
@ -421,7 +422,7 @@ class BaseMixTransform:
Examples: Examples:
>>> transform = BaseMixTransform(dataset) >>> transform = BaseMixTransform(dataset)
>>> labels = {'image': img, 'bboxes': boxes, 'mix_labels': [{'image': img2, 'bboxes': boxes2}]} >>> labels = {"image": img, "bboxes": boxes, "mix_labels": [{"image": img2, "bboxes": boxes2}]}
>>> augmented_labels = transform._mix_transform(labels) >>> augmented_labels = transform._mix_transform(labels)
""" """
raise NotImplementedError raise NotImplementedError
@ -456,20 +457,17 @@ class BaseMixTransform:
Examples: Examples:
>>> labels = { >>> labels = {
... 'texts': [['cat'], ['dog']], ... "texts": [["cat"], ["dog"]],
... 'cls': torch.tensor([[0], [1]]), ... "cls": torch.tensor([[0], [1]]),
... 'mix_labels': [{ ... "mix_labels": [{"texts": [["bird"], ["fish"]], "cls": torch.tensor([[0], [1]])}],
... 'texts': [['bird'], ['fish']],
... 'cls': torch.tensor([[0], [1]])
... }]
... } ... }
>>> updated_labels = self._update_label_text(labels) >>> updated_labels = self._update_label_text(labels)
>>> print(updated_labels['texts']) >>> print(updated_labels["texts"])
[['cat'], ['dog'], ['bird'], ['fish']] [['cat'], ['dog'], ['bird'], ['fish']]
>>> print(updated_labels['cls']) >>> print(updated_labels["cls"])
tensor([[0], tensor([[0],
[1]]) [1]])
>>> print(updated_labels['mix_labels'][0]['cls']) >>> print(updated_labels["mix_labels"][0]["cls"])
tensor([[2], tensor([[2],
[3]]) [3]])
""" """
@ -616,9 +614,12 @@ class Mosaic(BaseMixTransform):
Examples: Examples:
>>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=3) >>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=3)
>>> labels = {'img': np.random.rand(480, 640, 3), 'mix_labels': [{'img': np.random.rand(480, 640, 3)} for _ in range(2)]} >>> labels = {
... "img": np.random.rand(480, 640, 3),
... "mix_labels": [{"img": np.random.rand(480, 640, 3)} for _ in range(2)],
... }
>>> result = mosaic._mosaic3(labels) >>> result = mosaic._mosaic3(labels)
>>> print(result['img'].shape) >>> print(result["img"].shape)
(640, 640, 3) (640, 640, 3)
""" """
mosaic_labels = [] mosaic_labels = []
@ -670,9 +671,10 @@ class Mosaic(BaseMixTransform):
Examples: Examples:
>>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=4) >>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=4)
>>> labels = {"img": np.random.rand(480, 640, 3), "mix_labels": [ >>> labels = {
... {"img": np.random.rand(480, 640, 3)} for _ in range(3) ... "img": np.random.rand(480, 640, 3),
... ]} ... "mix_labels": [{"img": np.random.rand(480, 640, 3)} for _ in range(3)],
... }
>>> result = mosaic._mosaic4(labels) >>> result = mosaic._mosaic4(labels)
>>> assert result["img"].shape == (1280, 1280, 3) >>> assert result["img"].shape == (1280, 1280, 3)
""" """
@ -734,7 +736,7 @@ class Mosaic(BaseMixTransform):
>>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=9) >>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=9)
>>> input_labels = dataset[0] >>> input_labels = dataset[0]
>>> mosaic_result = mosaic._mosaic9(input_labels) >>> mosaic_result = mosaic._mosaic9(input_labels)
>>> mosaic_image = mosaic_result['img'] >>> mosaic_image = mosaic_result["img"]
""" """
mosaic_labels = [] mosaic_labels = []
s = self.imgsz s = self.imgsz
@ -898,7 +900,7 @@ class MixUp(BaseMixTransform):
Examples: Examples:
>>> from ultralytics.data.dataset import YOLODataset >>> from ultralytics.data.dataset import YOLODataset
>>> dataset = YOLODataset('path/to/data.yaml') >>> dataset = YOLODataset("path/to/data.yaml")
>>> mixup = MixUp(dataset, pre_transform=None, p=0.5) >>> mixup = MixUp(dataset, pre_transform=None, p=0.5)
""" """
super().__init__(dataset=dataset, pre_transform=pre_transform, p=p) super().__init__(dataset=dataset, pre_transform=pre_transform, p=p)
@ -974,10 +976,10 @@ class RandomPerspective:
Examples: Examples:
>>> transform = RandomPerspective(degrees=10, translate=0.1, scale=0.1, shear=10) >>> transform = RandomPerspective(degrees=10, translate=0.1, scale=0.1, shear=10)
>>> image = np.random.randint(0, 255, (640, 640, 3), dtype=np.uint8) >>> image = np.random.randint(0, 255, (640, 640, 3), dtype=np.uint8)
>>> labels = {'img': image, 'cls': np.array([0, 1]), 'instances': Instances(...)} >>> labels = {"img": image, "cls": np.array([0, 1]), "instances": Instances(...)}
>>> result = transform(labels) >>> result = transform(labels)
>>> transformed_image = result['img'] >>> transformed_image = result["img"]
>>> transformed_instances = result['instances'] >>> transformed_instances = result["instances"]
""" """
def __init__( def __init__(
@ -1209,12 +1211,12 @@ class RandomPerspective:
>>> transform = RandomPerspective() >>> transform = RandomPerspective()
>>> image = np.random.randint(0, 255, (640, 640, 3), dtype=np.uint8) >>> image = np.random.randint(0, 255, (640, 640, 3), dtype=np.uint8)
>>> labels = { >>> labels = {
... 'img': image, ... "img": image,
... 'cls': np.array([0, 1, 2]), ... "cls": np.array([0, 1, 2]),
... 'instances': Instances(bboxes=np.array([[10, 10, 50, 50], [100, 100, 150, 150]])) ... "instances": Instances(bboxes=np.array([[10, 10, 50, 50], [100, 100, 150, 150]])),
... } ... }
>>> result = transform(labels) >>> result = transform(labels)
>>> assert result['img'].shape[:2] == result['resized_shape'] >>> assert result["img"].shape[:2] == result["resized_shape"]
""" """
if self.pre_transform and "mosaic_border" not in labels: if self.pre_transform and "mosaic_border" not in labels:
labels = self.pre_transform(labels) labels = self.pre_transform(labels)
@ -1358,9 +1360,9 @@ class RandomHSV:
Examples: Examples:
>>> hsv_augmenter = RandomHSV(hgain=0.5, sgain=0.5, vgain=0.5) >>> hsv_augmenter = RandomHSV(hgain=0.5, sgain=0.5, vgain=0.5)
>>> labels = {'img': np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)} >>> labels = {"img": np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)}
>>> hsv_augmenter(labels) >>> hsv_augmenter(labels)
>>> augmented_img = labels['img'] >>> augmented_img = labels["img"]
""" """
img = labels["img"] img = labels["img"]
if self.hgain or self.sgain or self.vgain: if self.hgain or self.sgain or self.vgain:
@ -1394,7 +1396,7 @@ class RandomFlip:
__call__: Applies the random flip transformation to an image and its annotations. __call__: Applies the random flip transformation to an image and its annotations.
Examples: Examples:
>>> transform = RandomFlip(p=0.5, direction='horizontal') >>> transform = RandomFlip(p=0.5, direction="horizontal")
>>> result = transform({"img": image, "instances": instances}) >>> result = transform({"img": image, "instances": instances})
>>> flipped_image = result["img"] >>> flipped_image = result["img"]
>>> flipped_instances = result["instances"] >>> flipped_instances = result["instances"]
@ -1416,8 +1418,8 @@ class RandomFlip:
AssertionError: If direction is not 'horizontal' or 'vertical', or if p is not between 0 and 1. AssertionError: If direction is not 'horizontal' or 'vertical', or if p is not between 0 and 1.
Examples: Examples:
>>> flip = RandomFlip(p=0.5, direction='horizontal') >>> flip = RandomFlip(p=0.5, direction="horizontal")
>>> flip = RandomFlip(p=0.7, direction='vertical', flip_idx=[1, 0, 3, 2, 5, 4]) >>> flip = RandomFlip(p=0.7, direction="vertical", flip_idx=[1, 0, 3, 2, 5, 4])
""" """
assert direction in {"horizontal", "vertical"}, f"Support direction `horizontal` or `vertical`, got {direction}" assert direction in {"horizontal", "vertical"}, f"Support direction `horizontal` or `vertical`, got {direction}"
assert 0 <= p <= 1.0, f"The probability should be in range [0, 1], but got {p}." assert 0 <= p <= 1.0, f"The probability should be in range [0, 1], but got {p}."
@ -1446,8 +1448,8 @@ class RandomFlip:
'instances' (ultralytics.utils.instance.Instances): Updated instances matching the flipped image. 'instances' (ultralytics.utils.instance.Instances): Updated instances matching the flipped image.
Examples: Examples:
>>> labels = {'img': np.random.rand(640, 640, 3), 'instances': Instances(...)} >>> labels = {"img": np.random.rand(640, 640, 3), "instances": Instances(...)}
>>> random_flip = RandomFlip(p=0.5, direction='horizontal') >>> random_flip = RandomFlip(p=0.5, direction="horizontal")
>>> flipped_labels = random_flip(labels) >>> flipped_labels = random_flip(labels)
""" """
img = labels["img"] img = labels["img"]
@ -1493,8 +1495,8 @@ class LetterBox:
Examples: Examples:
>>> transform = LetterBox(new_shape=(640, 640)) >>> transform = LetterBox(new_shape=(640, 640))
>>> result = transform(labels) >>> result = transform(labels)
>>> resized_img = result['img'] >>> resized_img = result["img"]
>>> updated_instances = result['instances'] >>> updated_instances = result["instances"]
""" """
def __init__(self, new_shape=(640, 640), auto=False, scaleFill=False, scaleup=True, center=True, stride=32): def __init__(self, new_shape=(640, 640), auto=False, scaleFill=False, scaleup=True, center=True, stride=32):
@ -1548,9 +1550,9 @@ class LetterBox:
Examples: Examples:
>>> letterbox = LetterBox(new_shape=(640, 640)) >>> letterbox = LetterBox(new_shape=(640, 640))
>>> result = letterbox(labels={'img': np.zeros((480, 640, 3)), 'instances': Instances(...)}) >>> result = letterbox(labels={"img": np.zeros((480, 640, 3)), "instances": Instances(...)})
>>> resized_img = result['img'] >>> resized_img = result["img"]
>>> updated_instances = result['instances'] >>> updated_instances = result["instances"]
""" """
if labels is None: if labels is None:
labels = {} labels = {}
@ -1616,7 +1618,7 @@ class LetterBox:
Examples: Examples:
>>> letterbox = LetterBox(new_shape=(640, 640)) >>> letterbox = LetterBox(new_shape=(640, 640))
>>> labels = {'instances': Instances(...)} >>> labels = {"instances": Instances(...)}
>>> ratio = (0.5, 0.5) >>> ratio = (0.5, 0.5)
>>> padw, padh = 10, 20 >>> padw, padh = 10, 20
>>> updated_labels = letterbox._update_labels(labels, ratio, padw, padh) >>> updated_labels = letterbox._update_labels(labels, ratio, padw, padh)
@ -1643,7 +1645,7 @@ class CopyPaste:
Examples: Examples:
>>> copypaste = CopyPaste(p=0.5) >>> copypaste = CopyPaste(p=0.5)
>>> augmented_labels = copypaste(labels) >>> augmented_labels = copypaste(labels)
>>> augmented_image = augmented_labels['img'] >>> augmented_image = augmented_labels["img"]
""" """
def __init__(self, p=0.5) -> None: def __init__(self, p=0.5) -> None:
@ -1680,7 +1682,7 @@ class CopyPaste:
(Dict): Dictionary with augmented image and updated instances under 'img', 'cls', and 'instances' keys. (Dict): Dictionary with augmented image and updated instances under 'img', 'cls', and 'instances' keys.
Examples: Examples:
>>> labels = {'img': np.random.rand(640, 640, 3), 'cls': np.array([0, 1, 2]), 'instances': Instances(...)} >>> labels = {"img": np.random.rand(640, 640, 3), "cls": np.array([0, 1, 2]), "instances": Instances(...)}
>>> augmenter = CopyPaste(p=0.5) >>> augmenter = CopyPaste(p=0.5)
>>> augmented_labels = augmenter(labels) >>> augmented_labels = augmenter(labels)
""" """
@ -1765,8 +1767,8 @@ class Albumentations:
Examples: Examples:
>>> transform = Albumentations(p=0.5) >>> transform = Albumentations(p=0.5)
>>> augmented = transform(image=image, bboxes=bboxes, class_labels=classes) >>> augmented = transform(image=image, bboxes=bboxes, class_labels=classes)
>>> augmented_image = augmented['image'] >>> augmented_image = augmented["image"]
>>> augmented_bboxes = augmented['bboxes'] >>> augmented_bboxes = augmented["bboxes"]
Notes: Notes:
- Requires Albumentations version 1.0.3 or higher. - Requires Albumentations version 1.0.3 or higher.
@ -1871,7 +1873,7 @@ class Albumentations:
>>> labels = { >>> labels = {
... "img": np.random.rand(640, 640, 3), ... "img": np.random.rand(640, 640, 3),
... "cls": np.array([0, 1]), ... "cls": np.array([0, 1]),
... "instances": Instances(bboxes=np.array([[0, 0, 1, 1], [0.5, 0.5, 0.8, 0.8]])) ... "instances": Instances(bboxes=np.array([[0, 0, 1, 1], [0.5, 0.5, 0.8, 0.8]])),
... } ... }
>>> augmented = transform(labels) >>> augmented = transform(labels)
>>> assert augmented["img"].shape == (640, 640, 3) >>> assert augmented["img"].shape == (640, 640, 3)
@ -1927,11 +1929,11 @@ class Format:
_format_segments: Converts polygon points to bitmap masks. _format_segments: Converts polygon points to bitmap masks.
Examples: Examples:
>>> formatter = Format(bbox_format='xywh', normalize=True, return_mask=True) >>> formatter = Format(bbox_format="xywh", normalize=True, return_mask=True)
>>> formatted_labels = formatter(labels) >>> formatted_labels = formatter(labels)
>>> img = formatted_labels['img'] >>> img = formatted_labels["img"]
>>> bboxes = formatted_labels['bboxes'] >>> bboxes = formatted_labels["bboxes"]
>>> masks = formatted_labels['masks'] >>> masks = formatted_labels["masks"]
""" """
def __init__( def __init__(
@ -1975,7 +1977,7 @@ class Format:
bgr (float): The probability to return BGR images. bgr (float): The probability to return BGR images.
Examples: Examples:
>>> format = Format(bbox_format='xyxy', return_mask=True, return_keypoint=False) >>> format = Format(bbox_format="xyxy", return_mask=True, return_keypoint=False)
>>> print(format.bbox_format) >>> print(format.bbox_format)
xyxy xyxy
""" """
@ -2013,8 +2015,8 @@ class Format:
- 'batch_idx': Batch index tensor (if batch_idx is True). - 'batch_idx': Batch index tensor (if batch_idx is True).
Examples: Examples:
>>> formatter = Format(bbox_format='xywh', normalize=True, return_mask=True) >>> formatter = Format(bbox_format="xywh", normalize=True, return_mask=True)
>>> labels = {'img': np.random.rand(640, 640, 3), 'cls': np.array([0, 1]), 'instances': Instances(...)} >>> labels = {"img": np.random.rand(640, 640, 3), "cls": np.array([0, 1]), "instances": Instances(...)}
>>> formatted_labels = formatter(labels) >>> formatted_labels = formatter(labels)
>>> print(formatted_labels.keys()) >>> print(formatted_labels.keys())
""" """
@ -2275,8 +2277,8 @@ def v8_transforms(dataset, imgsz, hyp, stretch=False):
Examples: Examples:
>>> from ultralytics.data.dataset import YOLODataset >>> from ultralytics.data.dataset import YOLODataset
>>> dataset = YOLODataset(img_path='path/to/images', imgsz=640) >>> dataset = YOLODataset(img_path="path/to/images", imgsz=640)
>>> hyp = {'mosaic': 1.0, 'copy_paste': 0.5, 'degrees': 10.0, 'translate': 0.2, 'scale': 0.9} >>> hyp = {"mosaic": 1.0, "copy_paste": 0.5, "degrees": 10.0, "translate": 0.2, "scale": 0.9}
>>> transforms = v8_transforms(dataset, imgsz=640, hyp=hyp) >>> transforms = v8_transforms(dataset, imgsz=640, hyp=hyp)
>>> augmented_data = transforms(dataset[0]) >>> augmented_data = transforms(dataset[0])
""" """
@ -2343,7 +2345,7 @@ def classify_transforms(
Examples: Examples:
>>> transforms = classify_transforms(size=224) >>> transforms = classify_transforms(size=224)
>>> img = Image.open('path/to/image.jpg') >>> img = Image.open("path/to/image.jpg")
>>> transformed_img = transforms(img) >>> transformed_img = transforms(img)
""" """
import torchvision.transforms as T # scope for faster 'import ultralytics' import torchvision.transforms as T # scope for faster 'import ultralytics'
@ -2415,7 +2417,7 @@ def classify_augmentations(
(torchvision.transforms.Compose): A composition of image augmentation transforms. (torchvision.transforms.Compose): A composition of image augmentation transforms.
Examples: Examples:
>>> transforms = classify_augmentations(size=224, auto_augment='randaugment') >>> transforms = classify_augmentations(size=224, auto_augment="randaugment")
>>> augmented_image = transforms(original_image) >>> augmented_image = transforms(original_image)
""" """
# Transforms to apply if Albumentations not installed # Transforms to apply if Albumentations not installed

@ -298,10 +298,10 @@ class BaseDataset(Dataset):
im_file=im_file, im_file=im_file,
shape=shape, # format: (height, width) shape=shape, # format: (height, width)
cls=cls, cls=cls,
bboxes=bboxes, # xywh bboxes=bboxes, # xywh
segments=segments, # xy segments=segments, # xy
keypoints=keypoints, # xy keypoints=keypoints, # xy
normalized=True, # or False normalized=True, # or False
bbox_format="xyxy", # or xywh, ltwh bbox_format="xyxy", # or xywh, ltwh
) )
``` ```

@ -123,8 +123,8 @@ def coco80_to_coco91_class():
```python ```python
import numpy as np import numpy as np
a = np.loadtxt('data/coco.names', dtype='str', delimiter='\n') a = np.loadtxt("data/coco.names", dtype="str", delimiter="\n")
b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter='\n') b = np.loadtxt("data/coco_paper.names", dtype="str", delimiter="\n")
x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco
x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet
``` ```
@ -236,8 +236,8 @@ def convert_coco(
```python ```python
from ultralytics.data.converter import convert_coco from ultralytics.data.converter import convert_coco
convert_coco('../datasets/coco/annotations/', use_segments=True, use_keypoints=False, cls91to80=True) convert_coco("../datasets/coco/annotations/", use_segments=True, use_keypoints=False, cls91to80=True)
convert_coco('../datasets/lvis/annotations/', use_segments=True, use_keypoints=False, cls91to80=False, lvis=True) convert_coco("../datasets/lvis/annotations/", use_segments=True, use_keypoints=False, cls91to80=False, lvis=True)
``` ```
Output: Output:
@ -351,7 +351,7 @@ def convert_segment_masks_to_yolo_seg(masks_dir, output_dir, classes):
from ultralytics.data.converter import convert_segment_masks_to_yolo_seg 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 # The classes here is the total classes in the dataset, for COCO dataset we have 80 classes
convert_segment_masks_to_yolo_seg('path/to/masks_directory', 'path/to/output/directory', classes=80) convert_segment_masks_to_yolo_seg("path/to/masks_directory", "path/to/output/directory", classes=80)
``` ```
Notes: Notes:
@ -429,7 +429,7 @@ def convert_dota_to_yolo_obb(dota_root_path: str):
```python ```python
from ultralytics.data.converter import convert_dota_to_yolo_obb from ultralytics.data.converter import convert_dota_to_yolo_obb
convert_dota_to_yolo_obb('path/to/DOTA') convert_dota_to_yolo_obb("path/to/DOTA")
``` ```
Notes: Notes:

@ -163,7 +163,7 @@ class Explorer:
```python ```python
exp = Explorer() exp = Explorer()
exp.create_embeddings_table() exp.create_embeddings_table()
similar = exp.query(img='https://ultralytics.com/images/zidane.jpg') similar = exp.query(img="https://ultralytics.com/images/zidane.jpg")
``` ```
""" """
if self.table is None: if self.table is None:
@ -271,7 +271,7 @@ class Explorer:
```python ```python
exp = Explorer() exp = Explorer()
exp.create_embeddings_table() exp.create_embeddings_table()
similar = exp.get_similar(img='https://ultralytics.com/images/zidane.jpg') similar = exp.get_similar(img="https://ultralytics.com/images/zidane.jpg")
``` ```
""" """
assert return_type in {"pandas", "arrow"}, f"Return type should be `pandas` or `arrow`, but got {return_type}" assert return_type in {"pandas", "arrow"}, f"Return type should be `pandas` or `arrow`, but got {return_type}"
@ -306,7 +306,7 @@ class Explorer:
```python ```python
exp = Explorer() exp = Explorer()
exp.create_embeddings_table() exp.create_embeddings_table()
similar = exp.plot_similar(img='https://ultralytics.com/images/zidane.jpg') similar = exp.plot_similar(img="https://ultralytics.com/images/zidane.jpg")
``` ```
""" """
similar = self.get_similar(img, idx, limit, return_type="arrow") similar = self.get_similar(img, idx, limit, return_type="arrow")
@ -395,8 +395,8 @@ class Explorer:
exp.create_embeddings_table() exp.create_embeddings_table()
similarity_idx_plot = exp.plot_similarity_index() similarity_idx_plot = exp.plot_similarity_index()
similarity_idx_plot.show() # view image preview similarity_idx_plot.show() # view image preview
similarity_idx_plot.save('path/to/save/similarity_index_plot.png') # save contents to file similarity_idx_plot.save("path/to/save/similarity_index_plot.png") # save contents to file
``` ```
""" """
sim_idx = self.similarity_index(max_dist=max_dist, top_k=top_k, force=force) sim_idx = self.similarity_index(max_dist=max_dist, top_k=top_k, force=force)
@ -447,7 +447,7 @@ class Explorer:
```python ```python
exp = Explorer() exp = Explorer()
exp.create_embeddings_table() exp.create_embeddings_table()
answer = exp.ask_ai('Show images with 1 person and 2 dogs') answer = exp.ask_ai("Show images with 1 person and 2 dogs")
``` ```
""" """
result = prompt_sql_query(query) result = prompt_sql_query(query)

@ -438,11 +438,11 @@ class HUBDatasetStats:
```python ```python
from ultralytics.data.utils import HUBDatasetStats from ultralytics.data.utils import HUBDatasetStats
stats = HUBDatasetStats('path/to/coco8.zip', task='detect') # detect dataset stats = HUBDatasetStats("path/to/coco8.zip", task="detect") # detect dataset
stats = HUBDatasetStats('path/to/coco8-seg.zip', task='segment') # segment dataset stats = HUBDatasetStats("path/to/coco8-seg.zip", task="segment") # segment dataset
stats = HUBDatasetStats('path/to/coco8-pose.zip', task='pose') # pose dataset stats = HUBDatasetStats("path/to/coco8-pose.zip", task="pose") # pose dataset
stats = HUBDatasetStats('path/to/dota8.zip', task='obb') # OBB dataset stats = HUBDatasetStats("path/to/dota8.zip", task="obb") # OBB dataset
stats = HUBDatasetStats('path/to/imagenet10.zip', task='classify') # classification dataset stats = HUBDatasetStats("path/to/imagenet10.zip", task="classify") # classification dataset
stats.get_json(save=True) stats.get_json(save=True)
stats.process_images() stats.process_images()
@ -598,7 +598,7 @@ def compress_one_image(f, f_new=None, max_dim=1920, quality=50):
from pathlib import Path from pathlib import Path
from ultralytics.data.utils import compress_one_image from ultralytics.data.utils import compress_one_image
for f in Path('path/to/dataset').rglob('*.jpg'): for f in Path("path/to/dataset").rglob("*.jpg"):
compress_one_image(f) compress_one_image(f)
``` ```
""" """

@ -6,6 +6,7 @@ from typing import List, Union
import numpy as np import numpy as np
import torch import torch
from PIL import Image
from ultralytics.cfg import TASK2DATA, get_cfg, get_save_dir from ultralytics.cfg import TASK2DATA, get_cfg, get_save_dir
from ultralytics.engine.results import Results from ultralytics.engine.results import Results
@ -71,11 +72,11 @@ class Model(nn.Module):
Examples: Examples:
>>> from ultralytics import YOLO >>> from ultralytics import YOLO
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> results = model.predict('image.jpg') >>> results = model.predict("image.jpg")
>>> model.train(data='coco128.yaml', epochs=3) >>> model.train(data="coco128.yaml", epochs=3)
>>> metrics = model.val() >>> metrics = model.val()
>>> model.export(format='onnx') >>> model.export(format="onnx")
""" """
def __init__( def __init__(
@ -143,7 +144,7 @@ class Model(nn.Module):
def __call__( def __call__(
self, self,
source: Union[str, Path, int, list, tuple, np.ndarray, torch.Tensor] = None, source: Union[str, Path, int, Image.Image, list, tuple, np.ndarray, torch.Tensor] = None,
stream: bool = False, stream: bool = False,
**kwargs, **kwargs,
) -> list: ) -> list:
@ -165,8 +166,8 @@ class Model(nn.Module):
Results object. Results object.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> results = model('https://ultralytics.com/images/bus.jpg') >>> results = model("https://ultralytics.com/images/bus.jpg")
>>> for r in results: >>> for r in results:
... print(f"Detected {len(r)} objects in image") ... print(f"Detected {len(r)} objects in image")
""" """
@ -187,9 +188,9 @@ class Model(nn.Module):
(bool): True if the model string is a valid Triton Server URL, False otherwise. (bool): True if the model string is a valid Triton Server URL, False otherwise.
Examples: Examples:
>>> Model.is_triton_model('http://localhost:8000/v2/models/yolov8n') >>> Model.is_triton_model("http://localhost:8000/v2/models/yolov8n")
True True
>>> Model.is_triton_model('yolov8n.pt') >>> Model.is_triton_model("yolov8n.pt")
False False
""" """
from urllib.parse import urlsplit from urllib.parse import urlsplit
@ -252,7 +253,7 @@ class Model(nn.Module):
Examples: Examples:
>>> model = Model() >>> model = Model()
>>> model._new('yolov8n.yaml', task='detect', verbose=True) >>> model._new("yolov8n.yaml", task="detect", verbose=True)
""" """
cfg_dict = yaml_model_load(cfg) cfg_dict = yaml_model_load(cfg)
self.cfg = cfg self.cfg = cfg
@ -283,8 +284,8 @@ class Model(nn.Module):
Examples: Examples:
>>> model = Model() >>> model = Model()
>>> model._load('yolov8n.pt') >>> model._load("yolov8n.pt")
>>> model._load('path/to/weights.pth', task='detect') >>> model._load("path/to/weights.pth", task="detect")
""" """
if weights.lower().startswith(("https://", "http://", "rtsp://", "rtmp://", "tcp://")): if weights.lower().startswith(("https://", "http://", "rtsp://", "rtmp://", "tcp://")):
weights = checks.check_file(weights, download_dir=SETTINGS["weights_dir"]) # download and return local file weights = checks.check_file(weights, download_dir=SETTINGS["weights_dir"]) # download and return local file
@ -347,7 +348,7 @@ class Model(nn.Module):
AssertionError: If the model is not a PyTorch model. AssertionError: If the model is not a PyTorch model.
Examples: Examples:
>>> model = Model('yolov8n.pt') >>> model = Model("yolov8n.pt")
>>> model.reset_weights() >>> model.reset_weights()
""" """
self._check_is_pytorch_model() self._check_is_pytorch_model()
@ -376,8 +377,8 @@ class Model(nn.Module):
Examples: Examples:
>>> model = Model() >>> model = Model()
>>> model.load('yolov8n.pt') >>> model.load("yolov8n.pt")
>>> model.load(Path('path/to/weights.pt')) >>> model.load(Path("path/to/weights.pt"))
""" """
self._check_is_pytorch_model() self._check_is_pytorch_model()
if isinstance(weights, (str, Path)): if isinstance(weights, (str, Path)):
@ -401,8 +402,8 @@ class Model(nn.Module):
AssertionError: If the model is not a PyTorch model. AssertionError: If the model is not a PyTorch model.
Examples: Examples:
>>> model = Model('yolov8n.pt') >>> model = Model("yolov8n.pt")
>>> model.save('my_model.pt') >>> model.save("my_model.pt")
""" """
self._check_is_pytorch_model() self._check_is_pytorch_model()
from copy import deepcopy from copy import deepcopy
@ -438,7 +439,7 @@ class Model(nn.Module):
TypeError: If the model is not a PyTorch model. TypeError: If the model is not a PyTorch model.
Examples: Examples:
>>> model = Model('yolov8n.pt') >>> model = Model("yolov8n.pt")
>>> model.info() # Prints model summary >>> model.info() # Prints model summary
>>> info_list = model.info(detailed=True, verbose=False) # Returns detailed info as a list >>> info_list = model.info(detailed=True, verbose=False) # Returns detailed info as a list
""" """
@ -493,8 +494,8 @@ class Model(nn.Module):
AssertionError: If the model is not a PyTorch model. AssertionError: If the model is not a PyTorch model.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> image = 'https://ultralytics.com/images/bus.jpg' >>> image = "https://ultralytics.com/images/bus.jpg"
>>> embeddings = model.embed(image) >>> embeddings = model.embed(image)
>>> print(embeddings[0].shape) >>> print(embeddings[0].shape)
""" """
@ -504,7 +505,7 @@ class Model(nn.Module):
def predict( def predict(
self, self,
source: Union[str, Path, int, list, tuple, np.ndarray, torch.Tensor] = None, source: Union[str, Path, int, Image.Image, list, tuple, np.ndarray, torch.Tensor] = None,
stream: bool = False, stream: bool = False,
predictor=None, predictor=None,
**kwargs, **kwargs,
@ -517,7 +518,7 @@ class Model(nn.Module):
types of image sources and can operate in a streaming mode. types of image sources and can operate in a streaming mode.
Args: Args:
source (str | Path | int | List[str] | List[Path] | List[int] | np.ndarray | torch.Tensor): The source source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | List | Tuple): The source
of the image(s) to make predictions on. Accepts various types including file paths, URLs, PIL of the image(s) to make predictions on. Accepts various types including file paths, URLs, PIL
images, numpy arrays, and torch tensors. images, numpy arrays, and torch tensors.
stream (bool): If True, treats the input source as a continuous stream for predictions. stream (bool): If True, treats the input source as a continuous stream for predictions.
@ -530,8 +531,8 @@ class Model(nn.Module):
Results object. Results object.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> results = model.predict(source='path/to/image.jpg', conf=0.25) >>> results = model.predict(source="path/to/image.jpg", conf=0.25)
>>> for r in results: >>> for r in results:
... print(r.boxes.data) # print detection bounding boxes ... print(r.boxes.data) # print detection bounding boxes
@ -591,8 +592,8 @@ class Model(nn.Module):
AttributeError: If the predictor does not have registered trackers. AttributeError: If the predictor does not have registered trackers.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> results = model.track(source='path/to/video.mp4', show=True) >>> results = model.track(source="path/to/video.mp4", show=True)
>>> for r in results: >>> for r in results:
... print(r.boxes.id) # print tracking IDs ... print(r.boxes.id) # print tracking IDs
@ -634,8 +635,8 @@ class Model(nn.Module):
AssertionError: If the model is not a PyTorch model. AssertionError: If the model is not a PyTorch model.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> results = model.val(data='coco128.yaml', imgsz=640) >>> results = model.val(data="coco128.yaml", imgsz=640)
>>> print(results.box.map) # Print mAP50-95 >>> print(results.box.map) # Print mAP50-95
""" """
custom = {"rect": True} # method defaults custom = {"rect": True} # method defaults
@ -676,8 +677,8 @@ class Model(nn.Module):
AssertionError: If the model is not a PyTorch model. AssertionError: If the model is not a PyTorch model.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> results = model.benchmark(data='coco8.yaml', imgsz=640, half=True) >>> results = model.benchmark(data="coco8.yaml", imgsz=640, half=True)
>>> print(results) >>> print(results)
""" """
self._check_is_pytorch_model() self._check_is_pytorch_model()
@ -726,8 +727,8 @@ class Model(nn.Module):
RuntimeError: If the export process fails due to errors. RuntimeError: If the export process fails due to errors.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> model.export(format='onnx', dynamic=True, simplify=True) >>> model.export(format="onnx", dynamic=True, simplify=True)
'path/to/exported/model.onnx' 'path/to/exported/model.onnx'
""" """
self._check_is_pytorch_model() self._check_is_pytorch_model()
@ -781,8 +782,8 @@ class Model(nn.Module):
ModuleNotFoundError: If the HUB SDK is not installed. ModuleNotFoundError: If the HUB SDK is not installed.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> results = model.train(data='coco128.yaml', epochs=3) >>> results = model.train(data="coco128.yaml", epochs=3)
""" """
self._check_is_pytorch_model() self._check_is_pytorch_model()
if hasattr(self.session, "model") and self.session.model.id: # Ultralytics HUB session with loaded model if hasattr(self.session, "model") and self.session.model.id: # Ultralytics HUB session with loaded model
@ -846,7 +847,7 @@ class Model(nn.Module):
AssertionError: If the model is not a PyTorch model. AssertionError: If the model is not a PyTorch model.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> results = model.tune(use_ray=True, iterations=20) >>> results = model.tune(use_ray=True, iterations=20)
>>> print(results) >>> print(results)
""" """
@ -900,15 +901,15 @@ class Model(nn.Module):
initialized, it sets it up before retrieving the names. initialized, it sets it up before retrieving the names.
Returns: Returns:
(List[str]): A list of class names associated with the model. (Dict[int, str]): A dict of class names associated with the model.
Raises: Raises:
AttributeError: If the model or predictor does not have a 'names' attribute. AttributeError: If the model or predictor does not have a 'names' attribute.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> print(model.names) >>> print(model.names)
['person', 'bicycle', 'car', ...] {0: 'person', 1: 'bicycle', 2: 'car', ...}
""" """
from ultralytics.nn.autobackend import check_class_names from ultralytics.nn.autobackend import check_class_names
@ -956,7 +957,7 @@ class Model(nn.Module):
(object | None): The transform object of the model if available, otherwise None. (object | None): The transform object of the model if available, otherwise None.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> transforms = model.transforms >>> transforms = model.transforms
>>> if transforms: >>> if transforms:
... print(f"Model transforms: {transforms}") ... print(f"Model transforms: {transforms}")
@ -985,9 +986,9 @@ class Model(nn.Module):
Examples: Examples:
>>> def on_train_start(trainer): >>> def on_train_start(trainer):
... print("Training is starting!") ... print("Training is starting!")
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> model.add_callback("on_train_start", on_train_start) >>> model.add_callback("on_train_start", on_train_start)
>>> model.train(data='coco128.yaml', epochs=1) >>> model.train(data="coco128.yaml", epochs=1)
""" """
self.callbacks[event].append(func) self.callbacks[event].append(func)
@ -1004,9 +1005,9 @@ class Model(nn.Module):
recognized by the Ultralytics callback system. recognized by the Ultralytics callback system.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> model.add_callback('on_train_start', lambda: print('Training started')) >>> model.add_callback("on_train_start", lambda: print("Training started"))
>>> model.clear_callback('on_train_start') >>> model.clear_callback("on_train_start")
>>> # All callbacks for 'on_train_start' are now removed >>> # All callbacks for 'on_train_start' are now removed
Notes: Notes:
@ -1034,8 +1035,8 @@ class Model(nn.Module):
modifications, ensuring consistent behavior across different runs or experiments. modifications, ensuring consistent behavior across different runs or experiments.
Examples: Examples:
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> model.add_callback('on_train_start', custom_function) >>> model.add_callback("on_train_start", custom_function)
>>> model.reset_callbacks() >>> model.reset_callbacks()
# All callbacks are now reset to their default functions # All callbacks are now reset to their default functions
""" """
@ -1058,7 +1059,7 @@ class Model(nn.Module):
(dict): A new dictionary containing only the specified include keys from the input arguments. (dict): A new dictionary containing only the specified include keys from the input arguments.
Examples: Examples:
>>> original_args = {'imgsz': 640, 'data': 'coco.yaml', 'task': 'detect', 'batch': 16, 'epochs': 100} >>> original_args = {"imgsz": 640, "data": "coco.yaml", "task": "detect", "batch": 16, "epochs": 100}
>>> reset_args = Model._reset_ckpt_args(original_args) >>> reset_args = Model._reset_ckpt_args(original_args)
>>> print(reset_args) >>> print(reset_args)
{'imgsz': 640, 'data': 'coco.yaml', 'task': 'detect'} {'imgsz': 640, 'data': 'coco.yaml', 'task': 'detect'}
@ -1089,9 +1090,9 @@ class Model(nn.Module):
NotImplementedError: If the specified key is not supported for the current task. NotImplementedError: If the specified key is not supported for the current task.
Examples: Examples:
>>> model = Model(task='detect') >>> model = Model(task="detect")
>>> predictor = model._smart_load('predictor') >>> predictor = model._smart_load("predictor")
>>> trainer = model._smart_load('trainer') >>> trainer = model._smart_load("trainer")
Notes: Notes:
- This method is typically used internally by other methods of the Model class. - This method is typically used internally by other methods of the Model class.
@ -1127,8 +1128,8 @@ class Model(nn.Module):
Examples: Examples:
>>> model = Model() >>> model = Model()
>>> task_map = model.task_map >>> task_map = model.task_map
>>> detect_class_map = task_map['detect'] >>> detect_class_map = task_map["detect"]
>>> segment_class_map = task_map['segment'] >>> segment_class_map = task_map["segment"]
Note: Note:
The actual implementation of this method may vary depending on the specific tasks and The actual implementation of this method may vary depending on the specific tasks and

@ -143,7 +143,7 @@ class BaseTensor(SimpleClass):
Examples: Examples:
>>> base_tensor = BaseTensor(torch.randn(3, 4), orig_shape=(480, 640)) >>> base_tensor = BaseTensor(torch.randn(3, 4), orig_shape=(480, 640))
>>> cuda_tensor = base_tensor.to('cuda') >>> cuda_tensor = base_tensor.to("cuda")
>>> float16_tensor = base_tensor.to(dtype=torch.float16) >>> float16_tensor = base_tensor.to(dtype=torch.float16)
""" """
return self.__class__(torch.as_tensor(self.data).to(*args, **kwargs), self.orig_shape) return self.__class__(torch.as_tensor(self.data).to(*args, **kwargs), self.orig_shape)
@ -223,7 +223,7 @@ class Results(SimpleClass):
>>> for result in results: >>> for result in results:
... print(result.boxes) # Print detection boxes ... print(result.boxes) # Print detection boxes
... result.show() # Display the annotated image ... result.show() # Display the annotated image
... result.save(filename='result.jpg') # Save annotated image ... result.save(filename="result.jpg") # Save annotated image
""" """
def __init__( def __init__(
@ -280,7 +280,7 @@ class Results(SimpleClass):
(Results): A new Results object containing the specified subset of inference results. (Results): A new Results object containing the specified subset of inference results.
Examples: Examples:
>>> results = model('path/to/image.jpg') # Perform inference >>> results = model("path/to/image.jpg") # Perform inference
>>> single_result = results[0] # Get the first result >>> single_result = results[0] # Get the first result
>>> subset_results = results[1:4] # Get a slice of results >>> subset_results = results[1:4] # Get a slice of results
""" """
@ -319,7 +319,7 @@ class Results(SimpleClass):
obb (torch.Tensor | None): A tensor of shape (N, 5) containing oriented bounding box coordinates. obb (torch.Tensor | None): A tensor of shape (N, 5) containing oriented bounding box coordinates.
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> new_boxes = torch.tensor([[100, 100, 200, 200, 0.9, 0]]) >>> new_boxes = torch.tensor([[100, 100, 200, 200, 0.9, 0]])
>>> results[0].update(boxes=new_boxes) >>> results[0].update(boxes=new_boxes)
""" """
@ -370,7 +370,7 @@ class Results(SimpleClass):
(Results): A new Results object with all tensor attributes on CPU memory. (Results): A new Results object with all tensor attributes on CPU memory.
Examples: Examples:
>>> results = model('path/to/image.jpg') # Perform inference >>> results = model("path/to/image.jpg") # Perform inference
>>> cpu_result = results[0].cpu() # Move the first result to CPU >>> cpu_result = results[0].cpu() # Move the first result to CPU
>>> print(cpu_result.boxes.device) # Output: cpu >>> print(cpu_result.boxes.device) # Output: cpu
""" """
@ -384,7 +384,7 @@ class Results(SimpleClass):
(Results): A new Results object with all tensors converted to numpy arrays. (Results): A new Results object with all tensors converted to numpy arrays.
Examples: Examples:
>>> results = model('path/to/image.jpg') >>> results = model("path/to/image.jpg")
>>> numpy_result = results[0].numpy() >>> numpy_result = results[0].numpy()
>>> type(numpy_result.boxes.data) >>> type(numpy_result.boxes.data)
<class 'numpy.ndarray'> <class 'numpy.ndarray'>
@ -488,7 +488,7 @@ class Results(SimpleClass):
(np.ndarray): Annotated image as a numpy array. (np.ndarray): Annotated image as a numpy array.
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> for result in results: >>> for result in results:
... im = result.plot() ... im = result.plot()
... im.show() ... im.show()
@ -578,7 +578,7 @@ class Results(SimpleClass):
**kwargs (Any): Arbitrary keyword arguments to be passed to the `plot()` method. **kwargs (Any): Arbitrary keyword arguments to be passed to the `plot()` method.
Examples: Examples:
>>> results = model('path/to/image.jpg') >>> results = model("path/to/image.jpg")
>>> results[0].show() # Display the first result >>> results[0].show() # Display the first result
>>> for result in results: >>> for result in results:
... result.show() # Display all results ... result.show() # Display all results
@ -599,12 +599,12 @@ class Results(SimpleClass):
**kwargs (Any): Arbitrary keyword arguments to be passed to the `plot` method. **kwargs (Any): Arbitrary keyword arguments to be passed to the `plot` method.
Examples: Examples:
>>> results = model('path/to/image.jpg') >>> results = model("path/to/image.jpg")
>>> for result in results: >>> for result in results:
... result.save('annotated_image.jpg') ... result.save("annotated_image.jpg")
>>> # Or with custom plot arguments >>> # Or with custom plot arguments
>>> for result in results: >>> for result in results:
... result.save('annotated_image.jpg', conf=False, line_width=2) ... result.save("annotated_image.jpg", conf=False, line_width=2)
""" """
if not filename: if not filename:
filename = f"results_{Path(self.path).name}" filename = f"results_{Path(self.path).name}"
@ -623,7 +623,7 @@ class Results(SimpleClass):
number of detections per class. For classification tasks, it includes the top 5 class probabilities. number of detections per class. For classification tasks, it includes the top 5 class probabilities.
Examples: Examples:
>>> results = model('path/to/image.jpg') >>> results = model("path/to/image.jpg")
>>> for result in results: >>> for result in results:
... print(result.verbose()) ... print(result.verbose())
2 persons, 1 car, 3 traffic lights, 2 persons, 1 car, 3 traffic lights,
@ -660,7 +660,7 @@ class Results(SimpleClass):
Examples: Examples:
>>> from ultralytics import YOLO >>> from ultralytics import YOLO
>>> model = YOLO('yolov8n.pt') >>> model = YOLO("yolov8n.pt")
>>> results = model("path/to/image.jpg") >>> results = model("path/to/image.jpg")
>>> for result in results: >>> for result in results:
... result.save_txt("output.txt") ... result.save_txt("output.txt")
@ -757,7 +757,7 @@ class Results(SimpleClass):
task type (classification or detection) and available information (boxes, masks, keypoints). task type (classification or detection) and available information (boxes, masks, keypoints).
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> summary = results[0].summary() >>> summary = results[0].summary()
>>> print(summary) >>> print(summary)
""" """
@ -919,7 +919,7 @@ class Boxes(BaseTensor):
coordinates in [x1, y1, x2, y2] format, where n is the number of boxes. coordinates in [x1, y1, x2, y2] format, where n is the number of boxes.
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> boxes = results[0].boxes >>> boxes = results[0].boxes
>>> xyxy = boxes.xyxy >>> xyxy = boxes.xyxy
>>> print(xyxy) >>> print(xyxy)
@ -953,7 +953,7 @@ class Boxes(BaseTensor):
The shape is (N,), where N is the number of boxes. The shape is (N,), where N is the number of boxes.
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> boxes = results[0].boxes >>> boxes = results[0].boxes
>>> class_ids = boxes.cls >>> class_ids = boxes.cls
>>> print(class_ids) # tensor([0., 2., 1.]) >>> print(class_ids) # tensor([0., 2., 1.])
@ -970,7 +970,7 @@ class Boxes(BaseTensor):
otherwise None. Shape is (N,) where N is the number of boxes. otherwise None. Shape is (N,) where N is the number of boxes.
Examples: Examples:
>>> results = model.track('path/to/video.mp4') >>> results = model.track("path/to/video.mp4")
>>> for result in results: >>> for result in results:
... boxes = result.boxes ... boxes = result.boxes
... if boxes.is_track: ... if boxes.is_track:
@ -992,8 +992,8 @@ class Boxes(BaseTensor):
Convert bounding boxes from [x1, y1, x2, y2] format to [x, y, width, height] format. Convert bounding boxes from [x1, y1, x2, y2] format to [x, y, width, height] format.
Returns: Returns:
(torch.Tensor | numpy.ndarray): Boxes in [x, y, width, height] format, where x, y are the coordinates of (torch.Tensor | numpy.ndarray): Boxes in [x_center, y_center, width, height] format, where x_center, y_center are the coordinates of
the top-left corner of the bounding box, width, height are the dimensions of the bounding box and the the center point of the bounding box, width, height are the dimensions of the bounding box and the
shape of the returned tensor is (N, 4), where N is the number of boxes. shape of the returned tensor is (N, 4), where N is the number of boxes.
Examples: Examples:
@ -1116,7 +1116,7 @@ class Masks(BaseTensor):
mask contour. mask contour.
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> masks = results[0].masks >>> masks = results[0].masks
>>> normalized_coords = masks.xyn >>> normalized_coords = masks.xyn
>>> print(normalized_coords[0]) # Normalized coordinates of the first mask >>> print(normalized_coords[0]) # Normalized coordinates of the first mask
@ -1141,7 +1141,7 @@ class Masks(BaseTensor):
number of points in the segment. number of points in the segment.
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> masks = results[0].masks >>> masks = results[0].masks
>>> xy_coords = masks.xy >>> xy_coords = masks.xy
>>> print(len(xy_coords)) # Number of masks >>> print(len(xy_coords)) # Number of masks
@ -1223,7 +1223,7 @@ class Keypoints(BaseTensor):
the number of detections and K is the number of keypoints per detection. the number of detections and K is the number of keypoints per detection.
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> keypoints = results[0].keypoints >>> keypoints = results[0].keypoints
>>> xy = keypoints.xy >>> xy = keypoints.xy
>>> print(xy.shape) # (N, K, 2) >>> print(xy.shape) # (N, K, 2)
@ -1388,7 +1388,7 @@ class Probs(BaseTensor):
(torch.Tensor | numpy.ndarray): A tensor containing the confidence score of the top 1 class. (torch.Tensor | numpy.ndarray): A tensor containing the confidence score of the top 1 class.
Examples: Examples:
>>> results = model('image.jpg') # classify an image >>> results = model("image.jpg") # classify an image
>>> probs = results[0].probs # get classification probabilities >>> probs = results[0].probs # get classification probabilities
>>> top1_confidence = probs.top1conf # get confidence of top 1 class >>> top1_confidence = probs.top1conf # get confidence of top 1 class
>>> print(f"Top 1 class confidence: {top1_confidence.item():.4f}") >>> print(f"Top 1 class confidence: {top1_confidence.item():.4f}")
@ -1410,7 +1410,7 @@ class Probs(BaseTensor):
top 5 predicted classes, sorted in descending order of probability. top 5 predicted classes, sorted in descending order of probability.
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> probs = results[0].probs >>> probs = results[0].probs
>>> top5_conf = probs.top5conf >>> top5_conf = probs.top5conf
>>> print(top5_conf) # Prints confidence scores for top 5 classes >>> print(top5_conf) # Prints confidence scores for top 5 classes
@ -1497,7 +1497,7 @@ class OBB(BaseTensor):
[x_center, y_center, width, height, rotation]. The shape is (N, 5) where N is the number of boxes. [x_center, y_center, width, height, rotation]. The shape is (N, 5) where N is the number of boxes.
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> obb = results[0].obb >>> obb = results[0].obb
>>> xywhr = obb.xywhr >>> xywhr = obb.xywhr
>>> print(xywhr.shape) >>> print(xywhr.shape)
@ -1518,7 +1518,7 @@ class OBB(BaseTensor):
for N detections, where each score is in the range [0, 1]. for N detections, where each score is in the range [0, 1].
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> obb_result = results[0].obb >>> obb_result = results[0].obb
>>> confidence_scores = obb_result.conf >>> confidence_scores = obb_result.conf
>>> print(confidence_scores) >>> print(confidence_scores)
@ -1535,7 +1535,7 @@ class OBB(BaseTensor):
bounding box. The shape is (N,), where N is the number of boxes. bounding box. The shape is (N,), where N is the number of boxes.
Examples: Examples:
>>> results = model('image.jpg') >>> results = model("image.jpg")
>>> result = results[0] >>> result = results[0]
>>> obb = result.obb >>> obb = result.obb
>>> class_values = obb.cls >>> class_values = obb.cls
@ -1553,7 +1553,7 @@ class OBB(BaseTensor):
oriented bounding box. Returns None if tracking IDs are not available. oriented bounding box. Returns None if tracking IDs are not available.
Examples: Examples:
>>> results = model('image.jpg', tracker=True) # Run inference with tracking >>> results = model("image.jpg", tracker=True) # Run inference with tracking
>>> for result in results: >>> for result in results:
... if result.obb is not None: ... if result.obb is not None:
... track_ids = result.obb.id ... track_ids = result.obb.id
@ -1620,8 +1620,8 @@ class OBB(BaseTensor):
Examples: Examples:
>>> import torch >>> import torch
>>> from ultralytics import YOLO >>> from ultralytics import YOLO
>>> model = YOLO('yolov8n-obb.pt') >>> model = YOLO("yolov8n-obb.pt")
>>> results = model('path/to/image.jpg') >>> results = model("path/to/image.jpg")
>>> for result in results: >>> for result in results:
... obb = result.obb ... obb = result.obb
... if obb is not None: ... if obb is not None:

@ -56,8 +56,6 @@ from ultralytics.utils.torch_utils import (
class BaseTrainer: class BaseTrainer:
""" """
BaseTrainer.
A base class for creating trainers. A base class for creating trainers.
Attributes: Attributes:
@ -478,12 +476,16 @@ class BaseTrainer:
torch.cuda.empty_cache() torch.cuda.empty_cache()
self.run_callbacks("teardown") self.run_callbacks("teardown")
def read_results_csv(self):
"""Read results.csv into a dict using pandas."""
import pandas as pd # scope for faster 'import ultralytics'
return {k.strip(): v for k, v in pd.read_csv(self.csv).to_dict(orient="list").items()}
def save_model(self): def save_model(self):
"""Save model training checkpoints with additional metadata.""" """Save model training checkpoints with additional metadata."""
import io import io
import pandas as pd # scope for faster 'import ultralytics'
# Serialize ckpt to a byte buffer once (faster than repeated torch.save() calls) # Serialize ckpt to a byte buffer once (faster than repeated torch.save() calls)
buffer = io.BytesIO() buffer = io.BytesIO()
torch.save( torch.save(
@ -496,7 +498,7 @@ class BaseTrainer:
"optimizer": convert_optimizer_state_dict_to_fp16(deepcopy(self.optimizer.state_dict())), "optimizer": convert_optimizer_state_dict_to_fp16(deepcopy(self.optimizer.state_dict())),
"train_args": vars(self.args), # save as dict "train_args": vars(self.args), # save as dict
"train_metrics": {**self.metrics, **{"fitness": self.fitness}}, "train_metrics": {**self.metrics, **{"fitness": self.fitness}},
"train_results": {k.strip(): v for k, v in pd.read_csv(self.csv).to_dict(orient="list").items()}, "train_results": self.read_results_csv(),
"date": datetime.now().isoformat(), "date": datetime.now().isoformat(),
"version": __version__, "version": __version__,
"license": "AGPL-3.0 (https://ultralytics.com/license)", "license": "AGPL-3.0 (https://ultralytics.com/license)",
@ -646,6 +648,9 @@ class BaseTrainer:
if f.exists(): if f.exists():
strip_optimizer(f) # strip optimizers strip_optimizer(f) # strip optimizers
if f is self.best: if f is self.best:
if self.last.is_file(): # update best.pt train_metrics from last.pt
k = "train_results"
torch.save({**torch.load(self.best), **{k: torch.load(self.last)[k]}}, self.best)
LOGGER.info(f"\nValidating {f}...") LOGGER.info(f"\nValidating {f}...")
self.validator.args.plots = self.args.plots self.validator.args.plots = self.args.plots
self.metrics = self.validator(model=f) self.metrics = self.validator(model=f)

@ -12,8 +12,8 @@ Example:
```python ```python
from ultralytics import YOLO from ultralytics import YOLO
model = YOLO('yolov8n.pt') model = YOLO("yolov8n.pt")
model.tune(data='coco8.yaml', epochs=10, iterations=300, optimizer='AdamW', plots=False, save=False, val=False) model.tune(data="coco8.yaml", epochs=10, iterations=300, optimizer="AdamW", plots=False, save=False, val=False)
``` ```
""" """
@ -54,15 +54,15 @@ class Tuner:
```python ```python
from ultralytics import YOLO from ultralytics import YOLO
model = YOLO('yolov8n.pt') model = YOLO("yolov8n.pt")
model.tune(data='coco8.yaml', epochs=10, iterations=300, optimizer='AdamW', plots=False, save=False, val=False) model.tune(data="coco8.yaml", epochs=10, iterations=300, optimizer="AdamW", plots=False, save=False, val=False)
``` ```
Tune with custom search space. Tune with custom search space.
```python ```python
from ultralytics import YOLO from ultralytics import YOLO
model = YOLO('yolov8n.pt') model = YOLO("yolov8n.pt")
model.tune(space={key1: val1, key2: val2}) # custom search space dictionary model.tune(space={key1: val1, key2: val2}) # custom search space dictionary
``` ```
""" """

@ -136,11 +136,11 @@ def check_dataset(path: str, task: str) -> None:
```python ```python
from ultralytics.hub import check_dataset from ultralytics.hub import check_dataset
check_dataset('path/to/coco8.zip', task='detect') # detect dataset check_dataset("path/to/coco8.zip", task="detect") # detect dataset
check_dataset('path/to/coco8-seg.zip', task='segment') # segment dataset check_dataset("path/to/coco8-seg.zip", task="segment") # segment dataset
check_dataset('path/to/coco8-pose.zip', task='pose') # pose dataset check_dataset("path/to/coco8-pose.zip", task="pose") # pose dataset
check_dataset('path/to/dota8.zip', task='obb') # OBB dataset check_dataset("path/to/dota8.zip", task="obb") # OBB dataset
check_dataset('path/to/imagenet10.zip', task='classify') # classification dataset check_dataset("path/to/imagenet10.zip", task="classify") # classification dataset
``` ```
""" """
HUBDatasetStats(path=path, task=task).get_json() HUBDatasetStats(path=path, task=task).get_json()

@ -1,5 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license # Ultralytics YOLO 🚀, AGPL-3.0 license
import shutil
import threading import threading
import time import time
from http import HTTPStatus from http import HTTPStatus
@ -275,7 +276,7 @@ class HUBTrainingSession:
# if request related to metrics upload and exceed retries # if request related to metrics upload and exceed retries
if response is None and kwargs.get("metrics"): if response is None and kwargs.get("metrics"):
self.metrics_upload_failed_queue.update(kwargs.get("metrics", None)) self.metrics_upload_failed_queue.update(kwargs.get("metrics"))
return response return response
@ -344,23 +345,34 @@ class HUBTrainingSession:
map (float): Mean average precision of the model. map (float): Mean average precision of the model.
final (bool): Indicates if the model is the final model after training. final (bool): Indicates if the model is the final model after training.
""" """
if Path(weights).is_file(): weights = Path(weights)
progress_total = Path(weights).stat().st_size if final else None # Only show progress if final if not weights.is_file():
self.request_queue( last = weights.with_name("last" + weights.suffix)
self.model.upload_model, if final and last.is_file():
epoch=epoch, LOGGER.warning(
weights=weights, f"{PREFIX} WARNING ⚠ Model 'best.pt' not found, copying 'last.pt' to 'best.pt' and uploading. "
is_best=is_best, "This often happens when resuming training in transient environments like Google Colab. "
map=map, "For more reliable training, consider using Ultralytics HUB Cloud. "
final=final, "Learn more at https://docs.ultralytics.com/hub/cloud-training."
retry=10, )
timeout=3600, shutil.copy(last, weights) # copy last.pt to best.pt
thread=not final, else:
progress_total=progress_total, LOGGER.warning(f"{PREFIX} WARNING ⚠ Model upload issue. Missing model {weights}.")
stream_response=True, return
)
else: self.request_queue(
LOGGER.warning(f"{PREFIX}WARNING ⚠ Model upload issue. Missing model {weights}.") self.model.upload_model,
epoch=epoch,
weights=str(weights),
is_best=is_best,
map=map,
final=final,
retry=10,
timeout=3600,
thread=not final,
progress_total=weights.stat().st_size if final else None, # only show progress if final
stream_response=True,
)
@staticmethod @staticmethod
def _show_upload_progress(content_length: int, response: requests.Response) -> None: def _show_upload_progress(content_length: int, response: requests.Response) -> None:

@ -16,8 +16,8 @@ class FastSAM(Model):
```python ```python
from ultralytics import FastSAM from ultralytics import FastSAM
model = FastSAM('last.pt') model = FastSAM("last.pt")
results = model.predict('ultralytics/assets/bus.jpg') results = model.predict("ultralytics/assets/bus.jpg")
``` ```
""" """

@ -92,8 +92,8 @@ class FastSAMPredictor(SegmentationPredictor):
if labels.sum() == 0 # all negative points if labels.sum() == 0 # all negative points
else torch.zeros(len(result), dtype=torch.bool, device=self.device) else torch.zeros(len(result), dtype=torch.bool, device=self.device)
) )
for p, l in zip(points, labels): for point, label in zip(points, labels):
point_idx[torch.nonzero(masks[:, p[1], p[0]], as_tuple=True)[0]] = True if l else False point_idx[torch.nonzero(masks[:, point[1], point[0]], as_tuple=True)[0]] = True if label else False
idx |= point_idx idx |= point_idx
if texts is not None: if texts is not None:
if isinstance(texts, str): if isinstance(texts, str):

@ -6,8 +6,8 @@ Example:
```python ```python
from ultralytics import NAS from ultralytics import NAS
model = NAS('yolo_nas_s') model = NAS("yolo_nas_s")
results = model.predict('ultralytics/assets/bus.jpg') results = model.predict("ultralytics/assets/bus.jpg")
``` ```
""" """
@ -34,8 +34,8 @@ class NAS(Model):
```python ```python
from ultralytics import NAS from ultralytics import NAS
model = NAS('yolo_nas_s') model = NAS("yolo_nas_s")
results = model.predict('ultralytics/assets/bus.jpg') results = model.predict("ultralytics/assets/bus.jpg")
``` ```
Attributes: Attributes:

@ -22,7 +22,7 @@ class NASPredictor(BasePredictor):
```python ```python
from ultralytics import NAS from ultralytics import NAS
model = NAS('yolo_nas_s') model = NAS("yolo_nas_s")
predictor = model.predictor predictor = model.predictor
# Assumes that raw_preds, img, orig_imgs are available # Assumes that raw_preds, img, orig_imgs are available
results = predictor.postprocess(raw_preds, img, orig_imgs) results = predictor.postprocess(raw_preds, img, orig_imgs)

@ -24,7 +24,7 @@ class NASValidator(DetectionValidator):
```python ```python
from ultralytics import NAS from ultralytics import NAS
model = NAS('yolo_nas_s') model = NAS("yolo_nas_s")
validator = model.validator validator = model.validator
# Assumes that raw_preds are available # Assumes that raw_preds are available
final_preds = validator.postprocess(raw_preds) final_preds = validator.postprocess(raw_preds)

@ -21,7 +21,7 @@ class RTDETRPredictor(BasePredictor):
from ultralytics.utils import ASSETS from ultralytics.utils import ASSETS
from ultralytics.models.rtdetr import RTDETRPredictor from ultralytics.models.rtdetr import RTDETRPredictor
args = dict(model='rtdetr-l.pt', source=ASSETS) args = dict(model="rtdetr-l.pt", source=ASSETS)
predictor = RTDETRPredictor(overrides=args) predictor = RTDETRPredictor(overrides=args)
predictor.predict_cli() predictor.predict_cli()
``` ```

@ -25,7 +25,7 @@ class RTDETRTrainer(DetectionTrainer):
```python ```python
from ultralytics.models.rtdetr.train import RTDETRTrainer from ultralytics.models.rtdetr.train import RTDETRTrainer
args = dict(model='rtdetr-l.yaml', data='coco8.yaml', imgsz=640, epochs=3) args = dict(model="rtdetr-l.yaml", data="coco8.yaml", imgsz=640, epochs=3)
trainer = RTDETRTrainer(overrides=args) trainer = RTDETRTrainer(overrides=args)
trainer.train() trainer.train()
``` ```

@ -62,7 +62,7 @@ class RTDETRValidator(DetectionValidator):
```python ```python
from ultralytics.models.rtdetr import RTDETRValidator from ultralytics.models.rtdetr import RTDETRValidator
args = dict(model='rtdetr-l.pt', data='coco8.yaml') args = dict(model="rtdetr-l.pt", data="coco8.yaml")
validator = RTDETRValidator(args=args) validator = RTDETRValidator(args=args)
validator() validator()
``` ```

@ -41,8 +41,8 @@ class SAM(Model):
info: Logs information about the SAM model. info: Logs information about the SAM model.
Examples: Examples:
>>> sam = SAM('sam_b.pt') >>> sam = SAM("sam_b.pt")
>>> results = sam.predict('image.jpg', points=[[500, 375]]) >>> results = sam.predict("image.jpg", points=[[500, 375]])
>>> for r in results: >>> for r in results:
>>> print(f"Detected {len(r.masks)} masks") >>> print(f"Detected {len(r.masks)} masks")
""" """
@ -58,7 +58,7 @@ class SAM(Model):
NotImplementedError: If the model file extension is not .pt or .pth. NotImplementedError: If the model file extension is not .pt or .pth.
Examples: Examples:
>>> sam = SAM('sam_b.pt') >>> sam = SAM("sam_b.pt")
>>> print(sam.is_sam2) >>> print(sam.is_sam2)
""" """
if model and Path(model).suffix not in {".pt", ".pth"}: if model and Path(model).suffix not in {".pt", ".pth"}:
@ -78,8 +78,8 @@ class SAM(Model):
task (str | None): Task name. If provided, it specifies the particular task the model is being loaded for. task (str | None): Task name. If provided, it specifies the particular task the model is being loaded for.
Examples: Examples:
>>> sam = SAM('sam_b.pt') >>> sam = SAM("sam_b.pt")
>>> sam._load('path/to/custom_weights.pt') >>> sam._load("path/to/custom_weights.pt")
""" """
self.model = build_sam(weights) self.model = build_sam(weights)
@ -100,8 +100,8 @@ class SAM(Model):
(List): The model predictions. (List): The model predictions.
Examples: Examples:
>>> sam = SAM('sam_b.pt') >>> sam = SAM("sam_b.pt")
>>> results = sam.predict('image.jpg', points=[[500, 375]]) >>> results = sam.predict("image.jpg", points=[[500, 375]])
>>> for r in results: >>> for r in results:
... print(f"Detected {len(r.masks)} masks") ... print(f"Detected {len(r.masks)} masks")
""" """
@ -130,8 +130,8 @@ class SAM(Model):
(List): The model predictions, typically containing segmentation masks and other relevant information. (List): The model predictions, typically containing segmentation masks and other relevant information.
Examples: Examples:
>>> sam = SAM('sam_b.pt') >>> sam = SAM("sam_b.pt")
>>> results = sam('image.jpg', points=[[500, 375]]) >>> results = sam("image.jpg", points=[[500, 375]])
>>> print(f"Detected {len(results[0].masks)} masks") >>> print(f"Detected {len(results[0].masks)} masks")
""" """
return self.predict(source, stream, bboxes, points, labels, **kwargs) return self.predict(source, stream, bboxes, points, labels, **kwargs)
@ -151,7 +151,7 @@ class SAM(Model):
(Tuple): A tuple containing the model's information (string representations of the model). (Tuple): A tuple containing the model's information (string representations of the model).
Examples: Examples:
>>> sam = SAM('sam_b.pt') >>> sam = SAM("sam_b.pt")
>>> info = sam.info() >>> info = sam.info()
>>> print(info[0]) # Print summary information >>> print(info[0]) # Print summary information
""" """
@ -167,7 +167,7 @@ class SAM(Model):
class. For SAM2 models, it maps to SAM2Predictor, otherwise to the standard Predictor. class. For SAM2 models, it maps to SAM2Predictor, otherwise to the standard Predictor.
Examples: Examples:
>>> sam = SAM('sam_b.pt') >>> sam = SAM("sam_b.pt")
>>> task_map = sam.task_map >>> task_map = sam.task_map
>>> print(task_map) >>> print(task_map)
{'segment': <class 'ultralytics.models.sam.predict.Predictor'>} {'segment': <class 'ultralytics.models.sam.predict.Predictor'>}

@ -32,8 +32,9 @@ class MaskDecoder(nn.Module):
Examples: Examples:
>>> decoder = MaskDecoder(transformer_dim=256, transformer=transformer_module) >>> decoder = MaskDecoder(transformer_dim=256, transformer=transformer_module)
>>> masks, iou_pred = decoder(image_embeddings, image_pe, sparse_prompt_embeddings, >>> masks, iou_pred = decoder(
... dense_prompt_embeddings, multimask_output=True) ... image_embeddings, image_pe, sparse_prompt_embeddings, dense_prompt_embeddings, multimask_output=True
... )
>>> print(f"Predicted masks shape: {masks.shape}, IoU predictions shape: {iou_pred.shape}") >>> print(f"Predicted masks shape: {masks.shape}, IoU predictions shape: {iou_pred.shape}")
""" """
@ -213,7 +214,8 @@ class SAM2MaskDecoder(nn.Module):
>>> dense_prompt_embeddings = torch.rand(1, 256, 64, 64) >>> dense_prompt_embeddings = torch.rand(1, 256, 64, 64)
>>> decoder = SAM2MaskDecoder(256, transformer) >>> decoder = SAM2MaskDecoder(256, transformer)
>>> masks, iou_pred, sam_tokens_out, obj_score_logits = decoder.forward( >>> masks, iou_pred, sam_tokens_out, obj_score_logits = decoder.forward(
... image_embeddings, image_pe, sparse_prompt_embeddings, dense_prompt_embeddings, True, False) ... image_embeddings, image_pe, sparse_prompt_embeddings, dense_prompt_embeddings, True, False
... )
""" """
def __init__( def __init__(
@ -345,7 +347,8 @@ class SAM2MaskDecoder(nn.Module):
>>> dense_prompt_embeddings = torch.rand(1, 256, 64, 64) >>> dense_prompt_embeddings = torch.rand(1, 256, 64, 64)
>>> decoder = SAM2MaskDecoder(256, transformer) >>> decoder = SAM2MaskDecoder(256, transformer)
>>> masks, iou_pred, sam_tokens_out, obj_score_logits = decoder.forward( >>> masks, iou_pred, sam_tokens_out, obj_score_logits = decoder.forward(
... image_embeddings, image_pe, sparse_prompt_embeddings, dense_prompt_embeddings, True, False) ... image_embeddings, image_pe, sparse_prompt_embeddings, dense_prompt_embeddings, True, False
... )
""" """
masks, iou_pred, mask_tokens_out, object_score_logits = self.predict_masks( masks, iou_pred, mask_tokens_out, object_score_logits = self.predict_masks(
image_embeddings=image_embeddings, image_embeddings=image_embeddings,

@ -417,7 +417,15 @@ class SAM2Model(torch.nn.Module):
>>> point_inputs = {"point_coords": torch.rand(1, 2, 2), "point_labels": torch.tensor([[1, 0]])} >>> point_inputs = {"point_coords": torch.rand(1, 2, 2), "point_labels": torch.tensor([[1, 0]])}
>>> mask_inputs = torch.rand(1, 1, 512, 512) >>> mask_inputs = torch.rand(1, 1, 512, 512)
>>> results = model._forward_sam_heads(backbone_features, point_inputs, mask_inputs) >>> results = model._forward_sam_heads(backbone_features, point_inputs, mask_inputs)
>>> low_res_multimasks, high_res_multimasks, ious, low_res_masks, high_res_masks, obj_ptr, object_score_logits = results >>> (
... low_res_multimasks,
... high_res_multimasks,
... ious,
... low_res_masks,
... high_res_masks,
... obj_ptr,
... object_score_logits,
... ) = results
""" """
B = backbone_features.size(0) B = backbone_features.size(0)
device = backbone_features.device device = backbone_features.device

@ -716,7 +716,7 @@ class BasicLayer(nn.Module):
Examples: Examples:
>>> layer = BasicLayer(dim=96, input_resolution=(56, 56), depth=2, num_heads=3, window_size=7) >>> layer = BasicLayer(dim=96, input_resolution=(56, 56), depth=2, num_heads=3, window_size=7)
>>> x = torch.randn(1, 56*56, 96) >>> x = torch.randn(1, 56 * 56, 96)
>>> output = layer(x) >>> output = layer(x)
>>> print(output.shape) >>> print(output.shape)
""" """

@ -22,7 +22,7 @@ def select_closest_cond_frames(frame_idx, cond_frame_outputs, max_cond_frame_num
Examples: Examples:
>>> frame_idx = 5 >>> frame_idx = 5
>>> cond_frame_outputs = {1: 'a', 3: 'b', 7: 'c', 9: 'd'} >>> cond_frame_outputs = {1: "a", 3: "b", 7: "c", 9: "d"}
>>> max_cond_frame_num = 2 >>> max_cond_frame_num = 2
>>> selected, unselected = select_closest_cond_frames(frame_idx, cond_frame_outputs, max_cond_frame_num) >>> selected, unselected = select_closest_cond_frames(frame_idx, cond_frame_outputs, max_cond_frame_num)
>>> print(selected) >>> print(selected)

@ -69,8 +69,8 @@ class Predictor(BasePredictor):
Examples: Examples:
>>> predictor = Predictor() >>> predictor = Predictor()
>>> predictor.setup_model(model_path='sam_model.pt') >>> predictor.setup_model(model_path="sam_model.pt")
>>> predictor.set_image('image.jpg') >>> predictor.set_image("image.jpg")
>>> masks, scores, boxes = predictor.generate() >>> masks, scores, boxes = predictor.generate()
>>> results = predictor.postprocess((masks, scores, boxes), im, orig_img) >>> results = predictor.postprocess((masks, scores, boxes), im, orig_img)
""" """
@ -90,8 +90,8 @@ class Predictor(BasePredictor):
Examples: Examples:
>>> predictor = Predictor(cfg=DEFAULT_CFG) >>> predictor = Predictor(cfg=DEFAULT_CFG)
>>> predictor = Predictor(overrides={'imgsz': 640}) >>> predictor = Predictor(overrides={"imgsz": 640})
>>> predictor = Predictor(_callbacks={'on_predict_start': custom_callback}) >>> predictor = Predictor(_callbacks={"on_predict_start": custom_callback})
""" """
if overrides is None: if overrides is None:
overrides = {} overrides = {}
@ -188,8 +188,8 @@ class Predictor(BasePredictor):
Examples: Examples:
>>> predictor = Predictor() >>> predictor = Predictor()
>>> predictor.setup_model(model_path='sam_model.pt') >>> predictor.setup_model(model_path="sam_model.pt")
>>> predictor.set_image('image.jpg') >>> predictor.set_image("image.jpg")
>>> masks, scores, logits = predictor.inference(im, bboxes=[[0, 0, 100, 100]]) >>> masks, scores, logits = predictor.inference(im, bboxes=[[0, 0, 100, 100]])
""" """
# Override prompts if any stored in self.prompts # Override prompts if any stored in self.prompts
@ -475,8 +475,8 @@ class Predictor(BasePredictor):
Examples: Examples:
>>> predictor = Predictor() >>> predictor = Predictor()
>>> predictor.setup_source('path/to/images') >>> predictor.setup_source("path/to/images")
>>> predictor.setup_source('video.mp4') >>> predictor.setup_source("video.mp4")
>>> predictor.setup_source(None) # Uses default source if available >>> predictor.setup_source(None) # Uses default source if available
Notes: Notes:
@ -504,8 +504,8 @@ class Predictor(BasePredictor):
Examples: Examples:
>>> predictor = Predictor() >>> predictor = Predictor()
>>> predictor.set_image('path/to/image.jpg') >>> predictor.set_image("path/to/image.jpg")
>>> predictor.set_image(cv2.imread('path/to/image.jpg')) >>> predictor.set_image(cv2.imread("path/to/image.jpg"))
Notes: Notes:
- This method should be called before performing inference on a new image. - This method should be called before performing inference on a new image.

@ -21,7 +21,7 @@ class ClassificationPredictor(BasePredictor):
from ultralytics.utils import ASSETS from ultralytics.utils import ASSETS
from ultralytics.models.yolo.classify import ClassificationPredictor from ultralytics.models.yolo.classify import ClassificationPredictor
args = dict(model='yolov8n-cls.pt', source=ASSETS) args = dict(model="yolov8n-cls.pt", source=ASSETS)
predictor = ClassificationPredictor(overrides=args) predictor = ClassificationPredictor(overrides=args)
predictor.predict_cli() predictor.predict_cli()
``` ```

@ -22,7 +22,7 @@ class ClassificationTrainer(BaseTrainer):
```python ```python
from ultralytics.models.yolo.classify import ClassificationTrainer from ultralytics.models.yolo.classify import ClassificationTrainer
args = dict(model='yolov8n-cls.pt', data='imagenet10', epochs=3) args = dict(model="yolov8n-cls.pt", data="imagenet10", epochs=3)
trainer = ClassificationTrainer(overrides=args) trainer = ClassificationTrainer(overrides=args)
trainer.train() trainer.train()
``` ```

@ -20,7 +20,7 @@ class ClassificationValidator(BaseValidator):
```python ```python
from ultralytics.models.yolo.classify import ClassificationValidator from ultralytics.models.yolo.classify import ClassificationValidator
args = dict(model='yolov8n-cls.pt', data='imagenet10') args = dict(model="yolov8n-cls.pt", data="imagenet10")
validator = ClassificationValidator(args=args) validator = ClassificationValidator(args=args)
validator() validator()
``` ```

@ -14,7 +14,7 @@ class DetectionPredictor(BasePredictor):
from ultralytics.utils import ASSETS from ultralytics.utils import ASSETS
from ultralytics.models.yolo.detect import DetectionPredictor from ultralytics.models.yolo.detect import DetectionPredictor
args = dict(model='yolov8n.pt', source=ASSETS) args = dict(model="yolov8n.pt", source=ASSETS)
predictor = DetectionPredictor(overrides=args) predictor = DetectionPredictor(overrides=args)
predictor.predict_cli() predictor.predict_cli()
``` ```

@ -24,7 +24,7 @@ class DetectionTrainer(BaseTrainer):
```python ```python
from ultralytics.models.yolo.detect import DetectionTrainer from ultralytics.models.yolo.detect import DetectionTrainer
args = dict(model='yolov8n.pt', data='coco8.yaml', epochs=3) args = dict(model="yolov8n.pt", data="coco8.yaml", epochs=3)
trainer = DetectionTrainer(overrides=args) trainer = DetectionTrainer(overrides=args)
trainer.train() trainer.train()
``` ```

@ -22,7 +22,7 @@ class DetectionValidator(BaseValidator):
```python ```python
from ultralytics.models.yolo.detect import DetectionValidator from ultralytics.models.yolo.detect import DetectionValidator
args = dict(model='yolov8n.pt', data='coco8.yaml') args = dict(model="yolov8n.pt", data="coco8.yaml")
validator = DetectionValidator(args=args) validator = DetectionValidator(args=args)
validator() validator()
``` ```

@ -16,7 +16,7 @@ class OBBPredictor(DetectionPredictor):
from ultralytics.utils import ASSETS from ultralytics.utils import ASSETS
from ultralytics.models.yolo.obb import OBBPredictor from ultralytics.models.yolo.obb import OBBPredictor
args = dict(model='yolov8n-obb.pt', source=ASSETS) args = dict(model="yolov8n-obb.pt", source=ASSETS)
predictor = OBBPredictor(overrides=args) predictor = OBBPredictor(overrides=args)
predictor.predict_cli() predictor.predict_cli()
``` ```

@ -15,7 +15,7 @@ class OBBTrainer(yolo.detect.DetectionTrainer):
```python ```python
from ultralytics.models.yolo.obb import OBBTrainer from ultralytics.models.yolo.obb import OBBTrainer
args = dict(model='yolov8n-obb.pt', data='dota8.yaml', epochs=3) args = dict(model="yolov8n-obb.pt", data="dota8.yaml", epochs=3)
trainer = OBBTrainer(overrides=args) trainer = OBBTrainer(overrides=args)
trainer.train() trainer.train()
``` ```

@ -18,9 +18,9 @@ class OBBValidator(DetectionValidator):
```python ```python
from ultralytics.models.yolo.obb import OBBValidator from ultralytics.models.yolo.obb import OBBValidator
args = dict(model='yolov8n-obb.pt', data='dota8.yaml') args = dict(model="yolov8n-obb.pt", data="dota8.yaml")
validator = OBBValidator(args=args) validator = OBBValidator(args=args)
validator(model=args['model']) validator(model=args["model"])
``` ```
""" """

@ -14,7 +14,7 @@ class PosePredictor(DetectionPredictor):
from ultralytics.utils import ASSETS from ultralytics.utils import ASSETS
from ultralytics.models.yolo.pose import PosePredictor from ultralytics.models.yolo.pose import PosePredictor
args = dict(model='yolov8n-pose.pt', source=ASSETS) args = dict(model="yolov8n-pose.pt", source=ASSETS)
predictor = PosePredictor(overrides=args) predictor = PosePredictor(overrides=args)
predictor.predict_cli() predictor.predict_cli()
``` ```

@ -16,7 +16,7 @@ class PoseTrainer(yolo.detect.DetectionTrainer):
```python ```python
from ultralytics.models.yolo.pose import PoseTrainer from ultralytics.models.yolo.pose import PoseTrainer
args = dict(model='yolov8n-pose.pt', data='coco8-pose.yaml', epochs=3) args = dict(model="yolov8n-pose.pt", data="coco8-pose.yaml", epochs=3)
trainer = PoseTrainer(overrides=args) trainer = PoseTrainer(overrides=args)
trainer.train() trainer.train()
``` ```

@ -20,7 +20,7 @@ class PoseValidator(DetectionValidator):
```python ```python
from ultralytics.models.yolo.pose import PoseValidator from ultralytics.models.yolo.pose import PoseValidator
args = dict(model='yolov8n-pose.pt', data='coco8-pose.yaml') args = dict(model="yolov8n-pose.pt", data="coco8-pose.yaml")
validator = PoseValidator(args=args) validator = PoseValidator(args=args)
validator() validator()
``` ```

@ -14,7 +14,7 @@ class SegmentationPredictor(DetectionPredictor):
from ultralytics.utils import ASSETS from ultralytics.utils import ASSETS
from ultralytics.models.yolo.segment import SegmentationPredictor from ultralytics.models.yolo.segment import SegmentationPredictor
args = dict(model='yolov8n-seg.pt', source=ASSETS) args = dict(model="yolov8n-seg.pt", source=ASSETS)
predictor = SegmentationPredictor(overrides=args) predictor = SegmentationPredictor(overrides=args)
predictor.predict_cli() predictor.predict_cli()
``` ```

@ -16,7 +16,7 @@ class SegmentationTrainer(yolo.detect.DetectionTrainer):
```python ```python
from ultralytics.models.yolo.segment import SegmentationTrainer from ultralytics.models.yolo.segment import SegmentationTrainer
args = dict(model='yolov8n-seg.pt', data='coco8-seg.yaml', epochs=3) args = dict(model="yolov8n-seg.pt", data="coco8-seg.yaml", epochs=3)
trainer = SegmentationTrainer(overrides=args) trainer = SegmentationTrainer(overrides=args)
trainer.train() trainer.train()
``` ```

@ -22,7 +22,7 @@ class SegmentationValidator(DetectionValidator):
```python ```python
from ultralytics.models.yolo.segment import SegmentationValidator from ultralytics.models.yolo.segment import SegmentationValidator
args = dict(model='yolov8n-seg.pt', data='coco8-seg.yaml') args = dict(model="yolov8n-seg.pt", data="coco8-seg.yaml")
validator = SegmentationValidator(args=args) validator = SegmentationValidator(args=args)
validator() validator()
``` ```

@ -29,7 +29,7 @@ class WorldTrainer(yolo.detect.DetectionTrainer):
```python ```python
from ultralytics.models.yolo.world import WorldModel from ultralytics.models.yolo.world import WorldModel
args = dict(model='yolov8s-world.pt', data='coco8.yaml', epochs=3) args = dict(model="yolov8s-world.pt", data="coco8.yaml", epochs=3)
trainer = WorldTrainer(overrides=args) trainer = WorldTrainer(overrides=args)
trainer.train() trainer.train()
``` ```

@ -11,9 +11,9 @@ Example:
x = torch.ones(1, 128, 40, 40) x = torch.ones(1, 128, 40, 40)
m = Conv(128, 128) m = Conv(128, 128)
f = f'{m._get_name()}.onnx' f = f"{m._get_name()}.onnx"
torch.onnx.export(m, x, f) torch.onnx.export(m, x, f)
os.system(f'onnxslim {f} {f} && open {f}') # pip install onnxslim os.system(f"onnxslim {f} {f} && open {f}") # pip install onnxslim
``` ```
""" """

@ -713,7 +713,7 @@ def temporary_modules(modules=None, attributes=None):
Example: Example:
```python ```python
with temporary_modules({'old.module': 'new.module'}, {'old.module.attribute': 'new.module.attribute'}): with temporary_modules({"old.module": "new.module"}, {"old.module.attribute": "new.module.attribute"}):
import old.module # this will now import new.module import old.module # this will now import new.module
from old.module import attribute # this will now import new.module.attribute from old.module import attribute # this will now import new.module.attribute
``` ```

@ -20,4 +20,5 @@ __all__ = (
"QueueManager", "QueueManager",
"SpeedEstimator", "SpeedEstimator",
"Analytics", "Analytics",
"inference",
) )

@ -42,7 +42,7 @@ class STrack(BaseTrack):
Examples: Examples:
Initialize and activate a new track Initialize and activate a new track
>>> track = STrack(xywh=[100, 200, 50, 80, 0], score=0.9, cls='person') >>> track = STrack(xywh=[100, 200, 50, 80, 0], score=0.9, cls="person")
>>> track.activate(kalman_filter=KalmanFilterXYAH(), frame_id=1) >>> track.activate(kalman_filter=KalmanFilterXYAH(), frame_id=1)
""" """
@ -61,7 +61,7 @@ class STrack(BaseTrack):
Examples: Examples:
>>> xywh = [100.0, 150.0, 50.0, 75.0, 1] >>> xywh = [100.0, 150.0, 50.0, 75.0, 1]
>>> score = 0.9 >>> score = 0.9
>>> cls = 'person' >>> cls = "person"
>>> track = STrack(xywh, score, cls) >>> track = STrack(xywh, score, cls)
""" """
super().__init__() super().__init__()

@ -33,7 +33,7 @@ class GMC:
Examples: Examples:
Create a GMC object and apply it to a frame Create a GMC object and apply it to a frame
>>> gmc = GMC(method='sparseOptFlow', downscale=2) >>> gmc = GMC(method="sparseOptFlow", downscale=2)
>>> frame = np.array([[1, 2, 3], [4, 5, 6]]) >>> frame = np.array([[1, 2, 3], [4, 5, 6]])
>>> processed_frame = gmc.apply(frame) >>> processed_frame = gmc.apply(frame)
>>> print(processed_frame) >>> print(processed_frame)
@ -51,7 +51,7 @@ class GMC:
Examples: Examples:
Initialize a GMC object with the 'sparseOptFlow' method and a downscale factor of 2 Initialize a GMC object with the 'sparseOptFlow' method and a downscale factor of 2
>>> gmc = GMC(method='sparseOptFlow', downscale=2) >>> gmc = GMC(method="sparseOptFlow", downscale=2)
""" """
super().__init__() super().__init__()
@ -101,7 +101,7 @@ class GMC:
(np.ndarray): Processed frame with applied object detection. (np.ndarray): Processed frame with applied object detection.
Examples: Examples:
>>> gmc = GMC(method='sparseOptFlow') >>> gmc = GMC(method="sparseOptFlow")
>>> raw_frame = np.random.rand(480, 640, 3) >>> raw_frame = np.random.rand(480, 640, 3)
>>> processed_frame = gmc.apply(raw_frame) >>> processed_frame = gmc.apply(raw_frame)
>>> print(processed_frame.shape) >>> print(processed_frame.shape)
@ -127,7 +127,7 @@ class GMC:
(np.ndarray): The processed frame with the applied ECC transformation. (np.ndarray): The processed frame with the applied ECC transformation.
Examples: Examples:
>>> gmc = GMC(method='ecc') >>> gmc = GMC(method="ecc")
>>> processed_frame = gmc.applyEcc(np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])) >>> processed_frame = gmc.applyEcc(np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]))
>>> print(processed_frame) >>> print(processed_frame)
[[1. 0. 0.] [[1. 0. 0.]
@ -173,7 +173,7 @@ class GMC:
(np.ndarray): Processed frame. (np.ndarray): Processed frame.
Examples: Examples:
>>> gmc = GMC(method='orb') >>> gmc = GMC(method="orb")
>>> raw_frame = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) >>> raw_frame = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
>>> processed_frame = gmc.applyFeatures(raw_frame) >>> processed_frame = gmc.applyFeatures(raw_frame)
>>> print(processed_frame.shape) >>> print(processed_frame.shape)

@ -268,7 +268,7 @@ class KalmanFilterXYAH:
>>> mean = np.array([0, 0, 1, 1, 0, 0, 0, 0]) >>> mean = np.array([0, 0, 1, 1, 0, 0, 0, 0])
>>> covariance = np.eye(8) >>> covariance = np.eye(8)
>>> measurements = np.array([[1, 1, 1, 1], [2, 2, 1, 1]]) >>> measurements = np.array([[1, 1, 1, 1], [2, 2, 1, 1]])
>>> distances = kf.gating_distance(mean, covariance, measurements, only_position=False, metric='maha') >>> distances = kf.gating_distance(mean, covariance, measurements, only_position=False, metric="maha")
""" """
mean, covariance = self.project(mean, covariance) mean, covariance = self.project(mean, covariance)
if only_position: if only_position:

@ -121,7 +121,7 @@ def embedding_distance(tracks: list, detections: list, metric: str = "cosine") -
Compute the embedding distance between tracks and detections using cosine metric Compute the embedding distance between tracks and detections using cosine metric
>>> tracks = [STrack(...), STrack(...)] # List of track objects with embedding features >>> tracks = [STrack(...), STrack(...)] # List of track objects with embedding features
>>> detections = [BaseTrack(...), BaseTrack(...)] # List of detection objects with embedding features >>> detections = [BaseTrack(...), BaseTrack(...)] # List of detection objects with embedding features
>>> cost_matrix = embedding_distance(tracks, detections, metric='cosine') >>> cost_matrix = embedding_distance(tracks, detections, metric="cosine")
""" """
cost_matrix = np.zeros((len(tracks), len(detections)), dtype=np.float32) cost_matrix = np.zeros((len(tracks), len(detections)), dtype=np.float32)

@ -219,16 +219,19 @@ def plt_settings(rcparams=None, backend="Agg"):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
"""Sets rc parameters and backend, calls the original function, and restores the settings.""" """Sets rc parameters and backend, calls the original function, and restores the settings."""
original_backend = plt.get_backend() original_backend = plt.get_backend()
if backend.lower() != original_backend.lower(): switch = backend.lower() != original_backend.lower()
if switch:
plt.close("all") # auto-close()ing of figures upon backend switching is deprecated since 3.8 plt.close("all") # auto-close()ing of figures upon backend switching is deprecated since 3.8
plt.switch_backend(backend) plt.switch_backend(backend)
with plt.rc_context(rcparams): # Plot with backend and always revert to original backend
result = func(*args, **kwargs) try:
with plt.rc_context(rcparams):
if backend != original_backend: result = func(*args, **kwargs)
plt.close("all") finally:
plt.switch_backend(original_backend) if switch:
plt.close("all")
plt.switch_backend(original_backend)
return result return result
return wrapper return wrapper
@ -710,7 +713,7 @@ def colorstr(*input):
In the second form, 'blue' and 'bold' will be applied by default. In the second form, 'blue' and 'bold' will be applied by default.
Args: Args:
*input (str): A sequence of strings where the first n-1 strings are color and style arguments, *input (str | Path): A sequence of strings where the first n-1 strings are color and style arguments,
and the last string is the one to be colored. and the last string is the one to be colored.
Supported Colors and Styles: Supported Colors and Styles:
@ -762,8 +765,8 @@ def remove_colorstr(input_string):
(str): A new string with all ANSI escape codes removed. (str): A new string with all ANSI escape codes removed.
Examples: Examples:
>>> remove_colorstr(colorstr('blue', 'bold', 'hello world')) >>> remove_colorstr(colorstr("blue", "bold", "hello world"))
>>> 'hello world' >>> "hello world"
""" """
ansi_escape = re.compile(r"\x1B\[[0-9;]*[A-Za-z]") ansi_escape = re.compile(r"\x1B\[[0-9;]*[A-Za-z]")
return ansi_escape.sub("", input_string) return ansi_escape.sub("", input_string)
@ -777,12 +780,12 @@ class TryExcept(contextlib.ContextDecorator):
As a decorator: As a decorator:
>>> @TryExcept(msg="Error occurred in func", verbose=True) >>> @TryExcept(msg="Error occurred in func", verbose=True)
>>> def func(): >>> def func():
>>> # Function logic here >>> # Function logic here
>>> pass >>> pass
As a context manager: As a context manager:
>>> with TryExcept(msg="Error occurred in block", verbose=True): >>> with TryExcept(msg="Error occurred in block", verbose=True):
>>> # Code block here >>> # Code block here
>>> pass >>> pass
""" """
@ -813,7 +816,7 @@ class Retry(contextlib.ContextDecorator):
Example usage as a decorator: Example usage as a decorator:
>>> @Retry(times=3, delay=2) >>> @Retry(times=3, delay=2)
>>> def test_func(): >>> def test_func():
>>> # Replace with function logic that may raise exceptions >>> # Replace with function logic that may raise exceptions
>>> return True >>> return True
""" """

@ -71,7 +71,7 @@ def benchmark(
```python ```python
from ultralytics.utils.benchmarks import benchmark from ultralytics.utils.benchmarks import benchmark
benchmark(model='yolov8n.pt', imgsz=640) benchmark(model="yolov8n.pt", imgsz=640)
``` ```
""" """
import pandas as pd # scope for faster 'import ultralytics' import pandas as pd # scope for faster 'import ultralytics'
@ -302,7 +302,7 @@ class ProfileModels:
```python ```python
from ultralytics.utils.benchmarks import ProfileModels from ultralytics.utils.benchmarks import ProfileModels
ProfileModels(['yolov8n.yaml', 'yolov8s.yaml'], imgsz=640).profile() ProfileModels(["yolov8n.yaml", "yolov8s.yaml"], imgsz=640).profile()
``` ```
""" """

@ -23,6 +23,7 @@ from ultralytics.utils import (
ASSETS, ASSETS,
AUTOINSTALL, AUTOINSTALL,
IS_COLAB, IS_COLAB,
IS_GIT_DIR,
IS_JUPYTER, IS_JUPYTER,
IS_KAGGLE, IS_KAGGLE,
IS_PIP_PACKAGE, IS_PIP_PACKAGE,
@ -61,7 +62,7 @@ def parse_requirements(file_path=ROOT.parent / "requirements.txt", package=""):
```python ```python
from ultralytics.utils.checks import parse_requirements from ultralytics.utils.checks import parse_requirements
parse_requirements(package='ultralytics') parse_requirements(package="ultralytics")
``` ```
""" """
@ -196,16 +197,16 @@ def check_version(
Example: Example:
```python ```python
# Check if current version is exactly 22.04 # Check if current version is exactly 22.04
check_version(current='22.04', required='==22.04') check_version(current="22.04", required="==22.04")
# Check if current version is greater than or equal to 22.04 # Check if current version is greater than or equal to 22.04
check_version(current='22.10', required='22.04') # assumes '>=' inequality if none passed check_version(current="22.10", required="22.04") # assumes '>=' inequality if none passed
# Check if current version is less than or equal to 22.04 # Check if current version is less than or equal to 22.04
check_version(current='22.04', required='<=22.04') check_version(current="22.04", required="<=22.04")
# Check if current version is between 20.04 (inclusive) and 22.04 (exclusive) # Check if current version is between 20.04 (inclusive) and 22.04 (exclusive)
check_version(current='21.10', required='>20.04,<22.04') check_version(current="21.10", required=">20.04,<22.04")
``` ```
""" """
if not current: # if current is '' or None if not current: # if current is '' or None
@ -352,13 +353,13 @@ def check_requirements(requirements=ROOT.parent / "requirements.txt", exclude=()
from ultralytics.utils.checks import check_requirements from ultralytics.utils.checks import check_requirements
# Check a requirements.txt file # Check a requirements.txt file
check_requirements('path/to/requirements.txt') check_requirements("path/to/requirements.txt")
# Check a single package # Check a single package
check_requirements('ultralytics>=8.0.0') check_requirements("ultralytics>=8.0.0")
# Check multiple packages # Check multiple packages
check_requirements(['numpy', 'ultralytics>=8.0.0']) check_requirements(["numpy", "ultralytics>=8.0.0"])
``` ```
""" """
@ -582,10 +583,9 @@ def check_yolo(verbose=True, device=""):
def collect_system_info(): def collect_system_info():
"""Collect and print relevant system information including OS, Python, RAM, CPU, and CUDA.""" """Collect and print relevant system information including OS, Python, RAM, CPU, and CUDA."""
import psutil import psutil
from ultralytics.utils import ENVIRONMENT, IS_GIT_DIR from ultralytics.utils import ENVIRONMENT # scope to avoid circular import
from ultralytics.utils.torch_utils import get_cpu_info from ultralytics.utils.torch_utils import get_cpu_info
ram_info = psutil.virtual_memory().total / (1024**3) # Convert bytes to GB ram_info = psutil.virtual_memory().total / (1024**3) # Convert bytes to GB
@ -634,7 +634,7 @@ def check_amp(model):
from ultralytics import YOLO from ultralytics import YOLO
from ultralytics.utils.checks import check_amp from ultralytics.utils.checks import check_amp
model = YOLO('yolov8n.pt').model.cuda() model = YOLO("yolov8n.pt").model.cuda()
check_amp(model) check_amp(model)
``` ```

@ -75,7 +75,7 @@ def delete_dsstore(path, files_to_delete=(".DS_Store", "__MACOSX")):
```python ```python
from ultralytics.utils.downloads import delete_dsstore from ultralytics.utils.downloads import delete_dsstore
delete_dsstore('path/to/dir') delete_dsstore("path/to/dir")
``` ```
Note: Note:
@ -107,7 +107,7 @@ def zip_directory(directory, compress=True, exclude=(".DS_Store", "__MACOSX"), p
```python ```python
from ultralytics.utils.downloads import zip_directory from ultralytics.utils.downloads import zip_directory
file = zip_directory('path/to/dir') file = zip_directory("path/to/dir")
``` ```
""" """
from zipfile import ZIP_DEFLATED, ZIP_STORED, ZipFile from zipfile import ZIP_DEFLATED, ZIP_STORED, ZipFile
@ -153,7 +153,7 @@ def unzip_file(file, path=None, exclude=(".DS_Store", "__MACOSX"), exist_ok=Fals
```python ```python
from ultralytics.utils.downloads import unzip_file from ultralytics.utils.downloads import unzip_file
dir = unzip_file('path/to/file.zip') dir = unzip_file("path/to/file.zip")
``` ```
""" """
from zipfile import BadZipFile, ZipFile, is_zipfile from zipfile import BadZipFile, ZipFile, is_zipfile
@ -392,7 +392,7 @@ def get_github_assets(repo="ultralytics/assets", version="latest", retry=False):
Example: Example:
```python ```python
tag, assets = get_github_assets(repo='ultralytics/assets', version='latest') tag, assets = get_github_assets(repo="ultralytics/assets", version="latest")
``` ```
""" """
@ -425,7 +425,7 @@ def attempt_download_asset(file, repo="ultralytics/assets", release="v8.2.0", **
Example: Example:
```python ```python
file_path = attempt_download_asset('yolov8n.pt', repo='ultralytics/assets', release='latest') file_path = attempt_download_asset("yolov8n.pt", repo="ultralytics/assets", release="latest")
``` ```
""" """
from ultralytics.utils import SETTINGS # scoped for circular import from ultralytics.utils import SETTINGS # scoped for circular import
@ -480,7 +480,7 @@ def download(url, dir=Path.cwd(), unzip=True, delete=False, curl=False, threads=
Example: Example:
```python ```python
download('https://ultralytics.com/assets/example.zip', dir='path/to/dir', unzip=True) download("https://ultralytics.com/assets/example.zip", dir="path/to/dir", unzip=True)
``` ```
""" """
dir = Path(dir) dir = Path(dir)

@ -28,13 +28,13 @@ class WorkingDirectory(contextlib.ContextDecorator):
Examples: Examples:
Using as a context manager: Using as a context manager:
>>> with WorkingDirectory('/path/to/new/dir'): >>> with WorkingDirectory('/path/to/new/dir'):
>>> # Perform operations in the new directory >>> # Perform operations in the new directory
>>> pass >>> pass
Using as a decorator: Using as a decorator:
>>> @WorkingDirectory('/path/to/new/dir') >>> @WorkingDirectory('/path/to/new/dir')
>>> def some_function(): >>> def some_function():
>>> # Perform operations in the new directory >>> # Perform operations in the new directory
>>> pass >>> pass
""" """
@ -69,7 +69,7 @@ def spaces_in_path(path):
Use the context manager to handle paths with spaces: Use the context manager to handle paths with spaces:
>>> from ultralytics.utils.files import spaces_in_path >>> from ultralytics.utils.files import spaces_in_path
>>> with spaces_in_path('/path/with spaces') as new_path: >>> with spaces_in_path('/path/with spaces') as new_path:
>>> # Your code here >>> # Your code here
""" """
# If path has spaces, replace them with underscores # If path has spaces, replace them with underscores

@ -199,7 +199,7 @@ class Instances:
instances = Instances( instances = Instances(
bboxes=np.array([[10, 10, 30, 30], [20, 20, 40, 40]]), bboxes=np.array([[10, 10, 30, 30], [20, 20, 40, 40]]),
segments=[np.array([[5, 5], [10, 10]]), np.array([[15, 15], [20, 20]])], segments=[np.array([[5, 5], [10, 10]]), np.array([[15, 15], [20, 20]])],
keypoints=np.array([[[5, 5, 1], [10, 10, 1]], [[15, 15, 1], [20, 20, 1]]]) keypoints=np.array([[[5, 5, 1], [10, 10, 1]], [[15, 15, 1], [20, 20, 1]]]),
) )
``` ```

@ -445,7 +445,7 @@ def smooth(y, f=0.05):
@plt_settings() @plt_settings()
def plot_pr_curve(px, py, ap, save_dir=Path("pr_curve.png"), names=(), on_plot=None): def plot_pr_curve(px, py, ap, save_dir=Path("pr_curve.png"), names={}, on_plot=None):
"""Plots a precision-recall curve.""" """Plots a precision-recall curve."""
fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
py = np.stack(py, axis=1) py = np.stack(py, axis=1)
@ -470,7 +470,7 @@ def plot_pr_curve(px, py, ap, save_dir=Path("pr_curve.png"), names=(), on_plot=N
@plt_settings() @plt_settings()
def plot_mc_curve(px, py, save_dir=Path("mc_curve.png"), names=(), xlabel="Confidence", ylabel="Metric", on_plot=None): def plot_mc_curve(px, py, save_dir=Path("mc_curve.png"), names={}, xlabel="Confidence", ylabel="Metric", on_plot=None):
"""Plots a metric-confidence curve.""" """Plots a metric-confidence curve."""
fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
@ -528,7 +528,7 @@ def compute_ap(recall, precision):
def ap_per_class( def ap_per_class(
tp, conf, pred_cls, target_cls, plot=False, on_plot=None, save_dir=Path(), names=(), eps=1e-16, prefix="" tp, conf, pred_cls, target_cls, plot=False, on_plot=None, save_dir=Path(), names={}, eps=1e-16, prefix=""
): ):
""" """
Computes the average precision per class for object detection evaluation. Computes the average precision per class for object detection evaluation.
@ -541,7 +541,7 @@ def ap_per_class(
plot (bool, optional): Whether to plot PR curves or not. Defaults to False. plot (bool, optional): Whether to plot PR curves or not. Defaults to False.
on_plot (func, optional): A callback to pass plots path and data when they are rendered. Defaults to None. on_plot (func, optional): A callback to pass plots path and data when they are rendered. Defaults to None.
save_dir (Path, optional): Directory to save the PR curves. Defaults to an empty path. save_dir (Path, optional): Directory to save the PR curves. Defaults to an empty path.
names (tuple, optional): Tuple of class names to plot PR curves. Defaults to an empty tuple. names (dict, optional): Dict of class names to plot PR curves. Defaults to an empty tuple.
eps (float, optional): A small value to avoid division by zero. Defaults to 1e-16. eps (float, optional): A small value to avoid division by zero. Defaults to 1e-16.
prefix (str, optional): A prefix string for saving the plot files. Defaults to an empty string. prefix (str, optional): A prefix string for saving the plot files. Defaults to an empty string.
@ -799,13 +799,13 @@ class DetMetrics(SimpleClass):
save_dir (Path): A path to the directory where the output plots will be saved. Defaults to current directory. save_dir (Path): A path to the directory where the output plots will be saved. Defaults to current directory.
plot (bool): A flag that indicates whether to plot precision-recall curves for each class. Defaults to False. plot (bool): A flag that indicates whether to plot precision-recall curves for each class. Defaults to False.
on_plot (func): An optional callback to pass plots path and data when they are rendered. Defaults to None. on_plot (func): An optional callback to pass plots path and data when they are rendered. Defaults to None.
names (tuple of str): A tuple of strings that represents the names of the classes. Defaults to an empty tuple. names (dict of str): A dict of strings that represents the names of the classes. Defaults to an empty tuple.
Attributes: Attributes:
save_dir (Path): A path to the directory where the output plots will be saved. save_dir (Path): A path to the directory where the output plots will be saved.
plot (bool): A flag that indicates whether to plot the precision-recall curves for each class. plot (bool): A flag that indicates whether to plot the precision-recall curves for each class.
on_plot (func): An optional callback to pass plots path and data when they are rendered. on_plot (func): An optional callback to pass plots path and data when they are rendered.
names (tuple of str): A tuple of strings that represents the names of the classes. names (dict of str): A dict of strings that represents the names of the classes.
box (Metric): An instance of the Metric class for storing the results of the detection metrics. box (Metric): An instance of the Metric class for storing the results of the detection metrics.
speed (dict): A dictionary for storing the execution time of different parts of the detection process. speed (dict): A dictionary for storing the execution time of different parts of the detection process.
@ -822,7 +822,7 @@ class DetMetrics(SimpleClass):
curves_results: TODO curves_results: TODO
""" """
def __init__(self, save_dir=Path("."), plot=False, on_plot=None, names=()) -> None: def __init__(self, save_dir=Path("."), plot=False, on_plot=None, names={}) -> None:
"""Initialize a DetMetrics instance with a save directory, plot flag, callback function, and class names.""" """Initialize a DetMetrics instance with a save directory, plot flag, callback function, and class names."""
self.save_dir = save_dir self.save_dir = save_dir
self.plot = plot self.plot = plot

@ -902,8 +902,8 @@ def save_one_box(xyxy, im, file=Path("im.jpg"), gain=1.02, pad=10, square=False,
from ultralytics.utils.plotting import save_one_box from ultralytics.utils.plotting import save_one_box
xyxy = [50, 50, 150, 150] xyxy = [50, 50, 150, 150]
im = cv2.imread('image.jpg') im = cv2.imread("image.jpg")
cropped_im = save_one_box(xyxy, im, file='cropped.jpg', square=True) cropped_im = save_one_box(xyxy, im, file="cropped.jpg", square=True)
``` ```
""" """
@ -1109,7 +1109,7 @@ def plot_results(file="path/to/results.csv", dir="", segment=False, pose=False,
```python ```python
from ultralytics.utils.plotting import plot_results from ultralytics.utils.plotting import plot_results
plot_results('path/to/results.csv', segment=True) plot_results("path/to/results.csv", segment=True)
``` ```
""" """
import pandas as pd # scope for faster 'import ultralytics' import pandas as pd # scope for faster 'import ultralytics'
@ -1195,7 +1195,7 @@ def plot_tune_results(csv_file="tune_results.csv"):
csv_file (str, optional): Path to the CSV file containing the tuning results. Defaults to 'tune_results.csv'. csv_file (str, optional): Path to the CSV file containing the tuning results. Defaults to 'tune_results.csv'.
Examples: Examples:
>>> plot_tune_results('path/to/tune_results.csv') >>> plot_tune_results("path/to/tune_results.csv")
""" """
import pandas as pd # scope for faster 'import ultralytics' import pandas as pd # scope for faster 'import ultralytics'

@ -137,10 +137,10 @@ def select_device(device="", batch=0, newline=False, verbose=True):
devices when using multiple GPUs. devices when using multiple GPUs.
Examples: Examples:
>>> select_device('cuda:0') >>> select_device("cuda:0")
device(type='cuda', index=0) device(type='cuda', index=0)
>>> select_device('cpu') >>> select_device("cpu")
device(type='cpu') device(type='cpu')
Note: Note:
@ -331,11 +331,13 @@ def model_info_for_loggers(trainer):
Example: Example:
YOLOv8n info for loggers YOLOv8n info for loggers
```python ```python
results = {'model/parameters': 3151904, results = {
'model/GFLOPs': 8.746, "model/parameters": 3151904,
'model/speed_ONNX(ms)': 41.244, "model/GFLOPs": 8.746,
'model/speed_TensorRT(ms)': 3.211, "model/speed_ONNX(ms)": 41.244,
'model/speed_PyTorch(ms)': 18.755} "model/speed_TensorRT(ms)": 3.211,
"model/speed_PyTorch(ms)": 18.755,
}
``` ```
""" """
if trainer.args.profile: # profile ONNX and TensorRT times if trainer.args.profile: # profile ONNX and TensorRT times
@ -542,7 +544,7 @@ def strip_optimizer(f: Union[str, Path] = "best.pt", s: str = "") -> None:
from pathlib import Path from pathlib import Path
from ultralytics.utils.torch_utils import strip_optimizer from ultralytics.utils.torch_utils import strip_optimizer
for f in Path('path/to/model/checkpoints').rglob('*.pt'): for f in Path("path/to/model/checkpoints").rglob("*.pt"):
strip_optimizer(f) strip_optimizer(f)
``` ```

@ -28,10 +28,10 @@ def run_ray_tune(
from ultralytics import YOLO from ultralytics import YOLO
# Load a YOLOv8n model # Load a YOLOv8n model
model = YOLO('yolov8n.pt') model = YOLO("yolov8n.pt")
# Start tuning hyperparameters for YOLOv8n training on the COCO8 dataset # Start tuning hyperparameters for YOLOv8n training on the COCO8 dataset
result_grid = model.tune(data='coco8.yaml', use_ray=True) result_grid = model.tune(data="coco8.yaml", use_ray=True)
``` ```
""" """

Loading…
Cancel
Save