使用以下表示圖像的 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
提交
取消