gpt4 book ai didi

pytorch - 如何使用 pytorch grid_sample()?

转载 作者:行者123 更新时间:2023-12-04 00:19:34 28 4
gpt4 key购买 nike

我在获取 torch.nn.functional 时遇到了一些麻烦按照我的意愿工作,如下例所示:

import torch
import torch.nn.functional as F
import numpy as np

sz = 5
input_arr = torch.from_numpy(np.arange(sz*sz).reshape(1,1,sz,sz)).float()
indices = torch.from_numpy(np.array([-1,-1, -0.5,-0.5, 0,0, 0.5,0.5, 1,1]).reshape(1, 1, 5, 2)).float()

out = F.grid_sample(input_arr, indices)
print(input_arr)
print(out)

由于索引只是输入的对角线,我希望得到类似 tensor([[[[0., 6., 12., 18., 24.]]]]) 的结果。 (因为 (-1,-1) 应该给出左上角,而 (1,1) 应该给出右下角,根据 docs )。但是,我将其作为输出到控制台:
tensor([[[[ 0.,  1.,  2.,  3.,  4.],
[ 5., 6., 7., 8., 9.],
[10., 11., 12., 13., 14.],
[15., 16., 17., 18., 19.],
[20., 21., 22., 23., 24.]]]])
tensor([[[[ 0.0000, 4.5000, 12.0000, 19.5000, 6.0000]]]])

我究竟做错了什么?非常感谢!

最佳答案

您是否尝试过传递参数 align_corners = True ?如果您阅读文档,它会指出:

WARNING

When align_corners = True, the grid positions depend on the pixel sizerelative to the input image size, and so the locations sampled bygrid_sample() will differ for the same input given at differentresolutions (that is, after being upsampled or downsampled). Thedefault behavior up to version 1.2.0 was align_corners = True. Sincethen, the default behavior has been changed to align_corners = False,in order to bring it in line with the default for interpolate().


为了仔细检查,我运行了带有和不带有 align_corners = True 的代码, 以获得您需要的正确输出和您描述的错误输出。
# align_corners = False
out = F.grid_sample(input_arr, indices, align_corners = False)
print(out) # tensor([[[[ 0.0000, 4.5000, 12.0000, 19.5000, 6.0000]]]])

# align_corners = True
out = F.grid_sample(input_arr, indices, align_corners = True)
print(out) # tensor([[[[ 0., 6., 12., 18., 24.]]]])

关于pytorch - 如何使用 pytorch grid_sample()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61570727/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com