计算CP、CR、CF1、OP、OR、OF1和mAP的top-3评价指标
发布日期:2021-07-01 04:36:55
浏览次数:2
分类:技术文章
本文共 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)
转载地址:https://mymuli.blog.csdn.net/article/details/100829491 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月15日 02时48分59秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
CodeForces - 456C Boredom (dp)
2019-04-30
ACM 2013 长沙区域赛 Collision (几何)
2019-04-30
ACM 2014 鞍山区域赛 E - Hatsune Miku (dp)
2019-04-30
反向传播&梯度下降 的直观理解程序(numpy)
2019-04-30
ACM 2017 北京区域赛 J-Pangu and Stones(区间dp)
2019-04-30
java常用类 String面试题
2019-04-30
四线触摸屏原理
2019-04-30
腾讯AI语音识别API踩坑记录
2019-04-30
java.net.BindException: 无法指定被请求的地址
2019-05-01
svn服务器安装
2019-05-01
spark 笔记1
2019-05-01
shell dirname basename
2019-05-01
未来已至,5G加持下的云游戏将走向何方?
2019-05-01
计算机网络 —— 网络层 1.
2019-05-01
Android 之 ContentProvider 与 ContentResolver
2019-05-01
【接口自动化】
2019-05-01
Python解惑之:True与False
2019-05-01
你要的微信小程序终于来了
2019-05-01
有了这些 Chrome 插件,效率提升10倍(建议收藏)
2019-05-01
只有1%的程序员搞懂过浮点数陷阱
2019-05-01