Error: Cannot find fetch variable in scope, fetch_var_name is crop_tensor_0.tmp_0
Created by: txyugood
代码如下:
import numpy as np
from paddle import fluid
a = np.zeros([60000,4], dtype='float32')
# a[50000] = np.array([1,1,1,1])
input = fluid.layers.data(shape=[60000, 4], append_batch_size=False, name='image')
i = fluid.layers.fill_constant(shape=[1], dtype='int32', value=0) # 循环计数器
loop_len = fluid.layers.fill_constant(shape=[1], dtype='int32', value=input.shape[0])
cond = fluid.layers.less_than(x=i, y=loop_len) # 循环条件
while_op = fluid.layers.While(cond=cond)
offset = fluid.layers.zeros(shape=[1], dtype='int32')
with while_op.block(): # 循环体
crop = fluid.layers.crop_tensor(input,
shape=[1, 4],
offsets=[i, offset])
i = fluid.layers.increment(x=i, value=1, in_place=True)
fluid.layers.less_than(x=i, y=loop_len, cond=cond) # 更新循环条件
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
res = exe.run(fluid.default_main_program(), feed={'image':a}, fetch_list=[crop])
print(res[0])
log 如下:
Connected to pydev debugger (build 193.6494.30)
/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/executor.py:795: UserWarning: The current program is empty.
warnings.warn(error_info)
/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/executor.py:779: UserWarning: The following exception is not an EOF exception.
"The following exception is not an EOF exception.")
Traceback (most recent call last):
File "/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/executor.py", line 780, in run
six.reraise(*sys.exc_info())
File "/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/six.py", line 703, in reraise
raise value
File "/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/executor.py", line 775, in run
use_program_cache=use_program_cache)
File "/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/executor.py", line 822, in _run_impl
use_program_cache=use_program_cache)
File "/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/executor.py", line 899, in _run_program
fetch_var_name)
paddle.fluid.core_avx.EnforceNotMet:
--------------------------------------------
C++ Call Stacks (More useful to developers):
--------------------------------------------
0 std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > paddle::platform::GetTraceBackString<char const*>(char const*&&, char const*, int)
1 paddle::platform::EnforceNotMet::EnforceNotMet(std::exception_ptr, char const*, int)
2 paddle::operators::FetchOp::RunImpl(paddle::framework::Scope const&, paddle::platform::Place const&) const
3 paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, paddle::platform::Place const&)
4 paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool)
5 paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, bool)
6 void pybind11::cpp_function::initialize<paddle::pybind::pybind11_init_core_avx(pybind11::module&)::$_103, void, paddle::framework::Executor&, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, pybind11::name, pybind11::is_method, pybind11::sibling>(paddle::pybind::pybind11_init_core_avx(pybind11::module&)::$_103&&, void (*)(paddle::framework::Executor&, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::'lambda'(pybind11::detail::function_call&)::__invoke(pybind11::detail::function_call&)
7 pybind11::cpp_function::dispatcher(_object*, _object*, _object*)
8 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval
9 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval
10 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval
11 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval
12 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval
13 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval
------------------------------------------
Python Call Stacks (More useful to users):
------------------------------------------
File "/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/framework.py", line 2488, in append_op
attrs=kwargs.get("attrs", None))
File "/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/executor.py", line 563, in _add_feed_fetch_ops
attrs={'col': i})
File "/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/executor.py", line 894, in _run_program
fetch_var_name=fetch_var_name)
File "/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/executor.py", line 822, in _run_impl
use_program_cache=use_program_cache)
File "/Users/alex/miniconda3/envs/paddle161/lib/python3.6/site-packages/paddle/fluid/executor.py", line 775, in run
use_program_cache=use_program_cache)
File "/Users/alex/baidu/yolact_paddle/test.py", line 23, in <module>
res = exe.run(fluid.default_main_program(), feed={'image':a}, fetch_list=[crop])
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py", line 1434, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py", line 1427, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py", line 2118, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py", line 2127, in <module>
main()
----------------------
Error Message Summary:
----------------------
Error: Cannot find fetch variable in scope, fetch_var_name is crop_tensor_0.tmp_0 at (/home/teamcity/work/ef54dc8a5b211854/paddle/fluid/operators/controlflow/fetch_op.cc:38)
[operator < fetch > error]
在循环中使用 fluid.layers.crop_tensor报错,大概是说作用域的问题,但是文档里没有看到 While 作用域相关内容,请问这个需要如何解决?