fasterrcnn使用IOU损失
程序员文章站
2022-07-14 20:18:07
...
推理发现 利用fasterrcnn得到的dx,dy,dw,dh 和target中的gt的dx,dy,dw,dh就可以求得IOU
完全不需要变化
转换代码
def bbox_transform(deltas, weights):
wx, wy, ww, wh = weights
dx = deltas[:, 0::4] / wx
dy = deltas[:, 1::4] / wy
dw = deltas[:, 2::4] / ww
dh = deltas[:, 3::4] / wh
dw = torch.clamp(dw, max=cfg.BBOX_XFORM_CLIP)
dh = torch.clamp(dh, max=cfg.BBOX_XFORM_CLIP)
pred_ctr_x = dx
pred_ctr_y = dy
pred_w = torch.exp(dw)
pred_h = torch.exp(dh)
x1 = pred_ctr_x - 0.5 * pred_w
y1 = pred_ctr_y - 0.5 * pred_h
x2 = pred_ctr_x + 0.5 * pred_w
y2 = pred_ctr_y + 0.5 * pred_h
return x1.view(-1), y1.view(-1), x2.view(-1), y2.view(-1)
IOULoss
def iou_loss(pred, target, eps=1e-6):
"""IoU loss.
Computing the IoU loss between a set of predicted bboxes and target bboxes.
The loss is calculated as negative log of IoU.
Args:
pred (Tensor): Predicted bboxes of format (x1, y1, x2, y2),
shape (n, 4).
target (Tensor): Corresponding gt bboxes, shape (n, 4).
eps (float): Eps to avoid log(0).
Return:
Tensor: Loss tensor.
"""
ious = bbox_overlaps(pred, target, is_aligned=True).clamp(min=eps)
loss = -ious.log()
return loss