目标识别数据集扩充方法

本文探讨了如何通过数据增强技术提升深度学习模型在图像识别任务中的性能。具体方法包括亮度、对比度调整,图像滤波(如锐化和模糊),透视变换(如翻转和裁剪),图像拉伸和变形,以及噪声注入等。这些技术旨在模拟真实世界的变化,防止过拟合并提高模型的泛化能力。例如,通过弹性扭曲和旋转扩展MNIST数据集,分类准确率从98.9%提升至99.3%。数据增强对于保持模型的鲁棒性和准确性至关重要。

背景引入

在训练图像识别的深度神经网络时,使用大量更多的训练数据,可能会使网络得到更好的性能,例如提高网络的分类准确率,防止过拟合等。人为扩展训练数据时对数据的操作最好能反映真实世界的变化。人为扩充数据集之后如果分类准确率有明显的提升,说明我们对数据所做的拓展操作是良性的,能够“反映真实世界的变化”,就会被用到整个数据集的扩展。反之,则说明不能用此操作对数据集进行拓展。

例如在2003年Patrice Simard等人所著的一篇论文中[1]他们把MNIST手写数字数据集通过旋转,转换和扭曲进行扩展。通过在这个扩展后的数据集上的训练,他们把MNIST手写数字识别的准确率提升到了98.9%。然后还在“弹性扭曲”的数据集上进行了实验,这是一种特殊的为了模仿手部肌肉的随机抖动的图像扭曲方法。通过使用弹性扭曲扩展的数据,他们最终达到了99.3%的分类准确率。

具体方法

原图

demo.jpg

图像强度变换

亮度变化

lightness

darkness

图像整体加上一个随机偏差,或整体进行尺度的放缩

  • 亮度增强

    demo_brightness.jpg
  • 亮度减弱

    demo_darkness.jpg
brightness = 1 + np.random.randint(1, 9) / 10
brightness_img = img.point(lambda p: p * brightness)

不影响label的位置

对比度变化

contrast

扩展图像灰度级动态范围,对两极的像素进行压缩,对中间范围的像素进行扩展

range_contrast=(-50, 50)
contrast = np.random.randint(*range_contrast)
contrast_img = img.point(lambda p: p * (contrast / 127 + 1) - contrast)

不影响label的位置

demo_contrast.jpg

图像滤波

锐化

sharpen

增强图像边缘信息

identity = np.array([[0, 0, 0],
                     [0, 1, 0],
                     [0, 0, 0]])
sharpen = np.array([[ 0, -1,  0],
                    [-1,  4, -1],
                    [ 0, -1,  0]]) / 4
max_center = 4
sharp = sharpen * np.random.random() * max_center
kernel = identity + sharp
sharpen_img = cv2.filter2D(img, -1, kernel)

不影响label的位置

demo_sharpen.jpg
高斯模糊

blur

图像平滑

kernel_size = (7, 7)
blur_img = cv2.GaussianBlur(img,kernel_size,0)

不影响label的位置

demo_blur.jpg

透视变换

镜像翻转

flip

使图像沿长轴进行翻转

flip_img = cv2.flip(cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR), 1)

第一个位置的参数 pos = 1 - pos,其他信息不变,可以采用脚本自动生成

with open(name + "_flip.txt", "w") as outfile:
  with open(name + ".txt", "r") as infile:
    for line in infile.readlines():
      words = line.split(" ")
      horizontal_coord = float(words[1])
      outfile.write(words[0] + " " + str(format(1-horizontal_coord, ".6f")) + " " + words[2] + " " + words[3] + " " + words[4])
demo_flip.jpg
图像裁剪

crop

裁剪原图80%大小的中心图像,并进行随机移动

kernel_size = list(map(lambda x: int(x*0.8), size))
shift_min, shift_max = -50, 50
shift_size = [np.random.randint(shift_min, shift_max), np.random.randint(shift_min, shift_max)]

crop_img = img[
  (size[0]-kernel_size[0])//2+shift_size[0]:(size[0]-kernel_size[0])//2+kernel_size[0]+shift_size[0],
  (size[1]-kernel_size[1])//2+shift_size[1]:(size[1]-kernel_size[1])//2+kernel_size[1]+shift_size[1]
]

可能将目标对象裁减掉,因此采用手工重新标注

demo_crop.jpg
图像拉伸

deform

拉伸成长宽为原始宽的正方形图像

deform_img = img.resize((int(w), int(w)))

原图中比例信息改变,最好重新手工标注

demo_deform.jpg
镜头畸变

distortion

对图像进行透视变化,模拟鱼眼镜头的镜头畸变

通过播放径向系数k1,k2,k3和切向系数p1,p2实现

d_coef= np.array((0.15, 0.15, 0.1, 0.1, 0.05))
# get the height and the width of the image
h, w = img.shape[:2]
# compute its diagonal
f = (h ** 2 + w ** 2) ** 0.5
# set the image projective to carrtesian dimension
K = np.array([[f, 0, w / 2],
              [0, f, h / 2],
              [0, 0,   1  ]])
d_coef = d_coef * np.random.random(5) # value
d_coef = d_coef * (2 * (np.random.random(5) < 0.5) - 1) # sign
# Generate new camera matrix from parameters
M, _ = cv2.getOptimalNewCameraMatrix(K, d_coef, (w, h), 0)
# Generate look-up tables for remapping the camera image
remap = cv2.initUndistortRectifyMap(K, d_coef, None, M, (w, h), 5)
# Remap the original image to a new image
distortion_img = cv2.remap(img, *remap, cv2.INTER_LINEAR)

最好重新手工标注

demo_distortion.jpg

注入噪声

椒盐噪声

noise

在图像中随机添加白/黑像素

for i in range(5000):
  x = np.random.randint(0,rows)
  y = np.random.randint(0,cols)
  noise_img[x,y,:] = 255
  noise_img.flags.writeable = True

不影响label的位置

demo_noise.jpg
渐晕

vignetting

对图像添加一个圆范围内的噪声模拟光晕

ratio_min_dist=0.2
range_vignette=np.array((0.2, 0.8))
random_sign=False

h, w = img.shape[:2]
min_dist = np.array([h, w]) / 2 * np.random.random() * ratio_min_dist

# create matrix of distance from the center on the two axis
x, y = np.meshgrid(np.linspace(-w/2, w/2, w), np.linspace(-h/2, h/2, h))
x, y = np.abs(x), np.abs(y)
# create the vignette mask on the two axis
x = (x - min_dist[0]) / (np.max(x) - min_dist[0])
x = np.clip(x, 0, 1)
y = (y - min_dist[1]) / (np.max(y) - min_dist[1])
y = np.clip(y, 0, 1)
# then get a random intensity of the vignette
vignette = (x + y) / 2 * np.random.uniform(*range_vignette)
vignette = np.tile(vignette[..., None], [1, 1, 3])
sign = 2 * (np.random.random() < 0.5) * (random_sign) - 1
vignetting_img = img * (1 + sign * vignette)

不影响label的位置

demo_vignetting.jpg

其他

随机抠除

cutout

随机抠出四个位置,并用黑色/彩色矩形填充

channel_wise = False
max_crop = 4
replacement=0

size = np.array(img.shape[:2])
mini, maxi = min_size_ratio * size, max_size_ratio * size
cutout_img = img
for _ in range(max_crop):
  # random size
  h = np.random.randint(mini[0], maxi[0])
  w = np.random.randint(mini[1], maxi[1])
  # random place
  shift_h = np.random.randint(0, size[0] - h)
  shift_w = np.random.randint(0, size[1] - w)

  if channel_wise:
    c = np.random.randint(0, img.shape[-1])
    cutout_img[shift_h:shift_h+h, shift_w:shift_w+w, c] = replacement
    else:
      cutout_img[shift_h:shift_h+h, shift_w:shift_w+w] = replacement

不影响label的位置

demo_cutout.jpg
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

doubleZ0108

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值