使用 Albumentations 增强数据集以训练 YOLO26
构建计算机视觉模型时,训练数据的质量和多样性会对模型表现产生很大影响。Albumentations 提供了一种快速、灵活且高效的方法,可应用多种图像变换,提升模型适应真实场景的能力。它与 Ultralytics YOLO26 集成,可帮助你创建适用于目标检测、实例分割、语义分割、深度估计、姿态估计和有向边界框任务的稳健数据集。
Albumentations 可为 YOLO26 训练数据添加几何变换、颜色调整等处理。本指南将介绍 Albumentations 如何融入 Ultralytics数据增强流水线,以及如何针对你的项目进行自定义。
使用 Albumentations 进行图像增强#
Albumentations 是一个开源图像增强库,于2018 年 6 月创建,旨在简化并加速计算机视觉中的图像增强流程。它在设计时注重性能和灵活性,支持多种增强技术,从旋转、翻转等简单变换,到亮度和对比度调整等更复杂的处理。Albumentations 可帮助开发者为图像分类、目标检测和分割等任务生成丰富多样的数据集。
你可以使用 Albumentations 轻松增强图像、分割掩码、边界框和关键点,确保数据集中的所有元素都经过同步变换。它兼容 PyTorch 和 TensorFlow 等常用深度学习框架,可用于各种项目。
Albumentations 适用于小型数据集和大规模计算机视觉任务。它的快速处理能减少数据准备时间,而更多样的训练数据可提升真实应用中的模型性能。
Albumentations 的主要特性#
Albumentations 提供了许多实用功能,可简化各种计算机视觉应用中的复杂图像增强操作。以下是其中一些主要特性:
- 丰富的变换类型:Albumentations 提供超过 70 种不同的变换,包括几何变换(例如旋转、翻转)、颜色调整(例如亮度、对比度)和噪声添加(例如高斯噪声)。丰富的选项有助于创建高度多样且稳健的训练数据集。
-
高性能优化:Albumentations 基于 OpenCV 和 NumPy 构建,使用 SIMD(单指令多数据)等高级优化技术,同时处理多个数据点以加快处理速度。它能快速处理大型数据集,是目前速度最快的图像增强方案之一。
-
三个级别的增强:Albumentations 支持三个级别的增强:像素级变换、空间级变换和混合级变换。像素级变换只影响输入图像,不会改变掩码、边界框或关键点。空间级变换会同时改变图像及其元素,例如掩码和边界框。混合级变换会将多张图像合并为一张。

- 基准测试结果:Albumentations 发布的基准测试显示,相比其他增强库,它在速度方面表现出色,尤其是在处理大型数据集时。
为什么要在视觉 AI 项目中使用 Albumentations?#
在图像增强方面,Albumentations 是处理计算机视觉任务的可靠工具。以下是你应该考虑在视觉 AI 项目中使用它的一些主要原因:
-
易用的 API:Albumentations 提供了一个简洁统一的 API,可对图像、掩码、边界框和关键点应用多种增强。它能够轻松适配不同的数据集,让数据准备更简单、更高效。
-
严格的错误测试:增强流程中的错误可能会悄无声息地破坏输入数据,常常不易察觉,却最终会降低模型性能。Albumentations 通过全面的测试套件帮助及早发现开发过程中的错误。
-
可扩展性:Albumentations 通过统一接口,方便你添加新的增强方法,并与内置变换一同使用。
如何使用 Albumentations 增强 YOLO26 训练数据#
Albumentations 可直接集成到 Ultralytics 的训练模式中,安装 Albumentations 包后便会自动应用。
安装#
如需在 YOLO26 中使用 Albumentations,首先确保已安装所需的软件包。如果未安装 Albumentations,训练期间就不会应用其增强方法。安装完成后,变换会即时应用到训练图像上。
# Install the required packages
pip install albumentations ultralytics有关安装流程的详细说明和最佳实践,请查看我们的 Ultralytics 安装指南。安装 YOLO26 所需的软件包时,如果遇到困难,请查阅我们的常见问题指南,获取解决方案和建议。
本页面中的自定义变换示例需要 Albumentations 1.4.22 或更高版本,因此也需要 Python 3.9 或更高版本。
用法#
安装所需软件包后,就可以开始在 YOLO26 中使用 Albumentations。安装 Albumentations 后训练 YOLO26 时,系统会自动应用默认的一组 Albumentations 变换。
from ultralytics import YOLO
# 加载预训练模型
model = YOLO("yolo26n.pt")
# 使用默认增强训练模型
results = model.train(data="coco8.yaml", epochs=100, imgsz=640)以下部分介绍训练期间默认应用的 Albumentations 变换。
模糊#
Albumentations 中的 Blur 变换会对小方形区域(即卷积核)内的像素值求平均,从而对图像应用简单的模糊效果。此操作使用 OpenCV 的 cv2.blur 函数完成,有助于减少图像噪声,但也会略微降低图像细节。
以下是此集成中使用的参数和值:
-
blur_limit:控制模糊效果的尺寸范围。默认范围为 (3, 7),也就是说,模糊内核尺寸可以在 3 到 7 像素之间变化,并且只能使用奇数,以确保模糊效果居中。
-
p:应用模糊的概率。在此集成中,p=0.01,因此每张图像有 1% 的概率应用模糊。较低的概率会偶尔引入模糊效果,增加一些变化,帮助模型更好地泛化,同时避免图像过度模糊。
中值模糊#
Albumentations 中的 MedianBlur 变换会对图像应用中值模糊,特别适用于降低噪声并保留边缘。与常见的模糊方法不同,MedianBlur 使用中值滤波器,特别擅长去除椒盐噪声,同时保持边缘清晰。
以下是此集成中使用的参数和值:
-
blur_limit:此参数控制模糊内核的最大尺寸。在此集成中,默认范围为 (3, 7),也就是说,模糊内核尺寸会在 3 到 7 像素之间随机选取,并且只能使用奇数,以确保正确对齐。
-
p:设置应用中值模糊的概率。此处 p=0.01,因此每张图像有 1% 的概率应用此变换。较低的概率确保中值模糊只偶尔使用,使模型偶尔看到降噪且边缘清晰的图像,从而帮助模型泛化。
下图展示了对图像应用此增强的示例。
灰度#
Albumentations 中的 ToGray 变换会将图像转换为灰度图,将其缩减为单通道格式,并可选择复制此通道以匹配指定的输出通道数。灰度亮度的计算可使用不同方法,从简单的平均值到更高级的技术,帮助真实地呈现对比度和亮度。
以下是此集成中使用的参数和值:
-
num_output_channels:设置输出图像的通道数。如果此值大于 1,单个灰度通道会被复制,以生成多通道灰度图像。默认值为 3,因此生成的灰度图像包含三个相同的通道。
-
method:定义灰度转换方法。默认方法 "weighted_average" 使用公式 (0.299R + 0.587G + 0.114B),与人类视觉感知较为接近,可生成自然的灰度效果。其他选项,如 "from_lab"、"desaturation"、"average"、"max" 和 "pca",可根据速度、亮度强调或细节保留等不同需求,提供其他灰度图像生成方式。
-
p:控制灰度变换的应用频率。p=0.01 表示每张图像有 1% 的概率转换为灰度图,让彩色图像和灰度图像混合出现,帮助模型更好地泛化。
下图展示了应用此灰度变换的示例。
对比度受限的自适应直方图均衡化 (CLAHE)#
Albumentations 中的 CLAHE 变换会应用对比度受限的自适应直方图均衡化 (CLAHE)。这项技术通过在局部区域(图块)内均衡直方图来增强图像对比度,而不是对整幅图像进行均衡。CLAHE 能产生均衡的增强效果,避免标准直方图均衡化可能导致的对比度过度放大,尤其是在初始对比度较低的区域。
以下是此集成中使用的参数和值:
-
clip_limit:控制对比度增强范围。默认范围为 (1, 4),用于确定每个图块允许的最大对比度。较高的值允许更强的对比度,但也可能引入噪声。
-
tile_grid_size:定义图块网格的大小,通常表示为 (行数, 列数)。默认值为 (8, 8),即将图像划分为 8x8 网格。较小的图块尺寸能进行更局部的调整,而较大的图块产生的效果则更接近全局均衡化。
-
p:应用 CLAHE 的概率。此处 p=0.01,表示仅在 1% 的情况下应用增强效果,从而确保对比度调整只偶尔用于训练图像,带来少量变化。
下图展示了应用 CLAHE 变换的示例。
使用自定义 Albumentations 变换#
Albumentations 的默认集成提供了一套可靠的增强方法,但你可能需要针对特定用例自定义变换。使用 Ultralytics YOLO26 时,你可以通过 Python API 的 augmentations 参数传入自定义 Albumentations 变换。本节示例需要 Albumentations 1.4.22 或更高版本。
如何定义自定义变换#
你可以定义自己的 Albumentations 变换列表,并将其传递给训练函数。这会替换默认的 Albumentations 变换,同时保留其他所有 YOLO 数据增强(例如 hsv_h、degrees、mosaic 等)。
下面是一个使用更高级变换的示例:
import albumentations as A
from ultralytics import YOLO
# 加载模型
model = YOLO("yolo26n.pt")
# 定义使用多种增强技术的自定义变换
custom_transforms = [
# 模糊变体
A.OneOf(
[
A.MotionBlur(blur_limit=7, p=1.0),
A.MedianBlur(blur_limit=7, p=1.0),
A.GaussianBlur(blur_limit=7, p=1.0),
],
p=0.3,
),
# 噪声变体
A.OneOf(
[
A.GaussNoise(std_range=(0.0124, 0.0277), p=1.0),
A.ISONoise(color_shift=(0.01, 0.05), intensity=(0.1, 0.5), p=1.0),
],
p=0.2,
),
# 颜色和对比度调整
A.CLAHE(clip_limit=4.0, tile_grid_size=(8, 8), p=0.5),
A.RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3, p=0.5),
A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, p=0.5),
# 模拟遮挡
A.CoarseDropout(num_holes_range=(1, 8), hole_height_range=(8, 32), hole_width_range=(8, 32), fill=0, p=0.2),
]
# 使用自定义变换进行训练
results = model.train(
data="coco8.yaml",
epochs=100,
imgsz=640,
augmentations=custom_transforms,
)重要注意事项#
使用自定义 Albumentations 变换时,请牢记以下几点:
- 配置:通过 Python API 构建自定义变换对象。也可以通过 CLI 或 YAML 配置文件传入序列化后的变换,包括恢复训练时。
- 替换默认变换:你的自定义变换会完全替换默认的 Albumentations 变换。其他 YOLO 数据增强仍会生效。
- 标签处理:空间变换(包括嵌套在
A.OneOf或A.Compose中的变换)会让边界框、多边形、关键点以及深度或语义掩码与图像一同移动。A.RandomGridShuffle无法保留多边形或关键点的拓扑结构,因此在分割、姿态和 OBB 样本上会引发错误。 - 性能:某些变换的计算开销较大。请监控训练速度并据此调整。
- 任务兼容性:自定义 Albumentations 变换适用于检测、分割、语义分割、深度、姿态和 OBB 训练,但不适用于分类(分类使用不同的增强流水线)。
自定义变换的使用场景#
不同应用可以从不同的数据增强策略中受益:
- 医学影像:使用弹性形变、网格畸变和专用噪声模式
- 航空/卫星图像:应用模拟不同高度、天气状况和光照角度的变换
- 低光照场景:重点使用添加噪声和调整亮度的方法,训练模型以应对具有挑战性的光照条件
- 工业检测:添加纹理变化和模拟缺陷,用于质量控制应用
如需查看可用变换及其参数的完整列表,请访问 Albumentations 文档。
如需查看使用自定义 Albumentations 变换配合 YOLO26 的详细示例和最佳实践,请参阅 YOLO 数据增强指南。
继续了解 Albumentations#
如果你想进一步了解 Albumentations,可以查看以下资源,获取更深入的说明和示例:
-
Albumentations 文档:官方文档提供全面的支持变换说明和高级用法技巧。
-
Ultralytics Albumentations API 参考:查看实现此集成的类的详细信息。
-
Albumentations GitHub 仓库:该仓库包含示例、基准测试和讨论,帮助你开始自定义数据增强。
要点总结#
本指南介绍了 Albumentations 这一 Python 图像增强库的关键内容。我们探讨了它丰富的变换类型、优化后的性能,以及如何在下一个 YOLO26 项目中使用它。
常见问题#
Albumentations 与 YOLO26 集成;只要安装了该软件包,它就会在训练期间自动应用。以下是入门方法:
# 安装所需软件包 # !pip install albumentations ultralytics from ultralytics import YOLO # 加载模型并使用自动数据增强进行训练 model = YOLO("yolo26n.pt") model.train(data="coco8.yaml", epochs=100)默认集成会应用模糊、中值模糊、灰度转换和 CLAHE,每种变换的概率都较低,仅为 1%,因此它们只会偶尔增加变化,不会主导训练过程。
Albumentations 的优势体现在以下几个方面:
- 性能:基于 OpenCV 和 NumPy 构建,并通过 SIMD 优化实现高速处理
- 灵活性:支持 70 多种变换,涵盖像素级、空间级和混合级数据增强
- 兼容性:兼容 PyTorch 和 TensorFlow 等常用框架
- 可靠性:全面的测试套件可防止数据在未被察觉的情况下损坏
- 易用性:通过统一的 API 支持所有类型的数据增强
评论19
提出问题、分享结果,或帮助其他人。
anushka-kore
Is there a way that we can define more custom albumentations transforms to be applied during model train? If yes, will they also change the bounding box coords accordingly if they are spatial transforms?
manuelescobar5036
The albumentations transforms are defined in the ultralytics/data/augment.py file. There you can find the following function that returns the transforms to be applied to the images, which includes the Albumentations transform.
Then, in the same file you can find the Albumentations class which defines the pipeline. In this pipeline you can add the new transforms that you want.
Bouding boxes are changed accordingly to their spatial transforms. This is done when you pass bbox_params to the Albumentations pipeline.
Glenn JocherUltralytics(已编辑)@anushka-kore Yes. Pass your own Albumentations transforms to training with the
augmentationsargument (Python only):import albumentations as A from ultralytics import YOLO model = YOLO("yolo26n.pt") model.train(data="coco8.yaml", augmentations=[A.Blur(p=0.01), A.CLAHE(p=0.01)])Spatial transforms (rotation, flips, perspective, etc.) are applied together with the bounding boxes, which are updated automatically through Albumentations' bbox handling in YOLO format. Non-spatial transforms like color adjustments only change the image. See the Albumentations integration guide for details.
anushka-kore
Thanks a lot for the inputs @deox1994 and @glenn-jocher! I have made it work👍🏻
anushka-kore
Hey @glenn-jocher and @deox1994, I later realised that simply defining my list of custom transform in the augment.py file is not working. This is the part I changed in the augment.py file (original ones are commented out)-
T = [ #A.Blur(p=0.01), #A.MedianBlur(p=0.01), #A.ToGray(p=0.01), #A.CLAHE(p=0.01), A.RandomBrightnessContrast(p=0.2), #A.RandomGamma(p=0.0), #A.ImageCompression(quality_range=(75, 100), p=0.0), A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5), A.Rotate(limit=30, p=0.5), A.GaussianBlur(p=0.2), A.GaussNoise(p=0.2), ]For your reference, this is the error I get, you could view the list of albumentation transforms I applied here as well- ..... Transferred 1009/1015 items from pretrained weights Freezing layer 'model.23.dfl.conv.weight' AMP: running Automatic Mixed Precision (AMP) checks... AMP: checks passed ✅ train: Scanning /home/xxx/dataset_MIP_clean/train/labels.cache... 831 images, 73 backgrounds, 0 corrupt: 100%|██████████| 831/831 [00:00<?, ?it/s] albumentations: RandomBrightnessContrast(p=0.2, brightness_by_max=True, brightness_limit=(-0.2, 0.2), contrast_limit=(-0.2, 0.2), ensure_safe_range=False), HorizontalFlip(p=0.5), VerticalFlip(p=0.5), Rotate(p=0.5, border_mode=0, crop_border=False, fill=0.0, fill_mask=0.0, interpolation=1, limit=(-30.0, 30.0), mask_interpolation=0, rotate_method='largest_box'), GaussianBlur(p=0.2, blur_limit=(0, 0), sigma_limit=(0.5, 3.0)), GaussNoise(p=0.2, mean_range=(0.0, 0.0), noise_scale_factor=1.0, per_channel=True, std_range=(0.2, 0.44)) val: Scanning /home/xxx/dataset_MIP_clean/val/labels.cache... 210 images, 13 backgrounds, 0 corrupt: 100%|██████████| 210/210 [00:00<?, ?it/s] Plotting labels to /home/xxx/yolov11_predictions/test4/labels.jpg... optimizer: 'optimizer=auto' found, ignoring 'lr0=0.01' and 'momentum=0.937' and determining best 'optimizer', 'lr0' and 'momentum' automatically... optimizer: AdamW(lr=0.000769, momentum=0.9) with parameter groups 167 weight(decay=0.0), 174 weight(decay=0.0005), 173 bias(decay=0.0) Image sizes 640 train, 640 val Using 8 dataloader workers Logging results to /home/xxx/yolov11_predictions/test4 Starting training for 200 epochs...
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 1/200 15.4G 5.502 51.38 2.745 17 640: 10%|▉ | 5/52 [00:02<00:22, 2.07it/s]Traceback (most recent call last): File "/home/xxx/yolo_train.py", line 149, in train_yolo(output_dir) File "/home/xxx/yolo_train.py", line 116, in train_yolo results = model.train( ^^^^^^^^^^^^ File "/home/xxx/env/lib/python3.12/site-packages/ultralytics/engine/model.py", line 791, in train self.trainer.train() File "/home/xxx/env/lib/python3.12/site-packages/ultralytics/engine/trainer.py", line 211, in train self._do_train(world_size) File "/home/xxx/env/lib/python3.12/site-packages/ultralytics/engine/trainer.py", line 366, in _do_train for i, batch in pbar: ^^^^ File "/home/xxx/env/lib/python3.12/site-packages/tqdm/std.py", line 1178, in iter for obj in iterable: ^^^^^^^^ File "/home/xxx/env/lib/python3.12/site-packages/ultralytics/data/build.py", line 59, in iter yield next(self.iterator) ^^^^^^^^^^^^^^^^^^^ File "/home/xxx/env/lib/python3.12/site-packages/torch/utils/data/dataloader.py", line 708, in next data = self._next_data() ^^^^^^^^^^^^^^^^^ File "/home/xxx/env/lib/python3.12/site-packages/torch/utils/data/dataloader.py", line 1455, in _next_data return self._process_data(data) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/xxx/env/lib/python3.12/site-packages/torch/utils/data/dataloader.py", line 1505, in _process_data data.reraise() File "/home/xxx/env/lib/python3.12/site-packages/torch/_utils.py", line 733, in reraise raise exception RuntimeError: Caught RuntimeError in DataLoader worker process 5. Original Traceback (most recent call last): File "/home/xxx/env/lib/python3.12/site-packages/torch/utils/data/_utils/worker.py", line 349, in _worker_loop data = fetcher.fetch(index) # type: ignore[possibly-undefined] ^^^^^^^^^^^^^^^^^^^^ File "/home/xxx/env/lib/python3.12/site-packages/torch/utils/data/_utils/fetch.py", line 55, in fetch return self.collate_fn(data) ^^^^^^^^^^^^^^^^^^^^^ File "/home/xxx/env/lib/python3.12/site-packages/ultralytics/data/dataset.py", line 298, in collate_fn value = torch.cat(value, 0) ^^^^^^^^^^^^^^^^^^^ RuntimeError: Tensors must have same number of dimensions: got 1 and 2
All my transforms are correctly defined, and the spatial transforms (horizontalflip, verticalflip and rotate) exist in the spatial_transforms list. Could you please point where this is going wrong?
supriamir
Hi Glen,
I would like to optimize the hyperparameters using model.tune(). My goal is to identify the most important augmentations from Albumentations.
If I am not mistaken, the default hyperparameter optimization only includes a limited set of augmentation parameters, as described in the following link: https://docs.ultralytics.com/integrations/ray-tune/#tune-method-parameters
Could you advise how I can include and optimize additional Albumentations transforms beyond the default settings?
Glenn JocherUltralyticsYes, but not directly through the default
model.tune()search set: custom Albumentations are a Python-onlyaugmentationslist passed tomodel.train(), while the built-in tuner mutates the numericspace, so the usual workflow is to keep one fixedaugmentations=pipeline per tuning run and compare the best result across several candidate pipelines; if you want to search the pipeline itself, use a small custom Ray Tune wrapper aroundmodel.train()instead, and keep the standard YOLO augmentations fixed so you isolate the Albumentations effect—see the hyperparameter tuning guide and the Albumentations integration guide.
UltralyticsAssistantUltralytics
👋 Hello, thank you for your interest in Ultralytics 🚀! We recommend visiting the Docs for guidance—there you’ll find many Python and CLI usage examples, and answers to many common questions.
If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us investigate.
If you have a custom training ❓ Question, please share as much relevant information as possible, including dataset image examples and training logs. Also, make sure to review our Tips for Best Training Results.
You’re welcome to join the Ultralytics community where it fits you best:
- For real-time chat, join us on Discord 🎧
- For in-depth discussions, visit Discourse
- For community sharing, check out our Subreddit
Upgrade
Upgrade to the latest
ultralyticspackage and all requirements in a Python>=3.8 environment with PyTorch>=1.8 to ensure you’re on the latest version:pip install -U ultralyticsEnvironments
YOLO can be run in any of the following up-to-date verified environments (with all dependencies like CUDA/CUDNN, Python, and PyTorch preinstalled):
- Notebooks with free GPU:
- Google Cloud Deep Learning VM. See GCP Quickstart Guide
- Amazon Deep Learning AMI. See AWS Quickstart Guide
- Docker Image. See Docker Quickstart Guide
Status
If this badge is green, all Ultralytics CI tests are currently passing. These tests confirm correct operation of all YOLO Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.
This is an automated response 🤖. An Ultralytics engineer will assist you here soon!
Glenn JocherUltralytics欢迎来到 Ultralytics 中文文档的 Albumentations 集成讨论区!如果您对使用 Albumentations 来增强 YOLO11 训练数据有任何问题或想法,请在这里提出。
Albumentations 与 YOLO11 的集成非常简便,只需安装
albumentations包,就会在训练过程中自动应用多种图像增强技术,如模糊、中值模糊、灰度转换和 CLAHE,帮助您的模型更好地适应各种真实场景。您是否在特定项目中使用过 Albumentations?或者您对如何最佳配置这些增强技术有疑问?请随时在这里分享您的经验或提出问题,Ultralytics 团队将很乐意提供帮助!
UltralyticsAssistantUltralytics
👋 Hello, thank you for your interest in Ultralytics 🚀! We recommend visiting the Docs for guidance on integrations like Albumentations with YOLO11. You can also review many Python and CLI examples, and frequently asked questions may already be addressed there.
If this is a 🐛 Bug Report, please include a minimum reproducible example to help us investigate further.
If you have a custom training ❓ Question, please provide detailed info such as dataset image samples, training logs, and confirm you are following our Tips for Best Training Results.
Join the Ultralytics community wherever you prefer—real-time chat on Discord 🎧, in-depth discussions on Discourse, or knowledge-sharing on our Subreddit.
Upgrade
Upgrade to the latest
ultralyticspackage and all requirements in a Python>=3.8 environment with PyTorch>=1.8 to ensure your issue is not already fixed in the latest version:pip install -U ultralyticsEnvironments
YOLO may be run in any of the following verified and up-to-date environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
- Notebooks with free GPU:
- Google Cloud Deep Learning VM. See GCP Quickstart Guide
- Amazon Deep Learning AMI. See AWS Quickstart Guide
- Docker Image. See Docker Quickstart Guide
Status
If this badge is green, all Ultralytics CI tests are currently passing. These tests verify correct operation of all YOLO Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.
This is an automated response. An Ultralytics engineer will review and assist you soon!
Glenn JocherUltralyticsBonjour! N'hésitez pas à poser vos questions sur l'intégration d'Albumentations avec YOLO11 dans cette discussion. Cette intégration vous permet d'améliorer automatiquement votre jeu de données pendant l'entraînement avec diverses transformations comme le flou, le flou médian, la conversion en niveaux de gris et CLAHE.
L'avantage principal est sa simplicité d'utilisation - il suffit d'installer les packages requis et les augmentations sont appliquées automatiquement pendant l'entraînement:
pip install albumentations ultralyticsSi vous rencontrez des difficultés avec cette intégration ou si vous avez des questions sur l'augmentation de données pour améliorer les performances de vos modèles YOLO11, n'hésitez pas à les partager ici!
- Notebooks with free GPU:
manuelescobar5036(已编辑)
Dear Ultralytics team,
I’m working on a project focused on detecting the behavior of workers in underground mines. The classes and the number of labels per class in the training set are as follows:
- Sitting → 2500
- Standing → 9000
- Operation → 700
- Lean Against → 4000
- Tumble → 400
Since the dataset is imbalanced, I would like to apply data augmentation to avoid overfitting and improve accuracy. While researching YOLO's data augmentation capabilities, a few questions came up:
- As far as I understand, YOLO currently supports two methods for performing data augmentation:
- Modifying individual hyperparameters such as hsv_h, hsv_s, hsv_v, degrees, translate, etc., in the ultralytics/cfg/default.yaml file (does not require the Albumentations Python Library).
- Using the Albumentations Python Library (previously installed).
Is this correct? If so, what are the differences between the two methods, and which one is currently recommended?
-
Considering both options from question 1, does YOLO apply data augmentation only to images from the classes with less labels, or to images from all classes? I ask because if I have an imbalanced dataset and YOLO applies data augmentation to images from all classes, the imbalance would remain, wouldn’t it?
-
If I choose to use an older YOLO model, such as yolov8n.yaml, and I have Albumentations installed, will Albumentations-based data augmentation be applied during training?
-
What other suggestions would you recommend for dealing with imbalanced classes?
safwene-bahroun
Hello @glenn-jocher , Do you can , please , answer to these questions , because the same questions and problem.
THANKS
Glenn JocherUltralytics@deox1994 Thanks for your detailed questions about data augmentation in YOLO for handling your imbalanced mining worker dataset. Let me address each point:
- Yes, you're correct about the two augmentation approaches:
- Built-in YOLO augmentations (hsv_h, translate, scale, etc.) which are always available
- Albumentations integration which activates automatically when the library is installed
The key difference is that built-in augmentations focus primarily on geometric and color transformations, while Albumentations adds more specialized transformations like blur, grayscale, and CLAHE with carefully tuned probabilities (typically 0.01 as shown in the reference code). Both work together complementarily rather than being exclusive options.
-
Both augmentation methods apply to all images regardless of class. To address your imbalance specifically, consider:
- Using a weighted loss function
- Implementing class-aware sampling during training
- Creating custom data pipeline that applies additional augmentations to underrepresented classes
-
Yes, if Albumentations is installed, it will be automatically applied when training YOLOv8 models too. The integration is part of the Ultralytics framework, not specific to YOLO11.
-
For handling imbalanced classes beyond augmentation:
- Apply weighted loss based on inverse class frequency
- Use focal loss which naturally handles class imbalance better
- Consider different evaluation metrics (precision/recall per class rather than just mAP)
- Experiment with undersampling majority classes or oversampling minority classes
You can find implementation details for these approaches in the data augment reference documentation.
- Yes, you're correct about the two augmentation approaches:
safwene-bahroun
Good morning ,@glenn-jocher, do you can help me ?
{ "total_images": 39152, "total_detections": 70910, "instances_per_class": { "Dress": 52999, "Skirt": 1259, "Handbag": 2495, "High heels": 3124, "Swimwear": 10079, "Miniskirt": 954 } } i will train this with yolo11n.pt and with albumentation the code : from ultralytics import YOLO import torch
def main(): # Load a pretrained YOLO model model = YOLO("yolo11n.pt") # Set device device = "cuda" if torch.cuda.is_available() else "cpu" print(f"Using device: {device}")
if device == "cuda": print(f"GPU name: {torch.cuda.get_device_name(0)}") # Debug: Print dataset configuration print("Training with dataset: OpenImagesV7-women.yaml") # Train the model with enhanced hyperparameters results = model.train( data="OpenImagesV7-woman.yaml", epochs=150, imgsz=640, device=device, batch=16, optimizer ="auto")
Dataset configuration
path: ../datasets/open-images-v7 train: c:\Users\GIGABYTE\datasets\open-images-v7\train val: C:/Users/GIGABYTE/datasets/open-images-v7/val
nc: 6 names: 0: Dress 1: High heels 2: Miniskirt 3: Skirt 4: Swimwear 5: Handbag
Hyperparameters for extreme imbalance
loss: cls_weight: [0.3, 0.5, 7.0, 5.0, 0.4, 0.6] # Calculated weights below box: 0.05 obj: 1.0 label_smoothing: 0.1
Strategic augmentation
augment:
For rare classes (Miniskirt, Skirt)
copy_paste: 0.7 # High frequency for rare classes paste_classes: [2, 3] # Only Miniskirt(class2)/Skirt(class3) paste_scale: [0.3, 0.7] # Realistic resizing of pasted objects
General augmentations (applied to all)
mosaic: 1.0 # Helps context learning mixup: 0.05 # Reduced to avoid label confusion fliplr: 0.5 hsv_h: 0.015 # Mild color variation degrees: 5.0 # Small rotation for fashion items
Training parameters
batch: 16 # Match GPU capacity accumulate: 4 # Simulates batch=64 epochs: 150 # Longer training for rare classes patience: 50 # Early stopping if no improveme
but i trained untill now :
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 104/150 2.92G 1.026 0.9478 1.206 55 640: 100%|██████████| 2447/2447 [07:51<00:00, Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 32/32 [00:06 all 1016 1987 0.581 0.525 0.536 0.423Can you help me please to reach all metrics above 0.8
THANKS
hillsonghimire
Even when I turned off augment=False, albumentation-related operations were still performed in yolo11. How to completely turn it off/disable?
Glenn JocherUltralytics@hillsonghimire to completely disable Albumentations augmentations in YOLO11, uninstall the Albumentations package since its presence automatically enables the integration. Run
pip uninstall albumentationsand verify it's removed from your environment. This will prevent any Albumentations transformations from being applied regardless of other settings. For more details, see the Albumentations integration guide.
DP1701
Thanks for this explanation. But if I understand correctly, not all functions of Albuminations are directly supported, or am I misunderstanding?
The explanation says: The integration includes optimized augmentations like blur, median blur, grayscale conversion, and CLAHE with carefully tuned probabilities to enhance model performance.
So only blur, median blur, grayscale conversion and CLAHE are applied and the others cannot be individually selected and adjusted?
Glenn JocherUltralytics(已编辑)The integration with Albumentations in YOLO11 automatically applies specific augmentations like blur, median blur, grayscale conversion, and CLAHE. While these are the default transformations used to enhance model performance, you can pass your own Albumentations transforms to
model.train()with theaugmentationsargument (Python only).