簡體   English   中英

使用索引的二維張量訪問 3D 張量(圖像)

[英]Access 3D tensor (image) using a 2d tensor of indices

用下面的 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 work
new = [[  0,   1,   2],[ 90,  91,  92],[ 36,  37,  38]]

想法?

切片會起作用

img[indices[:,0], indices[:,1]]
tensor([[ 0,  1,  2],
        [90, 91, 92],
        [36, 37, 38]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM