in easycv/models/loss/iou_loss.py [0:0]
def forward(self, pred, target):
assert pred.shape[0] == target.shape[0]
if target.dtype != pred.dtype:
target = target.to(pred.dtype)
pred = pred.view(-1, 4)
target = target.view(-1, 4)
tl = torch.max((pred[:, :2] - pred[:, 2:] / 2),
(target[:, :2] - target[:, 2:] / 2))
br = torch.min((pred[:, :2] + pred[:, 2:] / 2),
(target[:, :2] + target[:, 2:] / 2))
area_p = torch.prod(pred[:, 2:], 1)
area_g = torch.prod(target[:, 2:], 1)
en = (tl < br).type(tl.type()).prod(dim=1)
area_i = torch.prod(br - tl, 1) * en
iou = (area_i) / (area_p + area_g - area_i + 1e-16)
if self.loss_type == 'iou':
loss = 1 - iou**2
elif self.loss_type == 'siou':
# angle cost
c_h = torch.max(pred[:, 1], target[:, 1]) - torch.min(
pred[:, 1], target[:, 1])
c_w = torch.max(pred[:, 0], target[:, 0]) - torch.min(
pred[:, 0], target[:, 0])
sigma = torch.sqrt(((pred[:, :2] - target[:, :2])**2).sum(dim=1))
angle_cost = 2 * (c_h * c_w) / (sigma**2)
# distance cost
gamma = 2 - angle_cost
# gamma = 1
c_dw = torch.max(pred[:, 0], target[:, 0]) - torch.min(
pred[:, 0], target[:, 0]) + (pred[:, 2] + target[:, 2]) / 2
c_dh = torch.max(pred[:, 1], target[:, 1]) - torch.min(
pred[:, 1], target[:, 1]) + (pred[:, 3] + target[:, 3]) / 2
p_x = ((target[:, 0] - pred[:, 0]) / c_dw)**2
p_y = ((target[:, 1] - pred[:, 1]) / c_dh)**2
dist_cost = 2 - torch.exp(-gamma * p_x) - torch.exp(-gamma * p_y)
# shape cost
theta = 4
w_w = torch.abs(pred[:, 2] - target[:, 2]) / torch.max(
pred[:, 2], target[:, 2])
w_h = torch.abs(pred[:, 3] - target[:, 3]) / torch.max(
pred[:, 3], target[:, 3])
shape_cost = torch.pow((1 - torch.exp(-w_w)), theta) + torch.pow(
(1 - torch.exp(-w_h)), theta)
loss = 1 - iou + (dist_cost + shape_cost) / 2
elif self.loss_type == 'giou':
c_tl = torch.min((pred[:, :2] - pred[:, 2:] / 2),
(target[:, :2] - target[:, 2:] / 2))
c_br = torch.max((pred[:, :2] + pred[:, 2:] / 2),
(target[:, :2] + target[:, 2:] / 2))
area_c = torch.prod(c_br - c_tl, 1)
giou = iou - (area_c - area_i) / area_c.clamp(1e-16)
loss = 1 - giou.clamp(min=-1.0, max=1.0)
elif self.loss_type == 'diou':
c_tl = torch.min((pred[:, :2] - pred[:, 2:] / 2),
(target[:, :2] - target[:, 2:] / 2))
c_br = torch.max((pred[:, :2] + pred[:, 2:] / 2),
(target[:, :2] + target[:, 2:] / 2))
convex_dis = torch.pow(c_br[:, 0] - c_tl[:, 0], 2) + torch.pow(
c_br[:, 1] - c_tl[:, 1], 2) + 1e-7 # convex diagonal squared
center_dis = (torch.pow(pred[:, 0] - target[:, 0], 2) +
torch.pow(pred[:, 1] - target[:, 1], 2)
) # center diagonal squared
diou = iou - (center_dis / convex_dis)
loss = 1 - diou.clamp(min=-1.0, max=1.0)
elif self.loss_type == 'ciou':
c_tl = torch.min((pred[:, :2] - pred[:, 2:] / 2),
(target[:, :2] - target[:, 2:] / 2))
c_br = torch.max((pred[:, :2] + pred[:, 2:] / 2),
(target[:, :2] + target[:, 2:] / 2))
convex_dis = torch.pow(c_br[:, 0] - c_tl[:, 0], 2) + torch.pow(
c_br[:, 1] - c_tl[:, 1], 2) + 1e-7 # convex diagonal squared
center_dis = (torch.pow(pred[:, 0] - target[:, 0], 2) +
torch.pow(pred[:, 1] - target[:, 1], 2)
) # center diagonal squared
v = (4 / math.pi**2) * torch.pow(
torch.atan(target[:, 2] / torch.clamp(target[:, 3], min=1e-7))
- torch.atan(pred[:, 2] / torch.clamp(pred[:, 3], min=1e-7)),
2)
with torch.no_grad():
alpha = v / ((1 + 1e-7) - iou + v)
ciou = iou - (center_dis / convex_dis + alpha * v)
loss = 1 - ciou.clamp(min=-1.0, max=1.0)
elif self.loss_type == 'eiou':
c_tl = torch.min((pred[:, :2] - pred[:, 2:] / 2),
(target[:, :2] - target[:, 2:] / 2))
c_br = torch.max((pred[:, :2] + pred[:, 2:] / 2),
(target[:, :2] + target[:, 2:] / 2))
convex_dis = torch.pow(c_br[:, 0] - c_tl[:, 0], 2) + torch.pow(
c_br[:, 1] - c_tl[:, 1], 2) + 1e-7 # convex diagonal squared
center_dis = (torch.pow(pred[:, 0] - target[:, 0], 2) +
torch.pow(pred[:, 1] - target[:, 1], 2)
) # center diagonal squared
dis_w = torch.pow(pred[:, 2] - target[:, 2], 2)
dis_h = torch.pow(pred[:, 3] - target[:, 3], 2)
C_w = torch.pow(c_br[:, 0] - c_tl[:, 0], 2) + 1e-7
C_h = torch.pow(c_br[:, 1] - c_tl[:, 1], 2) + 1e-7
eiou = iou - (center_dis / convex_dis) - (dis_w / C_w) - (
dis_h / C_h)
loss = 1 - eiou.clamp(min=-1.0, max=1.0)
if self.reduction == 'mean':
loss = loss.mean()
elif self.reduction == 'sum':
loss = loss.sum()
return loss