From 7be7a8fb30938373ef007365a957e130fd57954f Mon Sep 17 00:00:00 2001 From: Mateusz Tabaka Date: Thu, 7 Jan 2021 16:56:19 +0100 Subject: [PATCH] [ONNX] don't hardcode shapes in Interpolate and Shape operator (#3778) --- ngraph/frontend/onnx_import/src/op/resize.cpp | 49 ------------ ngraph/frontend/onnx_import/src/op/shape.cpp | 15 +--- .../test/models/onnx/resize_opset10.prototxt | 80 ------------------- ngraph/test/onnx/onnx_import.in.cpp | 15 ---- 4 files changed, 1 insertion(+), 158 deletions(-) delete mode 100644 ngraph/test/models/onnx/resize_opset10.prototxt diff --git a/ngraph/frontend/onnx_import/src/op/resize.cpp b/ngraph/frontend/onnx_import/src/op/resize.cpp index ff288d82d3f..a31a7b3df67 100644 --- a/ngraph/frontend/onnx_import/src/op/resize.cpp +++ b/ngraph/frontend/onnx_import/src/op/resize.cpp @@ -148,29 +148,6 @@ namespace ngraph calculate_output_shape_based_on_scales(const Output& data, const Output& scales) { - const auto& data_shape = data.get_partial_shape(); - const auto& scales_shape = scales.get_partial_shape(); - - if (ngraph::op::is_constant(scales.get_node()) && data_shape.is_static()) - { - const auto scales_const = - as_type_ptr(scales.get_node_shared_ptr()); - - const auto scales_vector = scales_const->cast_vector(); - const auto data_static_shape = data_shape.to_shape(); - - std::vector output_shape; - for (size_t i = 0; i < data_static_shape.size(); ++i) - { - output_shape.push_back( - std::floor(data_static_shape.at(i) * scales_vector.at(i))); - } - auto output_shape_const = default_opset::Constant::create( - element::u64, Shape({output_shape.size()}), output_shape); - - return output_shape_const; - } - const auto shape_of_data = std::make_shared( std::make_shared(data), scales.get_element_type()); const auto multiply = @@ -185,33 +162,7 @@ namespace ngraph calculate_scales_based_on_sizes(const Output& data, const Output& sizes) { - const auto& data_shape = data.get_partial_shape(); - const auto& sizes_shape = sizes.get_partial_shape(); - const float epsilon = 1.0e-5; - - if (ngraph::op::is_constant(sizes.get_node()) && data_shape.is_static()) - { - const auto sizes_const = - as_type_ptr(sizes.get_node_shared_ptr()); - - const auto sizes_vector = sizes_const->cast_vector(); - const auto data_static_shape = data_shape.to_shape(); - - std::vector scales; - for (size_t i = 0; i < data_static_shape.size(); ++i) - { - float scale = static_cast(sizes_vector.at(i)) / - static_cast(data_static_shape.at(i)) + - epsilon; - scales.push_back(scale); - } - auto scales_const = default_opset::Constant::create( - element::f32, Shape({scales.size()}), scales); - - return scales_const; - } - const auto shape_of_data = std::make_shared( std::make_shared(data), ngraph::element::f32); const auto converted_sizes = diff --git a/ngraph/frontend/onnx_import/src/op/shape.cpp b/ngraph/frontend/onnx_import/src/op/shape.cpp index c02df889f9a..b5e4f4f8eda 100644 --- a/ngraph/frontend/onnx_import/src/op/shape.cpp +++ b/ngraph/frontend/onnx_import/src/op/shape.cpp @@ -33,20 +33,7 @@ namespace ngraph OutputVector shape(const Node& node) { const auto data = node.get_ng_inputs().at(0); - const auto data_shape = data.get_partial_shape(); - - if (data_shape.is_static()) - { - const auto static_data_shape = data_shape.to_shape(); - - return {default_opset::Constant::create(ngraph::element::i64, - Shape{static_data_shape.size()}, - static_data_shape)}; - } - else - { - return {std::make_shared(data)}; - } + return {std::make_shared(data)}; } } // namespace set_1 diff --git a/ngraph/test/models/onnx/resize_opset10.prototxt b/ngraph/test/models/onnx/resize_opset10.prototxt deleted file mode 100644 index a8b253f9351..00000000000 --- a/ngraph/test/models/onnx/resize_opset10.prototxt +++ /dev/null @@ -1,80 +0,0 @@ -ir_version: 6 -producer_name: "test_model" -graph { - node { - output: "scales" - op_type: "Constant" - attribute { - name: "value" - t { - dims: 4 - data_type: 1 - float_data: 4.0 - float_data: 3.0 - float_data: 2.0 - float_data: 1.0 - name: "scales_const" - } - type: TENSOR - } - } - node { - input: "X" - input: "scales" - output: "output" - op_type: "Resize" - attribute { - name: "mode" - s: "nearest" - type: STRING - } - } - name: "test_model" - input { - name: "X" - type { - tensor_type { - elem_type: 1 - shape { - dim { - dim_value: 1 - } - dim { - dim_value: 2 - } - dim { - dim_value: 3 - } - dim { - dim_value: 4 - } - } - } - } - } - output { - name: "output" - type { - tensor_type { - elem_type: 1 - shape { - dim { - dim_value: 4 - } - dim { - dim_value: 6 - } - dim { - dim_value: 6 - } - dim { - dim_value: 4 - } - } - } - } - } -} -opset_import { - version: 10 -} diff --git a/ngraph/test/onnx/onnx_import.in.cpp b/ngraph/test/onnx/onnx_import.in.cpp index a3bf395b26b..28eab8c7ac6 100644 --- a/ngraph/test/onnx/onnx_import.in.cpp +++ b/ngraph/test/onnx/onnx_import.in.cpp @@ -1122,21 +1122,6 @@ NGRAPH_TEST(${BACKEND_NAME}, onnx_model_reduce_sum_square) test_case.run(); } -NGRAPH_TEST(${BACKEND_NAME}, onnx_model_resize10_import_only) -{ - const auto resize_fn = onnx_import::import_onnx_model( - file_util::path_join(SERIALIZED_ZOO, "onnx/resize_opset10.prototxt")); - - // Input data shape (1, 2, 3, 4) - // Scales input constant values {4, 3, 2, 1} - - Shape expected_output_shape{4, 6, 6, 4}; - EXPECT_EQ(resize_fn->get_output_size(), 1); - EXPECT_EQ(resize_fn->get_output_shape(0), expected_output_shape); - EXPECT_EQ(count_ops_of_type(resize_fn), 1); - EXPECT_EQ(count_ops_of_type(resize_fn), 1); -} - NGRAPH_TEST(${BACKEND_NAME}, onnx_resize11_empty_constant_as_input) { // this model contains a Constant node with an empty underlying tensor