IoU 是目标检测里面的一个基本的环节,这里看到别人的代码,感觉还是挺高效的,就记录一下:
torch.Tensor.expand
torch.Tensor.expand(*sizes) → Tensor
这是一个pytorch的函数,sizes是你想要扩展后的shape,其中原来tensor大小为1的维度可以扩展成任意值,并且这个操作不会分配新的内存。
栗子:
>>> x = torch.tensor([[1], [2], [3]])
>>> x.size()
torch.Size([3, 1])
>>> x.expand(3, 4)
tensor([[ 1, 1, 1, 1],
[ 2, 2, 2, 2],
[ 3, 3, 3, 3]])
>>> x.expand(-1, 4) # -1 means not changing the size of that dimension
tensor([[ 1, 1, 1, 1],
[ 2, 2, 2, 2],
[ 3, 3, 3, 3]])
torch.unsqueeze()
torch.unsqueeze(input, dim, out=None) → Tensor
这个函数就是在输入input的指定dim维度插入一个单一的维度,out可以指定输出的tensor,其中dim的范围是:[-input.dim() - 1, input.dim() + 1)
。如果dim是一个负值的话,事实上起作用的是dim = dim + input.dim() + 1
,也就是我们平常用的-1代表最后面。一般建议只用正数,以免混淆。
栗子:
>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1, 2, 3, 4]])
>>> torch.unsqueeze(x, -2)
tensor([[1, 2, 3, 4]])
>>> torch.unsqueeze(x, 1)
tensor([[ 1],
[ 2],
[ 3],
[ 4]])
>>> torch.unsqueeze(x, -1)
tensor([[1],
[2],
[3],
[4]])
IoU计算
主要有intersect、jaccard 两个函数,需要注意的一点就是这里喂入的bbox都是以
()给出的。
def intersect(box_a, box_b):
""" We resize both tensors to [A,B,2] without new malloc:
[A,2] -> [A,1,2] -> [A,B,2]
[B,2] -> [1,B,2] -> [A,B,2]
Then we compute the area of intersect between box_a and box_b.
Args:
box_a: (tensor) bounding boxes, Shape: [A,4].
box_b: (tensor) bounding boxes, Shape: [B,4].
Return:
(tensor) intersection area, Shape: [A,B].
"""
A = box_a.size(0)
B = box_b.size(0)
max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2),
box_b[:, 2:].unsqueeze(0).expand(A, B, 2))
min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2),
box_b[:, :2].unsqueeze(0).expand(A, B, 2))
inter = torch.clamp((max_xy - min_xy), min=0)
return inter[:, :, 0] * inter[:, :, 1]
# inter[:, :, 0] is the width of intersection and inter[:, :, 1] is height
def jaccard(box_a, box_b):
"""Compute the jaccard overlap of two sets of boxes. The jaccard overlap
is simply the intersection over union of two boxes. Here we operate on
ground truth boxes and default boxes.
E.g.:
A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)
Args:
box_a: (tensor) Ground truth bounding boxes, Shape: [A,4]
box_b: (tensor) Prior boxes from priorbox layers, Shape: [B,4]
Return:
jaccard overlap: (tensor) Shape: [A, B]
"""
inter = intersect(box_a, box_b)
area_a = ((box_a[:, 2]-box_a[:, 0]) *
(box_a[:, 3]-box_a[:, 1])).unsqueeze(1).expand_as(inter) # [A,B]
area_b = ((box_b[:, 2]-box_b[:, 0]) *
(box_b[:, 3]-box_b[:, 1])).unsqueeze(0).expand_as(inter) # [A,B]
union = area_a + area_b - inter
return inter / union # [A,B]
你们见到过的IoU计算方法都是怎样的呢?
来源:CSDN
作者:laizi_laizi
链接:https://blog.csdn.net/laizi_laizi/article/details/103463802