From 790d5226b544f15320bfcfc0bb0665627bb7ebe5 Mon Sep 17 00:00:00 2001 From: Ghost Under Moon Date: Tue, 24 Sep 2019 10:41:24 +0800 Subject: [PATCH] give warnings when save a model without any parameters (#19931) * give warnings when save a model without any parameters test=develop * delete one line comment test=develop --- python/paddle/fluid/io.py | 7 +++ .../unittests/test_save_model_without_var.py | 57 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_save_model_without_var.py diff --git a/python/paddle/fluid/io.py b/python/paddle/fluid/io.py index 858be1ff7d..7d864cfbb4 100644 --- a/python/paddle/fluid/io.py +++ b/python/paddle/fluid/io.py @@ -217,6 +217,13 @@ def save_vars(executor, vars=list(filter(predicate, main_program.list_vars())), filename=filename) else: + # give warning when there is no var in model + if len(list(vars)) == 0: + warnings.warn( + "no variable in your model, please ensure there are any variables in your model to save" + ) + return None + save_program = Program() save_block = save_program.global_block() diff --git a/python/paddle/fluid/tests/unittests/test_save_model_without_var.py b/python/paddle/fluid/tests/unittests/test_save_model_without_var.py new file mode 100644 index 0000000000..b74a6e1091 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_save_model_without_var.py @@ -0,0 +1,57 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import numpy as np +import warnings +import unittest +import paddle +import paddle.fluid as fluid +from paddle.fluid.layers.device import get_places +from paddle.fluid.executor import as_numpy + + +class TestSaveModelWithoutVar(unittest.TestCase): + def test_no_var_save(self): + data = fluid.layers.data( + name='data', + shape=[-1, 1], + dtype='float32', + append_batch_size=False) + data_plus = data + 1 + + if fluid.core.is_compiled_with_cuda(): + place = fluid.core.CUDAPlace(0) + else: + place = fluid.core.CPUPlace() + + exe = fluid.Executor(place) + exe.run(fluid.default_startup_program()) + + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + + fluid.io.save_inference_model( + dirname='test', + feeded_var_names=['data'], + target_vars=[data_plus], + executor=exe, + model_filename='model', + params_filename='params') + expected_warn = "no variable in your model, please ensure there are any variables in your model to save" + self.assertTrue(len(w) > 0) + self.assertTrue(expected_warn == str(w[0].message)) + + +if __name__ == '__main__': + unittest.main() -- GitLab