IoU (Intersection over Union)

IoU 是一个简单的测量标准,只要是在输出中得出一个预测范围(bounding boxex)的任务都可以用 IoU 来进行测量。为了可以使 IoU 用于测量任意大小形状的物体检测,我们需要:

  1. ground-truth bounding boxes(人为在训练集图像中标出要检测物体的大概范围);
  2. 我们的算法得出的结果范围。

$$IoU=\frac {Area\ of\ Overlap}{Area\ of\ Union}$$

一般来说,这个 score > 0.5 可以被认为一个不错的结果

python 实现

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

检测物体轮廓不一定非得是方框,也可以是沿着物体的边线,在实际的任务中,根据不同的任务要求来写不同具体实现的检测方法,但说白了其实都是IoU或者IU。

另外 mean IU 指的是不同类别识别准确度的平均值,比如一幅图中要识别三个物体,mean IU 就是三个物体分别准确度加起来的平均值。


mAP (Mean Average Precision)

  1. 对于某个类别C,在某一张图片上首先计算C在一张图片上的 \(Precision=\frac {在一张图片上类别C识别正确的个数(也就是IoU>0.5)}{一张图片上类别C的总个数}\)

$$Precision_C\ =\ \frac {N(TruePositives)_C}{N(TotalObjects)_C}$$

  1. 依然对于某个类别C,可能在多张图片上有该类别,下面计算类别C的AP指数: \(AP\ =\ \frac {每张图片上的Precision求和}{含有类别C的图片数目}\)

$$AveragePrecision_C\ =\ \frac {\sum Precision_C}{N(TotalImages)_C}$$

  1. 对于整个数据集,存在多个类别C1、C2、C3: \(mAP\ =\ \frac {上一步计算的所有类别的AP和}{总的类别数目相当于所有类别的AP的平均值}\)

$$MeanAveragePrecision\ =\ \frac {\sum AveragePrecision_C}{N(Classes)}$$

参考

  1. What is mAP ? Understanding the statistic of choice for comparing Object Detection models
  2. 目标检测中的mAP是什么含义?
  3. 深度学习中IU、IoU(Intersection over Union)的概念理解以及python程序实现