学习资源站

12-添加损失函数之EIoU、AlphaIoU、SIoU、WIoU_如何将yolov5s损失函数eiou

YOLOv5改进系列(11)——添加损失函数之EIoU、AlphaIoU、SIoU、WIoU


☀️一、不同IoU的介绍

要学会修改代码前一定要先了解这几个IoU的区别和计算方法:

  • IoU Loss:主要考虑检测框和目标框重叠面积
  • GIoU Loss:在IoU的基础上,解决边界框不相交时loss等于0的问题。
  • DIoU Loss:在IoU和GIoU的基础上,考虑边界框中心点距离的信息。
  • CIoU Loss:在DIoU的基础上,考虑边界框宽高比的尺度信息。
  • EIoU Loss:在CIoU的基础上,解决了纵横比的模糊定义,并添加Focal Loss解决BBox回归中的样本不平衡问题。
  • αIoU Loss:通过调节α,使探测器更灵活地实现不同水平的bbox回归精度。
  • SIoU Loss:在EIoU的基础上,加入了类别信息的权重因子,以提高检测模型的分类准确率
  • WIoU Loss:解决质量较好和质量较差的样本间的BBR平衡问题

更多详细的内容在上一篇就已经详细介绍过了,快上直通车:


☀️二、IoU、GIoU、DIoU、CIoU

 这几个IoU在YOLO v5的源码中都有提供,在utils/metrics.py文件夹下的bbox_iou函数里。

这个函数是用来计算矩阵框间的IoU的

def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7):

 通过这行代码我们可以看出,GIoU,DIoU,CIoU的bool值如果全部为False时,会返回最普通的IoU,如果其中一个为True的时候,即返回设定为True的那个IoU

通常用来在utils/loss.py文件夹下的__call__函数中计算回归损失(bbox损失)

可以看出YOLOv5中bbox_iou其默认用的是CIoU 


☀️三、EIoU

3.1 简介

EIoU 是在 CIoU 的惩罚项基础上将预测框和真实框的纵横比的影响因子拆开,分别计算预测框和真实框的长和宽,来解决 CIoU 存在的问题。

EIoU包括三个部分:IoU损失、距离损失、高宽损失(重叠面积、中心点举例、高宽比)。高宽损失直接最小化了预测目标边界框和真实边界框的高度和宽度的差异,使其有更快的收敛速度和更好的定位结果。


3.2 添加步骤

第①步 配置metric.py文件

首先我们找到刚才的utils/metrics.py文件夹下的bbox_iou函数,然后将函数整个替换成下面的代码:

  1. def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, EIoU=False, eps=1e-7):
  2. # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
  3. box2 = box2.T
  4. # Get the coordinates of bounding boxes
  5. if x1y1x2y2: # x1, y1, x2, y2 = box1
  6. b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
  7. b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
  8. else: # transform from xywh to xyxy
  9. b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
  10. b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
  11. b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
  12. b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2
  13. # Intersection area
  14. inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
  15. (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
  16. # Union Area
  17. w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
  18. w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
  19. union = w1 * h1 + w2 * h2 - inter + eps
  20. iou = inter / union
  21. if GIoU or DIoU or CIoU or EIoU:
  22. cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
  23. ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
  24. if CIoU or DIoU or EIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
  25. c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
  26. rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
  27. (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
  28. if DIoU:
  29. return iou - rho2 / c2 # DIoU
  30. elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
  31. v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
  32. with torch.no_grad():
  33. alpha = v / (v - iou + (1 + eps))
  34. return iou - (rho2 / c2 + v * alpha) # CIoU
  35. elif EIoU:
  36. rho_w2 = ((b2_x2 - b2_x1) - (b1_x2 - b1_x1)) ** 2
  37. rho_h2 = ((b2_y2 - b2_y1) - (b1_y2 - b1_y1)) ** 2
  38. cw2 = cw ** 2 + eps
  39. ch2 = ch ** 2 + eps
  40. return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2)
  41. else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
  42. c_area = cw * ch + eps # convex area
  43. return iou - (c_area - union) / c_area # GIoU
  44. else:
  45. return iou # IoU

第②步 配置loss.py文件

然后再找到utils/loss.py文件夹下的__call__函数,把Regression loss中计算IoU的代码,换成下面这句: 

iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=False, EIoU=True)  

如下图所示: 


☀️四、Alpha IoU

4.1 简介

作者将现有的基于IoU Loss推广到一个新的Power IoU系列 Loss,该系列具有一个Power IoU项和一个附加的Power正则项,具有单个Power参数α,称这种新的损失系列为α-IoU Loss

通过调节α,使检测器在实现不同水平的bbox回归精度方面具有更大的灵活性。并且α-IoU 对小数据集和噪声的鲁棒性更强。

通过实验发现,在大多数情况下,取α=3 的效果最好。


4.2 添加步骤

第①步 配置metric.py文件

首先我们找到刚才的utils/metrics.py文件夹下的bbox_iou函数,然后将函数整个替换成下面的代码,因为上文也提到α=3时效果最好,所以这里我们将alpha的值设置为3:

  1. def bbox_alpha_iou(box1, box2, x1y1x2y2=False, GIoU=False, DIoU=False, CIoU=False, alpha=3, eps=1e-7):
  2. # Returns tsqrt_he IoU of box1 to box2. box1 is 4, box2 is nx4
  3. box2 = box2.T
  4. # Get the coordinates of bounding boxes
  5. if x1y1x2y2: # x1, y1, x2, y2 = box1
  6. b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
  7. b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
  8. else: # transform from xywh to xyxy
  9. b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
  10. b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
  11. b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
  12. b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2
  13. # Intersection area
  14. inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
  15. (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
  16. # Union Area
  17. w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
  18. w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
  19. union = w1 * h1 + w2 * h2 - inter + eps
  20. # change iou into pow(iou+eps)
  21. # iou = inter / union
  22. iou = torch.pow(inter/union + eps, alpha)
  23. # beta = 2 * alpha
  24. if GIoU or DIoU or CIoU:
  25. cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
  26. ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
  27. if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
  28. c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal
  29. rho_x = torch.abs(b2_x1 + b2_x2 - b1_x1 - b1_x2)
  30. rho_y = torch.abs(b2_y1 + b2_y2 - b1_y1 - b1_y2)
  31. rho2 = ((rho_x ** 2 + rho_y ** 2) / 4) ** alpha # center distance
  32. if DIoU:
  33. return iou - rho2 / c2 # DIoU
  34. elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
  35. v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
  36. with torch.no_grad():
  37. alpha_ciou = v / ((1 + eps) - inter / union + v)
  38. # return iou - (rho2 / c2 + v * alpha_ciou) # CIoU
  39. return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)) # CIoU
  40. else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
  41. # c_area = cw * ch + eps # convex area
  42. # return iou - (c_area - union) / c_area # GIoU
  43. c_area = torch.max(cw * ch + eps, union) # convex area
  44. return iou - torch.pow((c_area - union) / c_area + eps, alpha) # GIoU
  45. else:
  46. return iou # torch.log(iou+eps) or iou

第②步 配置loss.py文件

然后再找到utils/loss.py文件夹下的__call__函数,将bbox_iou换成bbox_alpha_iou

iou = bbox_alpha_iou(pbox.T, tbox[i], x1y1x2y2=False, alpha=3, CIoU=True)

如下图所示: 


☀️五、SIoU

5.1 简介

SIoU考虑到期望回归之间向量的角度,重新定义角度惩罚度量,它可以使预测框快速漂移到最近的轴,随后则只需要回归一个坐标(X或Y),这有效地减少了自由度的总数。应用于传统的神经网络和数据集,表明SIoU提高了训练的速度和推理的准确性。

SIoU进一步考虑了真实框和预测框之间的向量角度,重新定义相关损失函数,具体包含四个部分:角度损失(Angle cost)、距离损失(Distance cost)、形状损失(Shape cost)、IoU损失(IoU cost)


5.2 添加步骤

第①步 配置metric.py文件

首先我们找到刚才的utils/metrics.py文件夹下的bbox_iou函数,然后将函数整个替换成下面的代码:

  1. def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, eps=1e-7):
  2. # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
  3. box2 = box2.T
  4. # Get the coordinates of bounding boxes
  5. if x1y1x2y2: # x1, y1, x2, y2 = box1
  6. b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
  7. b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
  8. else: # transform from xywh to xyxy
  9. b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
  10. b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
  11. b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
  12. b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2
  13. # Intersection area
  14. inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
  15. (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
  16. # Union Area
  17. w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
  18. w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
  19. union = w1 * h1 + w2 * h2 - inter + eps
  20. iou = inter / union
  21. if GIoU or DIoU or CIoU or SIoU:
  22. cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
  23. ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
  24. if SIoU: # SIoU Loss https://arxiv.org/pdf/2205.12740.pdf
  25. s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5
  26. s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5
  27. sigma = torch.pow(s_cw ** 2 + s_ch ** 2, 0.5)
  28. sin_alpha_1 = torch.abs(s_cw) / sigma
  29. sin_alpha_2 = torch.abs(s_ch) / sigma
  30. threshold = pow(2, 0.5) / 2
  31. sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1)
  32. # angle_cost = 1 - 2 * torch.pow( torch.sin(torch.arcsin(sin_alpha) - np.pi/4), 2)
  33. angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - np.pi / 2)
  34. rho_x = (s_cw / cw) ** 2
  35. rho_y = (s_ch / ch) ** 2
  36. gamma = angle_cost - 2
  37. distance_cost = 2 - torch.exp(gamma * rho_x) - torch.exp(gamma * rho_y)
  38. omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2)
  39. omiga_h = torch.abs(h1 - h2) / torch.max(h1, h2)
  40. shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4)
  41. return iou - 0.5 * (distance_cost + shape_cost)
  42. if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
  43. c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
  44. rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
  45. (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
  46. if DIoU:
  47. return iou - rho2 / c2 # DIoU
  48. elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
  49. v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
  50. with torch.no_grad():
  51. alpha = v / (v - iou + (1 + eps))
  52. return iou - (rho2 / c2 + v * alpha) # CIoU
  53. else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
  54. c_area = cw * ch + eps # convex area
  55. return iou - (c_area - union) / c_area # GIoU
  56. else:
  57. return iou # IoU

第②步 配置loss.py文件

然后再找到utils/loss.py文件夹下的__call__函数,把Regression loss中计算IoU的代码,换成下面这句: 

iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, SIoU=True)

如下图所示: 


☀️六、WIoU

6.1 简介

传统的Intersection over Union(IoU)只考虑了预测框和真实框的重叠部分,没有考虑两者之间的区域,导致在评估结果时可能存在偏差。基于这一思想,作者提出了一种基于IoU的损失,该损失具有动态非单调FM,名为Wise IoU(WIoU)


6.2 添加步骤

第①步 配置metric.py文件

首先我们找到刚才的utils/metrics.py文件夹下的bbox_iou函数,然后将函数整个替换成下面的代码:

  1. class WIoU_Scale:
  2. ''' monotonous: {
  3. None: origin v1
  4. True: monotonic FM v2
  5. False: non-monotonic FM v3
  6. }
  7. momentum: The momentum of running mean'''
  8. iou_mean = 1.
  9. monotonous = False
  10. _momentum = 1 - 0.5 ** (1 / 7000)
  11. _is_train = True
  12. def __init__(self, iou):
  13. self.iou = iou
  14. self._update(self)
  15. @classmethod
  16. def _update(cls, self):
  17. if cls._is_train: cls.iou_mean = (1 - cls._momentum) * cls.iou_mean + \
  18. cls._momentum * self.iou.detach().mean().item()
  19. @classmethod
  20. def _scaled_loss(cls, self, gamma=1.9, delta=3):
  21. if isinstance(self.monotonous, bool):
  22. if self.monotonous:
  23. return (self.iou.detach() / self.iou_mean).sqrt()
  24. else:
  25. beta = self.iou.detach() / self.iou_mean
  26. alpha = delta * torch.pow(gamma, beta - delta)
  27. return beta / alpha
  28. return 1
  29. 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):
  30. # Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
  31. # Get the coordinates of bounding boxes
  32. if xywh: # transform from xywh to xyxy
  33. (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1)
  34. w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
  35. b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
  36. b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
  37. else: # x1, y1, x2, y2 = box1
  38. b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
  39. b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
  40. w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
  41. w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
  42. # Intersection area
  43. inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \
  44. (b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0)
  45. # Union Area
  46. union = w1 * h1 + w2 * h2 - inter + eps
  47. if scale:
  48. self = WIoU_Scale(1 - (inter / union))
  49. # IoU
  50. # iou = inter / union # ori iou
  51. iou = torch.pow(inter/(union + eps), alpha) # alpha iou
  52. if CIoU or DIoU or GIoU or EIoU or SIoU or WIoU:
  53. cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width
  54. ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height
  55. if CIoU or DIoU or EIoU or SIoU or WIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
  56. c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal squared
  57. rho2 = (((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4) ** alpha # center dist ** 2
  58. if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
  59. v = (4 / math.pi ** 2) * (torch.atan(w2 / h2) - torch.atan(w1 / h1)).pow(2)
  60. with torch.no_grad():
  61. alpha_ciou = v / (v - iou + (1 + eps))
  62. if Focal:
  63. return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)), torch.pow(inter/(union + eps), gamma) # Focal_CIoU
  64. else:
  65. return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)) # CIoU
  66. elif EIoU:
  67. rho_w2 = ((b2_x2 - b2_x1) - (b1_x2 - b1_x1)) ** 2
  68. rho_h2 = ((b2_y2 - b2_y1) - (b1_y2 - b1_y1)) ** 2
  69. cw2 = torch.pow(cw ** 2 + eps, alpha)
  70. ch2 = torch.pow(ch ** 2 + eps, alpha)
  71. if Focal:
  72. return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2), torch.pow(inter/(union + eps), gamma) # Focal_EIou
  73. else:
  74. return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2) # EIou
  75. elif SIoU:
  76. # SIoU Loss https://arxiv.org/pdf/2205.12740.pdf
  77. s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5 + eps
  78. s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5 + eps
  79. sigma = torch.pow(s_cw ** 2 + s_ch ** 2, 0.5)
  80. sin_alpha_1 = torch.abs(s_cw) / sigma
  81. sin_alpha_2 = torch.abs(s_ch) / sigma
  82. threshold = pow(2, 0.5) / 2
  83. sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1)
  84. angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - math.pi / 2)
  85. rho_x = (s_cw / cw) ** 2
  86. rho_y = (s_ch / ch) ** 2
  87. gamma = angle_cost - 2
  88. distance_cost = 2 - torch.exp(gamma * rho_x) - torch.exp(gamma * rho_y)
  89. omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2)
  90. omiga_h = torch.abs(h1 - h2) / torch.max(h1, h2)
  91. shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4)
  92. if Focal:
  93. return iou - torch.pow(0.5 * (distance_cost + shape_cost) + eps, alpha), torch.pow(inter/(union + eps), gamma) # Focal_SIou
  94. else:
  95. return iou - torch.pow(0.5 * (distance_cost + shape_cost) + eps, alpha) # SIou
  96. elif WIoU:
  97. if Focal:
  98. raise RuntimeError("WIoU do not support Focal.")
  99. elif scale:
  100. return getattr(WIoU_Scale, '_scaled_loss')(self), (1 - iou) * torch.exp((rho2 / c2)), iou # WIoU https://arxiv.org/abs/2301.10051
  101. else:
  102. return iou, torch.exp((rho2 / c2)) # WIoU v1
  103. if Focal:
  104. return iou - rho2 / c2, torch.pow(inter/(union + eps), gamma) # Focal_DIoU
  105. else:
  106. return iou - rho2 / c2 # DIoU
  107. c_area = cw * ch + eps # convex area
  108. if Focal:
  109. return iou - torch.pow((c_area - union) / c_area + eps, alpha), torch.pow(inter/(union + eps), gamma) # Focal_GIoU https://arxiv.org/pdf/1902.09630.pdf
  110. else:
  111. return iou - torch.pow((c_area - union) / c_area + eps, alpha) # GIoU https://arxiv.org/pdf/1902.09630.pdf
  112. if Focal:
  113. return iou, torch.pow(inter/(union + eps), gamma) # Focal_IoU
  114. else:
  115. return iou # IoU

这里要注意几个问题:

(1)WIoU有三个版本,分别对应控制如下:

  • None: origin v1
  • True: monotonic FM v2
  • False: non-monotonic FM v3

(2) WIoU是不支持Focal的。


第②步 配置loss.py文件

这里和上面有点不同,我们需要找到utils/loss.py文件夹下的__call__函数,替换ioulbox

(loss_iou)函数,调用bbox iou损失函数时,将WIoU设置为True即可:

  1. # WIoU
  2. iou = bbox_iou(pbox, tbox[i], WIoU=True, Focal=False, scale=True)

如下图所示:

如果出现:TypeError: unsupported operand type(s) for -: 'float' and 'tuple', 这个报错(float和元组不能相减)

可参考评论区@ 同学的改正方法:

lbox += (1.0 - iou).mean()换成:
if isinstance(iou, tuple):
if len(iou) == 2:
lbox += (iou[1].detach().squeeze() * (1 - iou[0].squeeze())).mean()
iou = iou[0].squeeze()
else:
lbox += (iou[0] * iou[1]).mean()
iou = iou[2].squeeze()
else:
lbox += (1.0 - iou.squeeze()).mean() # iou loss
iou = iou.squeeze()