大家好,欢迎来到IT知识分享网。
IoU
1.普通IoU
1.1 简介
1.2 不足
- 若两个框之间没有相交, 则交集为0, 无法反映出在二者没有重叠的情况下距离多少; 且loss为0时,无法进行梯度回传,无法进行学习训练。
- 当预测框和真实框的交并比相同时,虽然预测框的位置不同,但IoU值仍相同,无法反映出那种预测框更为准确。
代码
# IoU import numpy as np def IoU(box1, box2): b1_x1, b1_y1, b1_x2, b1_y2 = box1 b2_x1, b2_y1, b2_x2, b2_y2 = box2 # 找到两个框中左上角坐标的最大值以及右下角坐标的最小值 xx1 = np.maximum(b1_x1, b2_x1) yy1 = np.maximum(b1_y1, b2_y1) xx2 = np.minimum(b1_x2, b2_x2) yy2 = np.minimum(b1_y2, b2_y2) w = np.maximum(0.0, yy2 - yy1) h = np.maximum(0.0, xx2 - xx1) inter = w * h # 交集面积 IoU = inter/((b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter) # 全部-交集=并集 print("IoU: ", IoU) if __name__ == "__main__": box1 = np.array([100, 100, 210, 210]) box2 = np.array([150, 150, 230, 220]) IoU(box1, box2)
2.GIoU(Generalized-IoU)
2.1 简介
引入最小封闭矩形,在两个矩形框无交集的情况下计算距离远近, 产生loss。具体来说,使用一个最小外接矩形 C C C包裹住两个矩形框,然后计算外接矩形减去两个矩形框的并集得到的空余面积,然后使用空余面积除以最小外接矩形面积。
G I o U = I o U − ∣ C − ( A ∩ B ) ∣ ∣ C ∣ − 1 ≤ G I o U ≤ 1 GIoU = IoU – \frac{|C – (A∩B)|}{|C|} \quad -1≤GIoU≤1 GIoU=IoU−∣C∣∣C−(A∩B)∣−1≤GIoU≤1
L G I o U = 1 − G I o U 0 ≤ L G I o U ≤ 2 L_{GIoU} = 1 – GIoU \quad 0≤L_{GIoU}≤2 LGIoU=1−GIoU0≤LGIoU≤2
2.2 不足
- 对于每个预测框都计算其与真实目标框之间的最小外接矩形,计算及收敛速度收到限制。
- 当某个矩形框完全包裹另外一个矩形框,无法反应出被包围的矩形框在另一个矩形框中的具体位置。
2.3 代码
# GIoU(同样的相交状况, GIoU的值为负值) import numpy as np def GIoU(box1, box2): b1_x1, b1_y1, b1_x2, b1_y2 = box1 b2_x1, b2_y1, b2_x2, b2_y2 = box2 # 获取IoU xx1 = np.maximum(b1_x1, b2_x1) yy1 = np.maximum(b1_y1, b2_y1) xx2 = np.minimum(b1_x2, b2_x2) yy2 = np.minimum(b1_y2, b2_y2) w = np.maximum(0.0, yy2 - yy1) h = np.maximum(0.0, xx2 - xx1) inter = w * h # 交集面积 IoU = inter/((b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter) # 计算最小外接矩形 min_xx1 = np.minimum(b1_x1, b2_x1) min_yy1 = np.minimum(b1_y1, b2_y1) max_xx2 = np.maximum(b1_x2, b2_x2) max_yy2 = np.maximum(b1_y2, b2_y2) C_w = np.maximum(0.0, max_yy2 - min_yy1) C_h = np.maximum(0.0, max_xx2 - min_xx1) C_inter = C_w * C_h GIoU = IoU - np.abs((C_inter - inter)/C_inter) print(GIoU) if __name__ == "__main__": box1 = np.array([100, 100, 210, 210]) box2 = np.array([150, 150, 230, 220]) GIoU(box1, box2)
3.DIoU(Distance-IoU)
3.1 简介(快速收敛+回归准确)
D I o U = I o U − ρ 2 ( A c t r , B c t r ) c 2 = I o U − d 2 c 2 − 1 ≤ D I o U ≤ 1 DIoU = IoU – \frac{\rho^2(A_{ctr},B_{ctr})}{c^2} = IoU – \frac{d^2}{c^2} \quad -1≤DIoU≤1 DIoU=IoU−c2ρ2(Actr,Bctr)=IoU−c2d2−1≤DIoU≤1
其中 d = ρ ( A c t r , B c t r ) d=\rho(A_{ctr},B_{ctr}) d=ρ(Actr,Bctr)是A框与B框中心点坐标的欧式距离,而 c c c则是包裹它们的最小方框的对角线距离。
L D I o U = 1 − D I o U 0 ≤ L D I o U ≤ 2 L_{DIoU} = 1-DIoU \quad 0≤L_{DIoU}≤2 LDIoU=1−DIoU0≤LDIoU≤2
3.2不足
DIoU考虑了重叠面积和中心点距离,当目标框包裹预测框的时候,直接度量2个框的距离,因此DIoU收敛的更快,但并没有考虑到长宽比。
3.3 代码
# DIoU(同样的相交状况, DIoU的值更小) import numpy as np def GIoU(box1, box2): b1_x1, b1_y1, b1_x2, b1_y2 = box1 b2_x1, b2_y1, b2_x2, b2_y2 = box2 # 获取IoU xx1 = np.maximum(b1_x1, b2_x1) yy1 = np.maximum(b1_y1, b2_y1) xx2 = np.minimum(b1_x2, b2_x2) yy2 = np.minimum(b1_y2, b2_y2) w = np.maximum(0.0, yy2 - yy1) h = np.maximum(0.0, xx2 - xx1) inter = w * h # 交集面积 IoU = inter/((b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter) # 计算最小外接矩形 min_xx1 = np.minimum(b1_x1, b2_x1) min_yy1 = np.minimum(b1_y1, b2_y1) max_xx2 = np.maximum(b1_x2, b2_x2) max_yy2 = np.maximum(b1_y2, b2_y2) C_w = np.maximum(0.0, max_yy2 - min_yy1) C_h = np.maximum(0.0, max_xx2 - min_xx1) C_inter = C_w * C_h # 分别计算两个中心点的目标框 A_center_x = (b1_x1 + b1_x2) / 2 A_center_y = (b1_y1 + b1_y2) / 2 B_center_x = (b2_x1 + b2_x2) / 2 B_center_y = (b2_y1 + b2_y2) / 2 # 对于目标框中心点欧式距离,先找中心点再计算; 对于整个外接矩形的对角线直接计算即可 center_distance = (B_center_x - A_center_x) 2 + (B_center_y - A_center_y) 2 C_center_distance = (max_yy2 - min_yy1) 2 + (max_xx2 - min_xx1) 2 DIoU = IoU - center_distance / C_center_distance print(DIoU) if __name__ == "__main__": box1 = np.array([100, 100, 210, 210]) box2 = np.array([150, 150, 230, 220]) GIoU(box1, box2)
4.CIoU
4.1 简介
CIoU就是在DIoU的基础上增加了检测框尺度的loss,增加了长和宽的loss,使得目标框回归更加稳定,不会像IoU和GIoU一样出现训练过程中发散等问题。
v = 4 π 2 ( a r c t a n w g t h g t − a r c t a n w h ) α = v ( 1 − I o U ) + v v=\frac{4}{\pi^2}(arctan\frac{w^{gt}}{h^{gt}}-arctan\frac{w}{h}) \quad \alpha=\frac{v}{(1-IoU)+v} v=π24(arctanhgtwgt−arctanhw)α=(1−IoU)+vv
C I o U = I o U − ( ρ 2 ( A c t r , B c t r ) c 2 + α v ) CIoU=IoU – (\frac{\rho^2(A_{ctr},B_{ctr})}{c^2}+\alpha v) CIoU=IoU−(c2ρ2(Actr,Bctr)+αv)
L C I o U = 1 − C I o U L_{CIoU}=1-CIoU LCIoU=1−CIoU
其中,A和B代表两个矩形框, A c t r , B c t r A_{ctr},B_{ctr} Actr,Bctr代表A和B的中心点。
4.2 代码
def CIoU(box1, box2): b1_x1, b1_y1, b1_x2, b1_y2 = box1 b2_x1, b2_y1, b2_x2, b2_y2 = box2 # IOU xx1 = np.maximum(b1_x1, b2_x1) yy1 = np.maximum(b1_y1, b2_y1) xx2 = np.minimum(b1_x2, b2_x2) yy2 = np.minimum(b1_y2, b2_y2) inter_w = np.maximum(0.0, xx2 - xx1) inter_h = np.maximum(0.0, yy2 - yy1) inter = inter_w*inter_h Union = (b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter IOU = inter/Union # 计算最小外接矩形 min_xx1 = np.minimum(b1_x1, b2_x1) min_yy1 = np.minimum(b1_y1, b2_y1) max_xx2 = np.maximum(b1_x2, b2_x2) max_yy2 = np.maximum(b1_y2, b2_y2) C_w = np.maximum(0.0, max_yy2 - min_yy1) C_h = np.maximum(0.0, max_xx2 - min_xx1) C_inter = C_w * C_h # 分别计算两个中心点的目标框 A_center_x = (b1_x1 + b1_x2) / 2 A_center_y = (b1_y1 + b1_y2) / 2 B_center_x = (b2_x1 + b2_x2) / 2 B_center_y = (b2_y1 + b2_y2) / 2 # 对于目标框中心点欧式距离,先找中心点再计算; 对于整个外接矩形的对角线直接计算即可 center_distance = (B_center_x - A_center_x) 2 + (B_center_y - A_center_y) 2 C_center_distance = (max_yy2 - min_yy1) 2 + (max_xx2 - min_xx1) 2 DIoU = IOU - center_distance / C_center_distance print(DIoU) # aspect ratio A_w = b1_y2 - b1_y1 A_h = b1_x2 - b1_x1 B_w = b2_y2 - b2_y1 B_h = b2_x2 - b2_x1 v = (4/(np.pi)2)*(np.arctan(B_w/B_h) - np.arctan(A_w/A_h))2 alpha = v/((1-IOU) + v) CIOU = DIoU - alpha*v print("CIOU:", CIOU) if __name__ == "__main__": box1 = np.array([100, 100, 210, 210]) box2 = np.array([150, 150, 230, 220]) CIoU(box1, box2)
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/127406.html