yolov5 数据增强代码 主要有以下几种方式:
class Albumentations # 数据增强package,比pytorch 自带的transform 更丰富
def augment_hsv(im, hgain=0.5, sgain=0.5, vgain=0.5) # 图像增强方式,hgain 是色调,不同色调不同颜色,sgain是饱和度, vgain是亮度
def hist_equalize(im, clahe=True, bgr=False):# 采用自适应直方图均衡化做图像增强
def replicate(im, labels) #
def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32) # 图像size扩充至指定大小
def random_perspective(im, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0,
border=(0, 0)) # 随机增强
def copy_paste(im, labels, segments, p=0.5)# 复制粘贴
def cutout(im, labels, p=0.5) # 裁剪
def mixup(im, labels, im2, labels2) # mixup
def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, eps=1e-16) # 框筛选
【目标检测|【yolov5 6.0 源码解析】---utils /augmentations.py】下面一个个来看图像增强的方式:
Albumentations 图像增强
class Albumentations:
# YOLOv5 Albumentations class (optional, only used if package is installed)
def __init__(self):
self.transform = None
try:
'''
albumentations --一个数据增强的package,比pytorch的transform丰富;详情
https://blog.csdn.net/cp1314971/article/details/106039800?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164015856916780261966386%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164015856916780261966386&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-1-106039800.pc_search_es_clickV2&utm_term=import+albumentations+&spm=1018.2226.3001.4187
'''
import albumentations as A
check_version(A.__version__, '1.0.3')# version requirement self.transform = A.Compose([
A.Blur(p=0.01), # 图像随机大小内核模糊输入图像
A.MedianBlur(p=0.01), # 图像随机模糊输入图像
A.ToGray(p=0.01), # 转成灰度图
A.CLAHE(p=0.01), #
A.RandomBrightnessContrast(p=0.0), # 随机亮度和对比度
A.RandomGamma(p=0.0), #
A.ImageCompression(quality_lower=75, p=0.0)], # 图像压缩
bbox_params=A.BboxParams(format='yolo', label_fields=['class_labels'])) # logging.info(colorstr('albumentations: ') + ', '.join(f'{x}' for x in self.transform.transforms if x.p))
except ImportError:# package not installed, skip
pass
except Exception as e:
logging.info(colorstr('albumentations: ') + f'{e}')def __call__(self, im, labels, p=1.0):
if self.transform and random.random() < p:
new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0])# transformed
im, labels = new['image'], np.array([[c, *b] for c, b in zip(new['class_labels'], new['bboxes'])])
return im, labels
hsv 色调-饱和度-亮度的图像增强
def augment_hsv(im, hgain=0.5, sgain=0.5, vgain=0.5): # 做h-色调, s-饱和度, v-亮度上面的随机增强
# HSV color-space augmentation
if hgain or sgain or vgain:
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1# random gains 生成3个[-1, 1)之间的随机数,分别与hsv相乘后+1 [0,2]之间
hue, sat, val = cv2.split(cv2.cvtColor(im, cv2.COLOR_BGR2HSV)) # 将图像从BGR 转成HSV ,拆分
dtype = im.dtype# uint8 x = np.arange(0, 256, dtype=r.dtype) # [0, 1, 2,...,255]
lut_hue = ((x * r[0]) % 180).astype(dtype) #[0, 180)
lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) # 将数组截断至[0, 255]
lut_val = np.clip(x * r[2], 0, 255).astype(dtype) # 将数组截断至[0, 255] im_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val)))
# cv2.LUT lookup-table 查找表方式,即通过lut_hue 这个表对之前hue数值做修正,返回0-255对应位置的lut_hue值具体: https://blog.csdn.net/Dontla/article/details/103963085
# cv2.merge 合并三个通道
cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR, dst=im)# no return needed
直方图均衡化增强
def hist_equalize(im, clahe=True, bgr=False): # 直方图均衡化增强 参考 https://www.cnblogs.com/my-love-is-python/p/10405811.html
# Equalize histogram on BGR image 'im' with im.shape(n,m,3) and range 0-255
yuv = cv2.cvtColor(im, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV) # 将图像从bgr转成YUV
if clahe:
c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
# cv2.createCLAHE 实例化自适应直方图均衡化函数 局部直方图均衡化 ,不会使得细节消失
# c.apply 进行自适应直方图均衡化
yuv[:, :, 0] = c.apply(yuv[:, :, 0])
else:
# cv2.equalizeHist 进行像素点的均衡化 ,即全局均衡化 ,使得整体亮度提升,但是局部会模糊
yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0])# equalize Y channel histogram
return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB)# convert YUV image to RGB
图像框的平移复制增强
def replicate(im, labels): # 复制,实际上指的是框的平移
# Replicate labels
h, w = im.shape[:2] # 获取图像长宽
boxes = labels[:, 1:].astype(int) # 获取框的位置和大小
x1, y1, x2, y2 = boxes.T # 框的左右和上下位置
s = ((x2 - x1) + (y2 - y1)) / 2# side length (pixels)
for i in s.argsort()[:round(s.size * 0.5)]:# smallest indices
x1b, y1b, x2b, y2b = boxes[i]
bh, bw = y2b - y1b, x2b - x1b
yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw))# offset x, y
x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh]
im[y1a:y2a, x1a:x2a] = im[y1b:y2b, x1b:x2b]# im4[ymin:ymax, xmin:xmax]
labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0)return im, labels
图像以letterbox缩放
def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
# 按比例缩放图片,并将其他部分填充,到resize图片的大小
# Resize and pad image while meeting stride-multiple constraints
shape = im.shape[:2]# current shape [height, width]
if isinstance(new_shape, int): # 如果输入是一个数字,默认长宽相等
new_shape = (new_shape, new_shape)# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) #
if not scaleup:# only scale down, do not scale up (for better val mAP) # 如果只缩小,不放大图片
r = min(r, 1.0)# Compute padding
ratio = r, r# width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) # 对图片按比例缩放后的长宽 (width, height)
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]# wh padding 对缩放后的图像 需要填充的size
if auto:# minimum rectangle
dw, dh = np.mod(dw, stride), np.mod(dh, stride)# wh padding 取能被stride 整除的dw 和dh
elif scaleFill:# stretch
dw, dh = 0.0, 0.0
new_unpad = (new_shape[1], new_shape[0])
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]# width, height ratiosdw /= 2# divide padding into 2 sides
dh /= 2if shape[::-1] != new_unpad:# resize
im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR) # 先将图片按比例缩放到指定大小
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) # 上下位置
left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) # 左右位置
im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=https://www.it610.com/article/color)# add border
# cv2.copyMakeBorder 对im设置边界框
return im, ratio, (dw, dh)
旋转等变换(未更新完全,后续补充)
def random_perspective(im, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0,
border=(0, 0)):
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10))
# targets = [cls, xyxy]height = im.shape[0] + border[0] * 2# shape(h,w,c)
width = im.shape[1] + border[1] * 2# Center [w, h, c] -->[w/2 , h/2, c]
'''
[ 10 -w/2
01 -h/2
001
]
'''
C = np.eye(3)
C[0, 2] = -im.shape[1] / 2# x translation (pixels)
C[1, 2] = -im.shape[0] / 2# y translation (pixels)# Perspective [w, h, c] -->[w/2 , h/2, c]
'''
[100
rand10
rand01
]
'''
P = np.eye(3)
P[2, 0] = random.uniform(-perspective, perspective)# x perspective (about y)
P[2, 1] = random.uniform(-perspective, perspective)# y perspective (about x)# Rotation and Scale
R = np.eye(3)
a = random.uniform(-degrees, degrees)
# a += random.choice([-180, -90, 0, 90])# add 90deg rotations to small rotations
s = random.uniform(1 - scale, 1 + scale)
# s = 2 ** random.uniform(-scale, scale)
R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s)# Shear
S = np.eye(3)
S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180)# x shear (deg)
S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180)# y shear (deg)# Translation
T = np.eye(3)
T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width# x translation (pixels)
T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height# y translation (pixels)# Combined rotation matrix
M = T @ S @ R @ P @ C# order of operations (right to left) is IMPORTANT tf.matmul(A,C)=np.dot(A,C)= A@C
if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any():# image changed
if perspective:
im = cv2.warpPerspective(im, M, dsize=(width, height), borderValue=https://www.it610.com/article/(114, 114, 114))
else:# affine
im = cv2.warpAffine(im, M[:2], dsize=(width, height), borderValue=(114, 114, 114)) # cv2.warpAffine()放射变换函数,可实现旋转,平移,缩放;变换后的平行线依旧平行
# cv2.warpAffine()放射变换函数,可实现旋转,平移,缩放;变换后的平行线依旧平行
# cv2.warpPerspective()透视变换函数,可保持直线不变形,但是平行线可能不再平行 # Visualize
# import matplotlib.pyplot as plt
# ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel()
# ax[0].imshow(im[:, :, ::-1])# base
# ax[1].imshow(im2[:, :, ::-1])# warped# Transform label coordinates
n = len(targets)
if n:
use_segments = any(x.any() for x in segments)
new = np.zeros((n, 4))
if use_segments:# warp segments
segments = resample_segments(segments)# upsample
for i, segment in enumerate(segments):
xy = np.ones((len(segment), 3))
xy[:, :2] = segment
xy = xy @ M.T# transform
xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]# perspective rescale or affine# clip
new[i] = segment2box(xy, width, height)else:# warp boxes
xy = np.ones((n * 4, 3))
xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2)# x1y1, x2y2, x1y2, x2y1
xy = xy @ M.T# transform
xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]).reshape(n, 8)# perspective rescale or affine# create new boxes
x = xy[:, [0, 2, 4, 6]]
y = xy[:, [1, 3, 5, 7]]
new = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T# clip
new[:, [0, 2]] = new[:, [0, 2]].clip(0, width)
new[:, [1, 3]] = new[:, [1, 3]].clip(0, height)# filter candidates
i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01 if use_segments else 0.10)
targets = targets[i]
targets[:, 1:5] = new[i]return im, targets
复制粘贴
def copy_paste(im, labels, segments, p=0.5):
# Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
n = len(segments)
if p and n:
h, w, c = im.shape# height, width, channels
im_new = np.zeros(im.shape, np.uint8)
for j in random.sample(range(n), k=round(p * n)):
l, s = labels[j], segments[j]
box = w - l[3], l[2], w - l[1], l[4]
ioa = bbox_ioa(box, labels[:, 1:5])# intersection over area
if (ioa < 0.30).all():# allow 30% obscuration of existing labels
labels = np.concatenate((labels, [[l[0], *box]]), 0)
segments.append(np.concatenate((w - s[:, 0:1], s[:, 1:2]), 1))
cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED)result = cv2.bitwise_and(src1=im, src2=im_new)
result = cv2.flip(result, 1)# augment segments (flip left-right)
i = result > 0# pixels to replace
# i[:, :] = result.max(2).reshape(h, w, 1)# act over ch
im[i] = result[i]# cv2.imwrite('debug.jpg', im)# debugreturn im, labels, segments
图片随机马赛克
def cutout(im, labels, p=0.5):# 随机马赛克
# Applies image cutout augmentation https://arxiv.org/abs/1708.04552
if random.random() < p:
h, w = im.shape[:2]
scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16
# [0.5 0.25 0.25 0.125 0.125 0.0125 ... 0.03125]31
# image size fraction
for s in scales:
mask_h = random.randint(1, int(h * s))# create random masks
mask_w = random.randint(1, int(w * s))# box 确定随机马赛克的位置
xmin = max(0, random.randint(0, w) - mask_w // 2)
ymin = max(0, random.randint(0, h) - mask_h // 2)
xmax = min(w, xmin + mask_w)
ymax = min(h, ymin + mask_h)# apply random color mask 确定随机马赛克的马赛克像素
im[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)] # # return unobscured labels
if len(labels) and s > 0.03: #
box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32)
ioa = bbox_ioa(box, labels[:, 1:5])# intersection over area 计算马赛克位置与框位置的IOU 大小
labels = labels[ioa < 0.60]# remove >60% obscured labels # 保留下与马赛克框iou小于 0.6的,大于0.6的认为没有学习的必须 return labels
mixup
def mixup(im, labels, im2, labels2): # 两张图片像素点以不同权重融合
# Applies MixUp augmentation https://arxiv.org/pdf/1710.09412.pdf
r = np.random.beta(32.0, 32.0)# mixup ratio, alpha=beta=32.0
im = (im * r + im2 * (1 - r)).astype(np.uint8) # img 做融合
labels = np.concatenate((labels, labels2), 0)
return im, labels
过滤图像
def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, eps=1e-16):# box1(4,n), box2(4,n)
# 过滤掉长宽小于阈值,面积图片占比小于阈值,框长宽比大于阈值的框
# Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
# box1 图片 ,box2 框
w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps))# aspect ratio 宽长比与长宽比的最大值
return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr)# candidates
推荐阅读
- 机器学习|西瓜书《机器学习》第三章重点总结(下,二分类线性判别分析)
- 机器学习|西瓜书《机器学习》第三章重点总结(上,一元线性回归)
- 人工智能|西瓜书《机器学习》第三章重点总结(中2,对数几率回归)
- 机器学习|西瓜书《机器学习》第三章重点总结(中,多元线性回归)
- 机器学习|西瓜书《机器学习》第二章重点总结(下)
- 机器学习|西瓜书《机器学习》第二章重点总结(上)
- 深度学习|MatchNet论文复现过程记录
- 暑期深度学习入门|第二周学习(卷积神经网络)
- 学习日志|Python MINIST手写集的识别,卷积神经网络,CNN(最简单PyTorch的使用)