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.,
torch.LongTensor转换成one hot tensor编码
最新推荐文章于 2025-05-16 02:50:48 发布