准备机器学习的训练数据第一步——计算\text{IoU}
从这里开始我们准备机器学习用的训练数据。
我的最终目标是创建一个能够判断图像是否是蝾螈的脸的判别器。因此,我们需要蝾螈的脸部图像和非蝾螈脸部的图像。我们需要编写程序来准备这样的图像。
为此,有必要从单个图像中用矩形框出蝾螈头部(即Ground-truth),如果随机切割的矩形与Ground-truth在一定程度上重合,那么这个矩形框处就是蝾螈的头。
重合程度通过检测评价函数\text{IoU}(Intersection over Union)来判断。通过下式进行计算:
\text{IoU}=\frac{|\text{Rol}|}{|R_1 + R_2 – \text{Rol}|}
其中:
- $$R_1$$:Ground-truth的范围;
- $$R_2$$:随机框出来的矩形的范围;
- $$\text{Rol}$$:$$R_1$$和$$R_2$$重合的范围。
计算以下两个矩形的\text{IoU}吧!
# [x1, y1, x2, y2] x1,y1...矩形左上的坐标 x2,y2...矩形右下的坐标
a = np.array((50, 50, 150, 150), dtype=np.float32)
b = np.array((60, 60, 170, 160), dtype=np.float32)
答案
0.627907
python实现:
import numpy as np
# get IoU overlap ratio
def iou(a, b):
# get area of a
area_a = (a[2] - a[0]) * (a[3] - a[1])
# get area of b
area_b = (b[2] - b[0]) * (b[3] - b[1])
# get left top x of IoU
iou_x1 = np.maximum(a[0], b[0])
# get left top y of IoU
iou_y1 = np.maximum(a[1], b[1])
# get right bottom of IoU
iou_x2 = np.minimum(a[2], b[2])
# get right bottom of IoU
iou_y2 = np.minimum(a[3], b[3])
# get width of IoU
iou_w = iou_x2 - iou_x1
# get height of IoU
iou_h = iou_y2 - iou_y1
# get area of IoU
area_iou = iou_w * iou_h
# get overlap ratio between IoU and all area
iou = area_iou / (area_a + area_b - area_iou)
return iou
# [x1, y1, x2, y2]
a = np.array((50, 50, 150, 150), dtype=np.float32)
b = np.array((60, 60, 170, 160), dtype=np.float32)
print(iou(a, b))