From 04f86364c9d07fae3cad8713e87ab1e73c381c62 Mon Sep 17 00:00:00 2001 From: Bobholamovic Date: Fri, 15 Jul 2022 20:34:56 +0800 Subject: [PATCH] Add tool unittests --- tests/deploy/test_export.py | 13 +++ tests/deploy/test_predict.py | 13 +++ tests/rs_models/test_model.py | 149 +++++++++++++++--------------- tests/tools/test_coco2mask.py | 0 tests/tools/test_geojson2mask.py | 0 tests/tools/test_match.py | 0 tests/tools/test_oif.py | 0 tests/tools/test_pca.py | 0 tests/tools/test_raster2vector.py | 0 tests/tools/test_split.py | 0 10 files changed, 103 insertions(+), 72 deletions(-) create mode 100644 tests/deploy/test_export.py create mode 100644 tests/deploy/test_predict.py create mode 100644 tests/tools/test_coco2mask.py create mode 100644 tests/tools/test_geojson2mask.py create mode 100644 tests/tools/test_match.py create mode 100644 tests/tools/test_oif.py create mode 100644 tests/tools/test_pca.py create mode 100644 tests/tools/test_raster2vector.py create mode 100644 tests/tools/test_split.py diff --git a/tests/deploy/test_export.py b/tests/deploy/test_export.py new file mode 100644 index 0000000..29c8b7d --- /dev/null +++ b/tests/deploy/test_export.py @@ -0,0 +1,13 @@ +# Copyright (c) 2022 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. diff --git a/tests/deploy/test_predict.py b/tests/deploy/test_predict.py new file mode 100644 index 0000000..29c8b7d --- /dev/null +++ b/tests/deploy/test_predict.py @@ -0,0 +1,13 @@ +# Copyright (c) 2022 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. diff --git a/tests/rs_models/test_model.py b/tests/rs_models/test_model.py index 743e293..8155556 100644 --- a/tests/rs_models/test_model.py +++ b/tests/rs_models/test_model.py @@ -15,75 +15,80 @@ import paddle import numpy as np -from test_utils import CommonTest - - -class TestModel(CommonTest): - MODEL_CLASS = None - DEFAULT_HW = (256, 256) - DEFAULT_BATCH_SIZE = 2 - - def setUp(self): - self.set_specs() - self.set_inputs() - self.set_targets() - self.set_models() - - def test_forward(self): - for i, (input, model, target - ) in enumerate(zip(self.inputs, self.models, self.targets)): - with self.subTest(i=i): - output = model(input) - self.check_output(output, target) - - def check_output(self, output, target): - pass - - def set_specs(self): - self.specs = [] - - def set_models(self): - self.models = (self.build_model(spec) for spec in self.specs) - - def set_inputs(self): - self.inputs = [] - - def set_targets(self): - self.targets = [] - - def build_model(self, spec): - if '_phase' in spec: - phase = spec.pop('_phase') - else: - phase = 'train' - if '_stop_grad' in spec: - stop_grad = spec.pop('_stop_grad') - else: - stop_grad = False - - model = self.MODEL_CLASS(**spec) - - if phase == 'train': - model.train() - elif phase == 'eval': - model.eval() - if stop_grad: - for p in model.parameters(): - p.stop_gradient = True - - return model - - def get_shape(self, c, b=None, h=None, w=None): - if h is None or w is None: - h, w = self.DEFAULT_HW - if b is None: - b = self.DEFAULT_BATCH_SIZE - return (b, c, h, w) - - def get_zeros_array(self, c, b=None, h=None, w=None): - shape = self.get_shape(c, b, h, w) - return np.zeros(shape) - - def get_randn_tensor(self, c, b=None, h=None, w=None): - shape = self.get_shape(c, b, h, w) - return paddle.randn(shape) +from testing_utils import CommonTest + + +class _TestModelNamespace: + class TestModel(CommonTest): + MODEL_CLASS = None + DEFAULT_HW = (256, 256) + DEFAULT_BATCH_SIZE = 2 + + def setUp(self): + self.set_specs() + self.set_inputs() + self.set_targets() + self.set_models() + + def test_forward(self): + for i, ( + input, model, target + ) in enumerate(zip(self.inputs, self.models, self.targets)): + with self.subTest(i=i): + output = model(input) + self.check_output(output, target) + + def check_output(self, output, target): + pass + + def set_specs(self): + self.specs = [] + + def set_models(self): + self.models = (self.build_model(spec) for spec in self.specs) + + def set_inputs(self): + self.inputs = [] + + def set_targets(self): + self.targets = [] + + def build_model(self, spec): + if '_phase' in spec: + phase = spec.pop('_phase') + else: + phase = 'train' + if '_stop_grad' in spec: + stop_grad = spec.pop('_stop_grad') + else: + stop_grad = False + + model = self.MODEL_CLASS(**spec) + + if phase == 'train': + model.train() + elif phase == 'eval': + model.eval() + if stop_grad: + for p in model.parameters(): + p.stop_gradient = True + + return model + + def get_shape(self, c, b=None, h=None, w=None): + if h is None or w is None: + h, w = self.DEFAULT_HW + if b is None: + b = self.DEFAULT_BATCH_SIZE + return (b, c, h, w) + + def get_zeros_array(self, c, b=None, h=None, w=None): + shape = self.get_shape(c, b, h, w) + return np.zeros(shape) + + def get_randn_tensor(self, c, b=None, h=None, w=None): + shape = self.get_shape(c, b, h, w) + return paddle.randn(shape) + + +TestModel = _TestModelNamespace.TestModel diff --git a/tests/tools/test_coco2mask.py b/tests/tools/test_coco2mask.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tools/test_geojson2mask.py b/tests/tools/test_geojson2mask.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tools/test_match.py b/tests/tools/test_match.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tools/test_oif.py b/tests/tools/test_oif.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tools/test_pca.py b/tests/tools/test_pca.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tools/test_raster2vector.py b/tests/tools/test_raster2vector.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tools/test_split.py b/tests/tools/test_split.py new file mode 100644 index 0000000..e69de29