F.grid_sample

torch.nn.functional.grid_sample()関数のパラメーターグリッドは、[-1、1]の範囲の座標系で(x、y、z)を表します。座標と配列の間の対応する関係は次のとおりです。

x-> w、y-> h、z-> d、テストコードは次のとおりです。

 
  1. import numpy as np

  2. from torch.nn import functional as F

  3. import torch

  4.  
  5. if __name__ == '__main__':

  6. d, h, w = 8, 10, 12

  7. input = torch.zeros((2, 1, 8, 10, 12), dtype=torch.float32)

  8. input[:, 0, 2, 3, 4] = 1

  9. grid = torch.zeros((2, 1, 1, 1, 3), dtype=torch.float32)

  10. x, y, z = 4, 3, 2 # 对应input的w, h, d

  11. # rescale to [-1, 1]

  12. x = 2. * x / (w - 1) - 1.

  13. y = 2. * y / (h - 1) - 1.

  14. z = 2. * z / (d - 1) - 1.

  15. grid[0, 0, 0, 0, :] = torch.from_numpy(np.array([x, y, z]).astype(np.float32))

  16. grid[1, 0, 0, 0, :] = torch.from_numpy(np.array([x, y, z]).astype(np.float32))

  17. out = F.grid_sample(input, grid, mode='nearest')

  18. print(out)

出力は次のとおりです。

テンソル([[[[[1。]]]]、

        [[[[1。]]]]])

おすすめ

転載: blog.csdn.net/Gussss/article/details/108671109