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

True positive, False positive, False Negative calculation data frame python

我训练了一个对象检测网络。我有一个格式为地面实况注释 CSV, filename, height, width, xmin, ymin, xmax, ymax, class。当我对输入图像进行评分时,我得到了这种格式的输出 CSV:filename, xmin, ymin, xmax, ymax, class, confidence.

考虑到 IoU,我需要合并这些数据帧。所以基本上输出数据框应该包含,

  1. 真值及其相应的预测以及 IoU 值(如果找到匹配项)
  2. 如果未找到IoU 匹配
  3. ,则为真值和 Nan 预测值
  4. 如果未找到 IoU 匹配项,则南真值和预测值。

这将是计算精度和召回值的中间步骤。

我只是在这里添加一个非常小的示例数据框作为示例,它测试这些条件。

样本预测:

        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

样本基本事实:

        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

预期最终输出:

我正在添加我当前的方法作为答案。有没有更好的方法来实现这一目标?数据可能会很大。

这是我发现的一种方法,

  1. 定义 IoU 函数:
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. 读取基本事实和预测 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 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. 从基本事实和预测中获取不匹配值。
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')