バウンディング ボックス回帰 (BBR) は、ターゲットの検出とインスタンスのセグメンテーションで広く使用されており、ターゲットを特定するための重要なステップです。ただし、既存のバウンディング ボックスの回帰損失関数のほとんどは、予測されたボックスと実際の注釈付きボックスのアスペクト比が同じでも、幅と高さの値が完全に異なる場合には最適化できません。上記の問題を解決するために、著者は横長の長方形の幾何学的特性を徹底的に調査し、最小点距離 - MPDIoU に基づくバウンディング ボックスの類似性比較メトリックを提案しました。これには、オーバーラップやオーバーラップなど、既存の損失関数で考慮されるすべての関連要素が含まれます。重複しない領域、中心点の距離、幅と高さの偏差を測定しながら、計算プロセスを簡素化します。これに基づいて、著者は MPDIoU に基づく境界ボックス回帰損失関数を提案します。
MPDIoU について:
MPDIoU: 効率的かつ正確な境界ボックス回帰の損失 -- 論文研究ノート_athrunsunny のブログ-CSDN ブログ
指標は以下の通りです
下の図の計算式と模式図に従って再現してください。ご質問がある場合は、お気軽に教えていただき、相互にコミュニケーションしてください。
metrics.py の bbox_iou 関数を変更する
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, MDPIoU=False, feat_h=640, feat_w=640,
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
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1
# Intersection area
inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
(torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
# Union Area
union = w1 * h1 + w2 * h2 - inter + eps
# IoU
iou = inter / union
if CIoU or DIoU or GIoU:
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center dist ** 2
if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / (h2 + eps)) - torch.atan(w1 / (h1 + eps)), 2)
with torch.no_grad():
alpha = v / (v - iou + (1 + eps))
return iou - (rho2 / c2 + v * alpha) # CIoU
return iou - rho2 / c2 # DIoU
c_area = cw * ch + eps # convex area
return iou - (c_area - union) / c_area # GIoU https://arxiv.org/pdf/1902.09630.pdf
elif MDPIoU:
d1 = (b2_x1 - b1_x1) ** 2 + (b2_y1 - b1_y1) ** 2
d2 = (b2_x2 - b1_x2) ** 2 + (b2_y2 - b1_y2) ** 2
mpdiou_hw_pow = feat_h ** 2 + feat_w ** 2
return iou - d1 / mpdiou_hw_pow - d2 / mpdiou_hw_pow # MPDIoU
return iou # IoU
loss.pyのiouの計算方法を修正
iou = bbox_iou(pbox, tbox[i], MDPIoU=True, feat_h=tobj.size()[2], feat_w=tobj.size()[3]).squeeze()