GIOU Loss
- 解决 iou loss 为1 时不优化的缺陷
- 增加c项,表示为预测框与标签框最小矩形
- 缺陷,当两个框相交时,尤其一个框包含另一个的时候,退化为iou loss,收敛会变慢
DIOU Loss
- 直接最小化Anchor和目标框之间的归一化距离以达到更快的收敛速度
- b、bgt分别是anchor、groundtrueth中心点坐标,p为其欧式距离,c为其包含最小矩形对角线长度
- 解决GIOU Loss 退化IOU loss的情况
CIOU Loss
- 回归损失在与目标框有重叠甚至有包含关系时更准确,收敛更快
- a是平衡系数参数,v为衡量anchor和目标框比例一致性参数
class IouLoss(nn.Module):
def __init__(self,pred_mode = 'Center',size_sum=True,variances=None,losstype='Giou'):
super(IouLoss, self).__init__()
self.size_sum = size_sum
self.pred_mode = pred_mode
self.variances = variances
self.loss = losstype
def forward(self, loc_p, loc_t,prior_data):
num = loc_p.shape[0]
if self.pred_mode == 'Center':
decoded_boxes = decode(loc_p, prior_data, self.variances)
else:
decoded_boxes = loc_p
if self.loss == 'Iou':
loss = torch.sum(1.0 - bbox_overlaps_iou(decoded_boxes, loc_t))
else:
if self.loss == 'Giou':
loss = torch.sum(1.0 - bbox_overlaps_giou(decoded_boxes,loc_t))
else:
if self.loss == 'Diou':
loss = torch.sum(1.0 - bbox_overlaps_diou(decoded_boxes,loc_t))
else:
loss = torch.sum(1.0 - bbox_overlaps_ciou(decoded_boxes, loc_t))
if self.size_sum:
loss = loss
else:
loss = loss/num
return loss