分割数据预处理

时间: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()

  不知再次训练结果会怎样。。。

原文地址:https://www.cnblogs.com/a-runner/p/14878741.html