深度學(xué)習(xí)任務(wù)中,用于訓(xùn)練的數(shù)據(jù)需要大小一樣,但現(xiàn)實(shí)使用的數(shù)據(jù)往往有大小的差異,有時大小一樣的數(shù)據(jù)進(jìn)行插值操作后大小也會不一樣,根據(jù)任務(wù)需要確定合適的size后,把所有用于訓(xùn)練的數(shù)據(jù)切成相同size是模型開始訓(xùn)練前很重要的一步,由于訓(xùn)練的image,label,測試的image,label都需要進(jìn)行相同的操作,因此我們把這個功能封裝成一個函數(shù),需要的時候調(diào)用就可以。
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ù)對原始圖像和要裁剪圖像大小做了比較,對于原始圖像大于和小于要裁剪圖像的情況都做了考慮。
=============================================================================
z, x, y = np.shape(imgs)
image_sizeX = image_size[0]
image_sizeY = image_size[1]
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
對應(yīng)模型預(yù)測階段不需要對label進(jìn)行操作,簡化后如下:
def cutting(image_size, imgs, pixelx, pixely):
# =============================================================================
# 參數(shù):
# image_size: 需要的圖像大小
# imgs:需要裁剪的數(shù)據(jù)
# pixel:為了使ROI區(qū)域在中心所做的在y軸方向的調(diào)整
# shift:目的是防止因?yàn)閜ixelx, pixely的偏移造成的越界
# 功能:
# 先通過對每層圖像的操作實(shí)現(xiàn)對圖像裁剪
# =============================================================================
image_sizeX = image_size[0]
image_sizeY = image_size[1]
z, x, y = np.shape(imgs)
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)])
imgs_new = []
# image_std = imgs
for i, image_std in enumerate(imgs):
if judge == 2:
image_std = image_std[int((x - image_sizeX) / 2 + pixelx):int((x + image_sizeX) / 2 + pixelx),
int((y - image_sizeY) / 2 + pixely):int((y + image_sizeY) / 2) + pixely]
imgs_new.append(image_std)
else:
image_new = np.min(image_std) * np.ones([image_sizeX + shift * 2, image_sizeY + shift * 2], dtype=np.int32)
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] = image_std
x1, y1 = np.shape(image_new)
image_std = 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(image_std)
imgs_new = np.array(imgs_new, np.float32)
return imgs_new
pixel參數(shù)在需要數(shù)據(jù)增強(qiáng)的時候可以用到,給pixel設(shè)置一個值可以實(shí)現(xiàn)所切區(qū)域在x,y軸的上下移動操作,可以使訓(xùn)練數(shù)據(jù)瞬間增加幾倍。