From ebcf21ea3d4faa7d99d8f1566c96bc9ca967fd10 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 25 Mar 2024 11:12:02 +0800 Subject: [PATCH 01/27] add pad for python --- .../graph/preprocess/pre_post_process.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp index 2cc33c8a2e4..750fdbf0a36 100644 --- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp +++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp @@ -370,6 +370,24 @@ static void regclass_graph_OutputTensorInfo(py::module m) { :param layout: layout to be set :type layout: Union[str, openvino.runtime.Layout] )"); + + steps.def("pad", + [](ov::preprocess::PreProcessSteps& self, + const std::vector& pads_begin, + const std::vector& pads_end, + float value, + ov::preprocess::PaddingMode mode) { + return &self.pad(pads_begin, pads_end, value, mode); + }); + + steps.def("pad", + [](ov::preprocess::PreProcessSteps& self, + const std::vector& pads_begin, + const std::vector& pads_end, + const std::vector& values, + ov::preprocess::PaddingMode mode) { + return &self.pad(pads_begin, pads_end, values, mode); + }); } static void regclass_graph_InputInfo(py::module m) { From 9179756652c3af8328679606500ba7c58b1209d8 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 25 Mar 2024 12:42:55 +0800 Subject: [PATCH 02/27] fix bug --- .../graph/preprocess/pre_post_process.cpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp index 750fdbf0a36..0fe6a204947 100644 --- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp +++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp @@ -169,6 +169,24 @@ static void regclass_graph_PreProcessSteps(py::module m) { steps.def("reverse_channels", [](ov::preprocess::PreProcessSteps& self) { return &self.reverse_channels(); }); + + steps.def("pad", + [](ov::preprocess::PreProcessSteps& self, + const std::vector& pads_begin, + const std::vector& pads_end, + float value, + ov::preprocess::PaddingMode mode) { + return &self.pad(pads_begin, pads_end, value, mode); + }); + + steps.def("pad", + [](ov::preprocess::PreProcessSteps& self, + const std::vector& pads_begin, + const std::vector& pads_end, + const std::vector& values, + ov::preprocess::PaddingMode mode) { + return &self.pad(pads_begin, pads_end, values, mode); + }); } static void regclass_graph_PostProcessSteps(py::module m) { @@ -370,24 +388,6 @@ static void regclass_graph_OutputTensorInfo(py::module m) { :param layout: layout to be set :type layout: Union[str, openvino.runtime.Layout] )"); - - steps.def("pad", - [](ov::preprocess::PreProcessSteps& self, - const std::vector& pads_begin, - const std::vector& pads_end, - float value, - ov::preprocess::PaddingMode mode) { - return &self.pad(pads_begin, pads_end, value, mode); - }); - - steps.def("pad", - [](ov::preprocess::PreProcessSteps& self, - const std::vector& pads_begin, - const std::vector& pads_end, - const std::vector& values, - ov::preprocess::PaddingMode mode) { - return &self.pad(pads_begin, pads_end, values, mode); - }); } static void regclass_graph_InputInfo(py::module m) { From 7a122dfdd1b9ed4bf221c2cb94b162e4d5c98ea3 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Tue, 26 Mar 2024 13:15:00 +0800 Subject: [PATCH 03/27] add test case --- .../tests/test_graph/test_preprocess.py | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index f3f93fd2095..55942191a0d 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -10,7 +10,7 @@ import openvino.runtime.opset13 as ops from openvino import Core, Layout, Model, Shape, Tensor, Type from openvino.runtime.utils.decorators import custom_preprocess_function from openvino.runtime import Output -from openvino.preprocess import PrePostProcessor, ColorFormat, ResizeAlgorithm +from openvino.preprocess import PrePostProcessor, ColorFormat, ResizeAlgorithm, PaddingMode def test_graph_preprocess_mean(): @@ -728,3 +728,28 @@ def test_graph_set_layout_by_layout_class_thow_exception(): layout = Layout("1-2-3D") ppp.input().model().set_layout(layout) assert "Layout name is invalid" in str(e.value) + +def test_pad_vector_constant_layout(): + shape = [1, 3, 200, 200] + parameter_a = ops.parameter(shape, dtype=np.float32, name="A") + model = parameter_a + model = Model(model, [parameter_a], "TestModel") + ppp = PrePostProcessor(model) + ppp.input().tensor().set_shape({1, 3, 199, 199}) + ppp.input().preprocess().pad({0, 0, 0, 0}, {0, 0, 1, 1}, 0, {0, 0, 0, 0}, {0, 0, 1, 1}, 0, PaddingMode.CONSTANT) + assert ppp.build() + + +def test_pad_vector_out_of_range(): + shape = [1, 3, 5, 5] + parameter_a = ops.parameter(shape, dtype=np.float32, name="A") + model = Model(model, [parameter_a], "TestModel") + ppp = PrePostProcessor(model) + assert not ppp.input().preprocess().pad({0, 0, -2, 0}, {0, 0, -4, 1}, 0, PaddingMode.CONSTANT) + +def test_pad_vector_dim_mismatch(): + shape = [1, 3, 5, 5] + parameter_a = ops.parameter(shape, dtype=np.float32, name="A") + model = Model(model, [parameter_a], "TestModel") + ppp = PrePostProcessor(model) + assert not ppp.input().preprocess().pad({0, 0, 2, 0, 1}, {0, 0, 4, 1, 1}, 0, PaddingMode.CONSTANT) \ No newline at end of file From ece4c42374bd92f43c1be02eb252823bd8d3d74f Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Tue, 26 Mar 2024 13:37:44 +0800 Subject: [PATCH 04/27] fix bug --- src/bindings/python/tests/test_graph/test_preprocess.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 55942191a0d..0bf41b8e106 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -736,13 +736,14 @@ def test_pad_vector_constant_layout(): model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) ppp.input().tensor().set_shape({1, 3, 199, 199}) - ppp.input().preprocess().pad({0, 0, 0, 0}, {0, 0, 1, 1}, 0, {0, 0, 0, 0}, {0, 0, 1, 1}, 0, PaddingMode.CONSTANT) + ppp.input().preprocess().pad({0, 0, 0, 0}, {0, 0, 1, 1}, 0, PaddingMode.CONSTANT) assert ppp.build() def test_pad_vector_out_of_range(): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") + model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) assert not ppp.input().preprocess().pad({0, 0, -2, 0}, {0, 0, -4, 1}, 0, PaddingMode.CONSTANT) @@ -750,6 +751,7 @@ def test_pad_vector_out_of_range(): def test_pad_vector_dim_mismatch(): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") + model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) assert not ppp.input().preprocess().pad({0, 0, 2, 0, 1}, {0, 0, 4, 1, 1}, 0, PaddingMode.CONSTANT) \ No newline at end of file From 0335341b7ca24b4b03a2f6f01dec6d39d49d34b8 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 1 Apr 2024 15:38:45 +0800 Subject: [PATCH 05/27] change shape size --- .../tests/test_graph/test_preprocess.py | 23 +++---------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 0bf41b8e106..9d1dfdd4c46 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -730,28 +730,11 @@ def test_graph_set_layout_by_layout_class_thow_exception(): assert "Layout name is invalid" in str(e.value) def test_pad_vector_constant_layout(): - shape = [1, 3, 200, 200] - parameter_a = ops.parameter(shape, dtype=np.float32, name="A") + shape = [1, 3, 224, 224] + parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - ppp.input().tensor().set_shape({1, 3, 199, 199}) + ppp.input().tensor().set_shape({1, 3, 223, 223}) ppp.input().preprocess().pad({0, 0, 0, 0}, {0, 0, 1, 1}, 0, PaddingMode.CONSTANT) assert ppp.build() - - -def test_pad_vector_out_of_range(): - shape = [1, 3, 5, 5] - parameter_a = ops.parameter(shape, dtype=np.float32, name="A") - model = parameter_a - model = Model(model, [parameter_a], "TestModel") - ppp = PrePostProcessor(model) - assert not ppp.input().preprocess().pad({0, 0, -2, 0}, {0, 0, -4, 1}, 0, PaddingMode.CONSTANT) - -def test_pad_vector_dim_mismatch(): - shape = [1, 3, 5, 5] - parameter_a = ops.parameter(shape, dtype=np.float32, name="A") - model = parameter_a - model = Model(model, [parameter_a], "TestModel") - ppp = PrePostProcessor(model) - assert not ppp.input().preprocess().pad({0, 0, 2, 0, 1}, {0, 0, 4, 1, 1}, 0, PaddingMode.CONSTANT) \ No newline at end of file From d917632c72c067f77b256d10ec897f17ca552912 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 8 Apr 2024 17:48:42 +0800 Subject: [PATCH 06/27] fix code style --- .../tests/test_graph/test_preprocess.py | 142 ++++++++++-------- 1 file changed, 81 insertions(+), 61 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 9d1dfdd4c46..f347cd3d507 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -41,7 +41,7 @@ def test_graph_preprocess_mean_vector(): ppp = PrePostProcessor(model) ppp.input().tensor().set_layout(layout) - ppp.input().preprocess().mean([1., 2.]) + ppp.input().preprocess().mean([1.0, 2.0]) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ordered_ops()] @@ -88,10 +88,10 @@ def test_graph_preprocess_mean_scale_convert(): ppp = PrePostProcessor(model) inp2 = ppp.input(1) inp2.tensor().set_element_type(Type.i32) - inp2.preprocess().convert_element_type(Type.f32).mean(1.).scale(2.) + inp2.preprocess().convert_element_type(Type.f32).mean(1.0).scale(2.0) inp2.preprocess().convert_element_type() inp1 = ppp.input(0) - inp1.preprocess().convert_element_type(Type.f32).mean(1.).custom(custom_preprocess) + inp1.preprocess().convert_element_type(Type.f32).mean(1.0).custom(custom_preprocess) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -127,9 +127,9 @@ def test_graph_preprocess_input_output_by_name(): ppp = PrePostProcessor(model) inp2 = ppp.input("B") inp2.tensor().set_element_type(Type.i32) - inp2.preprocess().convert_element_type(Type.f32).mean(1.).scale(2.) + inp2.preprocess().convert_element_type(Type.f32).mean(1.0).scale(2.0) inp1 = ppp.input("A") - inp1.preprocess().convert_element_type(Type.f32).mean(1.) + inp1.preprocess().convert_element_type(Type.f32).mean(1.0) out1 = ppp.output("A") out1.postprocess().custom(custom_preprocess) out2 = ppp.output("B") @@ -168,6 +168,7 @@ def test_graph_preprocess_output_postprocess(): @custom_preprocess_function def custom_postprocess(output: Output): return ops.abs(output) + ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) @@ -210,7 +211,7 @@ def test_graph_preprocess_spatial_static_shape(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout).set_spatial_static_shape(2, 2).set_color_format(color_format) - inp.preprocess().convert_element_type(Type.f32).mean([1., 2., 3.]) + inp.preprocess().convert_element_type(Type.f32).mean([1.0, 2.0, 3.0]) inp.model().set_layout(layout) out = ppp.output() out.tensor().set_layout(layout).set_element_type(Type.f32) @@ -302,9 +303,9 @@ def test_graph_preprocess_set_from_np_infer(): axis = ops.constant(np.array([0, 1, 2]), dtype=np.int32) return ops.slice(out_node, start, stop, step, axis) - input_data = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], - [[9, 10, 11], [12, 13, 14], [15, 16, 17]], - [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]).astype(np.int32) + input_data = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]).astype( + np.int32 + ) ppp = PrePostProcessor(model) inp = ppp.input() @@ -346,36 +347,38 @@ def test_graph_preprocess_set_memory_type(): @pytest.mark.parametrize( ("algorithm", "color_format1", "color_format2", "is_failing"), - [(ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.BGR, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_THREE_PLANES, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.BGR, False), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGB, False), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_THREE_PLANES, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.BGR, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.BGR, False), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGB, False), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.BGRX, True), - ]) + [ + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.BGR, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_THREE_PLANES, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.BGR, False), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGB, False), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_THREE_PLANES, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.BGR, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.BGR, False), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGB, False), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.BGRX, True), + ], +) def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_failing): shape = [1, 3, 3, 3] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") @@ -387,7 +390,7 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail custom_processor = PrePostProcessor(model) inp = custom_processor.input() inp.tensor().set_layout(layout1).set_color_format(color_format1, []) - inp.preprocess().mean(1.).resize(algorithm, 3, 3) + inp.preprocess().mean(1.0).resize(algorithm, 3, 3) inp.preprocess().convert_layout(layout2).convert_color(color_format2) if is_failing: @@ -414,9 +417,11 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail @pytest.mark.parametrize( ("color_format1", "color_format2", "tensor_in_shape", "model_in_shape"), - [(ColorFormat.RGB, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), - (ColorFormat.BGR, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), - ]) + [ + (ColorFormat.RGB, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), + (ColorFormat.BGR, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), + ], +) def test_graph_preprocess_convert_color(color_format1, color_format2, tensor_in_shape, model_in_shape): parameter_a = ops.parameter(model_in_shape, dtype=np.float32, name="A") model = parameter_a @@ -445,7 +450,7 @@ def test_graph_preprocess_postprocess_layout(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.).convert_layout(layout2).reverse_channels() + inp.preprocess().mean(1.0).convert_layout(layout2).reverse_channels() out = ppp.output() out.postprocess().convert_layout([0, 1, 2, 3]) model = ppp.build() @@ -477,7 +482,7 @@ def test_graph_preprocess_reverse_channels(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.).reverse_channels() + inp.preprocess().mean(1.0).reverse_channels() model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -535,7 +540,7 @@ def test_graph_preprocess_resize_algorithm(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.).resize(resize_alg, 3, 3) + inp.preprocess().mean(1.0).resize(resize_alg, 3, 3) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -555,7 +560,8 @@ def test_graph_preprocess_resize_algorithm(): def test_graph_preprocess_model(): - model = bytes(b""" + model = bytes( + b""" @@ -613,7 +619,8 @@ def test_graph_preprocess_model(): -""") +""" + ) core = Core() model = core.read_model(model=model) @@ -623,7 +630,7 @@ def test_graph_preprocess_model(): ppp = PrePostProcessor(model) ppp.input(1).preprocess().convert_element_type(Type.f32).scale(0.5) - ppp.input(0).preprocess().convert_element_type(Type.f32).mean(5.) + ppp.input(0).preprocess().convert_element_type(Type.f32).mean(5.0) ppp.output(0).postprocess().custom(custom_preprocess) model = ppp.build() @@ -670,9 +677,7 @@ def test_graph_preprocess_dump(): assert "convert layout " + Layout("NCHW").to_string() in p_str -@pytest.mark.parametrize( - ("layout", "layout_str"), - [("NHCW", "[N,H,C,W]"), ("NHWC", "[N,H,W,C]")]) +@pytest.mark.parametrize(("layout", "layout_str"), [("NHCW", "[N,H,C,W]"), ("NHWC", "[N,H,W,C]")]) def test_graph_set_layout_by_string(layout, layout_str): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -685,9 +690,7 @@ def test_graph_set_layout_by_string(layout, layout_str): assert f"{layout_str}" in p_str -@pytest.mark.parametrize( - ("layout", "layout_str"), - [(Layout("NHCW"), "[N,H,C,W]"), (Layout("NHWC"), "[N,H,W,C]")]) +@pytest.mark.parametrize(("layout", "layout_str"), [(Layout("NHCW"), "[N,H,C,W]"), (Layout("NHWC"), "[N,H,W,C]")]) def test_graph_set_layout_by_layout_class(layout, layout_str): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -700,9 +703,7 @@ def test_graph_set_layout_by_layout_class(layout, layout_str): assert f"{layout_str}" in p_str -@pytest.mark.parametrize( - ("layout"), - [("1-2-3D"), ("5-5")]) +@pytest.mark.parametrize(("layout"), [("1-2-3D"), ("5-5")]) def test_graph_set_layout_by_str_thow_exception(layout): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -729,12 +730,31 @@ def test_graph_set_layout_by_layout_class_thow_exception(): ppp.input().model().set_layout(layout) assert "Layout name is invalid" in str(e.value) + def test_pad_vector_constant_layout(): - shape = [1, 3, 224, 224] + shape = [1, 3, 200, 200] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - ppp.input().tensor().set_shape({1, 3, 223, 223}) - ppp.input().preprocess().pad({0, 0, 0, 0}, {0, 0, 1, 1}, 0, PaddingMode.CONSTANT) + ppp.input().tensor().set_shape([1, 3, 199, 199]) + ppp.input().preprocess().pad([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT) assert ppp.build() + + +def test_pad_vector_out_of_range(): + shape = [1, 3, 5, 5] + parameter_a = ops.parameter(shape, dtype=np.float32, name="A") + model = parameter_a + model = Model(model, [parameter_a], "TestModel") + ppp = PrePostProcessor(model) + assert not ppp.input().preprocess().pad([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT) + + +def test_pad_vector_dim_mismatch(): + shape = [1, 3, 5, 5] + parameter_a = ops.parameter(shape, dtype=np.float32, name="A") + model = parameter_a + model = Model(model, [parameter_a], "TestModel") + ppp = PrePostProcessor(model) + assert not ppp.input().preprocess().pad([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT) From 1a0d577303713c1b3b7d2210e0467e02b5ea9c94 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 8 Apr 2024 17:52:15 +0800 Subject: [PATCH 07/27] check model.get_output_shape(0) --- src/bindings/python/tests/test_graph/test_preprocess.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index f347cd3d507..758291ef927 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -740,6 +740,7 @@ def test_pad_vector_constant_layout(): ppp.input().tensor().set_shape([1, 3, 199, 199]) ppp.input().preprocess().pad([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT) assert ppp.build() + assert list(model.get_output_shape(0)) == shape def test_pad_vector_out_of_range(): @@ -749,6 +750,7 @@ def test_pad_vector_out_of_range(): model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) assert not ppp.input().preprocess().pad([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT) + assert list(model.get_output_shape(0)) == shape def test_pad_vector_dim_mismatch(): @@ -758,3 +760,4 @@ def test_pad_vector_dim_mismatch(): model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) assert not ppp.input().preprocess().pad([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT) + assert list(model.get_output_shape(0)) == shape From 425d3487b8fcbf6377911bf6b7c7a1c2c4a277aa Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 8 Apr 2024 17:56:06 +0800 Subject: [PATCH 08/27] Revert "check model.get_output_shape(0)" This reverts commit 1a0d577303713c1b3b7d2210e0467e02b5ea9c94. --- src/bindings/python/tests/test_graph/test_preprocess.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 758291ef927..f347cd3d507 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -740,7 +740,6 @@ def test_pad_vector_constant_layout(): ppp.input().tensor().set_shape([1, 3, 199, 199]) ppp.input().preprocess().pad([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT) assert ppp.build() - assert list(model.get_output_shape(0)) == shape def test_pad_vector_out_of_range(): @@ -750,7 +749,6 @@ def test_pad_vector_out_of_range(): model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) assert not ppp.input().preprocess().pad([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT) - assert list(model.get_output_shape(0)) == shape def test_pad_vector_dim_mismatch(): @@ -760,4 +758,3 @@ def test_pad_vector_dim_mismatch(): model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) assert not ppp.input().preprocess().pad([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT) - assert list(model.get_output_shape(0)) == shape From 733579dd7f9935a0d1558f33c0ec56af66d1a363 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 8 Apr 2024 17:56:21 +0800 Subject: [PATCH 09/27] Revert "fix code style" This reverts commit d917632c72c067f77b256d10ec897f17ca552912. --- .../tests/test_graph/test_preprocess.py | 142 ++++++++---------- 1 file changed, 61 insertions(+), 81 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index f347cd3d507..9d1dfdd4c46 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -41,7 +41,7 @@ def test_graph_preprocess_mean_vector(): ppp = PrePostProcessor(model) ppp.input().tensor().set_layout(layout) - ppp.input().preprocess().mean([1.0, 2.0]) + ppp.input().preprocess().mean([1., 2.]) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ordered_ops()] @@ -88,10 +88,10 @@ def test_graph_preprocess_mean_scale_convert(): ppp = PrePostProcessor(model) inp2 = ppp.input(1) inp2.tensor().set_element_type(Type.i32) - inp2.preprocess().convert_element_type(Type.f32).mean(1.0).scale(2.0) + inp2.preprocess().convert_element_type(Type.f32).mean(1.).scale(2.) inp2.preprocess().convert_element_type() inp1 = ppp.input(0) - inp1.preprocess().convert_element_type(Type.f32).mean(1.0).custom(custom_preprocess) + inp1.preprocess().convert_element_type(Type.f32).mean(1.).custom(custom_preprocess) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -127,9 +127,9 @@ def test_graph_preprocess_input_output_by_name(): ppp = PrePostProcessor(model) inp2 = ppp.input("B") inp2.tensor().set_element_type(Type.i32) - inp2.preprocess().convert_element_type(Type.f32).mean(1.0).scale(2.0) + inp2.preprocess().convert_element_type(Type.f32).mean(1.).scale(2.) inp1 = ppp.input("A") - inp1.preprocess().convert_element_type(Type.f32).mean(1.0) + inp1.preprocess().convert_element_type(Type.f32).mean(1.) out1 = ppp.output("A") out1.postprocess().custom(custom_preprocess) out2 = ppp.output("B") @@ -168,7 +168,6 @@ def test_graph_preprocess_output_postprocess(): @custom_preprocess_function def custom_postprocess(output: Output): return ops.abs(output) - ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) @@ -211,7 +210,7 @@ def test_graph_preprocess_spatial_static_shape(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout).set_spatial_static_shape(2, 2).set_color_format(color_format) - inp.preprocess().convert_element_type(Type.f32).mean([1.0, 2.0, 3.0]) + inp.preprocess().convert_element_type(Type.f32).mean([1., 2., 3.]) inp.model().set_layout(layout) out = ppp.output() out.tensor().set_layout(layout).set_element_type(Type.f32) @@ -303,9 +302,9 @@ def test_graph_preprocess_set_from_np_infer(): axis = ops.constant(np.array([0, 1, 2]), dtype=np.int32) return ops.slice(out_node, start, stop, step, axis) - input_data = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]).astype( - np.int32 - ) + input_data = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], + [[9, 10, 11], [12, 13, 14], [15, 16, 17]], + [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]).astype(np.int32) ppp = PrePostProcessor(model) inp = ppp.input() @@ -347,38 +346,36 @@ def test_graph_preprocess_set_memory_type(): @pytest.mark.parametrize( ("algorithm", "color_format1", "color_format2", "is_failing"), - [ - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.BGR, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_THREE_PLANES, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.BGR, False), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGB, False), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_THREE_PLANES, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.BGR, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.BGR, False), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGB, False), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.BGRX, True), - ], -) + [(ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.BGR, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_THREE_PLANES, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.BGR, False), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGB, False), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_THREE_PLANES, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.BGR, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.BGR, False), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGB, False), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.BGRX, True), + ]) def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_failing): shape = [1, 3, 3, 3] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") @@ -390,7 +387,7 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail custom_processor = PrePostProcessor(model) inp = custom_processor.input() inp.tensor().set_layout(layout1).set_color_format(color_format1, []) - inp.preprocess().mean(1.0).resize(algorithm, 3, 3) + inp.preprocess().mean(1.).resize(algorithm, 3, 3) inp.preprocess().convert_layout(layout2).convert_color(color_format2) if is_failing: @@ -417,11 +414,9 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail @pytest.mark.parametrize( ("color_format1", "color_format2", "tensor_in_shape", "model_in_shape"), - [ - (ColorFormat.RGB, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), - (ColorFormat.BGR, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), - ], -) + [(ColorFormat.RGB, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), + (ColorFormat.BGR, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), + ]) def test_graph_preprocess_convert_color(color_format1, color_format2, tensor_in_shape, model_in_shape): parameter_a = ops.parameter(model_in_shape, dtype=np.float32, name="A") model = parameter_a @@ -450,7 +445,7 @@ def test_graph_preprocess_postprocess_layout(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.0).convert_layout(layout2).reverse_channels() + inp.preprocess().mean(1.).convert_layout(layout2).reverse_channels() out = ppp.output() out.postprocess().convert_layout([0, 1, 2, 3]) model = ppp.build() @@ -482,7 +477,7 @@ def test_graph_preprocess_reverse_channels(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.0).reverse_channels() + inp.preprocess().mean(1.).reverse_channels() model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -540,7 +535,7 @@ def test_graph_preprocess_resize_algorithm(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.0).resize(resize_alg, 3, 3) + inp.preprocess().mean(1.).resize(resize_alg, 3, 3) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -560,8 +555,7 @@ def test_graph_preprocess_resize_algorithm(): def test_graph_preprocess_model(): - model = bytes( - b""" + model = bytes(b""" @@ -619,8 +613,7 @@ def test_graph_preprocess_model(): -""" - ) +""") core = Core() model = core.read_model(model=model) @@ -630,7 +623,7 @@ def test_graph_preprocess_model(): ppp = PrePostProcessor(model) ppp.input(1).preprocess().convert_element_type(Type.f32).scale(0.5) - ppp.input(0).preprocess().convert_element_type(Type.f32).mean(5.0) + ppp.input(0).preprocess().convert_element_type(Type.f32).mean(5.) ppp.output(0).postprocess().custom(custom_preprocess) model = ppp.build() @@ -677,7 +670,9 @@ def test_graph_preprocess_dump(): assert "convert layout " + Layout("NCHW").to_string() in p_str -@pytest.mark.parametrize(("layout", "layout_str"), [("NHCW", "[N,H,C,W]"), ("NHWC", "[N,H,W,C]")]) +@pytest.mark.parametrize( + ("layout", "layout_str"), + [("NHCW", "[N,H,C,W]"), ("NHWC", "[N,H,W,C]")]) def test_graph_set_layout_by_string(layout, layout_str): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -690,7 +685,9 @@ def test_graph_set_layout_by_string(layout, layout_str): assert f"{layout_str}" in p_str -@pytest.mark.parametrize(("layout", "layout_str"), [(Layout("NHCW"), "[N,H,C,W]"), (Layout("NHWC"), "[N,H,W,C]")]) +@pytest.mark.parametrize( + ("layout", "layout_str"), + [(Layout("NHCW"), "[N,H,C,W]"), (Layout("NHWC"), "[N,H,W,C]")]) def test_graph_set_layout_by_layout_class(layout, layout_str): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -703,7 +700,9 @@ def test_graph_set_layout_by_layout_class(layout, layout_str): assert f"{layout_str}" in p_str -@pytest.mark.parametrize(("layout"), [("1-2-3D"), ("5-5")]) +@pytest.mark.parametrize( + ("layout"), + [("1-2-3D"), ("5-5")]) def test_graph_set_layout_by_str_thow_exception(layout): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -730,31 +729,12 @@ def test_graph_set_layout_by_layout_class_thow_exception(): ppp.input().model().set_layout(layout) assert "Layout name is invalid" in str(e.value) - def test_pad_vector_constant_layout(): - shape = [1, 3, 200, 200] + shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - ppp.input().tensor().set_shape([1, 3, 199, 199]) - ppp.input().preprocess().pad([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT) + ppp.input().tensor().set_shape({1, 3, 223, 223}) + ppp.input().preprocess().pad({0, 0, 0, 0}, {0, 0, 1, 1}, 0, PaddingMode.CONSTANT) assert ppp.build() - - -def test_pad_vector_out_of_range(): - shape = [1, 3, 5, 5] - parameter_a = ops.parameter(shape, dtype=np.float32, name="A") - model = parameter_a - model = Model(model, [parameter_a], "TestModel") - ppp = PrePostProcessor(model) - assert not ppp.input().preprocess().pad([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT) - - -def test_pad_vector_dim_mismatch(): - shape = [1, 3, 5, 5] - parameter_a = ops.parameter(shape, dtype=np.float32, name="A") - model = parameter_a - model = Model(model, [parameter_a], "TestModel") - ppp = PrePostProcessor(model) - assert not ppp.input().preprocess().pad([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT) From aabe8dcfda6f352d349d9395e94764d14f102616 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 8 Apr 2024 17:58:00 +0800 Subject: [PATCH 10/27] fix code style --- .../tests/test_graph/test_preprocess.py | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 9d1dfdd4c46..7067fd5f010 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -729,12 +729,34 @@ def test_graph_set_layout_by_layout_class_thow_exception(): ppp.input().model().set_layout(layout) assert "Layout name is invalid" in str(e.value) + def test_pad_vector_constant_layout(): - shape = [1, 3, 224, 224] + shape = [1, 3, 200, 200] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - ppp.input().tensor().set_shape({1, 3, 223, 223}) - ppp.input().preprocess().pad({0, 0, 0, 0}, {0, 0, 1, 1}, 0, PaddingMode.CONSTANT) + ppp.input().tensor().set_shape([1, 3, 199, 199]) + ppp.input().preprocess().pad([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT) assert ppp.build() + assert list(model.get_output_shape(0)) == shape + + +def test_pad_vector_out_of_range(): + shape = [1, 3, 5, 5] + parameter_a = ops.parameter(shape, dtype=np.float32, name="A") + model = parameter_a + model = Model(model, [parameter_a], "TestModel") + ppp = PrePostProcessor(model) + assert not ppp.input().preprocess().pad([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT) + assert list(model.get_output_shape(0)) == shape + + +def test_pad_vector_dim_mismatch(): + shape = [1, 3, 5, 5] + parameter_a = ops.parameter(shape, dtype=np.float32, name="A") + model = parameter_a + model = Model(model, [parameter_a], "TestModel") + ppp = PrePostProcessor(model) + assert not ppp.input().preprocess().pad([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT) + assert list(model.get_output_shape(0)) == shape From 0db76bf4030fdb7838bb88627743ab0d306a8cc9 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Thu, 11 Apr 2024 09:45:19 +0800 Subject: [PATCH 11/27] fix bug and add py::arg --- .../graph/preprocess/pre_post_process.cpp | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp index 0fe6a204947..49bf0c418a0 100644 --- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp +++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp @@ -170,23 +170,27 @@ static void regclass_graph_PreProcessSteps(py::module m) { return &self.reverse_channels(); }); - steps.def("pad", - [](ov::preprocess::PreProcessSteps& self, - const std::vector& pads_begin, - const std::vector& pads_end, - float value, - ov::preprocess::PaddingMode mode) { - return &self.pad(pads_begin, pads_end, value, mode); - }); + steps.def( + "pad", + [](ov::preprocess::PreProcessSteps& self, + const std::vector& pads_begin, + const std::vector& pads_end, + float value, + ov::preprocess::PaddingMode mode) { + return &self.pad(pads_begin;, pads_end, value, mode); + }, + py::arg("pads_begin", "pads_end", "value", "mode")); - steps.def("pad", - [](ov::preprocess::PreProcessSteps& self, - const std::vector& pads_begin, - const std::vector& pads_end, - const std::vector& values, - ov::preprocess::PaddingMode mode) { - return &self.pad(pads_begin, pads_end, values, mode); - }); + steps.def( + "pad", + [](ov::preprocess::PreProcessSteps& self, + const std::vector& pads_begin, + const std::vector& pads_end, + const std::vector& values, + ov::preprocess::PaddingMode mode) { + return &self.pad(pads_begin, pads_end, values, mode); + }, + py::arg("pads_begin", "pads_end", "value", "mode")); } static void regclass_graph_PostProcessSteps(py::module m) { @@ -487,6 +491,14 @@ static void regenum_graph_ResizeAlgorithm(py::module m) { .export_values(); } +static void regenum_graph_PaddingMode(py::module m) { + py::enum_(m, "PaddingMode") + .value("CONSTANT", ov::preprocess::PaddingMode::CONSTANT) + .value("REFLECT", ov::preprocess::PaddingMode::REFLECT) + .value("SYMMETRIC", ov::preprocess::PaddingMode::SYMMETRIC) + .export_values(); +} + void regclass_graph_PrePostProcessor(py::module m) { regclass_graph_PreProcessSteps(m); regclass_graph_PostProcessSteps(m); @@ -498,6 +510,7 @@ void regclass_graph_PrePostProcessor(py::module m) { regclass_graph_OutputModelInfo(m); regenum_graph_ColorFormat(m); regenum_graph_ResizeAlgorithm(m); + regenum_graph_PaddingMode(m); py::class_> proc( m, "PrePostProcessor"); From dc1d27aaf840d78b3c1be165463a88bd24d29388 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Thu, 11 Apr 2024 10:12:48 +0800 Subject: [PATCH 12/27] fix build error --- .../pyopenvino/graph/preprocess/pre_post_process.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp index 49bf0c418a0..3f01fe8aadf 100644 --- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp +++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp @@ -179,7 +179,10 @@ static void regclass_graph_PreProcessSteps(py::module m) { ov::preprocess::PaddingMode mode) { return &self.pad(pads_begin;, pads_end, value, mode); }, - py::arg("pads_begin", "pads_end", "value", "mode")); + py::arg("pads_begin"), + py::arg("pads_end"), + py::arg("value"), + py::arg("mode")); steps.def( "pad", @@ -190,7 +193,10 @@ static void regclass_graph_PreProcessSteps(py::module m) { ov::preprocess::PaddingMode mode) { return &self.pad(pads_begin, pads_end, values, mode); }, - py::arg("pads_begin", "pads_end", "value", "mode")); + py::arg("pads_begin"), + py::arg("pads_end"), + py::arg("value"), + py::arg("mode")); } static void regclass_graph_PostProcessSteps(py::module m) { From 4ea78c683fda213a3fa95a1146e7386751b2d5b6 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Thu, 11 Apr 2024 10:35:08 +0800 Subject: [PATCH 13/27] fix bug --- .../python/src/pyopenvino/graph/preprocess/pre_post_process.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp index 3f01fe8aadf..72d562cbb9f 100644 --- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp +++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp @@ -177,7 +177,7 @@ static void regclass_graph_PreProcessSteps(py::module m) { const std::vector& pads_end, float value, ov::preprocess::PaddingMode mode) { - return &self.pad(pads_begin;, pads_end, value, mode); + return &self.pad(pads_begin, pads_end, value, mode); }, py::arg("pads_begin"), py::arg("pads_end"), From 78bf376d094622bd0f03f42da584514316dd3a15 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Thu, 11 Apr 2024 14:48:36 +0800 Subject: [PATCH 14/27] add PaddingMode to init --- src/bindings/python/src/openvino/preprocess/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/bindings/python/src/openvino/preprocess/__init__.py b/src/bindings/python/src/openvino/preprocess/__init__.py index 9b37f1f328d..c1a9150fea9 100644 --- a/src/bindings/python/src/openvino/preprocess/__init__.py +++ b/src/bindings/python/src/openvino/preprocess/__init__.py @@ -24,3 +24,5 @@ from openvino._pyopenvino.preprocess import PreProcessSteps from openvino._pyopenvino.preprocess import PostProcessSteps from openvino._pyopenvino.preprocess import ColorFormat from openvino._pyopenvino.preprocess import ResizeAlgorithm +from openvino._pyopenvino.preprocess import PaddingMode + From fc5efea464929c62d841f72d6b2439b5dbd0059a Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 15 Apr 2024 23:42:18 +0800 Subject: [PATCH 15/27] fix vector_out_of_range and vector_dim_mismatch --- .../python/tests/test_graph/test_preprocess.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 7067fd5f010..0c553707ad2 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -748,7 +748,10 @@ def test_pad_vector_out_of_range(): model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - assert not ppp.input().preprocess().pad([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT) + try: + ppp.input().preprocess().pad([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT) + except Exception: + raise assert list(model.get_output_shape(0)) == shape @@ -758,5 +761,8 @@ def test_pad_vector_dim_mismatch(): model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - assert not ppp.input().preprocess().pad([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT) + try: + ppp.input().preprocess().pad([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT) + except Exception: + raise assert list(model.get_output_shape(0)) == shape From 470b51a107a7322b02db9639482b5fd1c55dac19 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 22 Apr 2024 16:25:30 +0800 Subject: [PATCH 16/27] add docsrtrings --- .../graph/preprocess/pre_post_process.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp index 72d562cbb9f..b2601f7631d 100644 --- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp +++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp @@ -182,7 +182,14 @@ static void regclass_graph_PreProcessSteps(py::module m) { py::arg("pads_begin"), py::arg("pads_end"), py::arg("value"), - py::arg("mode")); + py::arg("mode"), + R"( + Adds padding preprocessing operation. + :param operation: Destination type. If not specified, type will be taken from model input's element type + :type operation: openvino.runtime.Type + :return: Reference to itself, allows chaining of calls in client's code in a builder-like manner. + :rtype: openvino.runtime.preprocess.PreProcessSteps + )"); steps.def( "pad", @@ -196,7 +203,14 @@ static void regclass_graph_PreProcessSteps(py::module m) { py::arg("pads_begin"), py::arg("pads_end"), py::arg("value"), - py::arg("mode")); + py::arg("mode"), + R"( + Adds padding preprocessing operation. + :param operation: Destination type. If not specified, type will be taken from model input's element type + :type operation: openvino.runtime.Type + :return: Reference to itself, allows chaining of calls in client's code in a builder-like manner. + :rtype: openvino.runtime.preprocess.PreProcessSteps + )"); } static void regclass_graph_PostProcessSteps(py::module m) { From e128c0dc00d42e1ecd1e9dacdad22ddd0daca924 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Thu, 25 Apr 2024 07:49:07 +0800 Subject: [PATCH 17/27] fix docstrings --- .../graph/preprocess/pre_post_process.cpp | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp index b2601f7631d..e3b2d451a85 100644 --- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp +++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp @@ -185,8 +185,14 @@ static void regclass_graph_PreProcessSteps(py::module m) { py::arg("mode"), R"( Adds padding preprocessing operation. - :param operation: Destination type. If not specified, type will be taken from model input's element type - :type operation: openvino.runtime.Type + :param pads_begin: Number of elements matches the number of indices in data attribute. Specifies the number of padding elements at the ending of each axis. + :type pads_begins: 1D tensor of type T_INT. + :param pads_end: Number of elements matches the number of indices in data attribute. Specifies the number of padding elements at the ending of each axis. + :type pads_end: 1D tensor of type T_INT. + :param value: All new elements are populated with this value or with 0 if input not provided. Shouldn’t be set for other pad_mode values. + :type value: scalar tensor of type T. + :param mode: ad_mode specifies the method used to generate new element values. + :type mode: string :return: Reference to itself, allows chaining of calls in client's code in a builder-like manner. :rtype: openvino.runtime.preprocess.PreProcessSteps )"); @@ -206,8 +212,14 @@ static void regclass_graph_PreProcessSteps(py::module m) { py::arg("mode"), R"( Adds padding preprocessing operation. - :param operation: Destination type. If not specified, type will be taken from model input's element type - :type operation: openvino.runtime.Type + :param pads_begin: Number of elements matches the number of indices in data attribute. Specifies the number of padding elements at the ending of each axis. + :type pads_begins: 1D tensor of type T_INT. + :param pads_end: Number of elements matches the number of indices in data attribute. Specifies the number of padding elements at the ending of each axis. + :type pads_end: 1D tensor of type T_INT. + :param value: All new elements are populated with this value or with 0 if input not provided. Shouldn’t be set for other pad_mode values. + :type value: scalar tensor of type T. + :param mode: ad_mode specifies the method used to generate new element values. + :type mode: string :return: Reference to itself, allows chaining of calls in client's code in a builder-like manner. :rtype: openvino.runtime.preprocess.PreProcessSteps )"); From ed5d779cdc192227d74b19dbeb8df68dca552e3a Mon Sep 17 00:00:00 2001 From: Haiqi Pan Date: Mon, 13 May 2024 09:46:19 +0800 Subject: [PATCH 18/27] Update src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp Co-authored-by: Anastasia Kuporosova --- .../python/src/pyopenvino/graph/preprocess/pre_post_process.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp index e3b2d451a85..5cbe02b5cbf 100644 --- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp +++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp @@ -185,6 +185,7 @@ static void regclass_graph_PreProcessSteps(py::module m) { py::arg("mode"), R"( Adds padding preprocessing operation. + :param pads_begin: Number of elements matches the number of indices in data attribute. Specifies the number of padding elements at the ending of each axis. :type pads_begins: 1D tensor of type T_INT. :param pads_end: Number of elements matches the number of indices in data attribute. Specifies the number of padding elements at the ending of each axis. From a09dfa0512007a426a635296cb44f16b93ae6c96 Mon Sep 17 00:00:00 2001 From: Haiqi Pan Date: Mon, 13 May 2024 09:46:30 +0800 Subject: [PATCH 19/27] Update src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp Co-authored-by: Anastasia Kuporosova --- .../python/src/pyopenvino/graph/preprocess/pre_post_process.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp index 5cbe02b5cbf..2991fa438d0 100644 --- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp +++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp @@ -213,6 +213,7 @@ static void regclass_graph_PreProcessSteps(py::module m) { py::arg("mode"), R"( Adds padding preprocessing operation. + :param pads_begin: Number of elements matches the number of indices in data attribute. Specifies the number of padding elements at the ending of each axis. :type pads_begins: 1D tensor of type T_INT. :param pads_end: Number of elements matches the number of indices in data attribute. Specifies the number of padding elements at the ending of each axis. From 68c1540bfd7d337d5cd2f55baaf90182e4922adf Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Sat, 18 May 2024 00:45:39 +0800 Subject: [PATCH 20/27] unit test --- .../python/tests/test_graph/test_preprocess.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 0c553707ad2..aa56616fb8d 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -730,39 +730,42 @@ def test_graph_set_layout_by_layout_class_thow_exception(): assert "Layout name is invalid" in str(e.value) -def test_pad_vector_constant_layout(): +@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT]) +def test_pad_vector_constant_layout(pads_begin, pads_end, values, mode): shape = [1, 3, 200, 200] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) ppp.input().tensor().set_shape([1, 3, 199, 199]) - ppp.input().preprocess().pad([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT) + ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) assert ppp.build() assert list(model.get_output_shape(0)) == shape -def test_pad_vector_out_of_range(): +@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT], pytest.raises(TypeError), "Incorrect value for pads_begin and pads_end") +def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) try: - ppp.input().preprocess().pad([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT) + ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) except Exception: raise assert list(model.get_output_shape(0)) == shape -def test_pad_vector_dim_mismatch(): +@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT], pytest.raises(TypeError), "Incorrect size for pads_begin and pads_end") +def test_pad_vector_dim_mismatch(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) try: - ppp.input().preprocess().pad([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT) + ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) except Exception: raise assert list(model.get_output_shape(0)) == shape From 8597cd0481adc80585ec3e253dd74eee198569ef Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Tue, 21 May 2024 11:17:03 +0800 Subject: [PATCH 21/27] fix format --- .../tests/test_graph/test_preprocess.py | 131 ++++++++++-------- 1 file changed, 71 insertions(+), 60 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index aa56616fb8d..7bcbce0da7f 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -41,7 +41,7 @@ def test_graph_preprocess_mean_vector(): ppp = PrePostProcessor(model) ppp.input().tensor().set_layout(layout) - ppp.input().preprocess().mean([1., 2.]) + ppp.input().preprocess().mean([1.0, 2.0]) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ordered_ops()] @@ -88,10 +88,10 @@ def test_graph_preprocess_mean_scale_convert(): ppp = PrePostProcessor(model) inp2 = ppp.input(1) inp2.tensor().set_element_type(Type.i32) - inp2.preprocess().convert_element_type(Type.f32).mean(1.).scale(2.) + inp2.preprocess().convert_element_type(Type.f32).mean(1.0).scale(2.0) inp2.preprocess().convert_element_type() inp1 = ppp.input(0) - inp1.preprocess().convert_element_type(Type.f32).mean(1.).custom(custom_preprocess) + inp1.preprocess().convert_element_type(Type.f32).mean(1.0).custom(custom_preprocess) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -127,9 +127,9 @@ def test_graph_preprocess_input_output_by_name(): ppp = PrePostProcessor(model) inp2 = ppp.input("B") inp2.tensor().set_element_type(Type.i32) - inp2.preprocess().convert_element_type(Type.f32).mean(1.).scale(2.) + inp2.preprocess().convert_element_type(Type.f32).mean(1.0).scale(2.0) inp1 = ppp.input("A") - inp1.preprocess().convert_element_type(Type.f32).mean(1.) + inp1.preprocess().convert_element_type(Type.f32).mean(1.0) out1 = ppp.output("A") out1.postprocess().custom(custom_preprocess) out2 = ppp.output("B") @@ -168,6 +168,7 @@ def test_graph_preprocess_output_postprocess(): @custom_preprocess_function def custom_postprocess(output: Output): return ops.abs(output) + ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) @@ -210,7 +211,7 @@ def test_graph_preprocess_spatial_static_shape(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout).set_spatial_static_shape(2, 2).set_color_format(color_format) - inp.preprocess().convert_element_type(Type.f32).mean([1., 2., 3.]) + inp.preprocess().convert_element_type(Type.f32).mean([1.0, 2.0, 3.0]) inp.model().set_layout(layout) out = ppp.output() out.tensor().set_layout(layout).set_element_type(Type.f32) @@ -302,9 +303,9 @@ def test_graph_preprocess_set_from_np_infer(): axis = ops.constant(np.array([0, 1, 2]), dtype=np.int32) return ops.slice(out_node, start, stop, step, axis) - input_data = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], - [[9, 10, 11], [12, 13, 14], [15, 16, 17]], - [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]).astype(np.int32) + input_data = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]).astype( + np.int32 + ) ppp = PrePostProcessor(model) inp = ppp.input() @@ -346,36 +347,38 @@ def test_graph_preprocess_set_memory_type(): @pytest.mark.parametrize( ("algorithm", "color_format1", "color_format2", "is_failing"), - [(ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.BGR, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_THREE_PLANES, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.BGR, False), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGB, False), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_THREE_PLANES, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.BGR, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.BGR, False), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGB, False), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.BGRX, True), - ]) + [ + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.BGR, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_THREE_PLANES, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.BGR, False), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGB, False), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_THREE_PLANES, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.BGR, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.BGR, False), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGB, False), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.BGRX, True), + ], +) def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_failing): shape = [1, 3, 3, 3] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") @@ -387,7 +390,7 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail custom_processor = PrePostProcessor(model) inp = custom_processor.input() inp.tensor().set_layout(layout1).set_color_format(color_format1, []) - inp.preprocess().mean(1.).resize(algorithm, 3, 3) + inp.preprocess().mean(1.0).resize(algorithm, 3, 3) inp.preprocess().convert_layout(layout2).convert_color(color_format2) if is_failing: @@ -414,9 +417,11 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail @pytest.mark.parametrize( ("color_format1", "color_format2", "tensor_in_shape", "model_in_shape"), - [(ColorFormat.RGB, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), - (ColorFormat.BGR, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), - ]) + [ + (ColorFormat.RGB, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), + (ColorFormat.BGR, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), + ], +) def test_graph_preprocess_convert_color(color_format1, color_format2, tensor_in_shape, model_in_shape): parameter_a = ops.parameter(model_in_shape, dtype=np.float32, name="A") model = parameter_a @@ -445,7 +450,7 @@ def test_graph_preprocess_postprocess_layout(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.).convert_layout(layout2).reverse_channels() + inp.preprocess().mean(1.0).convert_layout(layout2).reverse_channels() out = ppp.output() out.postprocess().convert_layout([0, 1, 2, 3]) model = ppp.build() @@ -477,7 +482,7 @@ def test_graph_preprocess_reverse_channels(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.).reverse_channels() + inp.preprocess().mean(1.0).reverse_channels() model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -535,7 +540,7 @@ def test_graph_preprocess_resize_algorithm(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.).resize(resize_alg, 3, 3) + inp.preprocess().mean(1.0).resize(resize_alg, 3, 3) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -555,7 +560,8 @@ def test_graph_preprocess_resize_algorithm(): def test_graph_preprocess_model(): - model = bytes(b""" + model = bytes( + b""" @@ -613,7 +619,8 @@ def test_graph_preprocess_model(): -""") +""" + ) core = Core() model = core.read_model(model=model) @@ -623,7 +630,7 @@ def test_graph_preprocess_model(): ppp = PrePostProcessor(model) ppp.input(1).preprocess().convert_element_type(Type.f32).scale(0.5) - ppp.input(0).preprocess().convert_element_type(Type.f32).mean(5.) + ppp.input(0).preprocess().convert_element_type(Type.f32).mean(5.0) ppp.output(0).postprocess().custom(custom_preprocess) model = ppp.build() @@ -670,9 +677,7 @@ def test_graph_preprocess_dump(): assert "convert layout " + Layout("NCHW").to_string() in p_str -@pytest.mark.parametrize( - ("layout", "layout_str"), - [("NHCW", "[N,H,C,W]"), ("NHWC", "[N,H,W,C]")]) +@pytest.mark.parametrize(("layout", "layout_str"), [("NHCW", "[N,H,C,W]"), ("NHWC", "[N,H,W,C]")]) def test_graph_set_layout_by_string(layout, layout_str): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -685,9 +690,7 @@ def test_graph_set_layout_by_string(layout, layout_str): assert f"{layout_str}" in p_str -@pytest.mark.parametrize( - ("layout", "layout_str"), - [(Layout("NHCW"), "[N,H,C,W]"), (Layout("NHWC"), "[N,H,W,C]")]) +@pytest.mark.parametrize(("layout", "layout_str"), [(Layout("NHCW"), "[N,H,C,W]"), (Layout("NHWC"), "[N,H,W,C]")]) def test_graph_set_layout_by_layout_class(layout, layout_str): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -700,9 +703,7 @@ def test_graph_set_layout_by_layout_class(layout, layout_str): assert f"{layout_str}" in p_str -@pytest.mark.parametrize( - ("layout"), - [("1-2-3D"), ("5-5")]) +@pytest.mark.parametrize(("layout"), [("1-2-3D"), ("5-5")]) def test_graph_set_layout_by_str_thow_exception(layout): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -743,7 +744,12 @@ def test_pad_vector_constant_layout(pads_begin, pads_end, values, mode): assert list(model.get_output_shape(0)) == shape -@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT], pytest.raises(TypeError), "Incorrect value for pads_begin and pads_end") +@pytest.mark.parametrize( + ("pads_begin", "pads_end", "values", "mode"), + [[0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT], + pytest.raises(TypeError), + "Incorrect value for pads_begin and pads_end", +) def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") @@ -757,7 +763,12 @@ def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): assert list(model.get_output_shape(0)) == shape -@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT], pytest.raises(TypeError), "Incorrect size for pads_begin and pads_end") +@pytest.mark.parametrize( + ("pads_begin", "pads_end", "values", "mode"), + [[0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT], + pytest.raises(TypeError), + "Incorrect size for pads_begin and pads_end", +) def test_pad_vector_dim_mismatch(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") From 46df2a22ffcd31755cf6d5a6c479803758560dfe Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Tue, 21 May 2024 13:19:23 +0800 Subject: [PATCH 22/27] fix error --- .../tests/test_graph/test_preprocess.py | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 7bcbce0da7f..5ce378574de 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -731,7 +731,7 @@ def test_graph_set_layout_by_layout_class_thow_exception(): assert "Layout name is invalid" in str(e.value) -@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT]) +@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT)]) def test_pad_vector_constant_layout(pads_begin, pads_end, values, mode): shape = [1, 3, 200, 200] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -746,9 +746,7 @@ def test_pad_vector_constant_layout(pads_begin, pads_end, values, mode): @pytest.mark.parametrize( ("pads_begin", "pads_end", "values", "mode"), - [[0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT], - pytest.raises(TypeError), - "Incorrect value for pads_begin and pads_end", + [([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT)] ) def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] @@ -756,18 +754,15 @@ def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - try: + with pytest.raises(TypeError) as e: ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) - except Exception: - raise + assert "value is invalid" in str.value(e.value) assert list(model.get_output_shape(0)) == shape @pytest.mark.parametrize( ("pads_begin", "pads_end", "values", "mode"), - [[0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT], - pytest.raises(TypeError), - "Incorrect size for pads_begin and pads_end", + [([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT)] ) def test_pad_vector_dim_mismatch(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] @@ -775,8 +770,7 @@ def test_pad_vector_dim_mismatch(pads_begin, pads_end, values, mode): model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - try: + with pytest.raises(RuntimeError) as e: ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) - except Exception: - raise + assert "vector is out of range" in str.value(e.value) assert list(model.get_output_shape(0)) == shape From 084b6afe9d877fc99ed049ab015890a401b541a6 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 3 Jun 2024 18:48:13 +0800 Subject: [PATCH 23/27] Revert "fix error" This reverts commit 46df2a22ffcd31755cf6d5a6c479803758560dfe. --- .../tests/test_graph/test_preprocess.py | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 5ce378574de..7bcbce0da7f 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -731,7 +731,7 @@ def test_graph_set_layout_by_layout_class_thow_exception(): assert "Layout name is invalid" in str(e.value) -@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT)]) +@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT]) def test_pad_vector_constant_layout(pads_begin, pads_end, values, mode): shape = [1, 3, 200, 200] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -746,7 +746,9 @@ def test_pad_vector_constant_layout(pads_begin, pads_end, values, mode): @pytest.mark.parametrize( ("pads_begin", "pads_end", "values", "mode"), - [([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT)] + [[0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT], + pytest.raises(TypeError), + "Incorrect value for pads_begin and pads_end", ) def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] @@ -754,15 +756,18 @@ def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - with pytest.raises(TypeError) as e: + try: ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) - assert "value is invalid" in str.value(e.value) + except Exception: + raise assert list(model.get_output_shape(0)) == shape @pytest.mark.parametrize( ("pads_begin", "pads_end", "values", "mode"), - [([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT)] + [[0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT], + pytest.raises(TypeError), + "Incorrect size for pads_begin and pads_end", ) def test_pad_vector_dim_mismatch(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] @@ -770,7 +775,8 @@ def test_pad_vector_dim_mismatch(pads_begin, pads_end, values, mode): model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - with pytest.raises(RuntimeError) as e: + try: ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) - assert "vector is out of range" in str.value(e.value) + except Exception: + raise assert list(model.get_output_shape(0)) == shape From 2b2c25179347399af03d42364aaa58cd9f66d977 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Mon, 3 Jun 2024 18:48:28 +0800 Subject: [PATCH 24/27] Revert "fix format" This reverts commit 8597cd0481adc80585ec3e253dd74eee198569ef. --- .../tests/test_graph/test_preprocess.py | 131 ++++++++---------- 1 file changed, 60 insertions(+), 71 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 7bcbce0da7f..aa56616fb8d 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -41,7 +41,7 @@ def test_graph_preprocess_mean_vector(): ppp = PrePostProcessor(model) ppp.input().tensor().set_layout(layout) - ppp.input().preprocess().mean([1.0, 2.0]) + ppp.input().preprocess().mean([1., 2.]) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ordered_ops()] @@ -88,10 +88,10 @@ def test_graph_preprocess_mean_scale_convert(): ppp = PrePostProcessor(model) inp2 = ppp.input(1) inp2.tensor().set_element_type(Type.i32) - inp2.preprocess().convert_element_type(Type.f32).mean(1.0).scale(2.0) + inp2.preprocess().convert_element_type(Type.f32).mean(1.).scale(2.) inp2.preprocess().convert_element_type() inp1 = ppp.input(0) - inp1.preprocess().convert_element_type(Type.f32).mean(1.0).custom(custom_preprocess) + inp1.preprocess().convert_element_type(Type.f32).mean(1.).custom(custom_preprocess) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -127,9 +127,9 @@ def test_graph_preprocess_input_output_by_name(): ppp = PrePostProcessor(model) inp2 = ppp.input("B") inp2.tensor().set_element_type(Type.i32) - inp2.preprocess().convert_element_type(Type.f32).mean(1.0).scale(2.0) + inp2.preprocess().convert_element_type(Type.f32).mean(1.).scale(2.) inp1 = ppp.input("A") - inp1.preprocess().convert_element_type(Type.f32).mean(1.0) + inp1.preprocess().convert_element_type(Type.f32).mean(1.) out1 = ppp.output("A") out1.postprocess().custom(custom_preprocess) out2 = ppp.output("B") @@ -168,7 +168,6 @@ def test_graph_preprocess_output_postprocess(): @custom_preprocess_function def custom_postprocess(output: Output): return ops.abs(output) - ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) @@ -211,7 +210,7 @@ def test_graph_preprocess_spatial_static_shape(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout).set_spatial_static_shape(2, 2).set_color_format(color_format) - inp.preprocess().convert_element_type(Type.f32).mean([1.0, 2.0, 3.0]) + inp.preprocess().convert_element_type(Type.f32).mean([1., 2., 3.]) inp.model().set_layout(layout) out = ppp.output() out.tensor().set_layout(layout).set_element_type(Type.f32) @@ -303,9 +302,9 @@ def test_graph_preprocess_set_from_np_infer(): axis = ops.constant(np.array([0, 1, 2]), dtype=np.int32) return ops.slice(out_node, start, stop, step, axis) - input_data = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]).astype( - np.int32 - ) + input_data = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], + [[9, 10, 11], [12, 13, 14], [15, 16, 17]], + [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]).astype(np.int32) ppp = PrePostProcessor(model) inp = ppp.input() @@ -347,38 +346,36 @@ def test_graph_preprocess_set_memory_type(): @pytest.mark.parametrize( ("algorithm", "color_format1", "color_format2", "is_failing"), - [ - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.BGR, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_THREE_PLANES, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.BGR, False), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGB, False), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_THREE_PLANES, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.BGR, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.BGRX, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), - (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.UNDEFINED, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.BGR, False), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGB, False), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGBX, True), - (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.BGRX, True), - ], -) + [(ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.BGR, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.RGB, ColorFormat.I420_THREE_PLANES, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_LINEAR, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.RGB, ColorFormat.BGR, False), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGB, False), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_CUBIC, ColorFormat.BGR, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.I420_THREE_PLANES, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_NEAREST, ColorFormat.BGR, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.BGR, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.BGRX, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.RGB, ColorFormat.NV12_TWO_PLANES, True), + (ResizeAlgorithm.RESIZE_BILINEAR_PILLOW, ColorFormat.UNDEFINED, ColorFormat.I420_SINGLE_PLANE, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.UNDEFINED, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.RGB, ColorFormat.BGR, False), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGB, False), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.RGBX, True), + (ResizeAlgorithm.RESIZE_BICUBIC_PILLOW, ColorFormat.BGR, ColorFormat.BGRX, True), + ]) def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_failing): shape = [1, 3, 3, 3] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") @@ -390,7 +387,7 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail custom_processor = PrePostProcessor(model) inp = custom_processor.input() inp.tensor().set_layout(layout1).set_color_format(color_format1, []) - inp.preprocess().mean(1.0).resize(algorithm, 3, 3) + inp.preprocess().mean(1.).resize(algorithm, 3, 3) inp.preprocess().convert_layout(layout2).convert_color(color_format2) if is_failing: @@ -417,11 +414,9 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail @pytest.mark.parametrize( ("color_format1", "color_format2", "tensor_in_shape", "model_in_shape"), - [ - (ColorFormat.RGB, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), - (ColorFormat.BGR, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), - ], -) + [(ColorFormat.RGB, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), + (ColorFormat.BGR, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), + ]) def test_graph_preprocess_convert_color(color_format1, color_format2, tensor_in_shape, model_in_shape): parameter_a = ops.parameter(model_in_shape, dtype=np.float32, name="A") model = parameter_a @@ -450,7 +445,7 @@ def test_graph_preprocess_postprocess_layout(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.0).convert_layout(layout2).reverse_channels() + inp.preprocess().mean(1.).convert_layout(layout2).reverse_channels() out = ppp.output() out.postprocess().convert_layout([0, 1, 2, 3]) model = ppp.build() @@ -482,7 +477,7 @@ def test_graph_preprocess_reverse_channels(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.0).reverse_channels() + inp.preprocess().mean(1.).reverse_channels() model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -540,7 +535,7 @@ def test_graph_preprocess_resize_algorithm(): ppp = PrePostProcessor(model) inp = ppp.input() inp.tensor().set_layout(layout1) - inp.preprocess().mean(1.0).resize(resize_alg, 3, 3) + inp.preprocess().mean(1.).resize(resize_alg, 3, 3) model = ppp.build() model_operators = [op.get_name().split("_")[0] for op in model.get_ops()] @@ -560,8 +555,7 @@ def test_graph_preprocess_resize_algorithm(): def test_graph_preprocess_model(): - model = bytes( - b""" + model = bytes(b""" @@ -619,8 +613,7 @@ def test_graph_preprocess_model(): -""" - ) +""") core = Core() model = core.read_model(model=model) @@ -630,7 +623,7 @@ def test_graph_preprocess_model(): ppp = PrePostProcessor(model) ppp.input(1).preprocess().convert_element_type(Type.f32).scale(0.5) - ppp.input(0).preprocess().convert_element_type(Type.f32).mean(5.0) + ppp.input(0).preprocess().convert_element_type(Type.f32).mean(5.) ppp.output(0).postprocess().custom(custom_preprocess) model = ppp.build() @@ -677,7 +670,9 @@ def test_graph_preprocess_dump(): assert "convert layout " + Layout("NCHW").to_string() in p_str -@pytest.mark.parametrize(("layout", "layout_str"), [("NHCW", "[N,H,C,W]"), ("NHWC", "[N,H,W,C]")]) +@pytest.mark.parametrize( + ("layout", "layout_str"), + [("NHCW", "[N,H,C,W]"), ("NHWC", "[N,H,W,C]")]) def test_graph_set_layout_by_string(layout, layout_str): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -690,7 +685,9 @@ def test_graph_set_layout_by_string(layout, layout_str): assert f"{layout_str}" in p_str -@pytest.mark.parametrize(("layout", "layout_str"), [(Layout("NHCW"), "[N,H,C,W]"), (Layout("NHWC"), "[N,H,W,C]")]) +@pytest.mark.parametrize( + ("layout", "layout_str"), + [(Layout("NHCW"), "[N,H,C,W]"), (Layout("NHWC"), "[N,H,W,C]")]) def test_graph_set_layout_by_layout_class(layout, layout_str): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -703,7 +700,9 @@ def test_graph_set_layout_by_layout_class(layout, layout_str): assert f"{layout_str}" in p_str -@pytest.mark.parametrize(("layout"), [("1-2-3D"), ("5-5")]) +@pytest.mark.parametrize( + ("layout"), + [("1-2-3D"), ("5-5")]) def test_graph_set_layout_by_str_thow_exception(layout): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -744,12 +743,7 @@ def test_pad_vector_constant_layout(pads_begin, pads_end, values, mode): assert list(model.get_output_shape(0)) == shape -@pytest.mark.parametrize( - ("pads_begin", "pads_end", "values", "mode"), - [[0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT], - pytest.raises(TypeError), - "Incorrect value for pads_begin and pads_end", -) +@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT], pytest.raises(TypeError), "Incorrect value for pads_begin and pads_end") def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") @@ -763,12 +757,7 @@ def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): assert list(model.get_output_shape(0)) == shape -@pytest.mark.parametrize( - ("pads_begin", "pads_end", "values", "mode"), - [[0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT], - pytest.raises(TypeError), - "Incorrect size for pads_begin and pads_end", -) +@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT], pytest.raises(TypeError), "Incorrect size for pads_begin and pads_end") def test_pad_vector_dim_mismatch(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") From f2dc308d31fafe7f90e9b1bca71d3f678a6cb106 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Tue, 4 Jun 2024 11:31:31 +0800 Subject: [PATCH 25/27] Revert "unit test" This reverts commit 68c1540bfd7d337d5cd2f55baaf90182e4922adf. --- .../python/tests/test_graph/test_preprocess.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index aa56616fb8d..0c553707ad2 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -730,42 +730,39 @@ def test_graph_set_layout_by_layout_class_thow_exception(): assert "Layout name is invalid" in str(e.value) -@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT]) -def test_pad_vector_constant_layout(pads_begin, pads_end, values, mode): +def test_pad_vector_constant_layout(): shape = [1, 3, 200, 200] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) ppp.input().tensor().set_shape([1, 3, 199, 199]) - ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) + ppp.input().preprocess().pad([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT) assert ppp.build() assert list(model.get_output_shape(0)) == shape -@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT], pytest.raises(TypeError), "Incorrect value for pads_begin and pads_end") -def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): +def test_pad_vector_out_of_range(): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) try: - ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) + ppp.input().preprocess().pad([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT) except Exception: raise assert list(model.get_output_shape(0)) == shape -@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [[0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT], pytest.raises(TypeError), "Incorrect size for pads_begin and pads_end") -def test_pad_vector_dim_mismatch(pads_begin, pads_end, values, mode): +def test_pad_vector_dim_mismatch(): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) try: - ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) + ppp.input().preprocess().pad([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT) except Exception: raise assert list(model.get_output_shape(0)) == shape From 509833c525a1d87393a5db1ec88053c5733b9564 Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Tue, 4 Jun 2024 11:35:06 +0800 Subject: [PATCH 26/27] fix did no raise error --- .../tests/test_graph/test_preprocess.py | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 0c553707ad2..24d8bce2f6f 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -730,39 +730,49 @@ def test_graph_set_layout_by_layout_class_thow_exception(): assert "Layout name is invalid" in str(e.value) -def test_pad_vector_constant_layout(): + +@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT)]) +def test_pad_vector_constant_layout(pads_begin, pads_end, values, mode): shape = [1, 3, 200, 200] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) ppp.input().tensor().set_shape([1, 3, 199, 199]) - ppp.input().preprocess().pad([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT) + ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) assert ppp.build() assert list(model.get_output_shape(0)) == shape -def test_pad_vector_out_of_range(): +@pytest.mark.parametrize( + ("pads_begin", "pads_end", "values", "mode"), + [([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT)] +) +def test_pad_vector_out_of_range(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - try: - ppp.input().preprocess().pad([0, 0, -2, 0], [0, 0, -4, 1], 0, PaddingMode.CONSTANT) - except Exception: - raise + with pytest.raises(RuntimeError) as e: + ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) + ppp.build() + assert "not aligned with original parameter's shape" in str(e.value) assert list(model.get_output_shape(0)) == shape -def test_pad_vector_dim_mismatch(): +@pytest.mark.parametrize( + ("pads_begin", "pads_end", "values", "mode"), + [([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT)] +) +def test_pad_vector_dim_mismatch(pads_begin, pads_end, values, mode): shape = [1, 3, 5, 5] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") model = parameter_a model = Model(model, [parameter_a], "TestModel") ppp = PrePostProcessor(model) - try: - ppp.input().preprocess().pad([0, 0, 2, 0, 1], [0, 0, 4, 1, 1], 0, PaddingMode.CONSTANT) - except Exception: - raise - assert list(model.get_output_shape(0)) == shape + with pytest.raises(RuntimeError) as e: + ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) + ppp.build() + assert "mismatches with rank of input" in str(e.value) + assert list(model.get_output_shape(0)) == shape \ No newline at end of file From de2175f64e4a78e72c298a81135fa0f55c0a092b Mon Sep 17 00:00:00 2001 From: panhaiqi Date: Tue, 4 Jun 2024 11:42:25 +0800 Subject: [PATCH 27/27] fix format --- src/bindings/python/tests/test_graph/test_preprocess.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 24d8bce2f6f..556ef4d4a41 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -730,8 +730,9 @@ def test_graph_set_layout_by_layout_class_thow_exception(): assert "Layout name is invalid" in str(e.value) - -@pytest.mark.parametrize(("pads_begin", "pads_end", "values", "mode"), [([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT)]) +@pytest.mark.parametrize( + ("pads_begin", "pads_end", "values", "mode"), + [([0, 0, 0, 0], [0, 0, 1, 1], 0, PaddingMode.CONSTANT)]) def test_pad_vector_constant_layout(pads_begin, pads_end, values, mode): shape = [1, 3, 200, 200] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") @@ -775,4 +776,4 @@ def test_pad_vector_dim_mismatch(pads_begin, pads_end, values, mode): ppp.input().preprocess().pad(pads_begin, pads_end, values, mode) ppp.build() assert "mismatches with rank of input" in str(e.value) - assert list(model.get_output_shape(0)) == shape \ No newline at end of file + assert list(model.get_output_shape(0)) == shape