我是靠谱客的博主 无奈画笔,这篇文章主要介绍F.grid_sample,现在分享给大家,希望可以做个参考。

torch.nn.functional.grid_sample()函数的参数grid,表示的是范围为[-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)###torch.Size([2, 1, 1, 1, 1])

可以看到输出为:

tensor([[[[[1.]]]],

        [[[[1.]]]]])

最后

以上就是无奈画笔最近收集整理的关于F.grid_sample的全部内容,更多相关F内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(108)

评论列表共有 0 条评论

立即
投稿
返回
顶部