torch.t()、torch.min()、*、torch.stack、.long()

yolov3中的bbox_wh_iou代码如下:

def bbox_wh_iou(wh1, wh2):
    wh2 = wh2.t()
    # w1是当前anchor的w,h1是当前anchor的h
    w1, h1 = wh1[0], wh1[1]
    # w2是所有目标的w,h2是所有目标的h
    w2, h2 = wh2[0], wh2[1]
    inter_area = torch.min(w1, w2) * torch.min(h1, h2)
    union_area = (w1 * h1 + 1e-16) + w2 * h2 - inter_area
    return inter_area / union_area
>>> c = torch.tensor([[1,2],[3,4],[5,6],[7,8]])
>>> c
tensor([[1, 2],
        [3, 4],
        [5, 6],
        [7, 8]])
>>> c.shape
torch.Size([4, 2])
>>> c.t()
tensor([[1, 3, 5, 7],
        [2, 4, 6, 8]])
>>> c.t().shape
torch.Size([2, 4])

>>> d = torch.tensor([4,2])

>>> f = torch.min(d[0],c.t()[0])
>>> g = torch.min(d[1],c.t()[1])
>>> f
tensor([1, 3, 4, 4])
>>> g
tensor([2, 2, 2, 2])
>>> h = f*g
>>> h
tensor([2, 6, 8, 8])

>>> j = d[0]*d[1]+c.t()[0]*c.t()[1]
>>> j
tensor([10, 20, 38, 64])

>>> m = j-h
>>> m
tensor([ 8, 14, 30, 56])
>>> m1 = m+1
>>> m1
tensor([ 9, 15, 31, 57])
>>> m2=m1+1
>>> m2
tensor([10, 16, 32, 58])
>>> torch.stack((m,m1,m2))
tensor([[ 8, 14, 30, 56],
        [ 9, 15, 31, 57],
        [10, 16, 32, 58]])
>>> torch.stack([m,m1,m2])
tensor([[ 8, 14, 30, 56],
        [ 9, 15, 31, 57],
        [10, 16, 32, 58]])



>>> s=torch.stack([m,m1,m2])
>>> s.shape
torch.Size([3, 4])
>>> m.shape
torch.Size([4])

>>> s.max(0)
torch.return_types.max(
values=tensor([10, 16, 32, 58]),
indices=tensor([2, 2, 2, 2]))
>>> s.max(1)
torch.return_types.max(
values=tensor([56, 57, 58]),
indices=tensor([3, 3, 3]))

参考博客:
torch.stack()的官方解释,详解以及例子

>>> b1 = torch.rand(3,3)
>>> b1
tensor([[0.7498, 0.2052, 0.9352],
        [0.1171, 0.2046, 0.1682],
        [0.3003, 0.7483, 0.0089]])
>>> b1.long()
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])

你可能感兴趣的:(PyTorch,目标检测,深度学习)