深度學(xué)習(xí)數(shù)據(jù)預(yù)處理中的數(shù)據(jù)增強(qiáng)

在某些深度學(xué)習(xí)任務(wù)中,有時候會遇到數(shù)據(jù)不足的尷尬,這時候數(shù)據(jù)增強(qiáng)就派上用場了,數(shù)據(jù)不夠,增強(qiáng)來湊。通過旋轉(zhuǎn),上下抖動等方式可以實(shí)現(xiàn)數(shù)據(jù)量的成倍數(shù)增長。

    def im_translation(self, image_size, imgs, labels, pixelx, pixely, pixel0, pixel1):
# =============================================================================
#         參數(shù):
#         image_size:要裁剪的圖像的大小
#         imgs,labels:圖像數(shù)據(jù)和label數(shù)據(jù)
#         pixel:根據(jù)裁剪圖像大小和ROI在圖像中的位置,通過設(shè)置pixel大小使ROI在裁剪后圖像的中間位置
#         pixel0:數(shù)據(jù)抖動在y軸方向的大小
#         pixel1:數(shù)據(jù)抖動在x軸方向的大小
#         功能:
#         1、通過enumerate()和zip()函數(shù)實(shí)現(xiàn)在一個for循環(huán)里面同時對imgs_slice和labels_slice的操作,因?yàn)槭褂玫氖请S機(jī)抖動,圖像和label的抖動
#         必須保證一致性,在一個for循環(huán)可以很好的達(dá)到這個目的。
#         2、函數(shù)對原始圖像和要裁剪圖像大小做了比較,對于原始圖像大于和小于要裁剪圖像的情況都做了考慮。
#         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):
# =============================================================================
#         參數(shù):
#         imgs,labels: 輸入的圖像和label數(shù)據(jù)
#         r1,r2: 需要圖像和label繞中心點(diǎn)旋轉(zhuǎn)的角度
#         功能:
#         實(shí)現(xiàn)對圖像和label同時繞中心點(diǎn)隨機(jī)旋轉(zhuǎn)一定的角度來實(shí)現(xiàn)數(shù)據(jù)增強(qiáng)。
#         利用OpenCV的getRotationMatrix2D()生成旋轉(zhuǎn)操作需要的矩陣,利用warpAffine()完成旋轉(zhuǎn)操作。
# =============================================================================
    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

第一個函數(shù)是實(shí)現(xiàn)數(shù)據(jù)的裁剪和抖動,第二個是實(shí)現(xiàn)以中心為原點(diǎn)的旋轉(zhuǎn)。
然后通過調(diào)用這兩個函數(shù)就可以實(shí)現(xiàn)數(shù)據(jù)增強(qiáng)了

#實(shí)現(xiàn)數(shù)據(jù)增強(qiáng)
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) 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容