IoU(Intersection over Union)

Introduction

在目标检测中,我们需要定位出目标的位置,而我们的算法不可能百分百跟人工标注的数据完全匹配,因此需要一种衡量目标定位精度的标准。

IoU(Intersection over Union)是一种常见的用于衡量目标定位精度的标准,可以理解为重叠度,是一种简单的测量标准,只要是在输出中得出一个预测范围(bounding box)的任务都可以用IoU来进行测量。

1

如上图所示,ground-truth和predicted的存在误差,绿色框是人为标记的正确结果,红色框是算法预测出来的结果,IoU要做的就是在这两个结果中测量算法的准确度,它定义了两个bounding box的重叠度 ,如下图所示:

2

$$IoU=\frac{A\bigcap B}{A\bigcup B}=\frac{A\bigcap B}{A+B-A\bigcap B}$$

就是矩形框A、B的重叠面积,占$A\bigcup B$的编辑的比例。

一般来说,IoU>0.5 就可以被认为一个不错的结果,IoU>0.7 结果就非常不错了,可以参见下图

3

Implementation

因为IoU的思想很简单,Python实现可以参考[1]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def bb_intersection_over_union(boxA, boxB):
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])

# compute the area of intersection rectangle
interArea = (xB - xA + 1) * (yB - yA + 1)

# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)

# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)

# return the intersection over union value
return iou

Reference

[1] Intersection over Union (IoU) for object detection


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!