包括MAE、Pre、Rec、F-measure、Auc、CC、Nss
平均绝对误差MAE(mean absolute error),范围[0,+∞),当预测值与真实值完全吻合时等于0,即完美模型;误差越大,该值越大。
行表示预测的label值,列表示真实label值。TP,FP,FN,TN分别表示如下意思:
F-Measure又称为F-Score,F-Measure是Precision和Recall加权调和平均
其中是参数,P是精确率(Precision),R是召回率(Recall)。当参数=1时,就是最常见的F1-Measure了:
ROC曲线下方的面积(Area under the Curve of ROC (AUC ROC)),其意义是:
因为是在1x1的方格里求面积,AUC必在0~1之间。
假设阈值以上是阳性,以下是阴性;
若随机抽取一个阳性样本和一个阴性样本,分类器正确判断阳性样本的值高于阴性样本之机率=AUC
简单说:AUC值越大的分类器,正确率越高。
CC是指皮尔逊相关系数,用来评价预测的眼关注点显著图和参考图ground truth之间的线性相关性,CC越大说明该模型性能越好。
NSS是指标准化扫描路径显着性,用来评价两者之间的差异值,NSS越大说明模型性能越好。
import numpy as np
from sklearn.metrics import precision_score, recall_score, f1_score, roc_auc_score
from sklearn.preprocessing import Binarizer
from scipy.stats import pearsonr
def mask_normalize(mask):
mask = (mask - np.min(mask))/(np.max(mask) - np.min(mask) + 1e-8)
return mask
def compute_mae(mask,label):
h, w = mask.shape[0], mask.shape[1]
sumError = np.sum(np.absolute((mask.astype(float) - label.astype(float))))
maeError = sumError/(float(h)*float(w)+1e-8)
return maeError
def compute_p_r_f(mask,label):
pre = precision_score(mask, label, average='binary')
rec = recall_score(mask, label, average='binary')
f_1 = f1_score(mask, label, average='binary')
return pre, rec, f_1
def compute_auc(mask,label):
auc = roc_auc_score(mask,label)
return auc
def compute_cc(mask,label):
mask = mask - np.mean(mask)
label = label - np.mean(label)
cov = np.sum(mask * label)
d1 = np.sum(mask * mask)
d2 = np.sum(label * label)
cc = cov / (np.sqrt(d1) * np.sqrt(d2) + 1e-8)
return cc
def compute_nss(mask,label):
std=np.std(mask)
u=np.mean(mask)
mask=(mask-u)/std
nss=mask*label
nss=np.sum(nss)/np.sum(label)
return nss
def evaluate(mask,label):
if len(mask.shape)==3:
mask = mask[0]
if len(label.shape)==3:
label = label[0]
mask = mask_normalize(mask)
label = mask_normalize(label)
mae = compute_mae(mask,label)
binarizer = Binarizer(threshold=0.5)
mask_b = binarizer.transform(mask)
label_b = binarizer.transform(label)
mask_br = np.reshape(mask_b, newshape=(-1))
label_br = np.reshape(label_b, newshape=(-1))
pre, rec, f_1 = compute_p_r_f(mask_br,label_br)
auc = compute_auc(mask_br,label_br)
cc = compute_cc(mask,label)
nss = compute_nss(mask,label_b)
return mae, pre, rec, f_1, auc, cc, nss
from trans_evaluate import *
import glob
from skimage import io, transform
data_dir = '/data3/QHL/HSOD/output/SUDF/batch_bsal_result/'
gt_dir = '/data3/QHL/DATA/SOD/HL-SOD/ground_truth/'
name_list = glob.glob(data_dir+'/'+'*.png')
img_num = len(name_list)
mae, pre, rec, f_1, auc, cc, nss = 0, 0, 0, 0, 0, 0, 0
for i in range(img_num):
name = name_list[i].split("/")[-1].split(".")[-2]
image = io.imread(name_list[i])
if len(image.shape) == 2:
h,w = image.shape[0],image.shape[1]
image = np.swapaxes(image, 1, 0)
image = image.astype(np.float32)
if len(image.shape) == 3:
h,w,c = image.shape[0],image.shape[1],image.shape[2]
image = np.swapaxes(image, 2, 0)
image = image.astype(np.float32)
label = io.imread(gt_dir + name + '.jpg')
label = transform.resize(label, (h,w,3))
label = np.swapaxes(label, 2, 0)
label = label.astype(np.float32)
mae_, pre_, rec_, f_1_, auc_, cc_, nss_ = evaluate(image, label)
mae = mae + mae_
pre = pre + pre_
rec = rec + rec_
f_1 = f_1 + f_1_
auc = auc + auc_
cc = cc + cc_
nss = nss + nss_
len = img_num
mae = mae / len
pre = pre / len
rec = rec / len
f_1 = f_1 / len
auc = auc / len
cc = cc / len
nss = nss / len
print('pre, rec, f_1', pre, rec, f_1)
print('auc, cc, nss', auc, cc, nss)
print('mae',mae)