提交 feed94e2 编写于 作者: Q qiaolongfei

should load parameter before create parallel_executor

上级 e8d24aa1
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import contextlib
import core import core
import executor import executor
...@@ -41,31 +43,36 @@ class Inferencer(object): ...@@ -41,31 +43,36 @@ class Inferencer(object):
with unique_name.guard(): with unique_name.guard():
self.predict_var = infer_func() self.predict_var = infer_func()
with self._prog_and_scope_guard():
# load params from param_path into scope
io.load_params(executor.Executor(self.place), param_path)
if parallel: if parallel:
self.exe = parallel_executor.ParallelExecutor( with self._prog_and_scope_guard():
use_cuda=isinstance(self.place, core.CUDAPlace), self.exe = parallel_executor.ParallelExecutor(
loss_name=self.predict_var.name) use_cuda=isinstance(self.place, core.CUDAPlace),
loss_name=self.predict_var.name)
else: else:
self.exe = executor.Executor(self.place) self.exe = executor.Executor(self.place)
with executor.scope_guard(self.scope):
# load params from param_path into scope
io.load_params(self.exe, param_path, self.inference_program)
def infer(self, inputs, return_numpy=True): def infer(self, inputs):
""" """
:param inputs: a map of {"input_name": input_var} that will be feed into the inference program :param inputs: a map of {"input_name": input_var} that will be feed into the inference program
to get the predict value to get the predict value
:param return_numpy: if return numpy value for row tensor
:return: the predict value of the inference model :return: the predict value of the inference model
""" """
if not isinstance(inputs, dict): if not isinstance(inputs, dict):
raise ValueError( raise ValueError(
"inputs should be a map of {'input_name': input_var}") "inputs should be a map of {'input_name': input_var}")
with executor.scope_guard(self.scope): with self._prog_and_scope_guard():
results = self.exe.run(self.inference_program, results = self.exe.run(feed=inputs,
feed=inputs, fetch_list=[self.predict_var.name])
fetch_list=[self.predict_var],
return_numpy=return_numpy)
return results return results
@contextlib.contextmanager
def _prog_and_scope_guard(self):
with framework.program_guard(main_program=self.inference_program):
with executor.scope_guard(self.scope):
yield
...@@ -94,7 +94,7 @@ def infer(use_cuda, inference_program, save_dirname=None): ...@@ -94,7 +94,7 @@ def infer(use_cuda, inference_program, save_dirname=None):
tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32") tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32")
results = inferencer.infer({'x': tensor_x}) results = inferencer.infer({'x': tensor_x})
print("infer results: ", results[0]) print("infer results: ", numpy.array(results[0]))
def main(use_cuda): def main(use_cuda):
......
...@@ -118,7 +118,7 @@ def infer(use_cuda, inference_program, save_dirname=None): ...@@ -118,7 +118,7 @@ def infer(use_cuda, inference_program, save_dirname=None):
results = inferencer.infer({'img': tensor_img}) results = inferencer.infer({'img': tensor_img})
print("infer results: ", results[0]) print("infer results: ", numpy.array(results[0]))
def main(use_cuda): def main(use_cuda):
......
...@@ -99,7 +99,7 @@ def infer(use_cuda, inference_program, save_dirname=None): ...@@ -99,7 +99,7 @@ def infer(use_cuda, inference_program, save_dirname=None):
results = inferencer.infer({'img': tensor_img}) results = inferencer.infer({'img': tensor_img})
print("infer results: ", results[0]) print("infer results: ", numpy.array(results[0]))
def main(use_cuda): def main(use_cuda):
......
...@@ -127,14 +127,12 @@ def infer(use_cuda, inference_program, save_path): ...@@ -127,14 +127,12 @@ def infer(use_cuda, inference_program, save_path):
third_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1) third_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1)
fourth_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1) fourth_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1)
result = inferencer.infer( result = inferencer.infer({
{ 'firstw': first_word,
'firstw': first_word, 'secondw': second_word,
'secondw': second_word, 'thirdw': third_word,
'thirdw': third_word, 'forthw': fourth_word
'forthw': fourth_word })
},
return_numpy=False)
print(np.array(result[0])) print(np.array(result[0]))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册