纯小白在进行yolov5改进时,纯看各iou的公式有点费解,结合代码以及实例来进行学习,因为实在实力不够,代码冗长重复,代码摘抄于YOLOv5官方7.0版本,并在其基础上添加了Alpha-IOU、EIOU、SIOU和WIOU(后续添加)损失函数。
参考了博客:IOU系列:IOU、GIOU、DIOU、CIOU、SIOU、Alpha-IoU、WIOU详解-CSDN博客
一、GIOU:
附上GIOU公式:
代码如下,以box1和box2来作为实例来理解,直接放到jupyternotebook里跑就ok:
import numpy as np
import torch
import math
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, EIoU=False, Focal=False, alpha=1, gamma=0.5, eps=1e-7):
# Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
# Get the coordinates of bounding boxes
if xywh: # transform from xywh to xyxy
(x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1) #把坐标信息分成四个块,方便后续对每个坐标进行操作
w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
print(b1_x1)
else: # x1, y1, x2, y2 = box1
b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
# Intersection area
inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \
(b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0)
print('Intersection:', inter)
# Union Area
union = w1 * h1 + w2 * h2 - inter + eps
print('Union:', union)
# IoU
# iou = inter / union # ori iou
iou = torch.pow(inter/(union + eps), alpha) # alpha iou
print('IoU:', iou)
if CIoU or DIoU or GIoU or EIoU or SIoU:
cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width
ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height
c_area = cw * ch + eps # convex area
return iou - torch.pow((c_area - union) / c_area + eps, alpha) # GIoU https://arxiv.org/pdf/1902.09630.pdf
return iou # IoU
if __name__ == "__main__":
# 将 NumPy 数组转换为 PyTorch 张量
box1_tensor = torch.tensor([0, 0, 100, 100], dtype=torch.float32)
box2_tensor = torch.tensor([50, 50, 150, 150], dtype=torch.float32)
# 调用 bbox_iou 方法,计算 GIoU
giou = bbox_iou(box1_tensor, box2_tensor,xywh=False, GIoU=True)
# 输出 GIoU 的返回值
print('GIoU: {:.4f}'.format(giou.item()))
跑出来结果如下,和自己手算结果相同:
Intersection: tensor([2500.])
Union: tensor([17500.])
IoU: tensor([0.1429])
GIoU: -0.0794
二、DIOU
附上DIOU公式:
代码如下:
import numpy as np
import torch
import math
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, EIoU=False, Focal=False, alpha=1, gamma=0.5, eps=1e-7):
# Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
# Get the coordinates of bounding boxes
if xywh: # transform from xywh to xyxy
(x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1) #把坐标信息分成四个块,方便后续对每个坐标进行操作
w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
print(b1_x1)
else: # x1, y1, x2, y2 = box1
b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
# Intersection area
inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \
(b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0)
print('Intersection:', inter)
# Union Area
union = w1 * h1 + w2 * h2 - inter + eps
print('Union:', union)
# IoU
# iou = inter / union # ori iou
iou = torch.pow(inter/(union + eps), alpha) # alpha iou
print('IoU:', iou)
if CIoU or DIoU or GIoU or EIoU or SIoU:
cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width;150
ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height;150
if CIoU or DIoU or EIoU or SIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal squared;45000
print('c2:',c2)
rho2 = (((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4) ** alpha # center dist ** 2;5000
print('rho2:',rho2)
return iou - rho2 / c2 # DIoU
return iou # IoU
if __name__ == "__main__":
# 将 NumPy 数组转换为 PyTorch 张量
box1_tensor = torch.tensor([0, 0, 100, 100], dtype=torch.float32)
box2_tensor = torch.tensor([50, 50, 150, 150], dtype=torch.float32)
# 调用 bbox_iou 方法,计算 DIoU
diou = bbox_iou(box1_tensor, box2_tensor,xywh=False, DIoU=True)
# 输出 DIoU 的返回值
print('DIoU: {:.4f}'.format(diou.item()))
跑出来结果如下:
Intersection: tensor([2500.])
Union: tensor([17500.])
IoU: tensor([0.1429])
c2: tensor([45000.])
rho2: tensor([5000.])
DIoU: 0.0317
三、CIOU
CIOU在DIOU的基础上添加了长宽比的惩罚项,附上CIOU公式:
附上代码:
import numpy as np
import torch
import math
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, EIoU=False, Focal=False, alpha=1, gamma=0.5, eps=1e-7):
# Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
# Get the coordinates of bounding boxes
if xywh: # transform from xywh to xyxy
(x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1) #把坐标信息分成四个块,方便后续对每个坐标进行操作
w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
print(b1_x1)
else: # x1, y1, x2, y2 = box1
b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
# Intersection area
inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \
(b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0)
print('Intersection:', inter)
# Union Area
union = w1 * h1 + w2 * h2 - inter + eps
print('Union:', union)
# IoU
# iou = inter / union # ori iou
iou = torch.pow(inter/(union + eps), alpha) # alpha iou
print('IoU:', iou)
if CIoU or DIoU or GIoU or EIoU or SIoU:
cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width;150
ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height;150
if CIoU or DIoU or EIoU or SIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal squared
print('c2:',c2)
rho2 = (((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4) ** alpha # center dist ** 2
print('rho2:',rho2)
if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
v = (4 / math.pi ** 2) * (torch.atan(w2 / h2) - torch.atan(w1 / h1)).pow(2)
print('v:',v)
with torch.no_grad(): #用于在其范围内禁用梯度计算,alpha_ciou与后续的梯度计算无关
alpha_ciou = v / (v - iou + (1 + eps))
print('alpha_ciou:',alpha_ciou)
return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)) # CIoU
return iou # IoU
if __name__ == "__main__":
# 将 NumPy 数组转换为 PyTorch 张量
box1_tensor = torch.tensor([0, 0, 100, 100], dtype=torch.float32)
box2_tensor = torch.tensor([50, 50, 150, 150], dtype=torch.float32)
# 调用 bbox_iou 方法,计算 CIoU
ciou = bbox_iou(box1_tensor, box2_tensor,xywh=False, CIoU=True)
# 输出 CIoU 的返回值
print('CIoU: {:.4f}'.format(ciou.item()))
跑出来结果如下:
Intersection: tensor([2500.])
Union: tensor([17500.])
IoU: tensor([0.1429])
c2: tensor([45000.])
rho2: tensor([5000.])
v: tensor([0.])
alpha_ciou: tensor([0.])
CIoU: 0.0317
四、EIOU
EIOU在CIOU的基础上将纵横比的影响拆开,分别计算长和宽各自的影响因子,并且加入了Focal来聚焦优质锚框,附上EIOU的公式:
附上Focal-EIOU的公式:
代码上因为原先的示例框长宽都一样,所以将box2变成150的正方形:
import numpy as np
import torch
import math
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, EIoU=False, Focal=False, alpha=1, gamma=0.5, eps=1e-7):
# Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
# Get the coordinates of bounding boxes
if xywh: # transform from xywh to xyxy
(x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1) #把坐标信息分成四个块,方便后续对每个坐标进行操作
w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
print(b1_x1)
else: # x1, y1, x2, y2 = box1
b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
# Intersection area
inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \
(b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0)
print('Intersection:', inter)
# Union Area
union = w1 * h1 + w2 * h2 - inter + eps
print('Union:', union)
# IoU
# iou = inter / union # ori iou
iou = torch.pow(inter/(union + eps), alpha) # alpha iou
print('IoU:', iou)
if CIoU or DIoU or GIoU or EIoU or SIoU:
cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width;200
ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height;200
if CIoU or DIoU or EIoU or SIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal squared
print('c2:',c2)
rho2 = (((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4) ** alpha # center dist ** 2
print('rho2:',rho2)
if EIoU:
rho_w2 = ((b2_x2 - b2_x1) - (b1_x2 - b1_x1)) ** 2
print('rho_w2',rho_w2)
rho_h2 = ((b2_y2 - b2_y1) - (b1_y2 - b1_y1)) ** 2
print('rho_h2',rho_h2)
cw2 = torch.pow(cw ** 2 + eps, alpha) #torch.pow是对cw**2进行alpha次方操作
print('cw2',cw2)
ch2 = torch.pow(ch ** 2 + eps, alpha)
if Focal:
return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2), torch.pow(inter/(union + eps), gamma) # Focal_EIou;gamma是对IoU进行gamma次方操作
else:
return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2) # EIoU
return iou
if __name__ == "__main__":
# 将 NumPy 数组转换为 PyTorch 张量
box1_tensor = torch.tensor([0, 0, 100, 100], dtype=torch.float32)
box2_tensor = torch.tensor([50, 50, 200, 200], dtype=torch.float32)
# 调用 bbox_iou 方法,计算 EIoU
eiou,focal_iou= bbox_iou(box1_tensor, box2_tensor,xywh=False, EIoU=True,Focal=True,gamma=0.5)
# 输出 EIoU 的返回值
print('EIou Loss: {:.4f}'.format(eiou.item()))
print('Focal IoU: {:.4f}'.format(focal_iou.item()))
输出结果为,目前有点不懂Focal IoU的输出,输出数值计算不太对,但是EIOU是对的:
Intersection: tensor([2500.])
Union: tensor([30000.])
IoU: tensor([0.0833])
c2: tensor([80000.])
rho2: tensor([11250.])
rho_w2 tensor([2500.])
rho_h2 tensor([2500.])
cw2 tensor([40000.])
EIou Loss: -0.1823
Focal IoU: 0.2887
五、SIOU
SIOU包涵4项损失函数,分别是Angle cost、Distance cost、Shape cost和IOU cost,具体参考博客:
附上SIOU代码:
import numpy as np
import torch
import math
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, EIoU=False, Focal=False, alpha=1, gamma=0.5, eps=1e-7):
# Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
# Get the coordinates of bounding boxes
if xywh: # transform from xywh to xyxy
(x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1) #把坐标信息分成四个块,方便后续对每个坐标进行操作
w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
print(b1_x1)
else: # x1, y1, x2, y2 = box1
b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
# Intersection area
inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \
(b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0)
print('Intersection:', inter)
# Union Area
union = w1 * h1 + w2 * h2 - inter + eps
print('Union:', union)
# IoU
# iou = inter / union # ori iou
iou = torch.pow(inter/(union + eps), alpha) # alpha iou
print('IoU:', iou)
if CIoU or DIoU or GIoU or EIoU or SIoU:
cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width;200
ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height;200
if CIoU or DIoU or EIoU or SIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal squared
print('c2:',c2)
rho2 = (((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4) ** alpha # center dist ** 2
print('rho2:',rho2)
if SIoU:
# SIoU Loss https://arxiv.org/pdf/2205.12740.pdf
s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5 + eps
s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5 + eps
sigma = torch.pow(s_cw ** 2 + s_ch ** 2, 0.5)
sin_alpha_1 = torch.abs(s_cw) / sigma
sin_alpha_2 = torch.abs(s_ch) / sigma
threshold = pow(2, 0.5) / 2 #计算阈值2分之根号2
sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1) #如果 sin_alpha_1 中的元素大于 threshold,则选取 sin_alpha_2 中对应位置的元素,否则选取 sin_alpha_1 中的元素
print('sin_alpha',sin_alpha) #alpha是45度
angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - math.pi / 2) #参考博客angle loss的损失函数cos(2alpha-pi/2)
print('angle_cost',angle_cost)
rho_x = (s_cw / cw) ** 2 #(75/200)**2
rho_y = (s_ch / ch) ** 2 #(75/200)**2
gamma = angle_cost - 2 #-1
distance_cost = 2 - torch.exp(gamma * rho_x) - torch.exp(gamma * rho_y)
print('distance_cost',distance_cost)
omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2)
omiga_h = torch.abs(h1 - h2) / torch.max(h1, h2)
shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4)
print('shape_cost',shape_cost)
return iou - torch.pow(0.5 * (distance_cost + shape_cost) + eps, alpha) # SIou
return iou - torch.pow((c_area - union) / c_area + eps, alpha) # GIoU https://arxiv.org/pdf/1902.09630.pdf
return iou # IoU
if __name__ == "__main__":
# 将 NumPy 数组转换为 PyTorch 张量
box1_tensor = torch.tensor([0, 0, 100, 100], dtype=torch.float32)
box2_tensor = torch.tensor([50, 50, 200, 200], dtype=torch.float32)
# 调用 bbox_iou 方法,计算 SIoU
siou = bbox_iou(box1_tensor, box2_tensor,xywh=False, SIoU=True)
# 输出 SIoU 的返回值
print('SIoU: {:.4f}'.format(siou.item()))
运行结果如下:
Intersection: tensor([2500.])
Union: tensor([30000.])
IoU: tensor([0.0833])
c2: tensor([80000.])
rho2: tensor([11250.])
sin_alpha tensor([0.7071])
angle_cost tensor([1.])
distance_cost tensor([0.2624])
shape_cost tensor([0.0129])
SIoU: -0.0543
六、WIOU
WIOU分为v1,v2和v3,
如果WIOU=True,scale=False,则执行WIOU v1的计算;
如果WIOU=True,scale=True,monotonous=True,则执行WIOU v2;
如果WIOU=True,scale=True,monotonous=False,则执行WIOU v3。
附上WIOU公式:
v1:
v2:
v3:
(、都是超参数)
附上WIOU v2的代码:
#WIOU v2
import numpy as np
import torch
import math
class WIoU_Scale:
''' monotonous: {
None: origin v1
True: monotonic FM v2
False: non-monotonic FM v3
}
momentum: The momentum of running mean'''
iou_mean = 1.
monotonous = True
_momentum = 1 - 0.5 ** (1 / 7000)
_is_train = True
def __init__(self, iou):
self.iou = iou
self._update(self)
@classmethod
def _update(cls, self):
if cls._is_train: cls.iou_mean = (1 - cls._momentum) * cls.iou_mean + \
cls._momentum * self.iou.detach().mean().item()
@classmethod
def _scaled_loss(cls, self, gamma=1.9, delta=3):
if isinstance(self.monotonous, bool):
if self.monotonous:
print('self.iou.detach() / self.iou_mean).sqrt():',(self.iou.detach() / self.iou_mean).sqrt())
print('self.iou.detach() :',self.iou.detach()) #返回了1-iou
print('self.iou_mean :',self.iou_mean)
return (self.iou.detach() / self.iou_mean).sqrt() #.detach() 方法会返回一个新的张量,该张量不再与后续计算相关;计算iou与iou均值之比的平方根
else:
beta = self.iou.detach() / self.iou_mean
alpha = delta * torch.pow(gamma, beta - delta)
return beta / alpha
return 1
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, EIoU=False, WIoU=False, Focal=False, alpha=1, gamma=0.5, scale=False, eps=1e-7):
# Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
# Get the coordinates of bounding boxes
if xywh: # transform from xywh to xyxy
(x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1)
w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
else: # x1, y1, x2, y2 = box1
b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
# Intersection area
inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \
(b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0)
# Union Area
union = w1 * h1 + w2 * h2 - inter + eps
if scale:
self = WIoU_Scale(1 - (inter / union))
# IoU
# iou = inter / union # ori iou
iou = torch.pow(inter/(union + eps), alpha) # alpha iou
if CIoU or DIoU or GIoU or EIoU or SIoU or WIoU:
cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width
ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height
if CIoU or DIoU or EIoU or SIoU or WIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal squared
rho2 = (((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4) ** alpha # center dist ** 2
if WIoU:
if Focal:
raise RuntimeError("WIoU do not support Focal.")
elif scale:
return getattr(WIoU_Scale, '_scaled_loss')(self), (1 - iou) * torch.exp((rho2 / c2)), iou # WIoU https://arxiv.org/abs/2301.10051
else:
return iou, torch.exp((rho2 / c2)) # WIoU v1
else:
return iou # IoU
if __name__ == "__main__":
# 将 NumPy 数组转换为 PyTorch 张量
box1_tensor = torch.tensor([0, 0, 100, 100], dtype=torch.float32)
box2_tensor = torch.tensor([50, 50, 200, 200], dtype=torch.float32)
# 调用 bbox_iou 方法,计算 WIoU
wiou = bbox_iou(box1_tensor, box2_tensor,xywh=False,WIoU=True,scale=True)
# 输出 WIoU 的返回值
print('WIoU: {:.4f}'.format(wiou[0].item()))
运行结果如下:
self.iou.detach() / self.iou_mean).sqrt(): tensor([0.9574])
self.iou.detach() : tensor([0.9167])
self.iou_mean : 0.9999917486583527
WIoU: 0.9574