数据预处理

马上毕业了,毕业之前将研究生阶段做过的一些工作做一些分享。本期将分享语义分割的一些前期预处理以及论文。

1 数据增强

1.1 几何增强

原图:

voc语义分割数据集 语义分割数据预处理_voc语义分割数据集


voc语义分割数据集 语义分割数据预处理_voc语义分割数据集_02

import cv2
import matplotlib.pyplot as plt
%matplotlib inline
from skimage import io


# img = io.imread('image.jpg')
# label = io.imread('label.jpg')

img = cv2.imread('image.jpg')
label = cv2.imread('label.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # cv2默认为bgr顺序
label = cv2.cvtColor(label, cv2.COLOR_BGR2RGB) # cv2默认为bgr顺序
io.imshow(img)
<matplotlib.image.AxesImage at 0x274358194c8>

voc语义分割数据集 语义分割数据预处理_自动驾驶_03

io.imshow(label)
<matplotlib.image.AxesImage at 0x274358e2e08>

voc语义分割数据集 语义分割数据预处理_pytorch_04

水平翻转

  • 水平翻转: 水平镜像图像有助于增加方向的不变性(例如,行人可以以不同的方向出现)。在自然场景下,不建议垂直翻转,因为物体的垂直外观在场景中增加了重要的一致性(例如,网络知道天空是由它的位置决定的),但是像遥感图像这种俯瞰图,是可以垂直翻转的。
img_flip = cv2.flip(img, 1) # 1代表水平翻转,0代表垂直翻转,-1代表对角翻转 
label_flip = cv2.flip(label, 1)
io.imshow(img_flip)
<matplotlib.image.AxesImage at 0x2743595c8c8>

voc语义分割数据集 语义分割数据预处理_voc语义分割数据集_05

io.imshow(label_flip)
<matplotlib.image.AxesImage at 0x2743699c608>

voc语义分割数据集 语义分割数据预处理_语义分割_06

平移

  • 平移: 移动图像会阻止CNN总是看到训练图像的相同位置,因此它不会总是从第一层产生相同的激活(移位不变性)。
# 定义平移矩阵 
import numpy as np

M = np.float32([[1, 0, 100], [0, 1, 0]])
# 变换公式:dst(x, y) = src(M11x + M12y + M13, M21x + M22y + M23)
img_shift = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
label_shift = cv2.warpAffine(label, M, (label.shape[1], label.shape[0]))
io.imshow(img_shift)
<matplotlib.image.AxesImage at 0x27436a09248>

voc语义分割数据集 语义分割数据预处理_自动驾驶_07

io.imshow(label_shift)
<matplotlib.image.AxesImage at 0x27436a73d08>

voc语义分割数据集 语义分割数据预处理_pytorch_08

随机裁剪和缩放

  • 随机裁剪和缩放: 随机调整图像大小有助于模型看到每个对象的不同比例,并提高网络对不同图像分辨率的不变性。
h, w = img.shape[0], img.shape[1]
# 获取随机裁剪长度 
crop_scale = np.random.randint(h//2, h) / h
crop_h = int(crop_scale * h)
crop_w = int(crop_scale * w)
h_begin = np.random.randint(0, h - crop_h)
w_begin = np.random.randint(0, w - crop_w)

img_crop = img[h_begin: h_begin+crop_h, w_begin:w_begin+crop_w, :]
label_crop = label[h_begin: h_begin+crop_h, w_begin:w_begin+crop_w]

# resize函数使用时dsize先是w再是h,正好相反 
img_resize = cv2.resize(img_crop, (w, h))
label_resize = cv2.resize(label_crop, (w, h))
io.imshow(img_resize)
<matplotlib.image.AxesImage at 0x27436adefc8>

voc语义分割数据集 语义分割数据预处理_voc语义分割数据集_09

io.imshow(label_resize)
<matplotlib.image.AxesImage at 0x27436b524c8>

voc语义分割数据集 语义分割数据预处理_自动驾驶_10

1.2 纹理增强

亮度和对比度

  • 亮度和对比度: 物体在图像中的清晰度取决于场景照明和相机灵敏度。通过随机增加或减少图像亮度来增加输入图像的虚拟变化,改善了网络的照明不变性。对比度就是图像中最暗和最亮区域之间的分隔。通过随机增强来增加这个范围有助于增加对阴影的不变性,并且通常会提高网络在低光照条件下的性能。
from albumentations import (
    Compose,
    RandomBrightnessContrast
)

aug = Compose([RandomBrightnessContrast(brightness_limit=(0, 0.4), contrast_limit=(0, 0.4), p=1)])

augmented = aug(image=img, mask=label)
image_bright_contrast = augmented['image']
gt_bright_contrast = augmented['mask']
io.imshow(image_bright_contrast)
<matplotlib.image.AxesImage at 0x27438dd1d08>

voc语义分割数据集 语义分割数据预处理_voc语义分割数据集_11

io.imshow(gt_bright_contrast)
<matplotlib.image.AxesImage at 0x27438e42d08>

voc语义分割数据集 语义分割数据预处理_语义分割_12

  • 色调 饱和度 明度(HSV): 色调的取值范围是0~360度,用来表示颜色的类别,其中红色是0度,绿色是120度,蓝色是240度。饱和度高,颜色则深而艳。亮度的用来表示颜色的明暗程度。
from albumentations import (
    Compose,
    HueSaturationValue
)

aug = Compose([HueSaturationValue(hue_shift_limit=20,
                                  sat_shift_limit=30,
                                  val_shift_limit=20,
                                  p=1)])

augmented = aug(image=img, mask=label)
image_hsv = augmented['image']
gt_hsv = augmented['mask']
io.imshow(image_hsv)
<matplotlib.image.AxesImage at 0x27438eb0b08>

voc语义分割数据集 语义分割数据预处理_voc语义分割数据集_13

io.imshow(gt_hsv)
<matplotlib.image.AxesImage at 0x27438f16d88>

voc语义分割数据集 语义分割数据预处理_自动驾驶_14

  • 运动模糊: 对图像进行模糊可以模拟部分拍摄场景的运动模糊,让网络对模糊图像的边界也具有较强的识别能力。
from albumentations import (
    Compose,
    MotionBlur
)

aug = Compose([MotionBlur(blur_limit=7, p=1.0)])

augmented = aug(image=img, mask=label)
image_MotionBlur = augmented['image']
gt_MotionBlur = augmented['mask']
io.imshow(image_MotionBlur)
<matplotlib.image.AxesImage at 0x2743906e448>

voc语义分割数据集 语义分割数据预处理_深度学习_15

io.imshow(gt_MotionBlur)
<matplotlib.image.AxesImage at 0x274390d6c48>

voc语义分割数据集 语义分割数据预处理_pytorch_16

  • 颜色抖动: 向每个RGB像素添加小的随机噪声有助于获得对一些相机失真的不变性。
from albumentations import (
    Compose,
    RGBShift
)

aug = Compose([RGBShift(r_shift_limit=20,
                        g_shift_limit=20,
                        b_shift_limit=20,
                        p=1.0)])

augmented = aug(image=img, mask=label)
image_rgbshift = augmented['image']
gt_rgbshift = augmented['mask']
io.imshow(image_rgbshift)
<matplotlib.image.AxesImage at 0x2743a10eec8>

voc语义分割数据集 语义分割数据预处理_深度学习_17

io.imshow(gt_rgbshift)
<matplotlib.image.AxesImage at 0x2743a17e408>

voc语义分割数据集 语义分割数据预处理_语义分割_18

限制对比度的自适应直方图均衡化:

from albumentations import (
    Compose,
    CLAHE
)

aug = Compose([CLAHE(clip_limit=2,
                     tile_grid_size=(8, 8),
                     p=1)])

augmented = aug(image=img, mask=label)
image_CLAHE = augmented['image']
gt_CLAHE = augmented['mask']
io.imshow(image_CLAHE)
<matplotlib.image.AxesImage at 0x2743a1e2c88>

voc语义分割数据集 语义分割数据预处理_voc语义分割数据集_19

io.imshow(gt_CLAHE)
<matplotlib.image.AxesImage at 0x2743a5b0288>

voc语义分割数据集 语义分割数据预处理_深度学习_20

其他增强

  • 超像素: 随机将图像上某个超像素块内的颜色替换为超像素的均值。
from albumentations import Compose
from albumentations.imgaug.transforms import IAASuperpixels

aug = Compose([IAASuperpixels(p_replace=0.1,
                              n_segments=500,
                              p=1)])

augmented = aug(image=img, mask=label)
image_Superpixels = augmented['image']
gt_Superpixels = augmented['mask']
io.imshow(image_Superpixels)
<matplotlib.image.AxesImage at 0x2743a612ac8>

voc语义分割数据集 语义分割数据预处理_语义分割_21

io.imshow(gt_Superpixels)
<matplotlib.image.AxesImage at 0x2743a658fc8>

voc语义分割数据集 语义分割数据预处理_语义分割_22

  • 锐化: 增强图像细节。
from albumentations import Compose
from albumentations.imgaug.transforms import IAASharpen

aug = Compose([IAASharpen(p=1)])

augmented = aug(image=img, mask=label)
image_Sharpen = augmented['image']
gt_Sharpen = augmented['mask']
io.imshow(image_Sharpen)
<matplotlib.image.AxesImage at 0x2743a704c08>

voc语义分割数据集 语义分割数据预处理_语义分割_23

io.imshow(gt_Sharpen)
<matplotlib.image.AxesImage at 0x2743b7457c8>

voc语义分割数据集 语义分割数据预处理_voc语义分割数据集_24

  • 透视变换:
from albumentations import Compose
from albumentations.imgaug.transforms import IAAPerspective

aug = Compose([IAAPerspective(p=1)])

augmented = aug(image=img, mask=label)
image_Perspective = augmented['image']
gt_Perspective = augmented['mask']
io.imshow(image_Perspective)
<matplotlib.image.AxesImage at 0x2743b7c9a08>

voc语义分割数据集 语义分割数据预处理_深度学习_25

io.imshow(gt_Perspective)
<matplotlib.image.AxesImage at 0x2743b835288>

voc语义分割数据集 语义分割数据预处理_深度学习_26

2 标准化与归一化

2.1 min-max归一化

  • 公式:对数据的数值范围进行特定缩放,但不改变其数据分布的一种线性特征变换。
import matplotlib.pyplot as plt
import cv2
import numpy as np
import seaborn as sns

image = cv2.imread('image.jpg')[:, :, 0]
min_ = np.min(image)
max_ = np.max(image)

new_image = (image-min_) / (max_-min_)

flat_image = np.reshape(image, -1)
flat_new_image = np.reshape(new_image, -1)
fig, ax = plt.subplots(1, 2)
sns.distplot(flat_image, ax=ax[0])
sns.distplot(flat_new_image, ax=ax[1])
plt.show()

voc语义分割数据集 语义分割数据预处理_pytorch_27

2.2 z-score 标准化

  • 公式:将数值范围缩放到0附近, 但没有改变数据分布;
import matplotlib.pyplot as plt
import cv2
import numpy as np
import seaborn as sns

image = cv2.imread('image.jpg')[:, :, 0]
mean_ = np.mean(image)
std_ = np.std(image)

new_image = (image-mean_) / std_

flat_image = np.reshape(image, -1)
flat_new_image = np.reshape(new_image, -1)
fig, ax = plt.subplots(1, 2)
sns.distplot(flat_image, ax=ax[0])
sns.distplot(flat_new_image, ax=ax[1])
plt.show()

voc语义分割数据集 语义分割数据预处理_voc语义分割数据集_28