分割資料預處理
阿新 • • 發佈:2021-06-12
日常報錯(累~):
小編最近用yolact對BraTS資料集做預測,驗證結果如下:
發現ET對於Dice和PPV太小了,根據公式,我一開始以為是模型預測的區域過多導致的。
後面生成圖片觀察:
忽然之間,意識到,是自己的target生成錯了。筆者對這個三個區域,首先是採用邊緣提取,獲取邊緣的座標,之後進行一次判斷,只保留點數大於20的區域,所以就可能出現上述圖片的情況。我們真是target是由ET區域(黃色區域),但我最後生成的target是沒有ET區域的。
所以再模型預測的階段,也不會預測出ET,造成的PPV值偏小,進而Dice偏小。
如下是我生成邊緣座標(改進後,每個if價格else:continue)的部分程式碼:
f=open('F:/DATA/train_mask4.txt','w')
for r in tqdm(range(len(image_traintest_paths))):
# for r in tqdm(range(4)):
# 獲取路徑
a=0
b=0
c=0
# fold_path=mask_train_paths[r]
fold_path=mask_traintest_paths[r]
npmask=np.load(fold_path)
#print(npmask.shape)
txt_line = ''
vertices_list_WT =[]
vertices_list_ET=[]
vertices_list_TC=[]
# 獲取邊緣
WT_Label = npmask.copy()
WT_Label[npmask == 1] = 1.
WT_Label[npmask == 2] = 1.
WT_Label[npmask == 4] = 1.
WT_Label=WT_Label.astype(np.uint8)
WT_Label=WT_Label*255
TC_Label = npmask.copy()
TC_Label[npmask == 1] = 1.
TC_Label[npmask == 2] = 0.
TC_Label[npmask == 4] = 1.
TC_Label=TC_Label.astype(np.uint8)
TC_Label=TC_Label*255
ET_Label = npmask.copy()
ET_Label[npmask == 1] = 0.
ET_Label[npmask == 2] = 0.
ET_Label[npmask == 4] = 1
ET_Label=ET_Label.astype(np.uint8)
ET_Label=ET_Label*255
# 寫入到text line
path_forward=fold_path.split('\\')[-1]
txt_line=txt_line+path_forward+" "
# 獲取WT邊緣座標
detected_edges = cv2.Canny(WT_Label, lowThreshold, lowThreshold+10 * ratio, apertureSize=kernel_size)
# print(detected_edges.shape)
if SumNum(detected_edges)>20:
a=1
for i in range(detected_edges.shape[0]):
for j in range(detected_edges.shape[1]):
if detected_edges[i,j] !=0:
vertices_list_WT.append([j,i])
vertices_list_WT=np.array(vertices_list_WT)
# 獲取WTbounding box
max_x_WT=np.max(vertices_list_WT[:,0])
max_y_WT=np.max(vertices_list_WT[:,1])
min_x_WT=np.min(vertices_list_WT[:,0])
min_y_WT=np.min(vertices_list_WT[:,1])
# 新增WT bounding box , vertices
txt_line=txt_line+ str(min_x_WT)+','+str(min_y_WT)+','+str(max_x_WT)+','+str(max_y_WT)+','+'1'+','
for i in range(len(vertices_list_WT)):
txt_line=txt_line+str(vertices_list_WT[i][0])+','
txt_line=txt_line+str(vertices_list_WT[i][1])+','
txt_line=txt_line[:-1]+" "
else:continue
# 獲取TC邊緣座標
detected_edges = cv2.Canny(TC_Label, lowThreshold, lowThreshold+10 * ratio, apertureSize=kernel_size)
if SumNum(detected_edges)>20:
b=1
for i in range(detected_edges.shape[0]):
for j in range(detected_edges.shape[1]):
if detected_edges[i,j] !=0:
vertices_list_TC.append([j,i])
vertices_list_TC=np.array(vertices_list_TC)
max_x_TC=np.max(vertices_list_TC[:,0])
max_y_TC=np.max(vertices_list_TC[:,1])
min_x_TC=np.min(vertices_list_TC[:,0])
min_y_TC=np.min(vertices_list_TC[:,1])
# 新增TC bounding box, vertices
txt_line=txt_line+ str(min_x_TC)+','+str(min_y_TC)+','+str(max_x_TC)+','+str(max_y_TC)+','+'2'+','
for i in range(len(vertices_list_TC)):
txt_line=txt_line+str(vertices_list_TC[i][0])+','
txt_line=txt_line+str(vertices_list_TC[i][1])+','
txt_line=txt_line[:-1]+" "
else:continue
# 獲取ET邊緣座標
detected_edges = cv2.Canny(ET_Label, lowThreshold, lowThreshold+10 * ratio, apertureSize=kernel_size)
if SumNum(detected_edges)>20:
c=1
for i in range(detected_edges.shape[0]):
for j in range(detected_edges.shape[1]):
if detected_edges[i,j] !=0:
vertices_list_ET.append([j,i])
vertices_list_ET=np.array(vertices_list_ET)
max_x_ET=np.max(vertices_list_ET[:,0])
max_y_ET=np.max(vertices_list_ET[:,1])
min_x_ET=np.min(vertices_list_ET[:,0])
min_y_ET=np.min(vertices_list_ET[:,1])
# 新增ET bounding box, vertices
txt_line=txt_line+ str(min_x_ET)+','+str(min_y_ET)+','+str(max_x_ET)+','+str(max_y_ET)+','+'3'+','
for i in range(len(vertices_list_ET)):
txt_line=txt_line+str(vertices_list_ET[i][0])+','
txt_line=txt_line+str(vertices_list_ET[i][1])+','
else:continue
txt_line=txt_line[:-1]+'\n'
if (a==b==c==0):
continue
f.write(txt_line)
f.close()
不知再次訓練結果會怎樣。。。