深度學習任務中,用于訓練的數據需要大小一樣,但現實使用的數據往往有大小的差異,有時大小一樣的數據進行插值操作后大小也會不一樣,根據任務需要確定合適的size后,把所有用于訓練的數據切成相同size是模型開始訓練前很重要的一步,由于訓練的image,label,測試的image,label都需要進行相同的操作,因此我們把這個功能封裝成一個函數,需要的時候調用就可以。
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、函數對原始圖像和要裁剪圖像大小做了比較,對于原始圖像大于和小于要裁剪圖像的情況都做了考慮。
=============================================================================
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
對應模型預測階段不需要對label進行操作,簡化后如下:
def cutting(image_size, imgs, pixelx, pixely):
# =============================================================================
# 參數:
# image_size: 需要的圖像大小
# imgs:需要裁剪的數據
# pixel:為了使ROI區域在中心所做的在y軸方向的調整
# shift:目的是防止因為pixelx, pixely的偏移造成的越界
# 功能:
# 先通過對每層圖像的操作實現對圖像裁剪
# =============================================================================
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參數在需要數據增強的時候可以用到,給pixel設置一個值可以實現所切區域在x,y軸的上下移動操作,可以使訓練數據瞬間增加幾倍。