通常我们用目标分割网络,预测结果后。为了得知网络的准确度,可以计算其Dice系数,通过比较其系数,可以得知网络的准确性。
import numpy as np import cv2 from PIL import Image if __name__ == '__main__': y_true_path = 'E:/AI-challenge/2021/results/label_show/'(这个是标签的文件地址) y_pred_path = 'E:/AI-challenge/2021/results/predict/'(这个是预测后的图像输出地址) dice_list = [] for i in range(398): y_true = np.array(Image.open(y_true_path+str(i+1)+'.jpg')) y_pred = cv2.imread(y_pred_path+str(i+1)+'.jpg',cv2.IMREAD_GRAYSCALE) y_true = y_true/255 y_pred = y_pred/255 union = y_true * y_pred dice = 2*np.sum(union)/(np.sum(y_true)+np.sum(y_pred)) dice_list.append(dice) print(i+1, ' ', dice, '\n') print(dice_list) print(np.mean(np.array(dice_list))) }
这样就可以求出每张图片对应的Dice系数。