美文网首页
深度学习实践——图像分类:训练数据增广

深度学习实践——图像分类:训练数据增广

作者: zbharper | 来源:发表于2019-10-31 16:48 被阅读0次

    图像增广(augmentation)是在有限训练数据集的情况下,有效扩大数据规模的方式。它的作用有:
    1)通过随机增广由一张图片产生一组互不相同的图片,扩大数据规模
    2)通过位移/旋转/缩放等方式的增广,能够减小模型对于图像中物体位置的敏感度
    3)通过调节颜色/亮度等方式,减小对相应属性的敏感度
    4)模糊/噪点/降低分辨率,减小对边界及图片细节像素的敏感度

    训练数据增广办法:

    1. 位移:
    import numpy as np
    import cv2
    
    def shift(img, delta_width, delta_height):
        height,width,channel = img.shape
        mat = np.float32([[1,0,delta_width],[0,1,delta_height]])
        new_img = cv2.warpAffine(img, mat, (width,height))
        
        return new_img
    

    比较处理前后变化

    import matplotlib.pyplot as plt
    import cv2
    img_file = "~/dog.jpeg"
    img0 = cv2.cvtColor(cv2.imread(img_file), cv2.COLOR_BGR2RGB)
    img = shift(img0, 50, 20)
    
    image.png
    1. 缩放
    def zoom(img, scale):
        new_img = cv2.resize(img, (0,0), fx=scale, fy=scale)
        if scale <= 1:
            top = bottom = int((img.shape[0]-new_img.shape[0])/2)
            left = right = int((img.shape[1]-new_img.shape[1])/2)
            new_img = cv2.copyMakeBorder(new_img.copy(), top, bottom, left, right,cv2.BORDER_CONSTANT, value=[0, 0, 0])
        else:
            top = bottom = int((new_img.shape[0]-img.shape[0])/2)
            left = right = int((new_img.shape[1]-img.shape[1])/2)
            new_img = new_img[top:-top,left:-left,:]
        
        return new_img
    
    image.png
    1. 旋转
    def rotate(img, aug_value):
        M = cv2.getRotationMatrix2D((0.5*img.shape[1], 0.5*img.shape[0]), aug_value, 1.0)
        img = cv2.warpAffine(img.astype(np.uint8), M, (img.shape[1], img.shape[0]), flags=cv2.INTER_LINEAR)
        return img
    
    image.png
    1. 翻转
    img1 = np.fliplr(img)  # 水平翻转
    img2 = np.flipud(img) # 上下翻转
    
    image.png
    1. 颜色变化

    2. 亮度变化

    
    
    1. 对比度变化
    #直方图均衡,可用于调整图片的局部对比度
    for ch in range(img.shape[2]):
        img[..., ch] = cv2.equalizeHist(img[..., ch])
    
    image.png
    1. 模糊
    #高斯模糊
    def blur(img, value):
        return cv2.GaussianBlur(img0, (value, value), 0)
    
    image.png
    1. 降低分辨率
    def zoom_blur(img,aug_value):
        img0 = cv2.resize(img, (0, 0), fx=aug_value,fy=aug_value)
        img0 = cv2.resize(img0, (0, 0), fx=1/aug_value,fy=1/aug_value, interpolation=cv2.INTER_NEAREST)
        return img0
    
    image.png
    1. 增加噪点
    def gaussian_noise(img, aug_value):
        noise = np.zeros(img.shape)
        num_ch = 1 if img.ndim == 2 else img.shape[2]
        cv2.randn(noise, tuple([0]*num_ch), tuple([aug_value]*num_ch))
        img0 = np.clip(img.astype(np.float)+noise, 0, 255).astype(np.uint8)
        return img0
    
    image.png

    相关文章

      网友评论

          本文标题:深度学习实践——图像分类:训练数据增广

          本文链接:https://www.haomeiwen.com/subject/tnhavctx.html