提交 a1f1d11e 编写于 作者: R rensilin

exec_in_scope

Change-Id: I4f4e030e86ec28ca6977f311e7998e0350e1ea06
上级 8ab19129
...@@ -15,7 +15,9 @@ def print_help(this_name): ...@@ -15,7 +15,9 @@ def print_help(this_name):
print("Usage: {} <network building filename> [model_dir]\n".format(this_name)) print("Usage: {} <network building filename> [model_dir]\n".format(this_name))
print(" example: {} {}".format(this_name, os.path.join(dirname, 'example.py'))) print(" example: {} {}".format(this_name, os.path.join(dirname, 'example.py')))
def inference(filename):
def inference_warpper(filename):
"""Build inference network(without loss and optimizer) """Build inference network(without loss and optimizer)
Args: Args:
filename: path of file which defined real inference function filename: path of file which defined real inference function
...@@ -24,11 +26,14 @@ def inference(filename): ...@@ -24,11 +26,14 @@ def inference(filename):
and and
Variable: ctr_output Variable: ctr_output
""" """
with open(filename, 'r') as f: with open(filename, 'r') as f:
code = f.read() code = f.read()
compiled = compile(code, filename, 'exec') compiled = compile(code, filename, 'exec')
exec(compiled)
return inference() scope = dict()
exec(compiled, scope)
return scope['inference']()
def main(argv): def main(argv):
"""Create programs """Create programs
...@@ -40,7 +45,7 @@ def main(argv): ...@@ -40,7 +45,7 @@ def main(argv):
exit(1) exit(1)
network_build_file = argv[1] network_build_file = argv[1]
if len(argv) >= 2: if len(argv) > 2:
model_dir = argv[2] model_dir = argv[2]
else: else:
model_dir = './model' model_dir = './model'
...@@ -48,7 +53,7 @@ def main(argv): ...@@ -48,7 +53,7 @@ def main(argv):
main_program = fluid.Program() main_program = fluid.Program()
startup_program = fluid.Program() startup_program = fluid.Program()
with fluid.program_guard(main_program, startup_program): with fluid.program_guard(main_program, startup_program):
inputs, ctr_output = inference(network_build_file) inputs, ctr_output = inference_warpper(network_build_file)
test_program = main_program.clone(for_test=True) test_program = main_program.clone(for_test=True)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册