1. 图片数据增强

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def display_augmentations(path):
original = cv2.imread(path) #读取原图
original = cv2.cvtColor(original, cv2.COLOR_BGR2RGB)#转RGB

#Transformations p=0.5一般是应用该变换的概率
# RGB平移 g通道平移最多50 注意添加[]
transform_rgb = alb.Compose([RGBShift(g_shift_limit=50, always_apply=True)])

#色调饱和度值,3个参数:随机色调、饱和度、值变化。
transform_hsv = alb.Compose([HueSaturationValue(hue_shift_limit=10,\
sat_shift_limit=60, always_apply=True)])

#沿y轴水平翻转输入图片
transform_hf = alb.Compose([HorizontalFlip(always_apply=True)])
#对比度受限自适应直方图均衡
transform_clahe = alb.Compose([CLAHE(clip_limit=10.0, always_apply=True)])
#随机裁剪
transform_rc = alb.Compose([RandomCrop(height=300, width=300, always_apply=True)])
# 随机gama
transform_rg = alb.Compose([RandomGamma(gamma_limit=(200, 400), always_apply=True)])
# 随机旋转, 最多90度
transform_rot = alb.Compose([Rotate(limit=90, always_apply=True)])
# 随机中心裁剪
transform_cc = alb.Compose([CenterCrop(height=450, width=450, always_apply=True)])
# 中值滤波
transform_mb = alb.Compose([MedianBlur(blur_limit=103, always_apply=True)])
#围绕X轴垂直翻转
transform_vf = alb.Compose([VerticalFlip(always_apply=True)])
#通过从255减去像素值来反转输入图像
transform_ii = alb.Compose([InvertImg(always_apply=True)])

#应用transform
transformed_rgb = transform_rgb(image=original)['image']
transformed_hsv = transform_hsv(image=original)["image"]
transformed_hf = transform_hf(image=original)["image"]
transformed_clahe = transform_clahe(image=original)["image"]
transformed_rc = transform_rc(image=original)["image"]
transformed_rg = transform_rg(image=original)["image"]
transformed_rot = transform_rot(image=original)["image"]
transformed_cc = transform_cc(image=original)["image"]
transformed_mb = transform_mb(image=original)["image"]
transformed_vf = transform_vf(image=original)["image"]
transformed_ii = transform_ii(image=original)["image"]

all_transformations = [original, transformed_rgb, transformed_hsv, transformed_hf,
transformed_clahe, transformed_rc, transformed_rg, transformed_rot,
transformed_cc, transformed_mb, transformed_vf, transformed_ii]
all_names = ["Original", "RGBShift", "HueStaurationValue", "HorizontalFlip", "CLAHE",
"RandomCrop", "RandomGamma", "Rotate", "CenterCrop", "MedianBlur",
"VerticalFlip", "InvertImg"]

fig = plt.figure(figsize=(28, 20))
plt.suptitle(f"Image Augmentations", fontsize=20)
for k, image in enumerate(all_transformations):
fig.add_subplot(3, 4, k+1)
plt.title(all_names[k])
plt.imshow(image)
plt.axis("off")
plt.show()

example_path = r"hellokity.jpg"
display_augmentations(path=example_path)

效果:

202203261728890

Inference

[1] Augmentation

[2] awesome-data-augmentation

[3] augmentations