未验证 提交 10af966a 编写于 作者: C CtfGo 提交者: GitHub

update the TraceLayer.save_inference_model method with add file suffix automatically (#31989)

As the title
上级 f5186c3c
...@@ -509,33 +509,33 @@ def save(layer, path, input_spec=None, **configs): ...@@ -509,33 +509,33 @@ def save(layer, path, input_spec=None, **configs):
Saves input Layer as ``paddle.jit.TranslatedLayer`` Saves input Layer as ``paddle.jit.TranslatedLayer``
format model, which can be used for inference or fine-tuning after loading. format model, which can be used for inference or fine-tuning after loading.
It will save the translated program and all related persistable It will save the translated program and all related persistable
variables of input Layer to given ``path`` . variables of input Layer to given ``path`` .
``path`` is the prefix of saved objects, and the saved translated program file ``path`` is the prefix of saved objects, and the saved translated program file
suffix is ``.pdmodel`` , the saved persistable variables file suffix is ``.pdiparams`` , suffix is ``.pdmodel`` , the saved persistable variables file suffix is ``.pdiparams`` ,
and here also saved some additional variable description information to a file, and here also saved some additional variable description information to a file,
its suffix is ``.pdiparams.info``, these additional information is used in fine-tuning. its suffix is ``.pdiparams.info``, these additional information is used in fine-tuning.
The saved model can be loaded by follow APIs: The saved model can be loaded by follow APIs:
- ``paddle.jit.load`` - ``paddle.jit.load``
- ``paddle.static.load_inference_model`` - ``paddle.static.load_inference_model``
- Other C++ inference APIs - Other C++ inference APIs
Args: Args:
layer (Layer): The Layer to be saved. layer (Layer): The Layer to be saved.
path (str): The path prefix to save model. The format is ``dirname/file_prefix`` or ``file_prefix``. path (str): The path prefix to save model. The format is ``dirname/file_prefix`` or ``file_prefix``.
input_spec (list[InputSpec|Tensor], optional): Describes the input of the saved model's forward input_spec (list[InputSpec|Tensor], optional): Describes the input of the saved model's forward
method, which can be described by InputSpec or example Tensor. If None, all input variables of method, which can be described by InputSpec or example Tensor. If None, all input variables of
the original Layer's forward method would be the inputs of the saved model. Default None. the original Layer's forward method would be the inputs of the saved model. Default None.
**configs (dict, optional): Other save configuration options for compatibility. We do not **configs (dict, optional): Other save configuration options for compatibility. We do not
recommend using these configurations, they may be removed in the future. If not necessary, recommend using these configurations, they may be removed in the future. If not necessary,
DO NOT use them. Default None. DO NOT use them. Default None.
The following options are currently supported: The following options are currently supported:
(1) output_spec (list[Tensor]): Selects the output targets of the saved model. (1) output_spec (list[Tensor]): Selects the output targets of the saved model.
By default, all return variables of original Layer's forward method are kept as the By default, all return variables of original Layer's forward method are kept as the
output of the saved model. If the provided ``output_spec`` list is not all output variables, output of the saved model. If the provided ``output_spec`` list is not all output variables,
the saved model will be pruned according to the given ``output_spec`` list. the saved model will be pruned according to the given ``output_spec`` list.
Returns: Returns:
None None
...@@ -793,8 +793,8 @@ def load(path, **configs): ...@@ -793,8 +793,8 @@ def load(path, **configs):
""" """
:api_attr: imperative :api_attr: imperative
Load model saved by ``paddle.jit.save`` or ``paddle.static.save_inference_model`` or Load model saved by ``paddle.jit.save`` or ``paddle.static.save_inference_model`` or
paddle 1.x API ``paddle.fluid.io.save_inference_model`` as ``paddle.jit.TranslatedLayer``, paddle 1.x API ``paddle.fluid.io.save_inference_model`` as ``paddle.jit.TranslatedLayer``,
then performing inference or fine-tune training. then performing inference or fine-tune training.
.. note:: .. note::
...@@ -807,14 +807,14 @@ def load(path, **configs): ...@@ -807,14 +807,14 @@ def load(path, **configs):
Args: Args:
path (str): The path prefix to load model. The format is ``dirname/file_prefix`` or ``file_prefix`` . path (str): The path prefix to load model. The format is ``dirname/file_prefix`` or ``file_prefix`` .
**configs (dict, optional): Other load configuration options for compatibility. We do not **configs (dict, optional): Other load configuration options for compatibility. We do not
recommend using these configurations, they may be removed in the future. If not necessary, recommend using these configurations, they may be removed in the future. If not necessary,
DO NOT use them. Default None. DO NOT use them. Default None.
The following options are currently supported: The following options are currently supported:
(1) model_filename (str): The inference model file name of the paddle 1.x (1) model_filename (str): The inference model file name of the paddle 1.x
``save_inference_model`` save format. Default file name is :code:`__model__` . ``save_inference_model`` save format. Default file name is :code:`__model__` .
(2) params_filename (str): The persistable variables file name of the paddle 1.x (2) params_filename (str): The persistable variables file name of the paddle 1.x
``save_inference_model`` save format. No default file name, save variables separately ``save_inference_model`` save format. No default file name, save variables separately
by default. by default.
...@@ -960,7 +960,7 @@ def load(path, **configs): ...@@ -960,7 +960,7 @@ def load(path, **configs):
loader = paddle.io.DataLoader(dataset, loader = paddle.io.DataLoader(dataset,
feed_list=[image, label], feed_list=[image, label],
places=place, places=place,
batch_size=BATCH_SIZE, batch_size=BATCH_SIZE,
shuffle=True, shuffle=True,
drop_last=True, drop_last=True,
num_workers=2) num_workers=2)
...@@ -969,7 +969,7 @@ def load(path, **configs): ...@@ -969,7 +969,7 @@ def load(path, **configs):
for data in loader(): for data in loader():
exe.run( exe.run(
static.default_main_program(), static.default_main_program(),
feed=data, feed=data,
fetch_list=[avg_loss]) fetch_list=[avg_loss])
model_path = "fc.example.model" model_path = "fc.example.model"
...@@ -1052,7 +1052,7 @@ def _trace(layer, ...@@ -1052,7 +1052,7 @@ def _trace(layer,
class TracedLayer(object): class TracedLayer(object):
""" """
:api_attr: imperative :api_attr: imperative
TracedLayer is used to convert a forward dygraph model to a static TracedLayer is used to convert a forward dygraph model to a static
graph model. This is mainly used to save the dygraph model for online graph model. This is mainly used to save the dygraph model for online
inference using C++. Besides, users can also do inference in Python inference using C++. Besides, users can also do inference in Python
...@@ -1132,7 +1132,7 @@ class TracedLayer(object): ...@@ -1132,7 +1132,7 @@ class TracedLayer(object):
def forward(self, input): def forward(self, input):
return self._fc(input) return self._fc(input)
layer = ExampleLayer() layer = ExampleLayer()
in_var = paddle.uniform(shape=[2, 3], dtype='float32') in_var = paddle.uniform(shape=[2, 3], dtype='float32')
out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var]) out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var])
...@@ -1244,13 +1244,16 @@ class TracedLayer(object): ...@@ -1244,13 +1244,16 @@ class TracedLayer(object):
return self._run(self._build_feed(inputs)) return self._run(self._build_feed(inputs))
@switch_to_static_graph @switch_to_static_graph
def save_inference_model(self, dirname, feed=None, fetch=None): def save_inference_model(self, path, feed=None, fetch=None):
""" """
Save the TracedLayer to a model for inference. The saved Save the TracedLayer to a model for inference. The saved
inference model can be loaded by C++ inference APIs. inference model can be loaded by C++ inference APIs.
``path`` is the prefix of saved objects, and the saved translated program file
suffix is ``.pdmodel`` , the saved persistable variables file suffix is ``.pdiparams`` .
Args: Args:
dirname (str): the directory to save the inference model. path(str): The path prefix to save model. The format is ``dirname/file_prefix`` or ``file_prefix``.
feed (list[int], optional): the input variable indices of the saved feed (list[int], optional): the input variable indices of the saved
inference model. If None, all input variables of the inference model. If None, all input variables of the
TracedLayer object would be the inputs of the saved inference TracedLayer object would be the inputs of the saved inference
...@@ -1294,7 +1297,7 @@ class TracedLayer(object): ...@@ -1294,7 +1297,7 @@ class TracedLayer(object):
fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars) fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars)
print(fetch.shape) # (2, 10) print(fetch.shape) # (2, 10)
""" """
check_type(dirname, "dirname", str, check_type(path, "path", str,
"fluid.dygraph.jit.TracedLayer.save_inference_model") "fluid.dygraph.jit.TracedLayer.save_inference_model")
check_type(feed, "feed", (type(None), list), check_type(feed, "feed", (type(None), list),
"fluid.dygraph.jit.TracedLayer.save_inference_model") "fluid.dygraph.jit.TracedLayer.save_inference_model")
...@@ -1309,6 +1312,18 @@ class TracedLayer(object): ...@@ -1309,6 +1312,18 @@ class TracedLayer(object):
check_type(f, "each element of fetch", int, check_type(f, "each element of fetch", int,
"fluid.dygraph.jit.TracedLayer.save_inference_model") "fluid.dygraph.jit.TracedLayer.save_inference_model")
# path check
file_prefix = os.path.basename(path)
if file_prefix == "":
raise ValueError(
"The input path MUST be format of dirname/file_prefix "
"[dirname\\file_prefix in Windows system], but received "
"file_prefix is empty string.")
dirname = os.path.dirname(path)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
from paddle.fluid.io import save_inference_model from paddle.fluid.io import save_inference_model
def get_feed_fetch(all_vars, partial_vars): def get_feed_fetch(all_vars, partial_vars):
...@@ -1326,9 +1341,14 @@ class TracedLayer(object): ...@@ -1326,9 +1341,14 @@ class TracedLayer(object):
assert target_var is not None, "{} cannot be found".format(name) assert target_var is not None, "{} cannot be found".format(name)
target_vars.append(target_var) target_vars.append(target_var)
model_filename = file_prefix + INFER_MODEL_SUFFIX
params_filename = file_prefix + INFER_PARAMS_SUFFIX
save_inference_model( save_inference_model(
dirname=dirname, dirname=dirname,
feeded_var_names=feeded_var_names, feeded_var_names=feeded_var_names,
target_vars=target_vars, target_vars=target_vars,
executor=self._exe, executor=self._exe,
main_program=self._program.clone()) main_program=self._program.clone(),
model_filename=model_filename,
params_filename=params_filename)
...@@ -75,10 +75,12 @@ class TestTracedLayerRecordNonPersistableInput(unittest.TestCase): ...@@ -75,10 +75,12 @@ class TestTracedLayerRecordNonPersistableInput(unittest.TestCase):
self.assertEqual(actual_persistable_vars, expected_persistable_vars) self.assertEqual(actual_persistable_vars, expected_persistable_vars)
dirname = './traced_layer_test_non_persistable_vars' traced_layer.save_inference_model(
traced_layer.save_inference_model(dirname=dirname) path='./traced_layer_test_non_persistable_vars')
filenames = set([f for f in os.listdir(dirname) if f != '__model__']) self.assertTrue('traced_layer_test_non_persistable_vars.pdmodel' in
self.assertEqual(filenames, expected_persistable_vars) os.listdir('./'))
self.assertTrue('traced_layer_test_non_persistable_vars.pdiparams' in
os.listdir('./'))
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -18,6 +18,7 @@ import paddle.fluid as fluid ...@@ -18,6 +18,7 @@ import paddle.fluid as fluid
import six import six
import unittest import unittest
import paddle.nn as nn import paddle.nn as nn
import os
class SimpleFCLayer(nn.Layer): class SimpleFCLayer(nn.Layer):
...@@ -115,36 +116,41 @@ class TestTracedLayerErrMsg(unittest.TestCase): ...@@ -115,36 +116,41 @@ class TestTracedLayerErrMsg(unittest.TestCase):
dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace( dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace(
self.layer, [in_x]) self.layer, [in_x])
dirname = './traced_layer_err_msg' path = './traced_layer_err_msg'
with self.assertRaises(TypeError) as e: with self.assertRaises(TypeError) as e:
traced_layer.save_inference_model([0]) traced_layer.save_inference_model([0])
self.assertEqual( self.assertEqual(
"The type of 'dirname' in fluid.dygraph.jit.TracedLayer.save_inference_model must be <{} 'str'>, but received <{} 'list'>. ". "The type of 'path' in fluid.dygraph.jit.TracedLayer.save_inference_model must be <{} 'str'>, but received <{} 'list'>. ".
format(self.type_str, self.type_str), str(e.exception)) format(self.type_str, self.type_str), str(e.exception))
with self.assertRaises(TypeError) as e: with self.assertRaises(TypeError) as e:
traced_layer.save_inference_model(dirname, [0], [None]) traced_layer.save_inference_model(path, [0], [None])
self.assertEqual( self.assertEqual(
"The type of 'each element of fetch' in fluid.dygraph.jit.TracedLayer.save_inference_model must be <{} 'int'>, but received <{} 'NoneType'>. ". "The type of 'each element of fetch' in fluid.dygraph.jit.TracedLayer.save_inference_model must be <{} 'int'>, but received <{} 'NoneType'>. ".
format(self.type_str, self.type_str), str(e.exception)) format(self.type_str, self.type_str), str(e.exception))
with self.assertRaises(TypeError) as e: with self.assertRaises(TypeError) as e:
traced_layer.save_inference_model(dirname, [0], False) traced_layer.save_inference_model(path, [0], False)
self.assertEqual( self.assertEqual(
"The type of 'fetch' in fluid.dygraph.jit.TracedLayer.save_inference_model must be (<{} 'NoneType'>, <{} 'list'>), but received <{} 'bool'>. ". "The type of 'fetch' in fluid.dygraph.jit.TracedLayer.save_inference_model must be (<{} 'NoneType'>, <{} 'list'>), but received <{} 'bool'>. ".
format(self.type_str, self.type_str, self.type_str), format(self.type_str, self.type_str, self.type_str),
str(e.exception)) str(e.exception))
with self.assertRaises(TypeError) as e: with self.assertRaises(TypeError) as e:
traced_layer.save_inference_model(dirname, [None], [0]) traced_layer.save_inference_model(path, [None], [0])
self.assertEqual( self.assertEqual(
"The type of 'each element of feed' in fluid.dygraph.jit.TracedLayer.save_inference_model must be <{} 'int'>, but received <{} 'NoneType'>. ". "The type of 'each element of feed' in fluid.dygraph.jit.TracedLayer.save_inference_model must be <{} 'int'>, but received <{} 'NoneType'>. ".
format(self.type_str, self.type_str), str(e.exception)) format(self.type_str, self.type_str), str(e.exception))
with self.assertRaises(TypeError) as e: with self.assertRaises(TypeError) as e:
traced_layer.save_inference_model(dirname, True, [0]) traced_layer.save_inference_model(path, True, [0])
self.assertEqual( self.assertEqual(
"The type of 'feed' in fluid.dygraph.jit.TracedLayer.save_inference_model must be (<{} 'NoneType'>, <{} 'list'>), but received <{} 'bool'>. ". "The type of 'feed' in fluid.dygraph.jit.TracedLayer.save_inference_model must be (<{} 'NoneType'>, <{} 'list'>), but received <{} 'bool'>. ".
format(self.type_str, self.type_str, self.type_str), format(self.type_str, self.type_str, self.type_str),
str(e.exception)) str(e.exception))
with self.assertRaises(ValueError) as e:
traced_layer.save_inference_model("")
self.assertEqual(
"The input path MUST be format of dirname/file_prefix [dirname\\file_prefix in Windows system], "
"but received file_prefix is empty string.", str(e.exception))
traced_layer.save_inference_model(dirname) traced_layer.save_inference_model(path)
def _train_simple_net(self): def _train_simple_net(self):
layer = None layer = None
...@@ -174,5 +180,25 @@ class TestOutVarWithNoneErrMsg(unittest.TestCase): ...@@ -174,5 +180,25 @@ class TestOutVarWithNoneErrMsg(unittest.TestCase):
[in_x]) [in_x])
class TestTracedLayerSaveInferenceModel(unittest.TestCase):
"""test save_inference_model will automaticlly create non-exist dir"""
def setUp(self):
self.save_path = "./nonexist_dir/fc"
import shutil
if os.path.exists(os.path.dirname(self.save_path)):
shutil.rmtree(os.path.dirname(self.save_path))
def test_mkdir_when_input_path_non_exist(self):
fc_layer = SimpleFCLayer(3, 4, 2)
input_var = paddle.to_tensor(np.random.random([4, 3]).astype('float32'))
with fluid.dygraph.guard():
dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace(
fc_layer, inputs=[input_var])
self.assertFalse(os.path.exists(os.path.dirname(self.save_path)))
traced_layer.save_inference_model(self.save_path)
self.assertTrue(os.path.exists(os.path.dirname(self.save_path)))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册