在对象检测中,IOU(并集上的交点)是介于0和1之间的值,表示在特定图像中绘制在对象上的两个框之间的重叠百分比
为了帮助您理解这是什么,这里有一个示例:
红色框是坐标x1(左上)、y1(左下)、x2(右上)、y2(右下)的实际值
紫色框是坐标x1_预测、y1_预测、x2_预测、y2_预测的预测值
黄色阴影的正方形是iou,如果其值大于某个阈值(按惯例为0.5),则预测结果为真,否则为假
以下是计算两个框的IOU的代码:
def get_iou(box_true, box_predicted):
x1, y1, x2, y2 = box_true
x1p, y1p, x2p, y2p = box_predicted
if not all([x2 > x1, y2 > y1, x2p > x1p, y2p > y1p]):
return 0
far_x = np.min([x2, x2p])
near_x = np.max([x1, x1p])
far_y = np.min([y2, y2p])
near_y = np.max([y1, y1p])
inter_area = (far_x - near_x + 1) * (far_y - near_y + 1)
true_box_area = (x2 - x1 + 1) * (y2 - y1 + 1)
pred_box_area = (x2p - x1p + 1) * (y2p - y1p + 1)
iou = inter_area / (true_box_area + pred_box_area - inter_area)
return iou
我有2个csv文件中包含的预测和实际数据,我将其读入2个DataFrame对象并从那里开始
对于每个图像,我提取特定对象类型(例如:car)的检测和实际数据,下面是一幅图像(Beverly_hills1.png)中1个对象(car)的示例
Actual:
Image Path Object Name X_min Y_min X_max Y_max
3842 Beverly_hills1.png Car 760 432 911 550
3843 Beverly_hills1.png Car 612 427 732 526
3844 Beverly_hills1.png Car 462 412 597 526
3845 Beverly_hills1.png Car 371 432 544 568
Detections:
image object_name x1 y1 x2 y2
594 Beverly_hills1.png Car 612 422 737 539
595 Beverly_hills1.png Car 383 414 560 583
以下是我的比较:
def calculate_overlaps(self, detections, actual):
calculations = []
detection_groups = detections.groupby('image')
actual_groups = actual.groupby('Image Path')
for item1, item2 in zip(actual_groups, detection_groups):
for detected_index, detected_row in item2[1].iterrows():
detected_coordinates = detected_row.values[2: 6]
detected_overlaps = []
coords = []
for actual_index, actual_row in item1[1].iterrows():
actual_coordinates = actual_row.values[4: 8]
detected_overlaps.append((
self.get_iou(actual_coordinates, detected_coordinates)))
coords.append(actual_coordinates)
detected_row['max_iou'] = max(detected_overlaps)
x1, y1, x2, y2 = coords[int(np.argmax(detected_overlaps))]
for match, value in zip([f'{item}_match'
for item in ['x1', 'y1', 'x2', 'y2']],
[x1, y1, x2, y2]):
detected_row[match] = value
calculations.append(detected_row)
return pd.DataFrame(calculations)
对于每种对象类型,这都将运行,这是低效的
最终结果如下所示:
image object_name x1 ... y1_match x2_match y2_match
594 Beverly_hills1.png Car 612 ... 427 732 526
595 Beverly_hills1.png Car 383 ... 432 544 568
1901 Beverly_hills10.png Car 785 ... 432 940 578
2015 Beverly_hills101.png Car 832 ... 483 1240 579
2708 Beverly_hills103.png Car 376 ... 466 1333 741
... ... ... ... ... ... ... ...
618 Beverly_hills93.png Car 922 ... 406 851 659
625 Beverly_hills93.png Car 1002 ... 406 851 659
1081 Beverly_hills94.png Car 398 ... 426 527 559
1745 Beverly_hills95.png Car 1159 ... 438 470 454
1746 Beverly_hills95.png Car 765 ... 441 772 474
[584 rows x 14 columns]
如何对此进行简化/矢量化并消除for循环?这可以用np.where()
来完成吗
首先,我注意到
get_iou
函数有一个条件:x2 > x1, y2 > y1, x2p > x1p, y2p > y1p
。您应该确保该条件适用于两个数据帧其次,
actual
有Image Path
和Object Name
列,而detections
有image
和object_name
。您可能希望将相应的列更改为一个名称这就是说,下面是我对
merge
的解决方案:相关问题 更多 >
编程相关推荐