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]])

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值