From 7e8b2f93a6983dd6dbf3dd1889082d750fefa24a Mon Sep 17 00:00:00 2001 From: LiuChiaChi <709153940@qq.com> Date: Sat, 10 Oct 2020 11:19:52 +0000 Subject: [PATCH 1/2] fix model bugs, inputs can be InputSpec instance --- python/paddle/hapi/model.py | 23 +++++++++++++---------- python/paddle/tests/test_model.py | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/python/paddle/hapi/model.py b/python/paddle/hapi/model.py index 459d6cd3284e9..d390ac04195a8 100644 --- a/python/paddle/hapi/model.py +++ b/python/paddle/hapi/model.py @@ -201,8 +201,11 @@ def _init_context(): def _update_input_shapes(inputs): + "Get input shape list by given inputs in Model initialization." shapes = None - if isinstance(inputs, list): + if isinstance(inputs, Input): + shapes = [list(inputs.shape)] + elif isinstance(inputs, list): shapes = [list(input.shape) for input in inputs] elif isinstance(inputs, dict): shapes = [list(inputs[name].shape) for name in inputs] @@ -917,9 +920,7 @@ def train_batch(self, inputs, labels=None): """ loss = self._adapter.train_batch(inputs, labels) if fluid.in_dygraph_mode() and self._input_shapes is None: - self._input_shapes = self._adapter._input_shapes - self._is_shape_inferred = True - self._inputs = self._verify_spec(None, self._input_shapes, True) + self._update_inputs() return loss def eval_batch(self, inputs, labels=None): @@ -967,9 +968,7 @@ def eval_batch(self, inputs, labels=None): """ loss = self._adapter.eval_batch(inputs, labels) if fluid.in_dygraph_mode() and self._input_shapes is None: - self._input_shapes = self._adapter._input_shapes - self._is_shape_inferred = True - self._inputs = self._verify_spec(None, self._input_shapes, True) + self._update_inputs() return loss def test_batch(self, inputs): @@ -1012,9 +1011,7 @@ def test_batch(self, inputs): """ loss = self._adapter.test_batch(inputs) if fluid.in_dygraph_mode() and self._input_shapes is None: - self._input_shapes = self._adapter._input_shapes - self._is_shape_inferred = True - self._inputs = self._verify_spec(None, self._input_shapes, True) + self._update_inputs() return loss def save(self, path, training=True): @@ -1953,3 +1950,9 @@ def _len_data_loader(self, data_loader): except Exception: steps = None return steps + + def _update_inputs(self): + "Update self._inputs according to given inputs." + self._input_shapes = self._adapter._input_shapes + self._is_shape_inferred = True + self._inputs = self._verify_spec(None, self._input_shapes, True) diff --git a/python/paddle/tests/test_model.py b/python/paddle/tests/test_model.py index 96c4483a35ba8..13fa5fe6dd298 100644 --- a/python/paddle/tests/test_model.py +++ b/python/paddle/tests/test_model.py @@ -551,9 +551,10 @@ def test_export_deploy_model(self): shutil.rmtree(save_dir) paddle.enable_static() - def test_dygraph_export_deploy_model_without_inputs(self): + def test_dygraph_export_deploy_model_about_inputs(self): mnist_data = MnistDataset(mode='train') paddle.disable_static() + # without inputs for initial in ["fit", "train_batch", "eval_batch", "test_batch"]: save_dir = tempfile.mkdtemp() if not os.path.exists(save_dir): @@ -579,6 +580,18 @@ def test_dygraph_export_deploy_model_without_inputs(self): model.save(save_dir, training=False) shutil.rmtree(save_dir) + # with inputs, and the type of inputs is InputSpec + save_dir = tempfile.mkdtemp() + if not os.path.exists(save_dir): + os.makedirs(save_dir) + net = LeNet() + inputs = InputSpec([None, 1, 28, 28], 'float32', 'x') + model = Model(net, inputs) + optim = fluid.optimizer.Adam( + learning_rate=0.001, parameter_list=model.parameters()) + model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum")) + model.save(save_dir, training=False) + shutil.rmtree(save_dir) class TestRaiseError(unittest.TestCase): From c927228d9188e38886ada5bd647578f1f0a9566b Mon Sep 17 00:00:00 2001 From: LiuChiaChi <709153940@qq.com> Date: Sat, 10 Oct 2020 13:27:10 +0000 Subject: [PATCH 2/2] correct error message --- python/paddle/hapi/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/hapi/model.py b/python/paddle/hapi/model.py index d390ac04195a8..21e3054dde7d7 100644 --- a/python/paddle/hapi/model.py +++ b/python/paddle/hapi/model.py @@ -1704,7 +1704,7 @@ def get_inout_spec(all_vars, return_name=False): layer = self.network if self._input_shapes is None: # No provided or inferred raise RuntimeError( - "Saving inference model needs 'inputs' or running before saving. Please specify 'inputs' in Model initialization or input training zqqdata and perform a training for shape derivation." + "Saving inference model needs 'inputs' or running before saving. Please specify 'inputs' in Model initialization or input training data and perform a training for shape derivation." ) if self._is_shape_inferred: warnings.warn(