1. 概述
YOLOv5的数据加载与增强模块是其高性能目标检测的重要组成部分。该模块不仅负责高效地加载和预处理数据,还集成了多种先进的数据增强技术,显著提升了模型的泛化能力和检测精度。
2. 核心数据结构
2.1 LoadImagesAndLabels类
这是训练阶段最主要的数据集类,具有以下核心功能:
python
class LoadImagesAndLabels(Dataset):
def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, ...):
# 初始化参数
self.img_files = [] # 图像文件列表
self.label_files = [] # 标签文件列表
self.img_size = img_size
self.augment = augment # 是否进行数据增强
self.rect = rect # 是否使用矩形训练
self.mosaic = self.augment and not self.rect # Mosaic增强
关键特性:
-
自动解析图像和标签文件路径
-
支持缓存机制加速训练
-
提供矩形训练和正方形训练两种模式
-
集成多种数据增强策略
2.2 数据加载流程
python
def __getitem__(self, index):
# Mosaic数据增强(训练时)
if mosaic:
img, labels = load_mosaic(self, index)
# MixUp增强
if random.random() < hyp['mixup']:
img2, labels2 = load_mosaic(self, random.randint(0, len(self.labels)-1))
img = (img * r + img2 * (1 - r)).astype(np.uint8)
labels = np.concatenate((labels, labels2), 0)
else:
# 单图像加载
img, (h0, w0), (h, w) = load_image(self, index)
# Letterbox调整
img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
3. 数据增强技术详解
3.1 Mosaic数据增强
Mosaic是YOLOv5中极具特色的增强技术,它将4张图像拼接成一张进行训练:
python
def load_mosaic(self, index):
# 随机选择4张图像
indices = [index] + [random.randint(0, len(self.labels)-1) for _ in range(3)]
# 创建2x2的大图
img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8)
# 将4张图像放置到对应位置
for i, index in enumerate(indices):
# 计算放置位置和裁剪区域
if i == 0: # 左上
x1a, y1a, x2a, y2a = max(xc-w,0), max(yc-h,0), xc, yc
x1b, y1b, x2b, y2b = w-(x2a-x1a), h-(y2a-y1a), w, h
# ... 其他三个位置
img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]
# 对拼接后的大图进行随机透视变换
img4, labels4 = random_perspective(img4, labels4, ...)
优势:
-
增加批内多样性
-
提升小目标检测能力
-
模拟多尺度训练
3.2 随机透视变换
python
def random_perspective(img, targets, degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0):
# 组合多种几何变换
M = T @ S @ R @ P @ C # 平移、裁剪、旋转、透视、中心化的组合矩阵
# 应用变换
img = cv2.warpPerspective(img, M, dsize=(width, height), borderValue=(114,114,114))
# 同步变换边界框坐标
xy = np.ones((n * 4, 3))
xy[:, :2] = targets[:, [1,2,3,4,1,4,3,2]].reshape(n*4, 2)
xy = xy @ M.T # 变换坐标
3.3 颜色空间增强
python
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
# HSV颜色空间增强
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
# 应用查找表变换
lut_hue = ((x * r[0]) % 180).astype(dtype)
lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
4. 高效数据加载机制
4.1 缓存优化
python
def cache_labels(self, path='labels.cache'):
# 缓存标签信息,避免重复解析
cache = torch.load(cache_path) if os.path.isfile(cache_path) else self.cache_labels(cache_path)
# 图像缓存(可选)
if cache_images:
self.imgs = [None] * n
for i in pbar:
self.imgs[i], self.img_hw0[i], self.img_hw[i] = load_image(self, i)
4.2 无限数据加载器
python
class InfiniteDataLoader(torch.utils.data.dataloader.DataLoader):
"""可重复使用工作进程的数据加载器"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
object.__setattr__(self, 'batch_sampler', _RepeatSampler(self.batch_sampler))
5. 推理阶段数据加载
5.1 图像/视频加载
python
class LoadImages: # 用于推理
def __init__(self, path, img_size=640):
# 支持多种输入源:单图像、图像目录、视频文件
self.files = images + videos
self.video_flag = [False] * ni + [True] * nv
5.2 网络摄像头支持
python
class LoadWebcam: # 网络摄像头推理
def __init__(self, pipe=0, img_size=640):
self.cap = cv2.VideoCapture(pipe)
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # 设置缓冲区大小
6. 实用工具函数
6.1 Letterbox函数
python
def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
# 保持宽高比的图像调整
shape = img.shape[:2] # 当前形状 [高度, 宽度]
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
# 计算填充
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]
# 添加边界
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
7. 总结
YOLOv5的数据加载与增强模块具有以下突出特点:
-
高效性:通过缓存和多进程加载实现高速数据流水线
-
多样性:集成Mosaic、MixUp、随机透视变换等多种先进增强技术
-
灵活性:支持多种输入源和训练模式
-
鲁棒性:完善的错误处理和数据验证机制
9万+

被折叠的 条评论
为什么被折叠?



