为了账号安全,请及时绑定邮箱和手机立即绑定

使用索引的二维张量访问 3D 张量(图像)

使用索引的二维张量访问 3D 张量(图像)

江户川乱折腾 2023-06-13 16:54:48
使用以下表示图像的 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]])


查看完整回答
反对 回复 2023-06-13
  • 1 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信