简体   繁体   English

True positive, False positive, False Negative 计算数据帧 python

[英]True positive, False positive, False Negative calculation data frame python

I have trained an object detection network.我训练了一个 object 检测网络。 I have a ground truth annotation CSV in the format, filename, height, width, xmin, ymin, xmax, ymax, class .我有一个基本事实注释 CSV 格式为, filename, height, width, xmin, ymin, xmax, ymax, class When I score an input image, I get the output CSV in this format: filename, xmin, ymin, xmax, ymax, class, confidence .当我对输入图像进行评分时,我得到 output CSV 的格式如下: filename, xmin, ymin, xmax, ymax, class, confidence

I need to merge these data frames considering the IoU .考虑到IoU ,我需要合并这些数据框。 SO basically the output dataframe should contain,所以基本上 output dataframe 应该包含,

  1. The ground truth and its corresponding prediction along with IoU value if a match is found如果找到匹配项,则基本事实及其相应的预测以及IoU
  2. The ground truth values and Nan Prediction values if IoU match was not found未找到IoU匹配时的地面实况值和 Nan 预测值
  3. Nan ground truth values and prediction values if IoU match was not found.如果没有找到IoU匹配,Nan 地面真值和预测值。

This will be an intermediate step for calculating Precision and recall values.这将是计算精度和召回值的中间步骤。

I am just adding a very small sample dataframe as an example here, which tests for these conditions.我只是在这里添加一个非常小的样本 dataframe 作为示例,用于测试这些条件。

sample prediction:样本预测:

        filename  xmin  ymin  xmax  ymax class  confidence
0  dummyfile.jpg  4060  2060  4214  2242    DR    0.999985
1  dummyfile.jpg  3599  1282  3732  1456    DR    0.999900

sample ground truth:样本基本事实:

        filename  width  height class  xmin  xmax  ymin  ymax
0  dummyfile.jpg   7201    5400    DR  3598  3728  1279  1451
1  dummyfile.jpg   7201    5400    DR  3916  4038  2186  2274

Expected final output:预期的最终 output:

最终输出

I am adding my current approach as an answer.我正在添加我目前的方法作为答案。 Is there any better way to achieve this?有没有更好的方法来实现这一目标? The data can be pretty large.数据可能非常大。

This is one approach I found,这是我发现的一种方法,

  1. Defining IoU function:定义 IoU function:
import pandas as pd
import numpy as np
import os

def IOU(df):
    '''funtion to calulcate IOU within rows of dataframe'''
    # determining the minimum and maximum -coordinates of the intersection rectangle
    xmin_inter = max(df.xmin, df.xmin_pred)
    ymin_inter = max(df.ymin, df.ymin_pred)
    xmax_inter = min(df.xmax, df.xmax_pred)
    ymax_inter = min(df.ymax, df.ymax_pred)

    # calculate area of intersection rectangle
    inter_area = max(0, xmax_inter - xmin_inter + 1) * max(0, ymax_inter - ymin_inter + 1)

    # calculate area of actual and predicted boxes
    actual_area = (df.xmax - df.xmin + 1) * (df.ymax - df.ymin + 1)
    pred_area = (df.xmax_pred - df.xmin_pred + 1) * (df.ymax_pred - df.ymin_pred+ 1)

    # computing intersection over union
    iou = inter_area / float(actual_area + pred_area - inter_area)

    # return the intersection over union value
    return iou
  1. reading ground truth and prediction CSV阅读地面实况和预测 CSV
ground_truth=pd.read_csv("sample_gt.csv")
prediction=pd.read_csv('sample_preds.csv')

###renaming prediction df columns with _pred suffix
pred_cols=prediction.columns.tolist()
pred_cols.remove('filename')
new_cols=[col+'_pred' for col in pred_cols ]
new_col_dict=dict(zip(pred_cols,new_cols))
prediction.rename(columns=new_col_dict,inplace=True)
  1. Outer join ground truth and prediction外连接地面实况和预测
###outer joining the prediciton and ground truth df
newdf=pd.merge(prediction,ground_truth,'outer',on='filename')

###applying iou calculation
newdf['iou']= newdf.apply(IOU, axis = 1)
###filtering all iou=0
newdf=newdf[newdf['iou']>0]

借据匹配

  1. getting non-match values from ground truth and prediction.从地面实况和预测中获取不匹配值。
final_df=pd.merge(prediction,newdf,on=prediction.columns.tolist(),how='left')
final_df=pd.merge(final_df,ground_truth,on=ground_truth.columns.tolist(),how='outer')

最终输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 哪个是哪个? (真阳性、真阴性、假阳性、假阴性) - Which one is which ? (True Positive, True Negative, False Positive, False Negative) 有没有办法用已知的真阳性、真阴性、假阳性和假阴性来绘制混淆矩阵? - Is there a way to draw confusion matrix with known True Positive, True Negative, False Positive and False Negative? Scikit-learn:如何获取True Positive、True Negative、False Positive和False Negative - Scikit-learn: How to obtain True Positive, True Negative, False Positive and False Negative 二元分类器只进行真阴性和假阳性预测 - Binary classifier making only making true negative and false positive predictions 从混淆矩阵中获取假阴性、假阳性、真阳性和真阴性的相关数据集 - Getting relevant datasets of false negatives, false positives, true positive and true negative from confusion matrix 自定义 TensorFlow 指标:给定假阳性率下的真阳性率 - Custom TensorFlow metric: true positive rate at given false positive rate filecmp.cmp()什么时候返回假阳性或假阴性? - When will filecmp.cmp() return a false positive or false negative? 如何计算误报率 (FPR) 和误报率百分比? - How to compute false positive rate (FPR) and False negative rate percantage? Python检查数据中的正数/负数 - Python Check Data for Positive/Negative Numbers 如何找到plot ROC曲线的真阳性率和假阳性率? - How to find true positive rate and false positive rate to plot ROC curve?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM