Это один из подходов, который я нашел,
- Определение функции 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
чтение достоверных данных и прогнозов 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)
Внешнее соединение наземной истины и предсказания
###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]
- получение несоответствующих значений из наземной истинности и предсказания.
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')
окончательный результат