深度学习模型训练数据增强方法汇总

  • 一、随机裁剪
  • 二、RGB-->BGR通道互换
  • 三、仿射变换(缩放)
  • 三、随机旋转
  • 四、对比度调整
  • 五、随机抠图
  • 六、bound box 中心点随机抠图
  • 七、随机缩放
  • 后面会增加一些非线性的数据增强的方法。。。。。。。。

一、随机裁剪

class RandomResize(object):def __init__(self, cfg):self.image_short_size = 768self.image_max_size = 768def __call__(self, data_dict):img_ori = data_dict["image"]bboxes = data_dict["bboxes"]keypoints = data_dict["keypoints"]availability = data_dict["availability"]mask_miss = data_dict["mask_miss"]bbox_idx = data_dict["crop_bbox_idx"]bboxes = bboxes.astype(np.float32).copy()keypoints = keypoints.astype(np.float32).copy()availability = availability.copy()scale_max = min(self.image_short_size / min(img_ori.shape[:2]), self.image_max_size / max(img_ori.shape[:2]))scale_max = min(2.5, scale_max)scale_min = .6 * scale_maxscale = np.random.uniform(scale_min, scale_max)img_resized = cv2.resize(img_ori, (0, 0), fx=scale, fy=scale)mask_miss_resized = cv2.resize(mask_miss, (0, 0), fx=scale, fy=scale)bboxes[:, :4] *= scalekeypoints *= scaledata_dict["image"] = img_resizeddata_dict["bboxes"] = bboxesdata_dict["keypoints"] = keypointsdata_dict["availability"] = availabilitydata_dict["mask_miss"] = mask_miss_resizedreturn data_dict

二、RGB–>BGR通道互换

class RandomBGRRGBInverse(object):def __init__(self, cfg):passdef __call__(self, data_dict):if np.random.random() > .5:data_dict["image"] = data_dict["image"][:, :, ::-1].copy()return data_dict

三、仿射变换(缩放)

注意:config.TRAIN.TRANSFORM_PARAMS.max_affine_xy_ratio=0.3,如何设太大,内容会丢失,所以只支持缩小。

class RandomAffineTransform(object):def __init__(self, config):self.max_affine_xy_ratio = config.TRAIN.TRANSFORM_PARAMS.max_affine_xy_ratiodef __call__(self, data_dict):img_ori = data_dict["image"]bbox = data_dict["bboxes"]keypoints = data_dict["keypoints"]h, w, c = img_ori.shapetry:assert h >0 and w > 0except Exception as e:import logginglogging.exception(e)src_points = np.array([[0, 0], [w - 1, 0], [w - 1, h - 1], [0, h - 1]])new_h=int(self.max_affine_xy_ratio * h)new_w=int(self.max_affine_xy_ratio * w)if new_h<1:rdh = lambda: np.random.randint(0, h)else:rdh = lambda: np.random.randint(0, new_h)if new_w<1:rdw = lambda: np.random.randint(0,  w)else:rdw = lambda: np.random.randint(0, new_w)dst_points = np.array([[rdw(), rdh()], [w - 1 - rdw(), rdh()], [w - 1 - rdw(), h - 1 - rdh()], [rdw(), h - 1 - rdh()]])H, _ = cv2.findHomography(src_points, dst_points, cv2.LMEDS)if keypoints.shape[1] >= 1:availability = data_dict["availability"]mask_miss = data_dict["mask_miss"]assert bbox.shape.__len__() == 2assert bbox.shape[1] == 4assert keypoints.shape.__len__() == 3assert keypoints.shape[2] == 2assert availability.shape.__len__() == 2image_transformed = cv2.warpPerspective(img_ori, H, (img_ori.shape[1], img_ori.shape[0]))mask_miss_transformed = cv2.warpPerspective(mask_miss, H, (img_ori.shape[1], img_ori.shape[0]))keypoints_dst = cv2.perspectiveTransform(keypoints, H)data_dict["image"] = image_transformeddata_dict["bboxes"] = bboxdata_dict["keypoints"] = keypoints_dst.reshape(keypoints.shape)data_dict["availability"] = availabilitydata_dict["mask_miss"] = mask_miss_transformedreturn data_dict

三、随机旋转

class RandomRotate(object):def __init__(self, config):self.min_angle = -1 * config.TRAIN.TRANSFORM_PARAMS.max_rotation_degreeself.max_angle = config.TRAIN.TRANSFORM_PARAMS.max_rotation_degreedef __call__(self, data_dict):img_ori = data_dict["image"]bbox = data_dict["bboxes"]keypoints = data_dict["keypoints"]availability = data_dict["availability"]mask_miss = data_dict["mask_miss"]assert bbox.shape.__len__() == 2assert bbox.shape[1] == 4assert keypoints.shape.__len__() == 3assert keypoints.shape[2] == 2assert availability.shape.__len__() == 2# rotate bbox and imagekps = np.empty(shape=(len(bbox), 4, 2), dtype=np.float32)kps[:, 0, :] = bbox[:, (0, 1)]kps[:, 1, :] = bbox[:, (2, 1)]kps[:, 2, :] = bbox[:, (2, 3)]kps[:, 3, :] = bbox[:, (0, 3)]if np.random.random() > .5:angle = np.random.uniform(self.min_angle, self.max_angle)else:angle = [0, 90, 180][np.random.randint(0, 3)]image_rotated, mask_miss_rotated, kps_rotated, M = rotate_bound(img_ori, mask_miss, kps.reshape((-1, 2)), angle)kps_rotated = kps_rotated.reshape(kps.shape)bbox_rotated = np.zeros_like(bbox)bbox_rotated[:, 0] = kps_rotated[:, :, 0].min(axis=1)bbox_rotated[:, 1] = kps_rotated[:, :, 1].min(axis=1)bbox_rotated[:, 2] = kps_rotated[:, :, 0].max(axis=1)bbox_rotated[:, 3] = kps_rotated[:, :, 1].max(axis=1)# rotate keypointskeypoints_reshapped = keypoints.reshape(-1, 2)keypoints_homo = np.ones(shape=(keypoints_reshapped.shape[0], 3))keypoints_homo[:, :2] = keypoints_reshappedkeypoints_rotated = keypoints_homo.dot(M.T)data_dict["image"] = image_rotateddata_dict["bboxes"] = bbox_rotateddata_dict["keypoints"] = keypoints_rotated.reshape(keypoints.shape)data_dict["availability"] = availabilitydata_dict["mask_miss"] = mask_miss_rotatedreturn data_dict

四、对比度调整

class brightnessJust():def __init__(self, config):self.alpha=config.BrightnessJust.alphaself.beta=config.BrightnessJust.alphadef __call__(self, data_dict):gamma=np.random.randint(0,127)src2=np.uint8(np.zeros([data_dict["image"].shape[0],data_dict["image"].shape[1],data_dict["image"].shape[2]]))data_dict["image"]=cv2.addWeighted(data_dict["image"],self.alpha,src2,self.beta,gamma)return data_dict

五、随机抠图

class RandomCrop(object):def __init__(self, cfg):self.center_perterb_max = cfg.TRAIN.TRANSFORM_PARAMS.center_perterb_max  # type: floatself.crop_size_x = cfg.TRAIN.TRANSFORM_PARAMS.crop_size_xself.crop_size_y = cfg.TRAIN.TRANSFORM_PARAMS.crop_size_yself.pad_value = cfg.TRAIN.TRANSFORM_PARAMS.PAD_VALUEdef __call__(self, data_dict):img_ori = data_dict["image"]bboxes = data_dict["bboxes"]keypoints = data_dict["keypoints"]availability = data_dict["availability"]mask_miss = data_dict["mask_miss"]bbox_idx = data_dict["crop_bbox_idx"]bboxes = bboxes.copy()keypoints = keypoints.copy()availability = availability.copy()center_x = np.random.randint(0, img_ori.shape[1])center_y = np.random.randint(0, img_ori.shape[0])center_x = int(np.round(center_x))center_y = int(np.round(center_y))start_x = max(center_x - self.crop_size_x // 2, 0)start_y = max(center_y - self.crop_size_y // 2, 0)end_x = min(center_x + self.crop_size_x // 2, img_ori.shape[1])end_y = min(center_y + self.crop_size_y // 2, img_ori.shape[0])offset_x = center_x - self.crop_size_x // 2offset_y = center_y - self.crop_size_y // 2image_cropped = img_ori[start_y:end_y, start_x:end_x]image_cropped_padded = np.ones(shape=(self.crop_size_y, self.crop_size_x, img_ori.shape[2]), dtype=np.float32) * self.pad_valuedst_start_x = start_x - offset_xdst_start_y = start_y - offset_ydst_end_x = dst_start_x + image_cropped.shape[1]dst_end_y = dst_start_y + image_cropped.shape[0]image_cropped_padded[dst_start_y:dst_end_y, dst_start_x:dst_end_x] = image_croppedmask_miss_cropped = mask_miss[start_y:end_y, start_x:end_x]mask_miss_cropped_padded = np.zeros(shape=(self.crop_size_y, self.crop_size_x), dtype=np.float32)mask_miss_cropped_padded[dst_start_y:dst_end_y, dst_start_x:dst_end_x] = mask_miss_croppedbboxes[:, (0, 2)] -= offset_xbboxes[:, (1, 3)] -= offset_ykeypoints[:, :, 0] -= offset_xkeypoints[:, :, 1] -= offset_yfor m in range(keypoints.shape[0]):for n in range(keypoints.shape[1]):x, y = keypoints[m, n]if not (0 <= x < image_cropped_padded.shape[1] and 0 <= y < image_cropped_padded.shape[0]):availability[m, n] = 0data_dict["image"] = image_cropped_paddeddata_dict["bboxes"] = bboxesdata_dict["keypoints"] = keypointsdata_dict["availability"] = availabilitydata_dict["crop_bbox_idx"] = bbox_idx  # to generate mask.data_dict["mask_miss"] = mask_miss_cropped_paddedreturn data_dict

六、bound box 中心点随机抠图

class RandomCenterCrop(object):def __init__(self, cfg):self.center_perterb_max = cfg.TRAIN.TRANSFORM_PARAMS.center_perterb_max  # type: floatself.crop_size_x = cfg.TRAIN.TRANSFORM_PARAMS.crop_size_xself.crop_size_y = cfg.TRAIN.TRANSFORM_PARAMS.crop_size_yself.pad_value = cfg.TRAIN.TRANSFORM_PARAMS.PAD_VALUEdef __call__(self, data_dict):img_ori = data_dict["image"]bboxes = data_dict["bboxes"]keypoints = data_dict["keypoints"]availability = data_dict["availability"]mask_miss = data_dict["mask_miss"]bbox_idx = data_dict["crop_bbox_idx"]bboxes = bboxes.copy()keypoints = keypoints.copy()availability = availability.copy()bbox = bboxes[bbox_idx]center_x = .5 * (bbox[0] + bbox[2])center_y = .5 * (bbox[1] + bbox[3])center_x += (np.random.random() * 2 - 1) * self.center_perterb_maxcenter_y += (np.random.random() * 2 - 1) * self.center_perterb_maxcenter_x = int(np.round(center_x))center_y = int(np.round(center_y))start_x = max(center_x - self.crop_size_x // 2, 0)start_y = max(center_y - self.crop_size_y // 2, 0)end_x = min(center_x + self.crop_size_x // 2, img_ori.shape[1])end_y = min(center_y + self.crop_size_y // 2, img_ori.shape[0])offset_x = center_x - self.crop_size_x // 2offset_y = center_y - self.crop_size_y // 2image_cropped = img_ori[start_y:end_y, start_x:end_x]image_cropped_padded = np.ones(shape=(self.crop_size_y, self.crop_size_x, img_ori.shape[2]), dtype=np.float32) * self.pad_valuedst_start_x = start_x - offset_xdst_start_y = start_y - offset_ydst_end_x = dst_start_x + image_cropped.shape[1]dst_end_y = dst_start_y + image_cropped.shape[0]image_cropped_padded[dst_start_y:dst_end_y, dst_start_x:dst_end_x] = image_croppedmask_miss_cropped = mask_miss[start_y:end_y, start_x:end_x]mask_miss_cropped_padded = np.zeros(shape=(self.crop_size_y, self.crop_size_x), dtype=np.float32)mask_miss_cropped_padded[dst_start_y:dst_end_y, dst_start_x:dst_end_x] = mask_miss_croppedbboxes[:, (0, 2)] -= offset_xbboxes[:, (1, 3)] -= offset_ykeypoints[:, :, 0] -= offset_xkeypoints[:, :, 1] -= offset_yfor m in range(keypoints.shape[0]):for n in range(keypoints.shape[1]):x, y = keypoints[m, n]if not (0 <= x < image_cropped_padded.shape[1] and 0 <= y < image_cropped_padded.shape[0]):availability[m, n] = 0data_dict["image"] = image_cropped_paddeddata_dict["bboxes"] = bboxesdata_dict["keypoints"] = keypointsdata_dict["availability"] = availabilitydata_dict["crop_bbox_idx"] = bbox_idx  # to generate mask.data_dict["mask_miss"] = mask_miss_cropped_paddedreturn data_dict

七、随机缩放

class RandomScale(object):def __init__(self, cfg):self.scale_min = cfg.TRAIN.TRANSFORM_PARAMS.scale_minself.scale_max = cfg.TRAIN.TRANSFORM_PARAMS.scale_maxself.crop_size_y = cfg.TRAIN.TRANSFORM_PARAMS.resize_base_size_yself.target_dist = cfg.TRAIN.TRANSFORM_PARAMS.target_distdef __call__(self, data_dict):img_ori = data_dict["image"]bboxes = data_dict["bboxes"]keypoints = data_dict["keypoints"]availability = data_dict["availability"]mask_miss = data_dict["mask_miss"]bbox_idx = data_dict["crop_bbox_idx"]bboxes = bboxes.astype(np.float32).copy()keypoints = keypoints.astype(np.float32).copy()availability = availability.copy()if np.random.random() > .5:scale_multiplier = np.random.random() * (self.scale_max - self.scale_min) + self.scale_minscale_self = (bboxes[bbox_idx][3] - bboxes[bbox_idx][1]) / self.crop_size_yscale_abs = self.target_dist / scale_selfscale = scale_abs * scale_multiplierelse:scale = 1img_resized = cv2.resize(img_ori, (0, 0), fx=scale, fy=scale)mask_miss_resized = cv2.resize(mask_miss, (0, 0), fx=scale, fy=scale)bboxes[:, :4] *= scalekeypoints *= scaledata_dict["image"] = img_resizeddata_dict["bboxes"] = bboxesdata_dict["keypoints"] = keypointsdata_dict["availability"] = availabilitydata_dict["mask_miss"] = mask_miss_resizedreturn data_dict

下面展示一些 内联代码片

from __future__ import division
import cv2
from PIL import Image,ImageFilter,ImageEnhance
import numpy as np
from numpy import random
import math
import glob
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
import os__all__ = ['Compose','RandomHflip', 'RandomUpperCrop', 'Resize', 'UpperCrop', 'RandomBottomCrop',"RandomErasing",'BottomCrop', 'Normalize','Normalize_test', 'RandomSwapChannels', 'FixRandomRotate','RandomRotate', 'RandomHShift',"CenterCrop",'ExpandBorder', 'RandomResizedCrop','RandomTwelveCrop','RandomDownCrop', 'DownCrop', 'ResizedCrop','Lighter']def ruihua(img):src = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)kernel2 = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)dst = cv2.filter2D(src, -1, kernel2)image = Image.fromarray(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB))return imagedef light_up(img):# img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)dst = np.uint8(np.clip((1.2 * img + 10), 0, 255))image = Image.fromarray(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB))return imageclass light_down(object):def __init__(self):passdef __call__(self, img):img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)dst = np.uint8(np.clip((0.6 * img + 10), 0, 255))# image = Image.fromarray(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB))return dst# def Laplas(img):
#     src = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
#
#     dst1 = cv2.pyrDown(src)
#
#     dst2 = cv2.pyrDown(dst1)
#     dst3 = cv2.pyrDown(dst2)
#
#     dst4 = cv2.pyrUp(dst3)
#     dst5 = cv2.pyrUp(dst4)
#     dst6 = cv2.pyrUp(dst5)
#
#     h1, w1, c1 = dst6.shape
#     src = cv2.resize(src, (w1, h1))
#     dst7 = src - dst6
#     image = Image.fromarray(cv2.cvtColor(dst7, cv2.COLOR_BGR2RGB))
#     return imagedef rotate_nobound(image, angle, center=None, scale=1.):(h, w) = image.shape[:2]# if the center is None, initialize it as the center of# the imageif center is None:center = (w // 2, h // 2)# perform the rotationM = cv2.getRotationMatrix2D(center, angle, scale)rotated = cv2.warpAffine(image, M, (w, h))return rotateddef scale_down(src_size, size):w, h = sizesw, sh = src_sizeif sh < h:w, h = float(w * sh) / h, shif sw < w:w, h = sw, float(h * sw) / wreturn int(w), int(h)def fixed_crop(src, x0, y0, w, h, size=None):out = src[y0:y0 + h, x0:x0 + w]if size is not None and (w, h) != size:out = cv2.resize(out, (size[0], size[1]), interpolation=cv2.INTER_CUBIC)return outdef center_crop(src, size):h, w = src.shape[0:2]new_w, new_h = scale_down((w, h), size)x0 = int((w - new_w) / 2)y0 = int((h - new_h) / 2)out = fixed_crop(src, x0, y0, new_w, new_h, size)return outdef bottom_crop(src, size):h, w = src.shape[0:2]new_w, new_h = scale_down((w, h), size)x0 = int((w - new_w) / 2)y0 = int((h - new_h) * 0.75)out = fixed_crop(src, x0, y0, new_w, new_h, size)return outdef rotate_bound(image, angle):# grab the dimensions of the image and then determine the# centerh, w = image.shape[:2](cX, cY) = (w // 2, h // 2)M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0)cos = np.abs(M[0, 0])sin = np.abs(M[0, 1])# compute the new bounding dimensions of the imagenW = int((h * sin) + (w * cos))nH = int((h * cos) + (w * sin))# adjust the rotation matrix to take into account translationM[0, 2] += (nW / 2) - cXM[1, 2] += (nH / 2) - cYrotated = cv2.warpAffine(image, M, (nW, nH))return rotatedclass Compose(object):def __init__(self, transforms):self.transforms = transformsdef __call__(self, img):for t in self.transforms:img = t(img)return imgclass pepper_salt_noise(object):def __init__(self):passdef __call__(self, image):image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)percetage = random.uniform(0, 0.017)Noiseimg = imageNoisenum = int(percetage * Noiseimg.shape[0] * Noiseimg.shape[1])for i in range(Noisenum):randx = random.randint(0, image.shape[0] - 1)randy = random.randint(0, image.shape[1] - 1)if random.uniform(0, 1) < 0.5:Noiseimg[randx, randy] = [0, 0, 0]else:Noiseimg[randx, randy] = [255, 255, 255]return Noiseimgclass perspective_transformation(object):def __init__(self, resize=50):self.resize = resizedef __call__(self, image):if random.random() > 0.5:image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)h, w, _ = image.shapesrc = np.array([[0, 0], [w-1, 0], [w-1, h-1], [0, h-1]], dtype=np.float32)l_up_x_scale = round(random.uniform(0.0, 0.2), 3)l_up_y_scale = round(random.uniform(0.0, 0.2), 3)r_up_x_scale = round(random.uniform(0.8, 1), 3)r_up_y_scale = round(random.uniform(0.0, 0.2), 3)r_down_x_scale = round(random.uniform(0.8, 1), 3)r_down_y_scale = round(random.uniform(0.8, 1), 3)l_down_x_scale = round(random.uniform(0.0, 0.2), 3)l_down_y_scale = round(random.uniform(0.8, 1), 3)dst = np.array([[w*l_up_x_scale, h*l_up_y_scale], [w*r_up_x_scale, h*r_up_y_scale],[w*r_down_x_scale, h*r_down_y_scale], [w*l_down_x_scale, h*l_down_y_scale]], dtype=np.float32)transform_matrix = cv2.getPerspectiveTransform(src, dst)warp_img = cv2.warpPerspective(image, transform_matrix, (w,h))# image = cv2.resize(warp_img, (self.resize, self.resize))image = cv2.resize(warp_img, (48, 26))return imageclass RandomRotate(object):def __init__(self, angles, bound=False):self.angles = anglesself.bound = bounddef __call__(self,img):if random.random() > 0.5:angle = np.random.uniform(self.angles[0], self.angles[1])if self.bound:img = rotate_bound(img, angle)else:img = rotate_nobound(img, angle)return imgclass dog_way(object):def __init__(self):self.clahe_block = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(5, 5))passdef __call__(self,img):if random.random() > 0.5:img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)img = cv2.circle(img, (5, 5), 2, 50, 1)self.clahe_block.apply(img, img)img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)# print("------  dog_way   ------- ", img.shape)# print("???")return imgclass RandomBrightness(object):def __init__(self, delta=10):assert delta >= 0assert delta <= 255self.delta = deltadef __call__(self, image):if random.randint(2):delta = random.uniform(-self.delta, self.delta)image = (image + delta).clip(0.0, 255.0)# print('RandomBrightness,delta ',delta)return imageclass RandomContrast(object):def __init__(self, lower=0.9, upper=1.05):self.lower = lowerself.upper = upperassert self.upper >= self.lower, "contrast upper must be >= lower."assert self.lower >= 0, "contrast lower must be non-negative."# expects float imagedef __call__(self, image):if random.randint(2):alpha = random.uniform(self.lower, self.upper)# print('contrast:', alpha)image = (image * alpha).clip(0.0,255.0)return imageclass RandomSaturation(object):def __init__(self, lower=0.8, upper=1.2):self.lower = lowerself.upper = upperassert self.upper >= self.lower, "contrast upper must be >= lower."assert self.lower >= 0, "contrast lower must be non-negative."def __call__(self, image):if random.randint(2):alpha = random.uniform(self.lower, self.upper)image[:, :, 1] *= alpha# print('RandomSaturation,alpha',alpha)return image
class FixRandomRotate(object):def __init__(self, angles=[0,90,180,270], bound=False):self.angles = anglesself.bound = bounddef __call__(self,img):do_rotate = random.randint(0, 4)angle=self.angles[do_rotate]if self.bound:img = rotate_bound(img, angle)else:img = rotate_nobound(img, angle)return imgclass RandomHue(object):def __init__(self, delta=18.0):assert delta >= 0.0 and delta <= 360.0self.delta = deltadef __call__(self, image):if random.randint(2):alpha = random.uniform(-self.delta, self.delta)image[:, :, 0] += alphaimage[:, :, 0][image[:, :, 0] > 360.0] -= 360.0image[:, :, 0][image[:, :, 0] < 0.0] += 360.0# print('RandomHue,alpha:', alpha)return imageclass ConvertColor(object):def __init__(self, current='BGR', transform='HSV'):self.transform = transformself.current = currentdef __call__(self, image):if self.current == 'BGR' and self.transform == 'HSV':image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)elif self.current == 'HSV' and self.transform == 'BGR':image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)else:raise NotImplementedErrorreturn imageclass RandomSwapChannels(object):def __call__(self, img):if np.random.randint(2):order = np.random.permutation(3)return img[:,:,order]return imgclass RandomCrop(object):def __init__(self, size):self.size = sizedef __call__(self, image):h, w, _ = image.shapenew_w, new_h = scale_down((w, h), self.size)if w == new_w:x0 = 0else:x0 = random.randint(0, w - new_w)if h == new_h:y0 = 0else:y0 = random.randint(0, h - new_h)out = fixed_crop(image, x0, y0, new_w, new_h, self.size)return outclass RandomResizedCrop(object):def __init__(self, size,scale=(0.49, 1.0), ratio=(1., 1.)):self.size = sizeself.scale = scaleself.ratio = ratiodef __call__(self,img):if random.random() < 0.2:return cv2.resize(img,self.size)h, w, _ = img.shapearea = h * wd=1for attempt in range(10):target_area = random.uniform(self.scale[0], self.scale[1]) * areaaspect_ratio = random.uniform(self.ratio[0], self.ratio[1])new_w = int(round(math.sqrt(target_area * aspect_ratio)))new_h = int(round(math.sqrt(target_area / aspect_ratio)))if random.random() < 0.5:new_h, new_w = new_w, new_hif new_w < w and new_h < h:x0 = random.randint(0, w - new_w)y0 = (random.randint(0, h - new_h))//dout = fixed_crop(img, x0, y0, new_w, new_h, self.size)return out# Fallbackreturn center_crop(img, self.size)class DownCrop():def __init__(self, size,  select, scale=(0.36,0.81)):self.size = sizeself.scale = scaleself.select = selectdef __call__(self,img, attr_idx):if attr_idx not in self.select:return img, attr_idxif attr_idx == 0:self.scale=(0.64,1.0)h, w, _ = img.shapearea = h * ws = (self.scale[0]+self.scale[1])/2.0target_area = s * areanew_w = int(round(math.sqrt(target_area)))new_h = int(round(math.sqrt(target_area)))if new_w < w and new_h < h:dw = w-new_wx0 = int(0.5*dw)y0 = h-new_hout = fixed_crop(img, x0, y0, new_w, new_h, self.size)return out, attr_idx# Fallbackreturn center_crop(img, self.size), attr_idxclass ResizedCrop(object):def __init__(self, size, select,scale=(0.64, 1.0), ratio=(3. / 4., 4. / 3.)):self.size = sizeself.scale = scaleself.ratio = ratioself.select = selectdef __call__(self,img, attr_idx):if attr_idx not in self.select:return img, attr_idxh, w, _ = img.shapearea = h * wd=1if attr_idx == 2:self.scale=(0.36,0.81)d=2if attr_idx == 0:self.scale=(0.81,1.0)target_area = (self.scale[0]+self.scale[1])/2.0 * area# aspect_ratio = random.uniform(self.ratio[0], self.ratio[1])new_w = int(round(math.sqrt(target_area)))new_h = int(round(math.sqrt(target_area)))# if random.random() < 0.5:#     new_h, new_w = new_w, new_hif new_w < w and new_h < h:x0 =  (w - new_w)//2y0 = (h - new_h)//d//2out = fixed_crop(img, x0, y0, new_w, new_h, self.size)# cv2.imshow('{}_img'.format(idx2attr_map[attr_idx]), img)# cv2.imshow('{}_crop'.format(idx2attr_map[attr_idx]), out)## cv2.waitKey(0)return out, attr_idx# Fallbackreturn center_crop(img, self.size), attr_idx
class RandomTwelveCrop(object):def __init__(self, size):self.size = sizedef __call__(self, image):sh, sw, _ = image.shapenew_w,new_h=self.sizeimage_list=[]image_list.append(cv2.resize(image, self.size))x0 = 0y0 = 0image_list.append(fixed_crop(image, x0, y0, new_w, new_h, self.size))x0 = int(sw - new_w)y0 = 0image_list.append(fixed_crop(image, x0, y0, new_w, new_h, self.size))x0 = 0y0 = int(sh - new_h)image_list.append(fixed_crop(image, x0, y0, new_w, new_h, self.size))x0 = int(sw-new_w)y0 = int(sh - new_h)image_list.append(fixed_crop(image, x0, y0, new_w, new_h, self.size))x0 = int((sw - new_w) / 2)y0 = int((sh - new_h)/ 2)image_list.append(fixed_crop(image, x0, y0, new_w, new_h, self.size))flipimage=cv2.flip(image, 0)image_list.append(cv2.resize(flipimage, self.size))x0 = 0y0 = 0image_list.append(fixed_crop(image, x0, y0, new_w, new_h, self.size))x0 = int(sw - new_w)y0 = 0image_list.append(fixed_crop(image, x0, y0, new_w, new_h, self.size))x0 = 0y0 = int(sh - new_h)image_list.append(fixed_crop(image, x0, y0, new_w, new_h, self.size))x0 = int(sw - new_w)y0 = int(sh - new_h)image_list.append(fixed_crop(image, x0, y0, new_w, new_h, self.size))x0 = int((sw - new_w) / 2)y0 = int((sh - new_h) / 2)image_list.append(fixed_crop(image, x0, y0, new_w, new_h, self.size))return image_list
class RandomHflip(object):def __call__(self, image):if random.randint(2):return cv2.flip(image, 1)else:return imageclass Hflip(object):def __init__(self,doHflip):self.doHflip = doHflipdef __call__(self, image):if self.doHflip:return cv2.flip(image, 1)else:return imageclass CenterCrop(object):def __init__(self, size):self.size = sizedef __call__(self, image):return center_crop(image, self.size)class UpperCrop():def __init__(self, size, scale=(0.09, 0.64)):self.size = sizeself.scale = scaledef __call__(self,img):h, w, _ = img.shapearea = h * ws = (self.scale[0]+self.scale[1])/2.0target_area = s * areanew_w = int(round(math.sqrt(target_area)))new_h = int(round(math.sqrt(target_area)))if new_w < w and new_h < h:dw = w-new_wx0 = int(0.5*dw)y0 = 0out = fixed_crop(img, x0, y0, new_w, new_h, self.size)return out# Fallbackreturn center_crop(img, self.size)class RandomUpperCrop(object):def __init__(self, size, select, scale=(0.09, 0.64), ratio=(3. / 4., 4. / 3.)):self.size = sizeself.scale = scaleself.ratio = ratioself.select = selectdef __call__(self,img, attr_idx):if random.random() < 0.2:return img, attr_idxif attr_idx not in self.select:return img, attr_idxh, w, _ = img.shapearea = h * wfor attempt in range(10):s = random.uniform(self.scale[0], self.scale[1])d = 0.1 + (0.3 - 0.1) / (self.scale[1] - self.scale[0]) * (s - self.scale[0])target_area = s * areaaspect_ratio = random.uniform(self.ratio[0], self.ratio[1])new_w = int(round(math.sqrt(target_area * aspect_ratio)))new_h = int(round(math.sqrt(target_area / aspect_ratio)))# new_w = int(round(math.sqrt(target_area)))# new_h = int(round(math.sqrt(target_area)))if new_w < w and new_h < h:dw = w-new_wx0 = random.randint(int((0.5-d)*dw), int((0.5+d)*dw)+1)y0 = (random.randint(0, h - new_h))//10out = fixed_crop(img, x0, y0, new_w, new_h, self.size)return out, attr_idx# Fallbackreturn center_crop(img, self.size), attr_idx
class RandomDownCrop(object):def __init__(self, size, select, scale=(0.36, 0.81), ratio=(3. / 4., 4. / 3.)):self.size = sizeself.scale = scaleself.ratio = ratioself.select = selectdef __call__(self,img, attr_idx):if random.random() < 0.2:return img, attr_idxif attr_idx not in self.select:return img, attr_idxif attr_idx == 0:self.scale=(0.64,1.0)h, w, _ = img.shapearea = h * wfor attempt in range(10):s = random.uniform(self.scale[0], self.scale[1])d = 0.1 + (0.3 - 0.1) / (self.scale[1] - self.scale[0]) * (s - self.scale[0])target_area = s * areaaspect_ratio = random.uniform(self.ratio[0], self.ratio[1])new_w = int(round(math.sqrt(target_area * aspect_ratio)))new_h = int(round(math.sqrt(target_area / aspect_ratio)))## new_w = int(round(math.sqrt(target_area)))# new_h = int(round(math.sqrt(target_area)))if new_w < w and new_h < h:dw = w-new_wx0 = random.randint(int((0.5-d)*dw), int((0.5+d)*dw)+1)y0 = (random.randint((h - new_h)*9//10, h - new_h))out = fixed_crop(img, x0, y0, new_w, new_h, self.size)# cv2.imshow('{}_img'.format(idx2attr_map[attr_idx]), img)# cv2.imshow('{}_crop'.format(idx2attr_map[attr_idx]), out)## cv2.waitKey(0)return out, attr_idx# Fallbackreturn center_crop(img, self.size), attr_idxclass RandomHShift(object):def __init__(self, select, scale=(0.0, 0.2)):self.scale = scaleself.select = selectdef __call__(self,img, attr_idx):if attr_idx not in self.select:return img, attr_idxdo_shift_crop = random.randint(0, 2)if do_shift_crop:h, w, _ = img.shapemin_shift = int(w*self.scale[0])max_shift = int(w*self.scale[1])shift_idx = random.randint(min_shift, max_shift)direction = random.randint(0,2)if direction:right_part = img[:, -shift_idx:, :]left_part = img[:, :-shift_idx, :]else:left_part = img[:, :shift_idx, :]right_part = img[:, shift_idx:, :]img = np.concatenate((right_part, left_part), axis=1)# Fallbackreturn img, attr_idxclass RandomBottomCrop(object):def __init__(self, size, select, scale=(0.4, 0.8)):self.size = sizeself.scale = scaleself.select = selectdef __call__(self,img, attr_idx):if attr_idx not in self.select:return img, attr_idxh, w, _ = img.shapearea = h * wfor attempt in range(10):s = random.uniform(self.scale[0], self.scale[1])d = 0.25 + (0.45 - 0.25) / (self.scale[1] - self.scale[0]) * (s - self.scale[0])target_area = s * areanew_w = int(round(math.sqrt(target_area)))new_h = int(round(math.sqrt(target_area)))if new_w < w and new_h < h:dw = w-new_wdh = h - new_hx0 = random.randint(int((0.5-d)*dw), min(int((0.5+d)*dw)+1,dw))y0 = (random.randint(max(0,int(0.8*dh)-1), dh))out = fixed_crop(img, x0, y0, new_w, new_h, self.size)return out, attr_idx# Fallbackreturn bottom_crop(img, self.size), attr_idxclass BottomCrop():def __init__(self, size,  select, scale=(0.4, 0.8)):self.size = sizeself.scale = scaleself.select = selectdef __call__(self,img, attr_idx):if attr_idx not in self.select:return img, attr_idxh, w, _ = img.shapearea = h * ws = (self.scale[0]+self.scale[1])/3.*2.target_area = s * areanew_w = int(round(math.sqrt(target_area)))new_h = int(round(math.sqrt(target_area)))if new_w < w and new_h < h:dw = w-new_wdh = h-new_hx0 = int(0.5*dw)y0 = int(0.9*dh)out = fixed_crop(img, x0, y0, new_w, new_h, self.size)return out, attr_idx# Fallbackreturn bottom_crop(img, self.size), attr_idxclass Resize(object):def __init__(self, size, inter=cv2.INTER_LINEAR):self.size = sizeself.inter = interdef __call__(self, image):return cv2.resize(image, (self.size[0], self.size[0]), interpolation=self.inter)class Lighter(object):def __init__(self, alpha=(8,12), beta=(-10,10)):self.alpha_upper = alpha[0]self.alpha_lower = alpha[1]self.beta_upper = beta[0]self.beta_lower = beta[1]def __call__(self, image):if random.random() > 0.5:image = np.float32(image)alpha = random.randint(self.alpha_upper,self.alpha_lower) /10beta = random.randint(self.beta_upper,self.beta_lower)image = alpha * image + betareturn imageclass ExpandBorder(object):def __init__(self,  mode='constant', value=255, size=(336,336), resize=False):self.mode = modeself.value = valueself.resize = resizeself.size = sizedef __call__(self, image):h, w, _ = image.shapeif h > w:pad1 = (h-w)//2pad2 = h - w - pad1if self.mode == 'constant':image = np.pad(image, ((0, 0), (pad1, pad2), (0, 0)),self.mode, constant_values=self.value)else:image = np.pad(image,((0,0), (pad1, pad2),(0,0)), self.mode)elif h < w:pad1 = (w-h)//2pad2 = w-h - pad1if self.mode == 'constant':image = np.pad(image, ((pad1, pad2),(0, 0), (0, 0)),self.mode,constant_values=self.value)else:image = np.pad(image, ((pad1, pad2), (0, 0), (0, 0)),self.mode)if self.resize:image = cv2.resize(image, (self.size[0], self.size[0]),interpolation=cv2.INTER_LINEAR)return imageclass AstypeToInt():def __call__(self, image, attr_idx):return image.clip(0,255.0).astype(np.uint8), attr_idxclass AstypeToFloat():def __call__(self, image, attr_idx):return image.astype(np.float32), attr_idximport matplotlib.pyplot as plt
class Normalize(object):def __init__(self,mean, std):''':param mean: RGB order:param std:  RGB order'''self.mean = np.array(mean).reshape(3,1,1)self.std = np.array(std).reshape(3,1,1)def __call__(self, image):''':param image:  (H,W,3)  RGB:return:'''# plt.figure(1)# plt.imshow(image)# plt.show()# return (image.transpose((2, 0, 1)) / 255. - self.mean) / self.stdreturn image.transpose((2, 0, 1))class Normalize_test(object):def __init__(self,mean, std):''':param mean: RGB order:param std:  RGB order'''self.mean = np.array(mean).reshape(3,1,1)self.std = np.array(std).reshape(3,1,1)def __call__(self, image_list):''':param image:  (H,W,3)  RGB:return:'''# plt.figure(1)# plt.imshow(image)# plt.show()for i in range(len(image_list)):image_list[i]=(image_list[i].transpose((2, 0, 1)) / 255. - self.mean) / self.stdreturn  image_listclass RandomErasing(object):def __init__(self, select,EPSILON=0.5,sl=0.02, sh=0.09, r1=0.3, mean=[0.485, 0.456, 0.406]):self.EPSILON = EPSILONself.mean = meanself.sl = slself.sh = shself.r1 = r1self.select = selectdef __call__(self, img,attr_idx):if attr_idx not in self.select:return img,attr_idxif random.uniform(0, 1) > self.EPSILON:return img,attr_idxfor attempt in range(100):area = img.shape[1] * img.shape[2]target_area = random.uniform(self.sl, self.sh) * areaaspect_ratio = random.uniform(self.r1, 1 / self.r1)h = int(round(math.sqrt(target_area * aspect_ratio)))w = int(round(math.sqrt(target_area / aspect_ratio)))if w <= img.shape[2] and h <= img.shape[1]:x1 = random.randint(0, img.shape[1] - h)y1 = random.randint(0, img.shape[2] - w)if img.shape[0] == 3:# img[0, x1:x1+h, y1:y1+w] = random.uniform(0, 1)# img[1, x1:x1+h, y1:y1+w] = random.uniform(0, 1)# img[2, x1:x1+h, y1:y1+w] = random.uniform(0, 1)img[0, x1:x1 + h, y1:y1 + w] = self.mean[0]img[1, x1:x1 + h, y1:y1 + w] = self.mean[1]img[2, x1:x1 + h, y1:y1 + w] = self.mean[2]# img[:, x1:x1+h, y1:y1+w] = torch.from_numpy(np.random.rand(3, h, w))else:img[0, x1:x1 + h, y1:y1 + w] = self.mean[1]# img[0, x1:x1+h, y1:y1+w] = torch.from_numpy(np.random.rand(1, h, w))return img,attr_idxreturn img,attr_idx'''
if __name__ == '__main__':import matplotlib.pyplot as pltclass FSAug(object):def __init__(self):self.augment = Compose([AstypeToFloat(),# RandomHShift(scale=(0.,0.2),select=range(8)),# RandomRotate(angles=(-20., 20.), bound=True),ExpandBorder(select=range(8), mode='symmetric'),# symmetric# Resize(size=(336, 336), select=[ 2, 7]),AstypeToInt()])def __call__(self, spct,attr_idx):return self.augment(spct,attr_idx)trans = FSAug()img_path = '/media/gserver/data/FashionAI/round2/train/Images/coat_length_labels/0b6b4a2146fc8616a19fcf2026d61d50.jpg'img = cv2.cvtColor(cv2.imread(img_path),cv2.COLOR_BGR2RGB)img_trans,_ = trans(img,5)# img_trans2,_ = trans(img,6)print (img_trans.max(), img_trans.min())print (img_trans.dtype)plt.figure()plt.subplot(221)plt.imshow(img)plt.subplot(222)plt.imshow(img_trans)# plt.subplot(223)# plt.imshow(img_trans2)# plt.imshow(img_trans2)plt.show()
'''
def Laplas(img):src = imgdst1 = cv2.pyrDown(src)dst2 = cv2.pyrDown(dst1)dst3 = cv2.pyrDown(dst2)dst4 = cv2.pyrUp(dst3)dst5 = cv2.pyrUp(dst4)dst6 = cv2.pyrUp(dst5)h1, w1, c1 = dst6.shapesrc = cv2.resize(src, (w1, h1))dst7 = src - dst6#image = Image.fromarray(cv2.cvtColor(dst7, cv2.COLOR_BGR2RGB))image=cv2.cvtColor(dst7, cv2.COLOR_BGR2RGB)return imageif __name__ == '__main__':pre_path = '/data1/digital_dataset/1030_data/train_img/'img_list = os.listdir(pre_path)for image_path in img_list:print(pre_path, image_path)img = cv2.imread(os.path.join(pre_path, image_path))cv2.imshow('test', img)# cv2.waitKey(0)# cv2.destroyAllWindows()# img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)l = perspective_transformation()img = l(img)plt.imshow(img)plt.show()print(666)

后面会增加一些非线性的数据增强的方法。。。。。。。。

二、深度学习数据增强方法汇总相关推荐

  1. 深度学习数据增强方法,利用仿射变换实现图像进行各种操作如平移、缩放、旋转、翻转

    导读 在深度学习的数据增强中,我们经常需要对图像进行各种增强操作如平移.缩放.旋转.翻转等,这些其实都是图像的仿射变换.通过本篇文章,你能够知道它们的实现原理以及如何应用它们.本文讲述如何通过仿射变换 ...

  2. 计算机视觉数据增强方法汇总

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 前言: 在计算机视觉方向,数据增强的本质是人为地引入人视觉上的先验知识,可以很好地提升模型的性能,目前 ...

  3. 深度学习-数据增强与扩充

    数据增强可以说是数据驱动下的深度学习必经之路,掌握数据,相当于掌握当下主流方向的自动驾驶的命脉,是人工智能不可或缺的资源.本文将介绍最新的利用大模型扩充数据的方式! 先看下变色效果: 左褐色背景图 为 ...

  4. 比较全的深度学习数据预处理方法

    当前深度学习的预处理方法 1.中心化/零均值化 程序代码 2.标准化/归一化 程序代码 (1)标准化与归一化的联系和差异 联系 差异 (2)为什么要归一化/标准化 ①某些模型求解需要 ②一些分类器需要 ...

  5. 深度学习数据增强数据扩增方法

    随机裁剪 对图片随机0.6~1.0比率大小的区域进行裁剪. 然后resize到固定大小. torch.API torchvision.transforms.RandomCrop(size,paddin ...

  6. 【深度学习数据增强处理】imgaug Augment Polygons 对标注图片和polygons的数据增强

    对于本地化进行图像的增强,大家都是非常好操作的.但是,对于标注信息一起增强,还是稍微有一些难度的,麻烦很多. 我是遇到一个数据集非常少的任务,只有40张图.就直接标记了去训练,发现几乎不拟合,当然这里 ...

  7. 深度学习--数据增强

    在深度学习中,为了避免出现过拟合(Overfitting),通常我们需要输入充足的数据量.本页面主要记录下常用的数据增强(Data Augmentation)变换方法. 不同的任务背景下, 我们可以通 ...

  8. 深度学习目标检测方法汇总

    目标检测简介   目标检测是计算机视觉的一个重要研究方向,是指从一个场景(或图片)中找到感兴趣的目标.任务大致分为三个流程: 从场景中提取候选区 从候选区提取特征 识别候选区的类别并对有效的候选框进行 ...

  9. 深度学习数据增强——扩充数据集

    在深度学习过程中经常会因为数据量少而发生过拟合现象,或者模型的泛化能力比较低.基于此,本文讲一下图像的数据增强,就是通过对图像简答你的形变,用来应对因拍照的角度不同而使得图谱变形.tensorflow ...

最新文章

  1. C#_Socket网络编程实现的简单局域网内即时聊天,发送文件,抖动窗口。
  2. 与客家土楼的约会(增城-河源)
  3. 全世界还有44亿人无法上网
  4. Java知识整理——ServletJSP
  5. 【1】SCN-Eureka服务注册中心
  6. Struts2的声明式异常处理
  7. L2-009. 抢红包-PAT团体程序设计天梯赛GPLT
  8. 【C#小知识】C#中一些易混淆概念总结(八)---------解析接口
  9. ffmpeg: 一款跨平台开源媒体格式转换器
  10. 什么原数据更容易平稳_判定数据序列平稳与否的方法都有哪些,什么是平稳序列...
  11. 等级保护三级信息系统安全设计
  12. 追赶法源程序c语言,追赶法求三对角线方程组程序设计.doc
  13. 常用编程语言开发工具
  14. KDD CUP 99 数据集解析、挖掘与下载
  15. UOS x86安装Intel网卡驱动
  16. html5 基本知识
  17. BZOJ1778: [Usaco2010 Hol]Dotp 驱逐猪猡
  18. mvp中的m作用_将M放入MVP
  19. [前端学习笔记1] 前端学习路线
  20. 一个回车键黑掉一台服务器——使用Docker时不要这么懒啊喂

热门文章

  1. Tomcat内存溢出解决方法
  2. 闰年流程图(18网三袁昳)
  3. tar 和gzip 的区别
  4. Django --ORM常用的字段和参数 多对多创建形式
  5. cwRsync文件双向同步问题
  6. node.js实现国标GB28181流媒体点播(即实时预览)服务解决方案
  7. 铁乐学Python_Day35_Socket模块3和hmac模块
  8. 十六.监控系统cpu.内存,磁盘等,自动报警,发送邮件
  9. 物联网兴起 嵌入式系统安全日益受关注
  10. linux下发布的执行文件崩溃的问题定位 心得一则