深度学习数据预处理中的数据增强

2017-11-21  本文已影响331人  chunleiml

在某些深度学习任务中,有时候会遇到数据不足的尴尬,这时候数据增强就派上用场了,数据不够,增强来凑。通过旋转,上下抖动等方式可以实现数据量的成倍数增长。

    def im_translation(self, image_size, imgs, labels, pixelx, pixely, pixel0, pixel1):
# =============================================================================
#         参数:
#         image_size:要裁剪的图像的大小
#         imgs,labels:图像数据和label数据
#         pixel:根据裁剪图像大小和ROI在图像中的位置,通过设置pixel大小使ROI在裁剪后图像的中间位置
#         pixel0:数据抖动在y轴方向的大小
#         pixel1:数据抖动在x轴方向的大小
#         功能:
#         1、通过enumerate()和zip()函数实现在一个for循环里面同时对imgs_slice和labels_slice的操作,因为使用的是随机抖动,图像和label的抖动
#         必须保证一致性,在一个for循环可以很好的达到这个目的。
#         2、函数对原始图像和要裁剪图像大小做了比较,对于原始图像大于和小于要裁剪图像的情况都做了考虑。
#         3、在对每层图像完成裁剪和抖动后,利用concatenate()完成了对每层图像的连接。
# =============================================================================
        z, x, y = np.shape(imgs)
        image_sizeX = image_size[0]
        image_sizeY = image_size[1]       
#        judge = sum([x > image_size, y > image_size])        
#        for i, imgs_slice in enumerate(imgs):
        imgs_new = []
        labels_new = []
        shift = np.max([abs(pixelx), abs(pixely), np.max((abs(x - image_sizeX), abs(y - image_sizeY)))])
        judge = sum([x > (image_sizeX + abs(pixelx) * 2), y > (image_sizeY + abs(pixely) * 2)])         
        for i, (imgs_slice, labels_slice) in enumerate(zip(imgs, labels)):
            width = random.randint(-pixel0, pixel0)
            height = random.randint(-pixel1, pixel1)
            if judge == 2 :
                imgs_slice = imgs_slice[int((x-image_sizeX)/2+pixelx+width):int((x+image_sizeX)/2+pixelx+width), int((y-image_sizeY)/2+pixely+height):int((y+image_sizeY)/2+pixely+height)]
                labels_slice = labels_slice[int((x-image_sizeX)/2+pixelx+width):int((x+image_sizeX)/2+pixelx+width), int((y-image_sizeY)/2+pixely+height):int((y+image_sizeY)/2+pixely+height)]
                imgs_new.append(imgs_slice)
                labels_new.append(labels_slice)              
                
            else:
                image_new = np.min(imgs_slice)*np.ones([image_sizeX+shift*2, image_sizeY+shift*2], dtype = np.float16)   
                image_new[int((image_sizeX+shift*2-x)/2):int((image_sizeX+shift*2-x)/2)+x, int((image_sizeY+shift*2-y)/2):int((image_sizeY+shift*2-y)/2)+y] = imgs_slice
                x1, y1 = np.shape(image_new)
                imgs_slice = image_new[int((x1-image_sizeX)/2 + pixelx):int((x1+image_sizeX)/2 + pixelx), int((y1-image_sizeY)/2 + pixely):int((y1+image_sizeY)/2) + pixely]            
                imgs_new.append(imgs_slice)    


                label_new = np.min(labels_slice)*np.ones([image_sizeX+shift*2, image_sizeY+shift*2], dtype = np.float16)   
                label_new[int((image_sizeX+shift*2-x)/2):int((image_sizeX+shift*2-x)/2)+x, int((image_sizeY+shift*2-y)/2):int((image_sizeY+shift*2-y)/2)+y] = labels_slice
                x1, y1 = np.shape(image_new)
                labels_slice = label_new[int((x1-image_sizeX)/2 + pixelx):int((x1+image_sizeX)/2 + pixelx), int((y1-image_sizeY)/2 + pixely):int((y1+image_sizeY)/2) + pixely]            
                labels_new.append(labels_slice)                
                

        imgs_new = np.array(imgs_new, np.float32)
        labels_new = np.array(labels_new, np.uint8)
        return imgs_new, labels_new

def im_rotation(imgs, labels, r1, r2, flages, borderValue):
# =============================================================================
#         参数:
#         imgs,labels: 输入的图像和label数据
#         r1,r2: 需要图像和label绕中心点旋转的角度
#         功能:
#         实现对图像和label同时绕中心点随机旋转一定的角度来实现数据增强。
#         利用OpenCV的getRotationMatrix2D()生成旋转操作需要的矩阵,利用warpAffine()完成旋转操作。
# =============================================================================
    z, x, y = np.shape(imgs)
    imgs_new = np.zeros((z, x, y))
    labels_new = np.zeros((z, x, y))
    for i in range(imgs.shape[0]):
        a = random.randint(r1, r2)        
        RotateMatrix  = cv2.getRotationMatrix2D(center=(imgs[i,:,:].shape[0]/2, imgs[i,:,:].shape[1]/2), angle=a, scale=1)
        imgs_new[i,:,:] = cv2.warpAffine(imgs[i,:,:], RotateMatrix, (imgs[i,:,:].shape[0], imgs[i,:,:].shape[1]),flags=cv2.INTER_NEAREST, borderValue=-1000)
        labels_new[i,:,:] = cv2.warpAffine(labels[i,:,:], RotateMatrix,(labels[i,:,:].shape[0], labels[i,:,:].shape[1]),flags=cv2.INTER_NEAREST)        
    return imgs_new, labels_new

第一个函数是实现数据的裁剪和抖动,第二个是实现以中心为原点的旋转。
然后通过调用这两个函数就可以实现数据增强了

#实现数据增强
imgs_r1, labels_r1 = im_rotation(imgs_i, labels_i, 0, 0, flages = cv2.INTER_NEAREST, borderValue=-1000)
imgs_r2, labels_r2 = im_rotation(imgs_i, labels_i, 0, 0, flages = cv2.INTER_NEAREST, borderValue=-1000)
            
imgs_t05, labels_t05 = im_translation([120,180], imgs_i, labels_i, 60, 0, 5,0)
imgs_r1, labels_r1 = im_translation([120,180], imgs_r1, labels_r1, 60, 0, 0,5) 
上一篇下一篇

猜你喜欢

热点阅读