深度学习数据增强的常用方法
以下是在深度学习中经常使用的图像增强的方法
目录
前言
1、加噪声
2、调整亮度
3、cutout
4、旋转
5、对比度增强
6、仿射变化扩充图像
7、HSV数据增强
8、错切变化扩充图像
9、平移扩充图像,根图像移动的像素距离可自行调整,具体方法如下注释所示
10、主函数(这里介绍如何调用前面的函数)
前言
数据增强是一种在深度学习中常用的技术,它通过生成新的训练样本来扩展现有的数据集。这一过程通常涉及对原始数据进行一系列变换,如旋转、缩放、裁剪、翻转、颜色调整等,从而创建出与原始数据略有不同的新样本。
1、加噪声
from skimage.util import random_noise
# ----1.加噪声---- #
def _addNoise(self, img):
'''
输入:
img:图像array
输出:
加噪声后的图像array,由于输出的像素是在[0,1]之间,所以得乘以255
'''
# return cv2.GaussianBlur(img, (11, 11), 0)
return random_noise(img, mode='gaussian', clip=True) * 255
2、调整亮度
# ---2.调整亮度--- #
def _changeLight(self, img):
# 从边缘分布中采样
alpha = random.uniform(0.35, 1)
# 做了一个零矩阵
blank = np.zeros(img.shape, img.dtype)
# alpha为权重,alpha的img内的像素点的值 + 1-alpha的黑颜色的值
return cv2.addWeighted(img, alpha, blank, 1 - alpha, 0)
3、cutout
# ---3.cutout--- #
def _cutout(self, img, bboxes, length=100, n_holes=1, threshold=0.5):
'''
原版本:https://github.com/uoguelph-mlrg/Cutout/blob/master/util/cutout.py
Randomly mask out one or more patches from an image.
Args:
img : a 3D numpy array,(h,w,c)
bboxes : 框的坐标
n_holes (int): Number of patches to cut out of each image.
length (int): The length (in pixels) of each square patch.
'''
def cal_iou(boxA, boxB):
# 两张图片重叠的部分称为交集,重叠的两张图片的实际占地面积成为并集
# IOU=交集:并集
'''
boxA, boxB为两个框,返回iou
boxB为bouding box
两张图的交集/两张图的并集
'''
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
if xB <= xA or yB <= yA:
return 0.0
# compute the area of intersection rectangle
interArea = (xB - xA + 1) * (yB - yA + 1)
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
iou = interArea / float(boxBArea)
return iou
# 得到h和w
if img.ndim == 3:
h, w, c = img.shape
else:
_, h, w, c = img.shape
mask = np.ones((h, w, c), np.float32)
for n in range(n_holes):
chongdie = True # 看切割的区域是否与box重叠太多
while chongdie:
# 随机选取的x和y会决定一片区域,这片区域最后被剪掉不要了
y = np.random.randint(h)
x = np.random.randint(w)
y1 = np.clip(y - length // 2, 0,
h) # numpy.clip(a, a_min, a_max, out=None), clip这个函数将将数组中的元素限制在a_min, a_max之间,大于a_max的就使得它等于 a_max,小于a_min,的就使得它等于a_min
y2 = np.clip(y + length // 2, 0, h)
x1 = np.clip(x - length // 2, 0, w)
x2 = np.clip(x + length // 2, 0, w)
chongdie = False
for box in bboxes:
if cal_iou([x1, y1, x2, y2], box) > threshold:
chongdie = True
break
mask[y1: y2, x1: x2, :] = 0.
img = img * mask
return img
4、旋转
def flip(root_path,img_name): #翻转图像
img = Image.open(os.path.join(root_path, img_name))
filp_img = img.transpose(Image.FLIP_LEFT_RIGHT)
# filp_img.save(os.path.join(root_path,img_name.split('.')[0] + '_flip.jpg'))
return filp_img
5、对比度增强
def contrastEnhancement(root_path, img_name): # 对比度增强
image = Image.open(os.path.join(root_path, img_name))
enh_con = ImageEnhance.Contrast(image)
# contrast = 1.1+0.4*np.random.random()#取值范围1.1-1.5
contrast = 1.5
image_contrasted = enh_con.enhance(contrast)
return image_contrasted
6、仿射变化扩充图像
def fangshe_bianhuan(root_path,img_name): #仿射变化扩充图像
img = Image.open(os.path.join(root_path, img_name))
img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)
h, w = img.shape[0], img.shape[1]
m = cv2.getRotationMatrix2D(center=(w // 2, h // 2), angle=-30, scale=0.5)
r_img = cv2.warpAffine(src=img, M=m, dsize=(w, h), borderValue=(0, 0, 0))
r_img = Image.fromarray(cv2.cvtColor(r_img, cv2.COLOR_BGR2RGB))
return r_img
7、HSV数据增强
def hsv(root_path,img_name):#HSV数据增强
h_gain , s_gain , v_gain = 0.5 , 0.5 , 0.5
img = Image.open(os.path.join(root_path, img_name))
img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)
r = np.random.uniform(-1, 1, 3) * [h_gain, s_gain, v_gain] + 1 # random gains
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
dtype = img.dtype # uint8
x = np.arange(0, 256, dtype=np.int16)
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)
img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype)
aug_img = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)
aug_img = Image.fromarray(cv2.cvtColor(aug_img, cv2.COLOR_BGR2RGB))
return aug_img
8、错切变化扩充图像
def cuoqie(root_path,img_name): #错切变化扩充图像
img = Image.open(os.path.join(root_path, img_name))
img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)
h, w = img.shape[0], img.shape[1]
origin_coord = np.array([[0, 0, 1], [w, 0, 1], [w, h, 1], [0, h, 1]])
theta = 30 # shear角度
tan = math.tan(math.radians(theta))
# x方向错切
m = np.eye(3)
m[0, 1] = tan
shear_coord = (m @ origin_coord.T).T.astype(np.int_)
shear_img = cv2.warpAffine(src=img, M=m[:2],
dsize=(np.max(shear_coord[:, 0]), np.max(shear_coord[:, 1])),
borderValue=(0, 0, 0))
c_img = Image.fromarray(cv2.cvtColor(shear_img, cv2.COLOR_BGR2RGB))
return c_img
9、平移扩充图像,根图像移动的像素距离可自行调整,具体方法如下注释所示
def pingyi(root_path,img_name):#平移扩充图像,根图像移动的像素距离可自行调整,具体方法如下注释所示
img = Image.open(os.path.join(root_path, img_name))
img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)
cols , rows= img.shape[0], img.shape[1]
M = np.float32([[1, 0, 50], [0, 1, 30]])#50为x即水平移动的距离,30为y 即垂直移动的距离
dst = cv2.warpAffine(img, M, (cols, rows),borderValue=(0,255,0))
pingyi_img = Image.fromarray(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB))
return pingyi_img
10、主函数(这里介绍如何调用前面的函数)
def createImage(imageDir,saveDir):#主函数,8种数据扩充方式,每种扩充一张
i=0
for name in os.listdir(imageDir):
i=i+1
saveName="cesun"+str(i)+".jpg"
saveImage=contrastEnhancement(imageDir,name)
saveImage.save(os.path.join(saveDir,saveName))
saveName1 = "flip" + str(i) + ".jpg"
saveImage1 = flip(imageDir,name)
saveImage1.save(os.path.join(saveDir, saveName1))
saveName2 = "brightnessE" + str(i) + ".jpg"
saveImage2 = brightnessEnhancement(imageDir, name)
saveImage2.save(os.path.join(saveDir, saveName2))
saveName3 = "rotate" + str(i) + ".jpg"
saveImage = rotation(imageDir, name)
saveImage.save(os.path.join(saveDir, saveName3))
saveName4 = "fangshe" + str(i) + ".jpg"
saveImage = fangshe_bianhuan(imageDir, name)
saveImage.save(os.path.join(saveDir, saveName4))
saveName5 = "cuoqie" + str(i) + ".jpg"
saveImage = cuoqie(imageDir, name)
saveImage.save(os.path.join(saveDir, saveName5))
saveName6 = "hsv" + str(i) + ".jpg"
saveImage = hsv(imageDir, name)
saveImage.save(os.path.join(saveDir, saveName6))
saveName6 = "pingyi" + str(i) + ".jpg" #不需要平移变换的,可以注释掉 这三行代码 135 136 137行
saveImage = pingyi(imageDir, name) #不需要平移变换的,可以注释掉 这三行代码
saveImage.save(os.path.join(saveDir, saveName6)) #不需要平移变换的,可以注释掉 这三行代码
imageDir="jpg" #要改变的图片的路径文件夹 在当前文件夹下,建立文件夹即可
saveDir="kuochong" #数据增强生成图片的路径文件夹
print('文件的初始文件夹为:' + imageDir)
print('----------------------------------------')
print('文件的转换后存入的文件夹为:' + saveDir)
print('----------------------------------------')
print('开始转换')
print('----------------------------------------')
createImage(imageDir,saveDir)
print('----------------------------------------')
print("数据扩充完成")