torch.LongTensor转换成one hot tensor编码

import torch
'''
pytorch0.4.0及以上的高版本,加上pytorch0.2.0都支持torch.Tensor._scatter
功能实现将torch.LongTensor编码成 one-hot vector的功能
'''
rois_label=torch.tensor(
    [[0,1,0,5,2,7,4,3,1,2]]
)#假设当前的ground truth boxes共有10个,类别范围从0-7,加上背景类别共有8个类别,背景类别用0表示
ids = rois_label.view(-1, 1)
cls_prob=torch.ones((10,8))
'''
cls_prob:表示网络模型所预测出来的当前region proposal(在RCNN中)
属于8个类别的概率值
'''
proposal_num=cls_prob.shape[0]
num_class=cls_prob.shape[1]
class_mask=cls_prob.data.new(proposal_num,num_class).fill_(0).cpu()
print('ids.data', type(ids.data), torch.min(ids), torch.max(ids))
class_mask.scatter_(1, ids.data.cpu(), 1.0)
print(class_mask.shape, class_mask)
'''
ids.data <class 'torch.Tensor'> tensor(0) tensor(7)
torch.Size([10, 8]) tensor([[1., 0., 0., 0., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0., 0., 0., 0.],
        [1., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 1., 0., 0.],
        [0., 0., 1., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值