使用以下表示图像的 3D 张量img.shape=[H,W,F]和表示该 img 索引的张量indices.shape=[N,2]例如,如果indices = [[0,1],[5,3],...]] 我想创建一个新的形状张量new.shape=[N,F],目前new[k] == img[indices[k][0],indices[k][1]] 为了解决这个问题,我将两个张量展平: idx_flattened = idx_flattened [:,0] * (idx_flattened [:,1].max()+1) + idx_flattened[:,1] img = img .reshape(-1,F) new = img[idx_flattened ]但我确信有更好的方法:)这是一个完整的最小示例:img = torch.arange(8*10*3).reshape(8,10,3)indices = torch.tensor([[0,0],[3,0],[1,2]])new = img[indices] <- This does not worknew = [[ 0, 1, 2],[ 90, 91, 92],[ 36, 37, 38]]想法?
1 回答

慕容3067478
TA贡献1773条经验 获得超3个赞
切片会起作用
img[indices[:,0], indices[:,1]]
tensor([[ 0, 1, 2],
[90, 91, 92],
[36, 37, 38]])
添加回答
举报
0/150
提交
取消