bobo0810 / PytorchNetHub

项目注释+论文复现+算法竞赛+Pytorch实践

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

交并比中为什么需要加1

sudospike opened this issue · comments

def bbox_iou(box1, box2, x1y1x2y2=True):
"""
Returns the IoU of two bounding boxes
"""
if not x1y1x2y2:
# Transform from center and width to exact coordinates
b1_x1, b1_x2 = box1[:, 0] - box1[:, 2] / 2, box1[:, 0] + box1[:, 2] / 2
b1_y1, b1_y2 = box1[:, 1] - box1[:, 3] / 2, box1[:, 1] + box1[:, 3] / 2
b2_x1, b2_x2 = box2[:, 0] - box2[:, 2] / 2, box2[:, 0] + box2[:, 2] / 2
b2_y1, b2_y2 = box2[:, 1] - box2[:, 3] / 2, box2[:, 1] + box2[:, 3] / 2
else:
# Get the coordinates of bounding boxes
b1_x1, b1_y1, b1_x2, b1_y2 = box1[:,0], box1[:,1], box1[:,2], box1[:,3]
b2_x1, b2_y1, b2_x2, b2_y2 = box2[:,0], box2[:,1], box2[:,2], box2[:,3]

# get the corrdinates of the intersection rectangle
inter_rect_x1 =  torch.max(b1_x1, b2_x1)
inter_rect_y1 =  torch.max(b1_y1, b2_y1)
inter_rect_x2 =  torch.min(b1_x2, b2_x2)
inter_rect_y2 =  torch.min(b1_y2, b2_y2)
# Intersection area 这一步的加1
inter_area =    torch.clamp(inter_rect_x2 - inter_rect_x1 + 1, min=0) * \
                torch.clamp(inter_rect_y2 - inter_rect_y1 + 1, min=0)
# Union Area 这一步的加1
b1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1)
b2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1)

iou = inter_area / (b1_area + b2_area - inter_area + 1e-16)

return iou

commented

你好,你可以尝试下 如果不加的效果,大佬应该是为了保险?
以下代码是我在别的项目计算IOU的代码,并没有加1操作。
https://github.com/bobo0810/XueLangTianchi/blob/master/code/utils/calculateIOU.py