1. 随机擦除(Random Erasing)
说明
随机在图像中选取一个矩形区域,将其像素值随机化或设为零,以增加模型对部分缺失信息的鲁棒性。
import numpy as np
import cv2def random_erasing(image, sl=0.02, sh=0.2, r1=0.3):h, w, _ = image.shapearea = h * wtarget_area = np.random.uniform(sl, sh) * areaaspect_ratio = np.random.uniform(r1, 1 / r1)h_erased = int(round(np.sqrt(target_area * aspect_ratio)))w_erased = int(round(np.sqrt(target_area / aspect_ratio)))if h_erased < h and w_erased < w:x1 = np.random.randint(0, h - h_erased)y1 = np.random.randint(0, w - w_erased)image[x1:x1 + h_erased, y1:y1 + w_erased, :] = np.random.randint(0, 255, (h_erased, w_erased, 3))return image
优缺点
- 优点: 增强模型对遮挡和部分信息缺失的鲁棒性。
- 缺点: 对图像语义信息敏感,可能擦除重要区域。
2. cutout
说明
类似随机擦除,但通常将被遮挡的区域设置为零而非随机值。
def cutout(image, mask_size=50):h, w, _ = image.shapex = np.random.randint(w)y = np.random.randint(h)x1 = max(0, x - mask_size // 2)y1 = max(0, y - mask_size // 2)x2 = min(w, x + mask_size // 2)y2 = min(h, y + mask_size // 2)image[y1:y2, x1:x2, :] = 0return image
优缺点
- 优点: 简单易用,增强遮挡鲁棒性。
- 缺点: 被遮挡区域始终为零,可能对模型学习不利。
3. Hide-and-Seek
说明
将图像分成多个网格,并随机遮挡部分网格。
def hide_and_seek(image, grid_size=4, hide_prob=0.5):h, w, _ = image.shapegh, gw = h // grid_size, w // grid_sizefor i in range(grid_size):for j in range(grid_size):if np.random.rand() < hide_prob:image[i * gh:(i + 1) * gh, j * gw:(j + 1) * gw, :] = 0return image
4. GridMask
说明
通过有规律地遮挡图像中的区域,形成网格状的遮挡图案。
def grid_mask(image, grid_size=4):h, w, _ = image.shapemask = np.ones((h, w, 3), dtype=np.uint8)for i in range(0, h, grid_size * 2):for j in range(0, w, grid_size * 2):mask[i:i + grid_size, j:j + grid_size, :] = 0return image * mask
优缺点
- 优点: 系统性遮挡,提高模型对特定模式的鲁棒性。
- 缺点: 固定遮挡模式可能使模型过拟合。
5. DropBlock
说明
类似于 Dropout,但随机“屏蔽”特定大小的连续区域。
def drop_block(image, block_size=5):h, w, _ = image.shapemask = np.ones((h, w), dtype=np.uint8)for _ in range(5): # Number of blocks to dropx = np.random.randint(block_size, w - block_size)y = np.random.randint(block_size, h - block_size)mask[y - block_size:y + block_size, x - block_size:x + block_size] = 0return image * mask[..., None]
优缺点
- 优点: 类似随机擦除,但对连续区域更敏感。
- 缺点: 参数设计复杂。
6. Mixup
说明
将两张图像和对应标签线性混合。
def mixup(image1, image2, label1, label2, alpha=0.2):lam = np.random.beta(alpha, alpha)mixed_image = lam * image1 + (1 - lam) * image2mixed_label = lam * label1 + (1 - lam) * label2return mixed_image, mixed_label
优缺点
- 优点: 提高模型对样本之间关系的学习能力。
- 缺点: 标签混合可能降低语义明确性。
7. CutMix
说明
CutMix 是一种数据增强方法,它将两张图像随机剪裁并混合,同时混合它们的标签。这种方法既保留了遮挡信息,又引入了跨图像的信息互补。
import numpy as npdef cutmix(image1, image2, label1, label2, alpha=1.0):"""CutMix增强方法。:param image1: 第一张图像 (numpy array):param image2: 第二张图像 (numpy array):param label1: 第一张图像的标签 (numpy array or scalar):param label2: 第二张图像的标签 (numpy array or scalar):param alpha: Beta分布的参数:return: 混合后的图像和标签"""h, w, _ = image1.shapelam = np.random.beta(alpha, alpha) # 生成混合比例 lambda# 随机裁剪区域cut_rat = np.sqrt(1 - lam)cut_h = int(h * cut_rat)cut_w = int(w * cut_rat)# 随机选择裁剪位置cx = np.random.randint(w)cy = np.random.randint(h)x1 = np.clip(cx - cut_w // 2, 0, w)y1 = np.clip(cy - cut_h // 2, 0, h)x2 = np.clip(cx + cut_w // 2, 0, w)y2 = np.clip(cy + cut_h // 2, 0, h)# 创建混合图像mixed_image = image1.copy()mixed_image[y1:y2, x1:x2, :] = image2[y1:y2, x1:x2, :]# 混合标签lam = 1 - ((x2 - x1) * (y2 - y1) / (h * w))mixed_label = lam * label1 + (1 - lam) * label2return mixed_image, mixed_label
优点
-
遮挡与信息互补:
- 同时增强了模型对遮挡和混合信息的鲁棒性。
- 保留了图像的空间结构特性。
-
提高泛化能力:
- 引入了跨样本的信息,减少模型对单一模式的依赖,防止过拟合。
-
简单高效:
- 相较于 Mosaic 或 Style Transfer,CutMix 的实现更简单,适用范围更广。
缺点
-
标签模糊性:
- 对于单标签分类任务,混合后的标签可能引入不确定性。
- 对于部分任务(如目标检测),需要额外处理目标框信息。
-
对语义敏感:
- 剪裁和混合的区域可能遮挡关键语义,影响模型学习。
8. Mosaic马赛克
说明
将 4 张图像拼接成一个新的图像,用于丰富样本上下文。
def mosaic(images):h, w, _ = images[0].shapecanvas = np.zeros((2 * h, 2 * w, 3), dtype=np.uint8)canvas[:h, :w] = images[0]canvas[:h, w:] = images[1]canvas[h:, :w] = images[2]canvas[h:, w:] = images[3]return canvas
优缺点
- 优点: 增强上下文多样性。
- 缺点: 较复杂,标签处理困难。
9. Style Transfer GAN
说明
通过生成对抗网络改变图像风格以增强数据。
# 伪代码:使用预训练的 GAN 模型
from some_gan_library import StyleGAN
gan = StyleGAN()
stylized_image = gan.transfer_style(image, target_style="van_gogh")
优缺点
- 优点: 提高数据多样性,尤其适合小样本任务。
- 缺点: 计算复杂,风格选择影响较大。
10. 对比与总结
每种方式适用于不同的场景,可以结合使用以提高模型的鲁棒性与泛化能力。