博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
计算CP、CR、CF1、OP、OR、OF1和mAP的top-3评价指标
阅读量:2013 次
发布时间:2019-04-28

本文共 2524 字,大约阅读时间需要 8 分钟。

计算CP、CR、CF1、OP、OR、OF1和mAP的top-3评价指标

# -*- coding: utf-8 -*-import numpy as np#  CP CR CF1 OP OR OF1  mAP 的函数返回值def calculate_metrics(labels, preds):    mAP = calculate_mAP(labels, preds)    pc_top3, rc_top3, f1c_top3, po_top3, ro_top3, f1o_top3 = calculate_top3_metrics(labels, preds) # top-3    return {'pc_top3': pc_top3, 'rc_top3': rc_top3, 'f1c_top3': f1c_top3,            'po_top3': po_top3, 'ro_top3': ro_top3, 'f1o_top3': f1o_top3, 'mAP': mAP}# top-3排序的标签def calculate_top3_metrics(labels, preds):    no_examples = labels.shape[0]    top3 = np.zeros_like(preds)    for ind_example in range(no_examples):        top_pred_inds = np.argsort(preds[ind_example])[::-1]        for k in range(3):            top3[ind_example, top_pred_inds[k]] = 1    pc_top3, rc_top3, f1c_top3 = prec_rec_f1(labels, top3)    po_top3, ro_top3, f1o_top3 = prec_rec_f1(labels.flatten(), top3.flatten())    return pc_top3, rc_top3, f1c_top3, po_top3, ro_top3, f1o_top3def prec_rec_f1(labels, pred_labels):    eps = np.finfo(np.float32).eps    tp = labels * pred_labels    if len(labels.shape) == 2:        no_tp = np.sum(tp, axis=1) + eps        no_pred = np.sum(pred_labels, axis=1) + eps        no_pos = np.sum(labels, axis=1) + eps    elif len(labels.shape) == 1:        no_tp = np.sum(tp) + eps        no_pred = np.sum(pred_labels) + eps        no_pos = np.sum(labels) + eps    prec_class = no_tp / no_pred + eps    rec_class = no_tp / no_pos + eps    f1_class = 2 * prec_class * rec_class / (prec_class + rec_class)    return 100 * np.mean(prec_class), 100 * np.mean(rec_class), 100 * np.mean(f1_class)# 计算mAPdef calculate_mAP(labels, preds):    no_examples = labels.shape[0]    no_classes = labels.shape[1]    ap_scores = np.empty((no_classes), dtype=np.float)    for ind_class in range(no_classes):        ground_truth = labels[:, ind_class]        out = preds[:, ind_class]        sorted_inds = np.argsort(out)[::-1] # in descending order        tp = ground_truth[sorted_inds]        fp = 1 - ground_truth[sorted_inds]        tp = np.cumsum(tp)        fp = np.cumsum(fp)        rec = tp / np.sum(ground_truth)        prec = tp / (fp + tp)        rec = np.insert(rec, 0, 0)        rec = np.append(rec, 1)        prec = np.insert(prec, 0, 0)        prec = np.append(prec, 0)        for ind in range(no_examples, -1, -1):            prec[ind] = max(prec[ind], prec[ind + 1])        inds = np.where(rec[1:] != rec[:-1])[0] + 1        ap_scores[ind_class] = np.sum((rec[inds] - rec[inds - 1]) * prec[inds])    return 100 * np.mean(ap_scores)

转载地址:http://bjfxf.baihongyu.com/

你可能感兴趣的文章
VB.NET中实现IEnumerator接口
查看>>
[C++]C++的函数重载
查看>>
用Visio绘制switch-case流程图
查看>>
中国各省的简称及简称的由来
查看>>
WINDOWS 文件夹内容
查看>>
i386和x86的区别
查看>>
日语输入中的促音怎么输入
查看>>
虚拟CentOS访问Windows下共享文件(一)
查看>>
[C++]构造函数与析构函数讲解
查看>>
虚拟CentOS访问Windows下共享文件(二)
查看>>
怎样在拼打日语汉字时,在字上同时显示假名
查看>>
虚拟CentOS访问Windows下共享文件(三)
查看>>
外语学习的真实方法及误区_读后感——方法、愿望、自律
查看>>
日文つ的浊音怎样打
查看>>
键盘常用ASCII码
查看>>
英文标点符号的使用
查看>>
G10通常参数设置
查看>>
网上超强摘录
查看>>
载噪比C/N和信噪比S/N
查看>>
了凡四训
查看>>