fluid.layers.affine_grid 与 fluid.layers.grid_sampler联合使用异常
Created by: kebinC
这个我的测试代码:
import paddle
import paddle.fluid as fluid
import numpy as np
#from stn import STN
def test():
input_np = np.random.randn(2, 3, 224, 224).astype(np.float32)
input = fluid.layers.create_global_var(
shape=[2, 3, 224, 224], value=0, dtype='float32', persistable=True)
fluid.layers.assign(input_np, input)
theta_np = np.random.randn(2, 2, 3).astype(np.float32)
theta = fluid.layers.create_global_var(
shape=[2, 2, 3], value=0, dtype='float32', persistable=True)
fluid.layers.assign(theta_np, theta)
#stn = STN()
#output = stn.net(input)
grid = fluid.layers.affine_grid(theta, out_shape=input.shape)
out = fluid.layers.grid_sampler(input, grid=grid)
fetch_list = [output.name]
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
res = exe.run(
fluid.default_main_program(),
fetch_list=fetch_list
)
#print res
print res[0].shape
test()
错误信息: Traceback (most recent call last): File "test_stn.py", line 37, in test() File "test_stn.py", line 22, in test out = fluid.layers.grid_sampler(input, grid=grid) File "/home/users/chenkaibing/env/paddle-release/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/layers/nn.py", line 8870, in grid_sampler helper.append_op(type='grid_sampler', inputs=ipts, outputs={'Output': out}) File "/home/users/chenkaibing/env/paddle-release/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/layer_helper.py", line 50, in append_op return self.main_program.current_block().append_op(*args, **kwargs) File "/home/users/chenkaibing/env/paddle-release/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/framework.py", line 1208, in append_op op = Operator(block=self, desc=op_desc, *args, **kwargs) File "/home/users/chenkaibing/env/paddle-release/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/framework.py", line 657, in init self.desc.infer_shape(self.block.desc) paddle.fluid.core.EnforceNotMet: Enforce failed. Expected grid_dims[1] == x_dims[2], but received grid_dims[1]:-1 != x_dims[2]:224. Input(X) dims[2] and Input(Grid) dims[1] should be equal. at [/sharefolder/duyuning/Paddle/paddle/fluid/operators/grid_sampler_op.cc:48] PaddlePaddle Call Stacks: