首页 > 其他分享 >目标识别数据集扩充方法

目标识别数据集扩充方法

时间:2023-01-03 19:04:38浏览次数:36  
标签:img min shift random 目标 np 识别 扩充 size


背景引入

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

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

具体方法

原图


目标识别数据集扩充方法_数据集

图像强度变换

亮度变化

​lightness​

​darkness​

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

  • 亮度增强
  • 亮度减弱
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的位置


目标识别数据集扩充方法_深度学习_02


图像滤波

锐化

​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的位置


目标识别数据集扩充方法_数据_03

高斯模糊

​blur​

图像平滑

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

不影响label的位置


目标识别数据集扩充方法_数据集_04


透视变换

镜像翻转

​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])


目标识别数据集扩充方法_数据集_05

图像裁剪

​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]
]

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


目标识别数据集扩充方法_计算机视觉_06

图像拉伸

​deform​

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

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

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


目标识别数据集扩充方法_数据_07

镜头畸变

​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)

最好重新手工标注


目标识别数据集扩充方法_计算机视觉_08


注入噪声

椒盐噪声

​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的位置


目标识别数据集扩充方法_计算机视觉_09

渐晕

​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的位置


目标识别数据集扩充方法_计算机视觉_10


其他

随机抠除

​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的位置


目标识别数据集扩充方法_深度学习_11



标签:img,min,shift,random,目标,np,识别,扩充,size
From: https://blog.51cto.com/doubleZ/5986597

相关文章