网络模型mAP计算实现代码

网络模型mAP计算实现代码

一、mAP精度计算

 

   这里首先介绍几个常见的模型评价术语,现在假设我们的分类目标只有两类,计为正例(positive)和负例(negtive)分别是:

   1)True positives(TP):  被正确地划分为正例的个数,即实际为正例且被分类器划分为正例的实例数(样本数);

   2)False positives(FP): 被错误地划分为正例的个数,即实际为负例但被分类器划分为正例的实例数;

   3)False negatives(FN):被错误地划分为负例的个数,即实际为正例但被分类器划分为负例的实例数;

   4)True negatives(TN): 被正确地划分为负例的个数,即实际为负例且被分类器划分为负例的实例数。 

 P  

   代表precision即准确率,计算公式为预测样本中实际正样本数 / 所有的正样本数 即  precision=TP/(TP+FP);

 R

   代表recall即召回率,计算公式为 预测样本中实际正样本数 / 预测的样本数即   Recall=TP/(TP+FN)=TP/P

 一般来说,precision和recall是鱼与熊掌的关系,往往召回率越高,准确率越低

 AP

     AP 即 Average Precision即平均精确度

 mAP

mAP 即 Mean Average Precision即平均AP值,是对多个验证集个体求平均AP值,作为 object dection中衡量检测精度的指标。

 F

度量(F-measure),F度量涵盖了准确率和召回率这两个指标。其计算公式如下:F = 2 * P * R / (P + R)

 P-R曲线

P-R曲线即 以 precision 和 recall 作为 纵、横轴坐标 的二维曲线。通过选取不同阈值时对应的精度和召回率画出

总体趋势,精度越高,召回越低,当召回达到1时,对应概率分数最低的正样本,这个时候正样本数量除以所有大于等于该阈值的样本数量就是最低的精度值。

另外,P-R曲线围起来的面积就是AP值,通常来说一个越好的分类器,AP值越高。

 总结一下,在目标检测中,每一类都可以根据 recall 和 precision绘制P-R曲线,AP就是该曲线下的面积,mAP就是所有类AP的平均值。

二.评测代码

import tensorflow as tf

 #精确率评价指标

def metric_precision(y_true,y_pred):   

    TP=tf.reduce_sum(y_true*tf.round(y_pred))

    TN=tf.reduce_sum((1-y_true)*(1-tf.round(y_pred)))

    FP=tf.reduce_sum((1-y_true)*tf.round(y_pred))

    FN=tf.reduce_sum(y_true*(1-tf.round(y_pred)))

    precision=TP/(TP+FP)

    return precision

 #召回率评价指标

def metric_recall(y_true,y_pred): 

    TP=tf.reduce_sum(y_true*tf.round(y_pred))

    TN=tf.reduce_sum((1-y_true)*(1-tf.round(y_pred)))

    FP=tf.reduce_sum((1-y_true)*tf.round(y_pred))

    FN=tf.reduce_sum(y_true*(1-tf.round(y_pred)))

    recall=TP/(TP+FN)

    return recall

#F1-score评价指标

def metric_F1score(y_true,y_pred):   

    TP=tf.reduce_sum(y_true*tf.round(y_pred))

    TN=tf.reduce_sum((1-y_true)*(1-tf.round(y_pred)))

    FP=tf.reduce_sum((1-y_true)*tf.round(y_pred))

    FN=tf.reduce_sum(y_true*(1-tf.round(y_pred)))

    precision=TP/(TP+FP)

    recall=TP/(TP+FN)

    F1score=2*precision*recall/(precision+recall)

    return F1score<br><br>

#编译阶段引用自定义评价指标示例

model.compile(optimizer='adam',

          loss='binary_crossentropy',

          metrics=['accuracy',

                metric_precision,

                metric_recall,

                metric_F1score])

# AUC for a binary classifier

def auc(y_true, y_pred):

    ptas = tf.stack([binary_PTA(y_true,y_pred,k) for k in np.linspace(0, 1, 1000)],axis=0)

    pfas = tf.stack([binary_PFA(y_true,y_pred,k) for k in np.linspace(0, 1, 1000)],axis=0)

    pfas = tf.concat([tf.ones((1,)) ,pfas],axis=0)

    binSizes = -(pfas[1:]-pfas[:-1])

    s = ptas*binSizes

    return K.sum(s, axis=0)

#-----------------------------------------------------------------------------------------------------------------------------------------------------

# PFA, prob false alert for binary classifier

def binary_PFA(y_true, y_pred, threshold=K.variable(value=0.5)):

    y_pred = K.cast(y_pred >= threshold, 'float32')

    # N = total number of negative labels

    N = K.sum(1 - y_true)

    # FP = total number of false alerts, alerts from the negative class labels

    FP = K.sum(y_pred - y_pred * y_true)

    return FP/N

#-----------------------------------------------------------------------------------------------------------------------------------------------------

# P_TA prob true alerts for binary classifier

def binary_PTA(y_true, y_pred, threshold=K.variable(value=0.5)):

    y_pred = K.cast(y_pred >= threshold, 'float32')

    # P = total number of positive labels

    P = K.sum(y_true)

    # TP = total number of correct alerts, alerts from the positive class labels

    TP = K.sum(y_pred * y_true)

    return TP/P

#接着在模型的compile中设置metrics

# False Discovery Rate(FDR)

from sklearn.metrics import confusion_matrix

y_true = [0,0,0,0,0,0,,1,1,1,1,1]

y_pred = [0,0,0,0,0,0,,1,1,1,1,1]

 tn, fp , fn, tp = confusion_matrix(y_true, y_pred).ravel()

fdr = fp / (fp + tp)

 print(fdr)

原文地址:https://www.cnblogs.com/wujianming-110117/p/12827215.html