IOU
$IOU$又名交并比,简单来说就是你网络给出的预测框与真实框的一个交集。

如上图所示,红色部分是我们的真实框也就是标注数据集的时候你自己画出来的,而绿的框则是网络预测出来的。他们之间的交集除以他们的并集就是$IOU$。
这里给出$IOU$的定义:
$$
IOU=\frac {|A\cap B |}{|A\cup B |}
$$
这里给出一个别人已经画好的图

对于这张图来说$IOU$要如何计算呢
$$
IOU=\frac {绿色}{黄色+蓝色-绿色}\
=\frac{(C_x-B_x)·(C_y-B_y)}{(C_x-A_x)·(C_y-A_y)+(D_x-B_x)·(D_y-D_y)-(C_x-B_x)·(C_y-B_y)}
$$
接下来给出代码实现
1 | def IoU(box1, box2): |
$IOU$的两个缺点:
(1).当预测框和真实框的不存在交集的时候,如下图所示.如何使用$IOU$进行计算的话,其定位损失就为0,导致梯度为0无法进行优化。

(2).IoU无法精确的反映两者的重合度大小。如下图所示,三种情况IoU都相等,但看得出来他们的重合度是不一样的,左边的图回归的效果最好,右边的最差。

GIOU
$GIOU$的提出解决了$IOU$的第一个缺点,给出$GIOU$的定义
$$
GIOU=IOU-\frac {C-|A\cap B |}{C}
$$
$C$为这两个框的最小外接矩形
下面还用刚刚那个例子来进行计算

$$
GIOU=IOU-\frac {红框-(黄色+蓝色-绿色)}{红框}
$$
给出$GIOU$的代码
1 | def GIoU(box1, box2): |
$GIOU$的缺点:当两个框处于下图状态的时候$GIOU$就退化成了$IOU$。

DIOU
$DIOU$在$GIOU$加上了预测框与真实框之间的距离,给出他的定义:
$$
DIOU=IOU-\frac {ρ^2·(b,b^gt)}{c^2}
$$
其中$b$,$b^gt$分别代表两个框的中心点,$ρ$代表两个中心点之间的欧氏距离,$C$代表最小包庇矩形的对角线,即如下图所示:

$DIOU$实现的代码
1 | def DIoU(box1, box2): |
CIOU
在$GIOU$的基础之上加入了形状的相似度:
$$
CIOU=IOU-\frac {ρ^2·(b,b^gt)}{c^2}-\alpha\upsilon
$$
其中$\alpha$是权重函数,而$\upsilon$用来度量长宽比的相似性:
$$
\upsilon= \frac{4}{\pi^{2}} *\left(\arctan \frac{w^{g t}}{h^{g t}}-\arctan \frac{w}{h}\right)^2
$$
$$
\alpha= \frac{\upsilon}{(1-IOU)+\upsilon}
$$
在使用$CIOU$作为Loss的时候,$\upsilon$的梯度同样会参与反向传播的计算,其中:
$$
\begin{aligned}
\frac{\partial v}{\partial w} & =2 * \frac{4}{\pi^{2}} *\left(\arctan \frac{w^{g t}}{h^{g t}}-\arctan \frac{w}{h}\right) *(-1) * \frac{1}{1+\left(\frac{w}{h}\right)^{2}} * \frac{1}{h} \
& =\frac{8}{\pi^{2}} *\left(\arctan \frac{w^{g t}}{h^{g t}}-\arctan \frac{w}{h}\right) *(-1) * \frac{h^{2}}{w^{2}+h^{2}} * \frac{1}{h} \
& =-\frac{8}{\pi^{2}} *\left(\arctan \frac{w^{g t}}{h^{g t}}-\arctan \frac{w}{h}\right) * \frac{h}{w^{2}+h^{2}}
\end{aligned}
$$
$$
\begin{aligned}
\frac{\partial v}{\partial h} & =2 * \frac{4}{\pi^{2}} *\left(\arctan \frac{w^{g t}}{h^{g t}}-\arctan \frac{w}{h}\right) *(-1) * \frac{1}{1+\left(\frac{w}{h}\right)^{2}} * w *(-1) * h^{-2} \
& =\frac{8}{\pi^{2}} *\left(\arctan \frac{w^{g t}}{h^{g t}}-\arctan \frac{w}{h}\right) * \frac{h^{2}}{w^{2}+h^{2}} * w * h^{-2} \
& =\frac{8}{\pi^{2}} *\left(\arctan \frac{w^{g t}}{h^{g t}}-\arctan \frac{w}{h}\right) * \frac{w}{w^{2}+h^{2}}
\end{aligned}
$$
1 | def CIoU(box1, box2): |