From 8c28e0c3fee2fdd00297b90222d39d4c4db89772 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 22 Feb 2024 22:16:52 +0100 Subject: [PATCH 01/10] Add missing single-line docstrings (#8362) Signed-off-by: Glenn Jocher Co-authored-by: Johannes Kaisinger --- docs/build_docs.py | 2 +- tests/test_python.py | 2 ++ ultralytics/models/yolo/obb/train.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/build_docs.py b/docs/build_docs.py index 71556781b..ddcbcc57c 100644 --- a/docs/build_docs.py +++ b/docs/build_docs.py @@ -115,7 +115,7 @@ def update_subdir_edit_links(subdir="", docs_url=""): def main(): - # Build the docs + """Builds docs, updates titles and edit links, and prints local server command.""" build_docs() # Update titles diff --git a/tests/test_python.py b/tests/test_python.py index d3b816b20..adfb5b037 100644 --- a/tests/test_python.py +++ b/tests/test_python.py @@ -555,6 +555,7 @@ def test_hub(): @pytest.fixture def image(): + """Loads an image from a predefined source using OpenCV.""" return cv2.imread(str(SOURCE)) @@ -568,6 +569,7 @@ def image(): ], ) def test_classify_transforms_train(image, auto_augment, erasing, force_color_jitter): + """Tests classification transforms during training with various augmentation settings.""" import torchvision.transforms as T from ultralytics.data.augment import classify_augmentations diff --git a/ultralytics/models/yolo/obb/train.py b/ultralytics/models/yolo/obb/train.py index 43ebaecd1..40a35a99e 100644 --- a/ultralytics/models/yolo/obb/train.py +++ b/ultralytics/models/yolo/obb/train.py @@ -15,7 +15,7 @@ class OBBTrainer(yolo.detect.DetectionTrainer): ```python from ultralytics.models.yolo.obb import OBBTrainer - args = dict(model='yolov8n-seg.pt', data='coco8-seg.yaml', epochs=3) + args = dict(model='yolov8n-obb.pt', data='dota8.yaml', epochs=3) trainer = OBBTrainer(overrides=args) trainer.train() ``` From 0572b294458524f3861bb4af6204150eaf47852b Mon Sep 17 00:00:00 2001 From: Lakshantha Dissanayake Date: Thu, 22 Feb 2024 14:43:09 -0800 Subject: [PATCH 02/10] `ultralytics 8.1.18` add cmake for building onnxsim on aarch64 (#8395) Co-authored-by: Glenn Jocher Co-authored-by: UltralyticsAssistant --- docker/Dockerfile-arm64 | 2 +- ultralytics/__init__.py | 2 +- ultralytics/engine/exporter.py | 10 ++++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docker/Dockerfile-arm64 b/docker/Dockerfile-arm64 index c56945f25..dd612b051 100644 --- a/docker/Dockerfile-arm64 +++ b/docker/Dockerfile-arm64 @@ -15,7 +15,7 @@ ADD https://github.com/ultralytics/assets/releases/download/v0.0.0/Arial.ttf \ # g++ required to build 'tflite_support' and 'lap' packages, libusb-1.0-0 required for 'tflite_support' package # cmake and build-essential is needed to build onnxsim when exporting to tflite RUN apt update \ - && apt install --no-install-recommends -y python3-pip git zip curl htop gcc libgl1 libglib2.0-0 libpython3-dev gnupg g++ libusb-1.0-0 cmake build-essential + && apt install --no-install-recommends -y python3-pip git zip curl htop gcc libgl1 libglib2.0-0 libpython3-dev gnupg g++ libusb-1.0-0 build-essential # Create working directory WORKDIR /usr/src/ultralytics diff --git a/ultralytics/__init__.py b/ultralytics/__init__.py index 7749fedd4..9c24e00cc 100644 --- a/ultralytics/__init__.py +++ b/ultralytics/__init__.py @@ -1,6 +1,6 @@ # Ultralytics YOLO 🚀, AGPL-3.0 license -__version__ = "8.1.17" +__version__ = "8.1.18" from ultralytics.data.explorer.explorer import Explorer from ultralytics.models import RTDETR, SAM, YOLO, YOLOWorld diff --git a/ultralytics/engine/exporter.py b/ultralytics/engine/exporter.py index 20c7d3b7d..68892958e 100644 --- a/ultralytics/engine/exporter.py +++ b/ultralytics/engine/exporter.py @@ -343,6 +343,8 @@ class Exporter: requirements = ["onnx>=1.12.0"] if self.args.simplify: requirements += ["onnxsim>=0.4.33", "onnxruntime-gpu" if torch.cuda.is_available() else "onnxruntime"] + if ARM64: + check_requirements("cmake") # 'cmake' is needed to build onnxsim on aarch64 check_requirements(requirements) import onnx # noqa @@ -712,8 +714,12 @@ class Exporter: try: import tensorflow as tf # noqa except ImportError: - check_requirements(f"tensorflow{'-macos' if MACOS else '-aarch64' if ARM64 else '' if cuda else '-cpu'}") + suffix = "-macos" if MACOS else "-aarch64" if ARM64 else "" if cuda else "-cpu" + version = "" if ARM64 else "<=2.13.1" + check_requirements(f"tensorflow{suffix}{version}") import tensorflow as tf # noqa + if ARM64: + check_requirements("cmake") # 'cmake' is needed to build onnxsim on aarch64 check_requirements( ( "onnx>=1.12.0", @@ -722,7 +728,7 @@ class Exporter: "onnxsim>=0.4.33", "onnx_graphsurgeon>=0.3.26", "tflite_support", - "flatbuffers>=23.5.26", # update old 'flatbuffers' included inside tensorflow package + "flatbuffers>=23.5.26,<100", # update old 'flatbuffers' included inside tensorflow package "onnxruntime-gpu" if cuda else "onnxruntime", ), cmds="--extra-index-url https://pypi.ngc.nvidia.com", From 21088e3986971a596fc3149ec017921eb2ce93c8 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 23 Feb 2024 11:52:43 +0100 Subject: [PATCH 03/10] Allow Annotator PIL.Image inputs (#8397) --- ultralytics/utils/plotting.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ultralytics/utils/plotting.py b/ultralytics/utils/plotting.py index 979ba2d84..3c83bc4f7 100644 --- a/ultralytics/utils/plotting.py +++ b/ultralytics/utils/plotting.py @@ -112,12 +112,12 @@ class Annotator: def __init__(self, im, line_width=None, font_size=None, font="Arial.ttf", pil=False, example="abc"): """Initialize the Annotator class with image and line width along with color palette for keypoints and limbs.""" - assert im.data.contiguous, "Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images." non_ascii = not is_ascii(example) # non-latin labels, i.e. asian, arabic, cyrillic - self.pil = pil or non_ascii - self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width + input_is_pil = isinstance(im, Image.Image) + self.pil = pil or non_ascii or input_is_pil + self.lw = line_width or max(round(sum(im.size if input_is_pil else im.shape) / 2 * 0.003), 2) if self.pil: # use PIL - self.im = im if isinstance(im, Image.Image) else Image.fromarray(im) + self.im = im if input_is_pil else Image.fromarray(im) self.draw = ImageDraw.Draw(self.im) try: font = check_font("Arial.Unicode.ttf" if non_ascii else font) @@ -129,6 +129,7 @@ class Annotator: if check_version(pil_version, "9.2.0"): self.font.getsize = lambda x: self.font.getbbox(x)[2:4] # text width, height else: # use cv2 + assert im.data.contiguous, "Image not contiguous. Apply np.ascontiguousarray(im) to Annotator input images." self.im = im if im.flags.writeable else im.copy() self.tf = max(self.lw - 1, 1) # font thickness self.sf = self.lw / 3 # font scale From d6edf1c1d8b67b7ff3829609f29bcacb4f107c29 Mon Sep 17 00:00:00 2001 From: AlainSchoebi <44315825+AlainSchoebi@users.noreply.github.com> Date: Fri, 23 Feb 2024 14:28:25 +0100 Subject: [PATCH 04/10] Add Non-Maximum Suppression (NMS) `inplace` flag (#8368) --- ultralytics/utils/ops.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ultralytics/utils/ops.py b/ultralytics/utils/ops.py index d1bfc6a87..4a8351609 100644 --- a/ultralytics/utils/ops.py +++ b/ultralytics/utils/ops.py @@ -173,6 +173,7 @@ def non_max_suppression( max_time_img=0.05, max_nms=30000, max_wh=7680, + in_place=True, rotated=False, ): """ @@ -197,7 +198,8 @@ def non_max_suppression( nc (int, optional): The number of classes output by the model. Any indices after this will be considered masks. max_time_img (float): The maximum time (seconds) for processing one image. max_nms (int): The maximum number of boxes into torchvision.ops.nms(). - max_wh (int): The maximum box width and height in pixels + max_wh (int): The maximum box width and height in pixels. + in_place (bool): If True, the input prediction tensor will be modified in place. Returns: (List[torch.Tensor]): A list of length batch_size, where each element is a tensor of @@ -224,7 +226,10 @@ def non_max_suppression( prediction = prediction.transpose(-1, -2) # shape(1,84,6300) to shape(1,6300,84) if not rotated: - prediction[..., :4] = xywh2xyxy(prediction[..., :4]) # xywh to xyxy + if in_place: + prediction[..., :4] = xywh2xyxy(prediction[..., :4]) # xywh to xyxy + else: + prediction = torch.cat((xywh2xyxy(prediction[..., :4]), prediction[..., 4:]), dim=-1) # xywh to xyxy t = time.time() output = [torch.zeros((0, 6 + nm), device=prediction.device)] * bs From 60b6cab6cfba0e6420f249cd2bb1eadc6b04deaf Mon Sep 17 00:00:00 2001 From: Mactarvish Date: Sat, 24 Feb 2024 03:04:41 +0800 Subject: [PATCH 05/10] Compare `plt.get_backend()` in lowercase (#8409) Co-authored-by: Glenn Jocher --- ultralytics/utils/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ultralytics/utils/__init__.py b/ultralytics/utils/__init__.py index 5fc0c71b7..ddc9ba3f8 100644 --- a/ultralytics/utils/__init__.py +++ b/ultralytics/utils/__init__.py @@ -212,7 +212,7 @@ def plt_settings(rcparams=None, backend="Agg"): def wrapper(*args, **kwargs): """Sets rc parameters and backend, calls the original function, and restores the settings.""" original_backend = plt.get_backend() - if backend != original_backend: + if backend.lower() != original_backend.lower(): plt.close("all") # auto-close()ing of figures upon backend switching is deprecated since 3.8 plt.switch_backend(backend) From 30729d73469fb23aff3d7f3684638366f03fe5ba Mon Sep 17 00:00:00 2001 From: Burhan <62214284+Burhan-Q@users.noreply.github.com> Date: Sun, 25 Feb 2024 09:42:17 -0500 Subject: [PATCH 06/10] Improve Docs arguments tables (#8415) Co-authored-by: Glenn Jocher --- docs/en/modes/export.md | 26 +++---- docs/en/modes/train.md | 34 ++++++++- docs/en/modes/val.md | 32 ++++----- docs/en/usage/cfg.md | 155 +++++++++++++++++++++------------------- 4 files changed, 141 insertions(+), 106 deletions(-) diff --git a/docs/en/modes/export.md b/docs/en/modes/export.md index a46abdd57..61576cf9e 100644 --- a/docs/en/modes/export.md +++ b/docs/en/modes/export.md @@ -74,19 +74,19 @@ Export a YOLOv8n model to a different format like ONNX or TensorRT. See Argument This table details the configurations and options available for exporting YOLO models to different formats. These settings are critical for optimizing the exported model's performance, size, and compatibility across various platforms and environments. Proper configuration ensures that the model is ready for deployment in the intended application with optimal efficiency. -| Key | Default Value | Description | -|-------------|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `format` | `'torchscript'` | Specifies the export format. Supported values include `'torchscript'`, `'onnx'`, `'coreml'`, `'engine'` (TensorRT), `'saved_model'` (TensorFlow SavedModel), etc. | -| `imgsz` | `640` | Defines the image size for export. Accepts an integer for square images or a tuple `(height, width)` for non-square images. | -| `keras` | `False` | When exporting to TensorFlow SavedModel, setting this to `True` utilizes Keras for the export process. | -| `optimize` | `False` | Applies to TorchScript exports, enabling optimization for mobile deployments. | -| `half` | `False` | Enables half-precision (FP16) quantization for the exported model, reducing size and potentially increasing inference speed on compatible hardware. | -| `int8` | `False` | Activates INT8 quantization, further reducing model size and increasing inference speed at the cost of precision. Useful for edge devices. | -| `dynamic` | `False` | For ONNX and TensorRT formats, enables dynamic axes, allowing variable input sizes for inference. | -| `simplify` | `False` | Simplifies the model structure for ONNX and TensorRT formats, potentially improving efficiency and compatibility. | -| `opset` | `None` | Specifies the ONNX opset version for export. If not set, uses the latest supported version. Useful for ensuring compatibility with older ONNX parsers. | -| `workspace` | `4` | Defines the maximum workspace size in GB for TensorRT exports, affecting the optimization process and memory usage. | -| `nms` | `False` | When exporting to CoreML, adds a Non-Maximum Suppression (NMS) layer to the model, useful for filtering overlapping detections. | +| Argument | Type | Default | Description | +|-------------|------------------|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `format` | `str` | `'torchscript'` | Target format for the exported model, such as `'onnx'`, `'torchscript'`, `'tensorflow'`, or others, defining compatibility with various deployment environments. | +| `imgsz` | `int` or `tuple` | `640` | Desired image size for the model input. Can be an integer for square images or a tuple `(height, width)` for specific dimensions. | +| `keras` | `bool` | `False` | Enables export to Keras format for TensorFlow SavedModel, providing compatibility with TensorFlow serving and APIs. | +| `optimize` | `bool` | `False` | Applies optimization for mobile devices when exporting to TorchScript, potentially reducing model size and improving performance. | +| `half` | `bool` | `False` | Enables FP16 (half-precision) quantization, reducing model size and potentially speeding up inference on supported hardware. | +| `int8` | `bool` | `False` | Activates INT8 quantization, further compressing the model and speeding up inference with minimal accuracy loss, primarily for edge devices. | +| `dynamic` | `bool` | `False` | Allows dynamic input sizes for ONNX and TensorRT exports, enhancing flexibility in handling varying image dimensions. | +| `simplify` | `bool` | `False` | Simplifies the model graph for ONNX exports, potentially improving performance and compatibility. | +| `opset` | `int` | `None` | Specifies the ONNX opset version for compatibility with different ONNX parsers and runtimes. If not set, uses the latest supported version. | +| `workspace` | `float` | `4.0` | Sets the maximum workspace size in GB for TensorRT optimizations, balancing memory usage and performance. | +| `nms` | `bool` | `False` | Adds Non-Maximum Suppression (NMS) to the CoreML export, essential for accurate and efficient detection post-processing. | Adjusting these parameters allows for customization of the export process to fit specific requirements, such as deployment environment, hardware constraints, and performance targets. Selecting the appropriate format and settings is essential for achieving the best balance between model size, speed, and accuracy. diff --git a/docs/en/modes/train.md b/docs/en/modes/train.md index 51e24840c..820ff4ba9 100644 --- a/docs/en/modes/train.md +++ b/docs/en/modes/train.md @@ -171,11 +171,11 @@ By setting `resume=True`, the `train` function will continue training from where Remember that checkpoints are saved at the end of every epoch by default, or at fixed interval using the `save_period` argument, so you must complete at least 1 epoch to resume a training run. -## Arguments +## Train Settings -Training settings for YOLO models refer to the various hyperparameters and configurations used to train the model on a dataset. These settings can affect the model's performance, speed, and accuracy. Some common YOLO training settings include the batch size, learning rate, momentum, and weight decay. Other factors that may affect the training process include the choice of optimizer, the choice of loss function, and the size and composition of the training dataset. It is important to carefully tune and experiment with these settings to achieve the best possible performance for a given task. +The training settings for YOLO models encompass various hyperparameters and configurations used during the training process. These settings influence the model's performance, speed, and accuracy. Key training settings include batch size, learning rate, momentum, and weight decay. Additionally, the choice of optimizer, loss function, and training dataset composition can impact the training process. Careful tuning and experimentation with these settings are crucial for optimizing performance. -| Key | Default | Description | +| Argument | Default | Description | |-------------------|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `model` | `None` | Specifies the model file for training. Accepts a path to either a `.pt` pretrained model or a `.yaml` configuration file. Essential for defining the model structure or initializing weights. | | `data` | `None` | Path to the dataset configuration file (e.g., `coco128.yaml`). This file contains dataset-specific parameters, including paths to training and validation data, class names, and number of classes. | @@ -226,6 +226,34 @@ Training settings for YOLO models refer to the various hyperparameters and confi | `val` | `True` | Enables validation during training, allowing for periodic evaluation of model performance on a separate dataset. | | `plots` | `False` | Generates and saves plots of training and validation metrics, as well as prediction examples, providing visual insights into model performance and learning progression. | +## Augmentation Settings and Hyperparameters + +Augmentation techniques are essential for improving the robustness and performance of YOLO models by introducing variability into the training data, helping the model generalize better to unseen data. The following table outlines the purpose and effect of each augmentation argument: + +| Argument | Type | Default | Range | Description | +|----------------|---------|---------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `hsv_h` | `float` | `0.015` | `0.0 - 1.0` | Adjusts the hue of the image by a fraction of the color wheel, introducing color variability. Helps the model generalize across different lighting conditions. | +| `hsv_s` | `float` | `0.7` | `0.0 - 1.0` | Alters the saturation of the image by a fraction, affecting the intensity of colors. Useful for simulating different environmental conditions. | +| `hsv_v` | `float` | `0.4` | `0.0 - 1.0` | Modifies the value (brightness) of the image by a fraction, helping the model to perform well under various lighting conditions. | +| `degrees` | `float` | `0.0` | `-180 - +180` | Rotates the image randomly within the specified degree range, improving the model's ability to recognize objects at various orientations. | +| `translate` | `float` | `0.1` | `0.0 - 1.0` | Translates the image horizontally and vertically by a fraction of the image size, aiding in learning to detect partially visible objects. | +| `scale` | `float` | `0.5` | `>=0.0` | Scales the image by a gain factor, simulating objects at different distances from the camera. | +| `shear` | `float` | `0.0` | `-180 - +180` | Shears the image by a specified degree, mimicking the effect of objects being viewed from different angles. | +| `perspective` | `float` | `0.0` | `0.0 - 0.001` | Applies a random perspective transformation to the image, enhancing the model's ability to understand objects in 3D space. | +| `flipud` | `float` | `0.0` | `0.0 - 1.0` | Flips the image upside down with the specified probability, increasing the data variability without affecting the object's characteristics. | +| `fliplr` | `float` | `0.5` | `0.0 - 1.0` | Flips the image left to right with the specified probability, useful for learning symmetrical objects and increasing dataset diversity. | +| `mosaic` | `float` | `1.0` | `0.0 - 1.0` | Combines four training images into one, simulating different scene compositions and object interactions. Highly effective for complex scene understanding. | +| `mixup` | `float` | `0.0` | `0.0 - 1.0` | Blends two images and their labels, creating a composite image. Enhances the model's ability to generalize by introducing label noise and visual variability. | +| `copy_paste` | `float` | `0.0` | `0.0 - 1.0` | Copies objects from one image and pastes them onto another, useful for increasing object instances and learning object occlusion. | +| `auto_augment` | `str` | `randaugment` | - | Automatically applies a predefined augmentation policy (`randaugment`, `autoaugment`, `augmix`), optimizing for classification tasks by diversifying the visual features. | +| `erasing` | `float` | `0.4` | `0.0 - 1.0` | Randomly erases a portion of the image during classification training, encouraging the model to focus on less obvious features for recognition. | + +These settings can be adjusted to meet the specific requirements of the dataset and task at hand. Experimenting with different values can help find the optimal augmentation strategy that leads to the best model performance. + +!!! info + + For more information about training augmentation operations, see the [reference section](../reference/data/augment.md). + ## Logging In training a YOLOv8 model, you might find it valuable to keep track of the model's performance over time. This is where logging comes into play. Ultralytics' YOLO provides support for three types of loggers - Comet, ClearML, and TensorBoard. diff --git a/docs/en/modes/val.md b/docs/en/modes/val.md index 63da70a7c..82eb4d96d 100644 --- a/docs/en/modes/val.md +++ b/docs/en/modes/val.md @@ -79,22 +79,22 @@ Validate trained YOLOv8n model accuracy on the COCO128 dataset. No argument need When validating YOLO models, several arguments can be fine-tuned to optimize the evaluation process. These arguments control aspects such as input image size, batch processing, and performance thresholds. Below is a detailed breakdown of each argument to help you customize your validation settings effectively. -| Key | Default Value | Description | -|---------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `data` | `None` | The path to the dataset configuration file (e.g., `coco128.yaml`). This file specifies the dataset's structure, including the classes, train, and validation set paths. | -| `imgsz` | `640` | The input image size as an integer. This size is used to resize images during validation, impacting detection accuracy and inference speed. | -| `batch` | `16` | The number of images processed in each batch. A larger batch size can speed up validation but requires more memory. Use `-1` for AutoBatch to automatically adjust based on available memory. | -| `save_json` | `False` | If set to `True`, validation results are saved in a JSON format, useful for further analysis or submission to evaluation servers. | -| `save_hybrid` | `False` | When `True`, saves a hybrid version of labels combining ground truth with model predictions. This can be useful for visualizing model performance or training enhancements. | -| `conf` | `0.001` | The minimum confidence threshold for considering detections. Increasing this value may reduce false positives but could also miss less confident detections. | -| `iou` | `0.6` | The Intersection Over Union (IoU) threshold for Non-Maximum Suppression (NMS). Higher values result in fewer detections by eliminating more overlapping boxes. | -| `max_det` | `300` | The maximum number of detections allowed per image. Useful for limiting outputs in images with many objects. | -| `half` | `True` | Enables half precision (FP16) to speed up validation on compatible hardware without significantly affecting accuracy. | -| `device` | `None` | Specifies the computation device, such as a specific GPU (`cuda:0`) or CPU (`cpu`). This setting allows for model validation on different hardware configurations. | -| `dnn` | `False` | If `True`, uses OpenCV's DNN module for ONNX model inference. This option can be beneficial for environments where CUDA is unavailable. | -| `plots` | `False` | Enables the generation of plots and saved images during validation, providing visual insights into model performance. | -| `rect` | `False` | Applies rectangular inference, minimizing padding by processing images in their original aspect ratio. This can improve accuracy and speed but may require more memory. | -| `split` | `val` | Defines the dataset split to use for validation (e.g., 'val', 'test', 'train'). This allows for flexible validation across different parts of the dataset. | +| Argument | Type | Default | Description | +|---------------|---------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `data` | `str` | `None` | Specifies the path to the dataset configuration file (e.g., `coco128.yaml`). This file includes paths to validation data, class names, and number of classes. | +| `imgsz` | `int` | `640` | Defines the size of input images. All images are resized to this dimension before processing. | +| `batch` | `int` | `16` | Sets the number of images per batch. Use `-1` for AutoBatch, which automatically adjusts based on GPU memory availability. | +| `save_json` | `bool` | `False` | If `True`, saves the results to a JSON file for further analysis or integration with other tools. | +| `save_hybrid` | `bool` | `False` | If `True`, saves a hybrid version of labels that combines original annotations with additional model predictions. | +| `conf` | `float` | `0.001` | Sets the minimum confidence threshold for detections. Detections with confidence below this threshold are discarded. | +| `iou` | `float` | `0.6` | Sets the Intersection Over Union (IoU) threshold for Non-Maximum Suppression (NMS). Helps in reducing duplicate detections. | +| `max_det` | `int` | `300` | Limits the maximum number of detections per image. Useful in dense scenes to prevent excessive detections. | +| `half` | `bool` | `True` | Enables half-precision (FP16) computation, reducing memory usage and potentially increasing speed with minimal impact on accuracy. | +| `device` | `str` | `None` | Specifies the device for validation (`cpu`, `cuda:0`, etc.). Allows flexibility in utilizing CPU or GPU resources. | +| `dnn` | `bool` | `False` | If `True`, uses OpenCV's DNN module for ONNX model inference, offering an alternative to PyTorch inference methods. | +| `plots` | `bool` | `False` | When set to `True`, generates and saves plots of predictions versus ground truth for visual evaluation of the model's performance. | +| `rect` | `bool` | `False` | If `True`, uses rectangular inference for batching, reducing padding and potentially increasing speed and efficiency. | +| `split` | `str` | `val` | Determines the dataset split to use for validation (`val`, `test`, or `train`). Allows flexibility in choosing the data segment for performance evaluation. | Each of these settings plays a vital role in the validation process, allowing for a customizable and efficient evaluation of YOLO models. Adjusting these parameters according to your specific needs and resources can help achieve the best balance between accuracy and performance. diff --git a/docs/en/usage/cfg.md b/docs/en/usage/cfg.md index 4631de2ba..f029ed398 100644 --- a/docs/en/usage/cfg.md +++ b/docs/en/usage/cfg.md @@ -55,10 +55,11 @@ YOLO models can be used for a variety of tasks, including detection, segmentatio - **Segment**: For dividing an image or video into regions or pixels that correspond to different objects or classes. - **Classify**: For predicting the class label of an input image. - **Pose**: For identifying objects and estimating their keypoints in an image or video. +- **OBB**: Oriented (i.e. rotated) bounding boxes suitable for satellite or medical imagery. -| Key | Value | Description | -|--------|------------|-------------------------------------------------| -| `task` | `'detect'` | YOLO task, i.e. detect, segment, classify, pose | +| Argument | Default | Description | +|----------|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `task` | `'detect'` | Specifies the YOLO task to be executed. Options include `detect` for object detection, `segment` for segmentation, `classify` for classification, `pose` for pose estimation and `OBB` for oriented bounding boxes. Each task is tailored to specific types of output and problems within image and video analysis. | [Tasks Guide](../tasks/index.md){ .md-button } @@ -73,17 +74,17 @@ YOLO models can be used in different modes depending on the specific problem you - **Track**: For tracking objects in real-time using a YOLOv8 model. - **Benchmark**: For benchmarking YOLOv8 exports (ONNX, TensorRT, etc.) speed and accuracy. -| Key | Value | Description | -|--------|-----------|---------------------------------------------------------------| -| `mode` | `'train'` | YOLO mode, i.e. train, val, predict, export, track, benchmark | +| Argument | Default | Description | +|----------|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `mode` | `'train'` | Specifies the mode in which the YOLO model operates. Options are `train` for model training, `val` for validation, `predict` for inference on new data, `export` for model conversion to deployment formats, `track` for object tracking, and `benchmark` for performance evaluation. Each mode is designed for different stages of the model lifecycle, from development through deployment. | [Modes Guide](../modes/index.md){ .md-button } -## Train +## Train Settings The training settings for YOLO models encompass various hyperparameters and configurations used during the training process. These settings influence the model's performance, speed, and accuracy. Key training settings include batch size, learning rate, momentum, and weight decay. Additionally, the choice of optimizer, loss function, and training dataset composition can impact the training process. Careful tuning and experimentation with these settings are crucial for optimizing performance. -| Key | Default | Description | +| Argument | Default | Description | |-------------------|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `model` | `None` | Specifies the model file for training. Accepts a path to either a `.pt` pretrained model or a `.yaml` configuration file. Essential for defining the model structure or initializing weights. | | `data` | `None` | Path to the dataset configuration file (e.g., `coco128.yaml`). This file contains dataset-specific parameters, including paths to training and validation data, class names, and number of classes. | @@ -136,7 +137,7 @@ The training settings for YOLO models encompass various hyperparameters and conf [Train Guide](../modes/train.md){ .md-button } -## Predict +## Predict Settings The prediction settings for YOLO models encompass a range of hyperparameters and configurations that influence the model's performance, speed, and accuracy during inference on new data. Careful tuning and experimentation with these settings are essential to achieve optimal performance for a specific task. Key settings include the confidence threshold, Non-Maximum Suppression (NMS) threshold, and the number of classes considered. Additional factors affecting the prediction process are input data size and format, the presence of supplementary features such as masks or multiple labels per box, and the particular task the model is employed for. @@ -177,72 +178,78 @@ Visualization arguments: [Predict Guide](../modes/predict.md){ .md-button } -## Val - -The val (validation) settings for YOLO models involve various hyperparameters and configurations used to evaluate the model's performance on a validation dataset. These settings influence the model's performance, speed, and accuracy. Common YOLO validation settings include batch size, validation frequency during training, and performance evaluation metrics. Other factors affecting the validation process include the validation dataset's size and composition, as well as the specific task the model is employed for. Careful tuning and experimentation with these settings are crucial to ensure optimal performance on the validation dataset and detect and prevent overfitting. - -| Key | Value | Description | -|---------------|---------|--------------------------------------------------------------------| -| `data` | `None` | path to data file, i.e. coco128.yaml | -| `imgsz` | `640` | size of input images as integer | -| `batch` | `16` | number of images per batch (-1 for AutoBatch) | -| `save_json` | `False` | save results to JSON file | -| `save_hybrid` | `False` | save hybrid version of labels (labels + additional predictions) | -| `conf` | `0.001` | object confidence threshold for detection | -| `iou` | `0.6` | intersection over union (IoU) threshold for NMS | -| `max_det` | `300` | maximum number of detections per image | -| `half` | `True` | use half precision (FP16) | -| `device` | `None` | device to run on, i.e. cuda device=0/1/2/3 or device=cpu | -| `dnn` | `False` | use OpenCV DNN for ONNX inference | -| `plots` | `False` | save plots and images during train/val | -| `rect` | `False` | rectangular val with each batch collated for minimum padding | -| `split` | `val` | dataset split to use for validation, i.e. 'val', 'test' or 'train' | +## Validation Settings + +The val (validation) settings for YOLO models involve various hyperparameters and configurations used to evaluate the model's performance on a validation dataset. These settings influence the model's performance, speed, and accuracy. Common YOLO validation settings include batch size, validation frequency during training, and performance evaluation metrics. Other factors affecting the validation process include the validation dataset's size and composition, as well as the specific task the model is employed for. + +| Argument | Type | Default | Description | +|---------------|---------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `data` | `str` | `None` | Specifies the path to the dataset configuration file (e.g., `coco128.yaml`). This file includes paths to validation data, class names, and number of classes. | +| `imgsz` | `int` | `640` | Defines the size of input images. All images are resized to this dimension before processing. | +| `batch` | `int` | `16` | Sets the number of images per batch. Use `-1` for AutoBatch, which automatically adjusts based on GPU memory availability. | +| `save_json` | `bool` | `False` | If `True`, saves the results to a JSON file for further analysis or integration with other tools. | +| `save_hybrid` | `bool` | `False` | If `True`, saves a hybrid version of labels that combines original annotations with additional model predictions. | +| `conf` | `float` | `0.001` | Sets the minimum confidence threshold for detections. Detections with confidence below this threshold are discarded. | +| `iou` | `float` | `0.6` | Sets the Intersection Over Union (IoU) threshold for Non-Maximum Suppression (NMS). Helps in reducing duplicate detections. | +| `max_det` | `int` | `300` | Limits the maximum number of detections per image. Useful in dense scenes to prevent excessive detections. | +| `half` | `bool` | `True` | Enables half-precision (FP16) computation, reducing memory usage and potentially increasing speed with minimal impact on accuracy. | +| `device` | `str` | `None` | Specifies the device for validation (`cpu`, `cuda:0`, etc.). Allows flexibility in utilizing CPU or GPU resources. | +| `dnn` | `bool` | `False` | If `True`, uses OpenCV's DNN module for ONNX model inference, offering an alternative to PyTorch inference methods. | +| `plots` | `bool` | `False` | When set to `True`, generates and saves plots of predictions versus ground truth for visual evaluation of the model's performance. | +| `rect` | `bool` | `False` | If `True`, uses rectangular inference for batching, reducing padding and potentially increasing speed and efficiency. | +| `split` | `str` | `val` | Determines the dataset split to use for validation (`val`, `test`, or `train`). Allows flexibility in choosing the data segment for performance evaluation. | + +Careful tuning and experimentation with these settings are crucial to ensure optimal performance on the validation dataset and detect and prevent overfitting. [Val Guide](../modes/val.md){ .md-button } -## Export +## Export Settings -Export settings for YOLO models encompass configurations and options related to saving or exporting the model for use in different environments or platforms. These settings can impact the model's performance, size, and compatibility with various systems. Key export settings include the exported model file format (e.g., ONNX, TensorFlow SavedModel), the target device (e.g., CPU, GPU), and additional features such as masks or multiple labels per box. The export process may also be affected by the model's specific task and the requirements or constraints of the destination environment or platform. It is crucial to thoughtfully configure these settings to ensure the exported model is optimized for the intended use case and functions effectively in the target environment. +Export settings for YOLO models encompass configurations and options related to saving or exporting the model for use in different environments or platforms. These settings can impact the model's performance, size, and compatibility with various systems. Key export settings include the exported model file format (e.g., ONNX, TensorFlow SavedModel), the target device (e.g., CPU, GPU), and additional features such as masks or multiple labels per box. The export process may also be affected by the model's specific task and the requirements or constraints of the destination environment or platform. -| Key | Value | Description | -|-------------|-----------------|------------------------------------------------------| -| `format` | `'torchscript'` | format to export to | -| `imgsz` | `640` | image size as scalar or (h, w) list, i.e. (640, 480) | -| `keras` | `False` | use Keras for TF SavedModel export | -| `optimize` | `False` | TorchScript: optimize for mobile | -| `half` | `False` | FP16 quantization | -| `int8` | `False` | INT8 quantization | -| `dynamic` | `False` | ONNX/TensorRT: dynamic axes | -| `simplify` | `False` | ONNX/TensorRT: simplify model | -| `opset` | `None` | ONNX: opset version (optional, defaults to latest) | -| `workspace` | `4` | TensorRT: workspace size (GB) | -| `nms` | `False` | CoreML: add NMS | +| Argument | Type | Default | Description | +|-------------|------------------|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `format` | `str` | `'torchscript'` | Target format for the exported model, such as `'onnx'`, `'torchscript'`, `'tensorflow'`, or others, defining compatibility with various deployment environments. | +| `imgsz` | `int` or `tuple` | `640` | Desired image size for the model input. Can be an integer for square images or a tuple `(height, width)` for specific dimensions. | +| `keras` | `bool` | `False` | Enables export to Keras format for TensorFlow SavedModel, providing compatibility with TensorFlow serving and APIs. | +| `optimize` | `bool` | `False` | Applies optimization for mobile devices when exporting to TorchScript, potentially reducing model size and improving performance. | +| `half` | `bool` | `False` | Enables FP16 (half-precision) quantization, reducing model size and potentially speeding up inference on supported hardware. | +| `int8` | `bool` | `False` | Activates INT8 quantization, further compressing the model and speeding up inference with minimal accuracy loss, primarily for edge devices. | +| `dynamic` | `bool` | `False` | Allows dynamic input sizes for ONNX and TensorRT exports, enhancing flexibility in handling varying image dimensions. | +| `simplify` | `bool` | `False` | Simplifies the model graph for ONNX exports, potentially improving performance and compatibility. | +| `opset` | `int` | `None` | Specifies the ONNX opset version for compatibility with different ONNX parsers and runtimes. If not set, uses the latest supported version. | +| `workspace` | `float` | `4.0` | Sets the maximum workspace size in GB for TensorRT optimizations, balancing memory usage and performance. | +| `nms` | `bool` | `False` | Adds Non-Maximum Suppression (NMS) to the CoreML export, essential for accurate and efficient detection post-processing. | + +It is crucial to thoughtfully configure these settings to ensure the exported model is optimized for the intended use case and functions effectively in the target environment. [Export Guide](../modes/export.md){ .md-button } -## Augmentation - -Augmentation settings for YOLO models refer to the various transformations and modifications applied to the training data to increase the diversity and size of the dataset. These settings can affect the model's performance, speed, and accuracy. Some common YOLO augmentation settings include the type and intensity of the transformations applied (e.g. random flips, rotations, cropping, color changes), the probability with which each transformation is applied, and the presence of additional features such as masks or multiple labels per box. Other factors that may affect the augmentation process include the size and composition of the original dataset and the specific task the model is being used for. It is important to carefully tune and experiment with these settings to ensure that the augmented dataset is diverse and representative enough to train a high-performing model. - -| Key | Value | Description | -|----------------|-----------------|--------------------------------------------------------------------------------| -| `hsv_h` | `0.015` | image HSV-Hue augmentation (fraction) | -| `hsv_s` | `0.7` | image HSV-Saturation augmentation (fraction) | -| `hsv_v` | `0.4` | image HSV-Value augmentation (fraction) | -| `degrees` | `0.0` | image rotation (+/- deg) | -| `translate` | `0.1` | image translation (+/- fraction) | -| `scale` | `0.5` | image scale (+/- gain) | -| `shear` | `0.0` | image shear (+/- deg) | -| `perspective` | `0.0` | image perspective (+/- fraction), range 0-0.001 | -| `flipud` | `0.0` | image flip up-down (probability) | -| `fliplr` | `0.5` | image flip left-right (probability) | -| `mosaic` | `1.0` | image mosaic (probability) | -| `mixup` | `0.0` | image mixup (probability) | -| `copy_paste` | `0.0` | segment copy-paste (probability) | -| `auto_augment` | `'randaugment'` | auto augmentation policy for classification (randaugment, autoaugment, augmix) | -| `erasing` | `0.4` | probability o random erasing during classification training (0-1) training | - -## Logging, checkpoints, plotting and file management +## Augmentation Settings + +Augmentation techniques are essential for improving the robustness and performance of YOLO models by introducing variability into the training data, helping the model generalize better to unseen data. The following table outlines the purpose and effect of each augmentation argument: + +| Argument | Type | Default | Range | Description | +|----------------|---------|---------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `hsv_h` | `float` | `0.015` | `0.0 - 1.0` | Adjusts the hue of the image by a fraction of the color wheel, introducing color variability. Helps the model generalize across different lighting conditions. | +| `hsv_s` | `float` | `0.7` | `0.0 - 1.0` | Alters the saturation of the image by a fraction, affecting the intensity of colors. Useful for simulating different environmental conditions. | +| `hsv_v` | `float` | `0.4` | `0.0 - 1.0` | Modifies the value (brightness) of the image by a fraction, helping the model to perform well under various lighting conditions. | +| `degrees` | `float` | `0.0` | `-180 - +180` | Rotates the image randomly within the specified degree range, improving the model's ability to recognize objects at various orientations. | +| `translate` | `float` | `0.1` | `0.0 - 1.0` | Translates the image horizontally and vertically by a fraction of the image size, aiding in learning to detect partially visible objects. | +| `scale` | `float` | `0.5` | `>=0.0` | Scales the image by a gain factor, simulating objects at different distances from the camera. | +| `shear` | `float` | `0.0` | `-180 - +180` | Shears the image by a specified degree, mimicking the effect of objects being viewed from different angles. | +| `perspective` | `float` | `0.0` | `0.0 - 0.001` | Applies a random perspective transformation to the image, enhancing the model's ability to understand objects in 3D space. | +| `flipud` | `float` | `0.0` | `0.0 - 1.0` | Flips the image upside down with the specified probability, increasing the data variability without affecting the object's characteristics. | +| `fliplr` | `float` | `0.5` | `0.0 - 1.0` | Flips the image left to right with the specified probability, useful for learning symmetrical objects and increasing dataset diversity. | +| `mosaic` | `float` | `1.0` | `0.0 - 1.0` | Combines four training images into one, simulating different scene compositions and object interactions. Highly effective for complex scene understanding. | +| `mixup` | `float` | `0.0` | `0.0 - 1.0` | Blends two images and their labels, creating a composite image. Enhances the model's ability to generalize by introducing label noise and visual variability. | +| `copy_paste` | `float` | `0.0` | `0.0 - 1.0` | Copies objects from one image and pastes them onto another, useful for increasing object instances and learning object occlusion. | +| `auto_augment` | `str` | `randaugment` | - | Automatically applies a predefined augmentation policy (`randaugment`, `autoaugment`, `augmix`), optimizing for classification tasks by diversifying the visual features. | +| `erasing` | `float` | `0.4` | `0.0 - 1.0` | Randomly erases a portion of the image during classification training, encouraging the model to focus on less obvious features for recognition. | + +These settings can be adjusted to meet the specific requirements of the dataset and task at hand. Experimenting with different values can help find the optimal augmentation strategy that leads to the best model performance. + +## Logging, Checkpoints and Plotting Settings Logging, checkpoints, plotting, and file management are important considerations when training a YOLO model. @@ -253,10 +260,10 @@ Logging, checkpoints, plotting, and file management are important considerations Effective logging, checkpointing, plotting, and file management can help you keep track of the model's progress and make it easier to debug and optimize the training process. -| Key | Value | Description | -|------------|----------|------------------------------------------------------------------------------------------------| -| `project` | `'runs'` | project name | -| `name` | `'exp'` | experiment name. `exp` gets automatically incremented if not specified, i.e, `exp`, `exp2` ... | -| `exist_ok` | `False` | whether to overwrite existing experiment | -| `plots` | `False` | save plots during train/val | -| `save` | `False` | save train checkpoints and predict results | +| Argument | Default | Description | +|------------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `project` | `'runs'` | Specifies the root directory for saving training runs. Each run will be saved in a separate subdirectory within this directory. | +| `name` | `'exp'` | Defines the name of the experiment. If not specified, YOLO automatically increments this name for each run, e.g., `exp`, `exp2`, etc., to avoid overwriting previous experiments. | +| `exist_ok` | `False` | Determines whether to overwrite an existing experiment directory if one with the same name already exists. Setting this to `True` allows overwriting, while `False` prevents it. | +| `plots` | `False` | Controls the generation and saving of training and validation plots. Set to `True` to create plots such as loss curves, precision-recall curves, and sample predictions. Useful for visually tracking model performance over time. | +| `save` | `False` | Enables the saving of training checkpoints and final model weights. Set to `True` to periodically save model states, allowing training to be resumed from these checkpoints or models to be deployed. | From 2d75f72598d5ce80786be61f140673a6d1dd7e57 Mon Sep 17 00:00:00 2001 From: Dean Mark <2552482+deanmark@users.noreply.github.com> Date: Sun, 25 Feb 2024 16:46:56 +0200 Subject: [PATCH 07/10] Add plot_images `conf_thresh` parameter (#8446) Co-authored-by: UltralyticsAssistant Co-authored-by: Glenn Jocher --- ultralytics/utils/plotting.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ultralytics/utils/plotting.py b/ultralytics/utils/plotting.py index 3c83bc4f7..66a5296fd 100644 --- a/ultralytics/utils/plotting.py +++ b/ultralytics/utils/plotting.py @@ -713,6 +713,7 @@ def plot_images( on_plot=None, max_subplots=16, save=True, + conf_thres=0.25, ): """Plot image grid with labels.""" if isinstance(images, torch.Tensor): @@ -778,7 +779,7 @@ def plot_images( c = classes[j] color = colors(c) c = names.get(c, c) if names else c - if labels or conf[j] > 0.25: # 0.25 conf thresh + if labels or conf[j] > conf_thres: label = f"{c}" if labels else f"{c} {conf[j]:.1f}" annotator.box_label(box, label, color=color, rotated=is_obb) @@ -800,7 +801,7 @@ def plot_images( kpts_[..., 0] += x kpts_[..., 1] += y for j in range(len(kpts_)): - if labels or conf[j] > 0.25: # 0.25 conf thresh + if labels or conf[j] > conf_thres: annotator.kpts(kpts_[j]) # Plot masks @@ -816,7 +817,7 @@ def plot_images( im = np.asarray(annotator.im).copy() for j in range(len(image_masks)): - if labels or conf[j] > 0.25: # 0.25 conf thresh + if labels or conf[j] > conf_thres: color = colors(classes[j]) mh, mw = image_masks[j].shape if mh != h or mw != w: From 0efbd2d7cfb71130d52ee05b31086b0403ca5c06 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Mon, 26 Feb 2024 23:09:07 +0100 Subject: [PATCH 08/10] Add YOLOv9 Docs page (#8478) Signed-off-by: Glenn Jocher Co-authored-by: UltralyticsAssistant --- docs/en/models/index.md | 13 ++--- docs/en/models/yolov9.md | 106 ++++++++++++++++++++++++++++++++++++++ mkdocs.yml | 1 + ultralytics/utils/loss.py | 7 ++- 4 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 docs/en/models/yolov9.md diff --git a/docs/en/models/index.md b/docs/en/models/index.md index 90b4cfbc0..70a70ef9a 100644 --- a/docs/en/models/index.md +++ b/docs/en/models/index.md @@ -18,12 +18,13 @@ Here are some of the key models supported: 4. **[YOLOv6](yolov6.md)**: Released by [Meituan](https://about.meituan.com/) in 2022, and in use in many of the company's autonomous delivery robots. 5. **[YOLOv7](yolov7.md)**: Updated YOLO models released in 2022 by the authors of YOLOv4. 6. **[YOLOv8](yolov8.md) NEW 🚀**: The latest version of the YOLO family, featuring enhanced capabilities such as instance segmentation, pose/keypoints estimation, and classification. -7. **[Segment Anything Model (SAM)](sam.md)**: Meta's Segment Anything Model (SAM). -8. **[Mobile Segment Anything Model (MobileSAM)](mobile-sam.md)**: MobileSAM for mobile applications, by Kyung Hee University. -9. **[Fast Segment Anything Model (FastSAM)](fast-sam.md)**: FastSAM by Image & Video Analysis Group, Institute of Automation, Chinese Academy of Sciences. -10. **[YOLO-NAS](yolo-nas.md)**: YOLO Neural Architecture Search (NAS) Models. -11. **[Realtime Detection Transformers (RT-DETR)](rtdetr.md)**: Baidu's PaddlePaddle Realtime Detection Transformer (RT-DETR) models. -12. **[YOLO-World](yolo-world.md)**: Real-time Open Vocabulary Object Detection models from Tencent AI Lab. +7. **[YOLOv9](yolov9.md)**: An experimental model trained on the Ultralytics [YOLOv5](yolov5.md) codebase implementing Programmable Gradient Information (PGI). +8. **[Segment Anything Model (SAM)](sam.md)**: Meta's Segment Anything Model (SAM). +9. **[Mobile Segment Anything Model (MobileSAM)](mobile-sam.md)**: MobileSAM for mobile applications, by Kyung Hee University. +10. **[Fast Segment Anything Model (FastSAM)](fast-sam.md)**: FastSAM by Image & Video Analysis Group, Institute of Automation, Chinese Academy of Sciences. +11. **[YOLO-NAS](yolo-nas.md)**: YOLO Neural Architecture Search (NAS) Models. +12. **[Realtime Detection Transformers (RT-DETR)](rtdetr.md)**: Baidu's PaddlePaddle Realtime Detection Transformer (RT-DETR) models. +13. **[YOLO-World](yolo-world.md)**: Real-time Open Vocabulary Object Detection models from Tencent AI Lab.


diff --git a/docs/en/models/yolov9.md b/docs/en/models/yolov9.md new file mode 100644 index 000000000..dcf10f451 --- /dev/null +++ b/docs/en/models/yolov9.md @@ -0,0 +1,106 @@ +--- +comments: true +description: Discover YOLOv9, the latest addition to the real-time object detection arsenal, leveraging Programmable Gradient Information and GELAN architecture for unparalleled performance. +keywords: YOLOv9, real-time object detection, Programmable Gradient Information, GELAN architecture, Ultralytics, MS COCO dataset, open-source, lightweight model, computer vision, AI +--- + +# YOLOv9: A Leap Forward in Object Detection Technology + +YOLOv9 marks a significant advancement in real-time object detection, introducing groundbreaking techniques such as Programmable Gradient Information (PGI) and the Generalized Efficient Layer Aggregation Network (GELAN). This model demonstrates remarkable improvements in efficiency, accuracy, and adaptability, setting new benchmarks on the MS COCO dataset. The YOLOv9 project, while developed by a separate open-source team, builds upon the robust codebase provided by [Ultralytics](https://ultralytics.com) [YOLOv5](yolov5.md), showcasing the collaborative spirit of the AI research community. + +![YOLOv9 performance comparison](https://github.com/ultralytics/ultralytics/assets/26833433/9f41ef7b-6008-43eb-8ba1-0a9b89600100) + +## Introduction to YOLOv9 + +In the quest for optimal real-time object detection, YOLOv9 stands out with its innovative approach to overcoming information loss challenges inherent in deep neural networks. By integrating PGI and the versatile GELAN architecture, YOLOv9 not only enhances the model's learning capacity but also ensures the retention of crucial information throughout the detection process, thereby achieving exceptional accuracy and performance. + +## Core Innovations of YOLOv9 + +YOLOv9's advancements are deeply rooted in addressing the challenges posed by information loss in deep neural networks. The Information Bottleneck Principle and the innovative use of Reversible Functions are central to its design, ensuring YOLOv9 maintains high efficiency and accuracy. + +### Information Bottleneck Principle + +The Information Bottleneck Principle reveals a fundamental challenge in deep learning: as data passes through successive layers of a network, the potential for information loss increases. This phenomenon is mathematically represented as: + +```python +I(X, X) >= I(X, f_theta(X)) >= I(X, g_phi(f_theta(X))) +``` + +where `I` denotes mutual information, and `f` and `g` represent transformation functions with parameters `theta` and `phi`, respectively. YOLOv9 counters this challenge by implementing Programmable Gradient Information (PGI), which aids in preserving essential data across the network's depth, ensuring more reliable gradient generation and, consequently, better model convergence and performance. + +### Reversible Functions + +The concept of Reversible Functions is another cornerstone of YOLOv9's design. A function is deemed reversible if it can be inverted without any loss of information, as expressed by: + +```python +X = v_zeta(r_psi(X)) +``` + +with `psi` and `zeta` as parameters for the reversible and its inverse function, respectively. This property is crucial for deep learning architectures, as it allows the network to retain a complete information flow, thereby enabling more accurate updates to the model's parameters. YOLOv9 incorporates reversible functions within its architecture to mitigate the risk of information degradation, especially in deeper layers, ensuring the preservation of critical data for object detection tasks. + +### Impact on Lightweight Models + +Addressing information loss is particularly vital for lightweight models, which are often under-parameterized and prone to losing significant information during the feedforward process. YOLOv9's architecture, through the use of PGI and reversible functions, ensures that even with a streamlined model, the essential information required for accurate object detection is retained and effectively utilized. + +### Programmable Gradient Information (PGI) + +PGI is a novel concept introduced in YOLOv9 to combat the information bottleneck problem, ensuring the preservation of essential data across deep network layers. This allows for the generation of reliable gradients, facilitating accurate model updates and improving the overall detection performance. + +### Generalized Efficient Layer Aggregation Network (GELAN) + +GELAN represents a strategic architectural advancement, enabling YOLOv9 to achieve superior parameter utilization and computational efficiency. Its design allows for flexible integration of various computational blocks, making YOLOv9 adaptable to a wide range of applications without sacrificing speed or accuracy. + +![YOLOv9 architecture comparison](https://github.com/ultralytics/ultralytics/assets/26833433/286a3971-677b-45e6-a90b-4b6bd565a7af) + +## Performance on MS COCO Dataset + +The performance of YOLOv9 on the [COCO dataset](../datasets/detect/coco.md) exemplifies its significant advancements in real-time object detection, setting new benchmarks across various model sizes. Table 1 presents a comprehensive comparison of state-of-the-art real-time object detectors, illustrating YOLOv9's superior efficiency and accuracy. + +**Table 1. Comparison of State-of-the-Art Real-Time Object Detectors** + +| Model | Parameters (M) | FLOPs (G) | APval 50:95 (%) | APval 50 (%) | APval 75 (%) | APval S (%) | APval M (%) | APval L (%) | +|----------|----------------|-----------|-----------------|--------------|--------------|-------------|-------------|-------------| +| YOLOv9-S | 7.2 | 26.7 | 46.8 | 63.4 | 50.7 | 26.6 | 56.0 | 64.5 | +| YOLOv9-M | 20.1 | 76.8 | 51.4 | 68.1 | 56.1 | 33.6 | 57.0 | 68.0 | +| YOLOv9-C | 25.5 | 102.8 | 53.0 | 70.2 | 57.8 | 36.2 | 58.5 | 69.3 | +| YOLOv9-E | 58.1 | 192.5 | 55.6 | 72.8 | 60.6 | 40.2 | 61.0 | 71.4 | + +YOLOv9's iterations, ranging from the smaller S variant to the extensive E model, demonstrate improvements not only in accuracy (AP metrics) but also in efficiency with a reduced number of parameters and computational needs (FLOPs). This table underscores YOLOv9's ability to deliver high precision while maintaining or reducing the computational overhead compared to prior versions and competing models. + +Comparatively, YOLOv9 exhibits remarkable gains: + +- **Lightweight Models**: YOLOv9-S surpasses the YOLO MS-S in parameter efficiency and computational load while achieving an improvement of 0.4∼0.6% in AP. +- **Medium to Large Models**: YOLOv9-M and YOLOv9-E show notable advancements in balancing the trade-off between model complexity and detection performance, offering significant reductions in parameters and computations against the backdrop of improved accuracy. + +The YOLOv9-C model, in particular, highlights the effectiveness of the architecture's optimizations. It operates with 42% fewer parameters and 21% less computational demand than YOLOv7 AF, yet it achieves comparable accuracy, demonstrating YOLOv9's significant efficiency improvements. Furthermore, the YOLOv9-E model sets a new standard for large models, with 15% fewer parameters and 25% less computational need than YOLOv8-X, alongside a substantial 1.7% improvement in AP. + +These results showcase YOLOv9's strategic advancements in model design, emphasizing its enhanced efficiency without compromising on the precision essential for real-time object detection tasks. The model not only pushes the boundaries of performance metrics but also emphasizes the importance of computational efficiency, making it a pivotal development in the field of computer vision. + +## Integration and Future Directions + +YOLOv9 embodies the spirit of open-source collaboration that is central to the advancement of AI technology. With plans for future integration into the Ultralytics package, YOLOv9 is poised to become an accessible tool for researchers and practitioners alike, further enhancing its impact on the field of computer vision. + +## Conclusion + +YOLOv9 represents a pivotal development in real-time object detection, offering significant improvements in terms of efficiency, accuracy, and adaptability. By addressing critical challenges through innovative solutions like PGI and GELAN, YOLOv9 sets a new precedent for future research and application in the field. As the AI community continues to evolve, YOLOv9 stands as a testament to the power of collaboration and innovation in driving technological progress. + +Stay tuned for updates on Ultralytics package integration and explore the possibilities that YOLOv9 brings to the realm of computer vision. + +## Citations and Acknowledgements + +We would like to acknowledge the YOLOv9 authors for their significant contributions in the field of real-time object detection: + +!!! Quote "" + + === "BibTeX" + + ```bibtex + @article{wang2024yolov9, + title={{YOLOv9}: Learning What You Want to Learn Using Programmable Gradient Information}, + author={Wang, Chien-Yao and Liao, Hong-Yuan Mark}, + booktitle={arXiv preprint arXiv:2402.13616}, + year={2024} + } + ``` + +The original YOLOv9 paper can be found on [arXiv](https://arxiv.org/pdf/2402.13616.pdf). The authors have made their work publicly available, and the codebase can be accessed on [GitHub](https://github.com/WongKinYiu/yolov9). We appreciate their efforts in advancing the field and making their work accessible to the broader community. diff --git a/mkdocs.yml b/mkdocs.yml index 84b1648d4..d20812239 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -222,6 +222,7 @@ nav: - YOLOv6: models/yolov6.md - YOLOv7: models/yolov7.md - YOLOv8: models/yolov8.md + - YOLOv9: models/yolov9.md - SAM (Segment Anything Model): models/sam.md - MobileSAM (Mobile Segment Anything Model): models/mobile-sam.md - FastSAM (Fast Segment Anything Model): models/fast-sam.md diff --git a/ultralytics/utils/loss.py b/ultralytics/utils/loss.py index 28dee2efb..271403e05 100644 --- a/ultralytics/utils/loss.py +++ b/ultralytics/utils/loss.py @@ -597,7 +597,12 @@ class v8ClassificationLoss: class v8OBBLoss(v8DetectionLoss): - def __init__(self, model): # model must be de-paralleled + def __init__(self, model): + """ + Initializes v8OBBLoss with model, assigner, and rotated bbox loss. + + Note model must be de-paralleled. + """ super().__init__(model) self.assigner = RotatedTaskAlignedAssigner(topk=10, num_classes=self.nc, alpha=0.5, beta=6.0) self.bbox_loss = RotatedBboxLoss(self.reg_max - 1, use_dfl=self.use_dfl).to(self.device) From 70f533fd4777f3b21c87f0f861ae0525d89c4e23 Mon Sep 17 00:00:00 2001 From: Yifei Liu <71677542+kaikai23@users.noreply.github.com> Date: Tue, 27 Feb 2024 06:26:06 +0800 Subject: [PATCH 09/10] Rename `model_id` to `model.id` (#8447) Co-authored-by: Glenn Jocher --- ultralytics/utils/callbacks/hub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ultralytics/utils/callbacks/hub.py b/ultralytics/utils/callbacks/hub.py index 8d93093cc..587b45902 100644 --- a/ultralytics/utils/callbacks/hub.py +++ b/ultralytics/utils/callbacks/hub.py @@ -46,7 +46,7 @@ def on_model_save(trainer): # Upload checkpoints with rate limiting is_best = trainer.best_fitness == trainer.fitness if time() - session.timers["ckpt"] > session.rate_limits["ckpt"]: - LOGGER.info(f"{PREFIX}Uploading checkpoint {HUB_WEB_ROOT}/models/{session.model_id}") + LOGGER.info(f"{PREFIX}Uploading checkpoint {HUB_WEB_ROOT}/models/{session.model.id}") session.upload_model(trainer.epoch, trainer.last, is_best) session.timers["ckpt"] = time() # reset timer From f8e681c2be251562633d424f4a1a1cc7da526bed Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 27 Feb 2024 00:02:29 +0100 Subject: [PATCH 10/10] `ultralytics 8.1.19` PNNX `aarch64` linux fix (#8480) Co-authored-by: Burhan <62214284+Burhan-Q@users.noreply.github.com> Co-authored-by: Kayzwer <68285002+Kayzwer@users.noreply.github.com> --- ultralytics/__init__.py | 2 +- ultralytics/data/utils.py | 3 +++ ultralytics/engine/exporter.py | 6 +++--- ultralytics/utils/ops.py | 12 +++++++----- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/ultralytics/__init__.py b/ultralytics/__init__.py index 9c24e00cc..0d72f48e6 100644 --- a/ultralytics/__init__.py +++ b/ultralytics/__init__.py @@ -1,6 +1,6 @@ # Ultralytics YOLO 🚀, AGPL-3.0 license -__version__ = "8.1.18" +__version__ = "8.1.19" from ultralytics.data.explorer.explorer import Explorer from ultralytics.models import RTDETR, SAM, YOLO, YOLOWorld diff --git a/ultralytics/data/utils.py b/ultralytics/data/utils.py index be331bf3c..1ce8127aa 100644 --- a/ultralytics/data/utils.py +++ b/ultralytics/data/utils.py @@ -365,6 +365,9 @@ def check_cls_dataset(dataset, split=""): # Download (optional if dataset=https://file.zip is passed directly) if str(dataset).startswith(("http:/", "https:/")): dataset = safe_download(dataset, dir=DATASETS_DIR, unzip=True, delete=False) + elif Path(dataset).suffix in (".zip", ".tar", ".gz"): + file = check_file(dataset) + dataset = safe_download(file, dir=DATASETS_DIR, unzip=True, delete=False) dataset = Path(dataset) data_dir = (dataset if dataset.is_dir() else (DATASETS_DIR / dataset)).resolve() diff --git a/ultralytics/engine/exporter.py b/ultralytics/engine/exporter.py index 68892958e..75f7a0f98 100644 --- a/ultralytics/engine/exporter.py +++ b/ultralytics/engine/exporter.py @@ -514,12 +514,12 @@ class Exporter: "https://github.com/pnnx/pnnx/.\nNote PNNX Binary file must be placed in current working directory " f"or in {ROOT}. See PNNX repo for full installation instructions." ) - system = ["macos"] if MACOS else ["windows"] if WINDOWS else ["ubuntu", "linux"] # operating system + system = "macos" if MACOS else "windows" if WINDOWS else "linux-aarch64" if ARM64 else "linux" try: _, assets = get_github_assets(repo="pnnx/pnnx", retry=True) - url = [x for x in assets if any(s in x for s in system)][0] + url = [x for x in assets if f"{system}.zip" in x][0] except Exception as e: - url = f"https://github.com/pnnx/pnnx/releases/download/20231127/pnnx-20231127-{system[0]}.zip" + url = f"https://github.com/pnnx/pnnx/releases/download/20240226/pnnx-20240226-{system}.zip" LOGGER.warning(f"{prefix} WARNING ⚠️ PNNX GitHub assets not found: {e}, using default {url}") asset = attempt_download_asset(url, repo="pnnx/pnnx", release="latest") if check_is_path_safe(Path.cwd(), asset): # avoid path traversal security vulnerability diff --git a/ultralytics/utils/ops.py b/ultralytics/utils/ops.py index 4a8351609..fb346a961 100644 --- a/ultralytics/utils/ops.py +++ b/ultralytics/utils/ops.py @@ -638,7 +638,7 @@ def crop_mask(masks, boxes): Returns: (torch.Tensor): The masks are being cropped to the bounding box. """ - n, h, w = masks.shape + _, h, w = masks.shape x1, y1, x2, y2 = torch.chunk(boxes[:, :, None], 4, 1) # x1 shape(n,1,1) r = torch.arange(w, device=masks.device, dtype=x1.dtype)[None, None, :] # rows shape(1,1,w) c = torch.arange(h, device=masks.device, dtype=x1.dtype)[None, :, None] # cols shape(1,h,1) @@ -686,12 +686,14 @@ def process_mask(protos, masks_in, bboxes, shape, upsample=False): c, mh, mw = protos.shape # CHW ih, iw = shape masks = (masks_in @ protos.float().view(c, -1)).sigmoid().view(-1, mh, mw) # CHW + width_ratio = mw / iw + height_ratio = mh / ih downsampled_bboxes = bboxes.clone() - downsampled_bboxes[:, 0] *= mw / iw - downsampled_bboxes[:, 2] *= mw / iw - downsampled_bboxes[:, 3] *= mh / ih - downsampled_bboxes[:, 1] *= mh / ih + downsampled_bboxes[:, 0] *= width_ratio + downsampled_bboxes[:, 2] *= width_ratio + downsampled_bboxes[:, 3] *= height_ratio + downsampled_bboxes[:, 1] *= height_ratio masks = crop_mask(masks, downsampled_bboxes) # CHW if upsample: