Unet项目解析(6): 图像分块、整合 / 数据对齐、网络输出转成图像
生活随笔
收集整理的這篇文章主要介紹了
Unet项目解析(6): 图像分块、整合 / 数据对齐、网络输出转成图像
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
項目GitHub主頁:https://github.com/orobix/retina-unet
參考論文:Retina blood vessel segmentation with a convolution neural network (U-net)
1. 訓(xùn)練數(shù)據(jù)
1.1 訓(xùn)練圖像、訓(xùn)練金標(biāo)準(zhǔn)隨機分塊
主代碼:
# 訓(xùn)練集太少,采用分塊的方法進(jìn)行訓(xùn)練 def get_data_training(DRIVE_train_imgs_original, #訓(xùn)練圖像路徑DRIVE_train_groudTruth, #金標(biāo)準(zhǔn)圖像路徑patch_height,patch_width,N_subimgs,inside_FOV):train_imgs_original = load_hdf5(DRIVE_train_imgs_original)train_masks = load_hdf5(DRIVE_train_groudTruth) #visualize(group_images(train_imgs_original[0:20,:,:,:],5),'imgs_train').show() train_imgs = my_PreProc(train_imgs_original) # 圖像預(yù)處理 歸一化等train_masks = train_masks/255.train_imgs = train_imgs[:,:,9:574,:] # 圖像裁剪 size=565*565train_masks = train_masks[:,:,9:574,:] # 圖像裁剪 size=565*565data_consistency_check(train_imgs,train_masks) # 訓(xùn)練圖像和金標(biāo)準(zhǔn)圖像一致性檢查assert(np.min(train_masks)==0 and np.max(train_masks)==1) #金標(biāo)準(zhǔn)圖像 2類 0-1print ("\n train images/masks shape:")print (train_imgs.shape)print ("train images range (min-max): " +str(np.min(train_imgs)) +' - '+str(np.max(train_imgs)))print ("train masks are within 0-1\n")# 從整張圖像中-隨機提取-訓(xùn)練子塊patches_imgs_train, patches_masks_train =extract_random(train_imgs,train_masks,patch_height,patch_width,N_subimgs,inside_FOV)data_consistency_check(patches_imgs_train, patches_masks_train) # 訓(xùn)練圖像子塊和金標(biāo)準(zhǔn)圖像子塊一致性檢查print ("\n train PATCHES images/masks shape:")print (patches_imgs_train.shape)print ("train PATCHES images range (min-max): " +str(np.min(patches_imgs_train)) +' - '+str(np.max(patches_imgs_train)))return patches_imgs_train, patches_masks_train隨機提取子塊:
# 訓(xùn)練集圖像 隨機 提取子塊 def extract_random(full_imgs,full_masks, patch_h,patch_w, N_patches, inside=True):if (N_patches%full_imgs.shape[0] != 0): # 檢驗每張圖像應(yīng)該提取多少塊print "N_patches: plase enter a multiple of 20"exit()assert (len(full_imgs.shape)==4 and len(full_masks.shape)==4) # 張量尺寸檢驗assert (full_imgs.shape[1]==1 or full_imgs.shape[1]==3) # 通道檢驗assert (full_masks.shape[1]==1) # 通道檢驗assert (full_imgs.shape[2] == full_masks.shape[2] and full_imgs.shape[3] == full_masks.shape[3]) # 尺寸檢驗patches = np.empty((N_patches,full_imgs.shape[1],patch_h,patch_w)) # 訓(xùn)練圖像總子塊patches_masks = np.empty((N_patches,full_masks.shape[1],patch_h,patch_w)) # 訓(xùn)練金標(biāo)準(zhǔn)總子塊img_h = full_imgs.shape[2] img_w = full_imgs.shape[3] patch_per_img = int(N_patches/full_imgs.shape[0]) # 每張圖像中提取的子塊數(shù)量print ("patches per full image: " +str(patch_per_img))iter_tot = 0 # 圖像子塊總量計數(shù)器for i in range(full_imgs.shape[0]): # 遍歷每一張圖像k=0 # 每張圖像子塊計數(shù)器while k <patch_per_img:x_center = random.randint(0+int(patch_w/2),img_w-int(patch_w/2)) # 塊中心的范圍y_center = random.randint(0+int(patch_h/2),img_h-int(patch_h/2))if inside==True:if is_patch_inside_FOV(x_center,y_center,img_w,img_h,patch_h)==False:continuepatch = full_imgs[i,:,y_center-int(patch_h/2):y_center+int(patch_h/2),x_center-int(patch_w/2):x_center+int(patch_w/2)]patch_mask = full_masks[i,:,y_center-int(patch_h/2):y_center+int(patch_h/2),x_center-int(patch_w/2):x_center+int(patch_w/2)]patches[iter_tot]=patch # size=[Npatches, 3, patch_h, patch_w]patches_masks[iter_tot]=patch_mask # size=[Npatches, 1, patch_h, patch_w]iter_tot +=1 # 子塊總量計數(shù)器k+=1 # 每張圖像子塊總量計數(shù)器return patches, patches_masks數(shù)據(jù)一致性檢查函數(shù):
# 訓(xùn)練集圖像 和 金標(biāo)準(zhǔn)圖像一致性檢驗 def data_consistency_check(imgs,masks):assert(len(imgs.shape)==len(masks.shape))assert(imgs.shape[0]==masks.shape[0])assert(imgs.shape[2]==masks.shape[2])assert(imgs.shape[3]==masks.shape[3])assert(masks.shape[1]==1)assert(imgs.shape[1]==1 or imgs.shape[1]==3)1.2 訓(xùn)練金標(biāo)準(zhǔn)改寫成Une輸出形式
# 將金標(biāo)準(zhǔn)圖像改寫成模型輸出形式 def masks_Unet(masks): # size=[Npatches, 1, patch_height, patch_width]assert (len(masks.shape)==4)assert (masks.shape[1]==1 )im_h = masks.shape[2]im_w = masks.shape[3]masks = np.reshape(masks,(masks.shape[0],im_h*im_w)) # 單像素建模new_masks = np.empty((masks.shape[0],im_h*im_w,2)) # 二分類輸出for i in range(masks.shape[0]):for j in range(im_h*im_w):if masks[i,j] == 0:new_masks[i,j,0]=1 # 金標(biāo)準(zhǔn)圖像的反轉(zhuǎn)new_masks[i,j,1]=0 # 金標(biāo)準(zhǔn)圖像else:new_masks[i,j,0]=0new_masks[i,j,1]=1return new_masks2. 網(wǎng)絡(luò)輸出轉(zhuǎn)換成圖像子塊
# 網(wǎng)絡(luò)輸出 size=[Npatches, patch_height*patch_width, 2] def pred_to_imgs(pred, patch_height, patch_width, mode="original"):assert (len(pred.shape)==3) assert (pred.shape[2]==2 ) # 確認(rèn)是否為二分類pred_images = np.empty((pred.shape[0],pred.shape[1])) #(Npatches,height*width)if mode=="original": # 網(wǎng)絡(luò)概率輸出for i in range(pred.shape[0]):for pix in range(pred.shape[1]):pred_images[i,pix]=pred[i,pix,1] #pred[:, :, 0] 是反分割圖像輸出 pred[:, :, 1]是分割輸出elif mode=="threshold": # 網(wǎng)絡(luò)概率-閾值輸出for i in range(pred.shape[0]):for pix in range(pred.shape[1]):if pred[i,pix,1]>=0.5:pred_images[i,pix]=1else:pred_images[i,pix]=0else:print ("mode " +str(mode) +" not recognized, it can be 'original' or 'threshold'")exit()# 改寫成(Npatches,1, height, width)pred_images = np.reshape(pred_images,(pred_images.shape[0],1, patch_height, patch_width)) return pred_images3. 測試圖像按順序分塊、預(yù)測子塊重新整合成圖像
3.1 測試圖像分塊
def get_data_testing_overlap(DRIVE_test_imgs_original, DRIVE_test_groudTruth, Imgs_to_test, # 20patch_height, patch_width, stride_height, stride_width):test_imgs_original = load_hdf5(DRIVE_test_imgs_original)test_masks = load_hdf5(DRIVE_test_groudTruth)test_imgs = my_PreProc(test_imgs_original)test_masks = test_masks/255.test_imgs = test_imgs[0:Imgs_to_test,:,:,:]test_masks = test_masks[0:Imgs_to_test,:,:,:]test_imgs = paint_border_overlap(test_imgs, patch_height, # 拓展圖像 可以準(zhǔn)確劃分patch_width, stride_height, stride_width)assert(np.max(test_masks)==1 and np.min(test_masks)==0)print ("\n test images shape:")print (test_imgs.shape)print ("\n test mask shape:")print (test_masks.shape)print ("test images range (min-max): " +str(np.min(test_imgs)) +' - '+str(np.max(test_imgs)))# 按照順序提取圖像快 方便后續(xù)進(jìn)行圖像恢復(fù)(作者采用了overlap策略)patches_imgs_test = extract_ordered_overlap(test_imgs,patch_height,patch_width,stride_height,stride_width)print ("\n test PATCHES images shape:")print (patches_imgs_test.shape)print ("test PATCHES images range (min-max): " +str(np.min(patches_imgs_test)) +' - '+str(np.max(patches_imgs_test)))return patches_imgs_test, test_imgs.shape[2], test_imgs.shape[3], test_masks #原始大小原始圖像進(jìn)行拓展填充:
def paint_border_overlap(full_imgs, patch_h, patch_w, stride_h, stride_w):assert (len(full_imgs.shape)==4) #4D arraysassert (full_imgs.shape[1]==1 or full_imgs.shape[1]==3) #check the channel is 1 or 3img_h = full_imgs.shape[2] #height of the full imageimg_w = full_imgs.shape[3] #width of the full imageleftover_h = (img_h-patch_h)%stride_h #leftover on the h dimleftover_w = (img_w-patch_w)%stride_w #leftover on the w dimif (leftover_h != 0): #change dimension of img_htmp_full_imgs = np.zeros((full_imgs.shape[0],full_imgs.shape[1],img_h+(stride_h-leftover_h),img_w))tmp_full_imgs[0:full_imgs.shape[0],0:full_imgs.shape[1],0:img_h,0:img_w] = full_imgsfull_imgs = tmp_full_imgsif (leftover_w != 0): #change dimension of img_wtmp_full_imgs = np.zeros((full_imgs.shape[0],full_imgs.shape[1],full_imgs.shape[2],img_w+(stride_w - leftover_w)))tmp_full_imgs[0:full_imgs.shape[0],0:full_imgs.shape[1],0:full_imgs.shape[2],0:img_w] = full_imgsfull_imgs = tmp_full_imgsreturn full_imgs按順序提取圖像子塊:
# 按照順序?qū)ν卣购蟮膱D像進(jìn)行子塊采樣 def extract_ordered_overlap(full_imgs, patch_h, patch_w,stride_h,stride_w):assert (len(full_imgs.shape)==4) assert (full_imgs.shape[1]==1 or full_imgs.shape[1]==3) img_h = full_imgs.shape[2] img_w = full_imgs.shape[3] assert ((img_h-patch_h)%stride_h==0 and (img_w-patch_w)%stride_w==0)N_patches_img = ((img_h-patch_h)//stride_h+1)*((img_w-patch_w)//stride_w+1) # 每張圖像采集到的子圖像N_patches_tot = N_patches_img*full_imgs.shape[0] # 測試集總共的子圖像數(shù)量patches = np.empty((N_patches_tot,full_imgs.shape[1],patch_h,patch_w))iter_tot = 0 for i in range(full_imgs.shape[0]): for h in range((img_h-patch_h)//stride_h+1):for w in range((img_w-patch_w)//stride_w+1):patch = full_imgs[i,:,h*stride_h:(h*stride_h)+patch_h,w*stride_w:(w*stride_w)+patch_w]patches[iter_tot]=patchiter_tot +=1 #totalassert (iter_tot==N_patches_tot)return patches3.2 對于圖像子塊進(jìn)行復(fù)原
# [Npatches, 1, patch_h, patch_w] img_h=new_height[588] img_w=new_width[568] stride-[10,10] def recompone_overlap(preds, img_h, img_w, stride_h, stride_w):assert (len(preds.shape)==4) # 檢查張量尺寸assert (preds.shape[1]==1 or preds.shape[1]==3)patch_h = preds.shape[2]patch_w = preds.shape[3]N_patches_h = (img_h-patch_h)//stride_h+1 # img_h方向包括的patch_h數(shù)量N_patches_w = (img_w-patch_w)//stride_w+1 # img_w方向包括的patch_w數(shù)量N_patches_img = N_patches_h * N_patches_w # 每張圖像包含的patch的數(shù)目assert (preds.shape[0]%N_patches_img==0 N_full_imgs = preds.shape[0]//N_patches_img # 全幅圖像的數(shù)目full_prob = np.zeros((N_full_imgs,preds.shape[1],img_h,img_w))full_sum = np.zeros((N_full_imgs,preds.shape[1],img_h,img_w))k = 0 #迭代所有的子塊for i in range(N_full_imgs):for h in range((img_h-patch_h)//stride_h+1):for w in range((img_w-patch_w)//stride_w+1):full_prob[i,:,h*stride_h:(h*stride_h)+patch_h,w*stride_w:(w*stride_w)+patch_w]+=preds[k]full_sum[i,:,h*stride_h:(h*stride_h)+patch_h,w*stride_w:(w*stride_w)+patch_w]+=1k+=1assert(k==preds.shape[0])assert(np.min(full_sum)>=1.0) final_avg = full_prob/full_sum # 疊加概率 / 疊加權(quán)重 : 采用了均值的方法print final_avg.shapeassert(np.max(final_avg)<=1.0)assert(np.min(final_avg)>=0.0)return final_avg總結(jié)
以上是生活随笔為你收集整理的Unet项目解析(6): 图像分块、整合 / 数据对齐、网络输出转成图像的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ 中重载 + 操作符的正确方法
- 下一篇: 发些c/c++/vc/驱动/网络安全的好