From a0da3d360c5178adfd5400bb981c31f090f45bb4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomasz=20Do=C5=82bniak?= Date: Fri, 18 Sep 2020 10:08:29 +0200 Subject: [PATCH] Accept ONNX Constants with empty tensors (#2287) --- ngraph/frontend/onnx_import/src/op/constant.cpp | 18 +++- ngraph/frontend/onnx_import/src/op/resize.cpp | 5 + .../onnx/resize11_empty_constant_as_input.prototxt | 119 +++++++++++++++++++++ ngraph/test/onnx/onnx_import.in.cpp | 24 +++++ ngraph/test/runtime/ie/unit_test.manifest | 9 -- ngraph/test/runtime/interpreter/unit_test.manifest | 1 - 6 files changed, 164 insertions(+), 12 deletions(-) create mode 100644 ngraph/test/models/onnx/resize11_empty_constant_as_input.prototxt diff --git a/ngraph/frontend/onnx_import/src/op/constant.cpp b/ngraph/frontend/onnx_import/src/op/constant.cpp index fdaf65f..3a1718f 100644 --- a/ngraph/frontend/onnx_import/src/op/constant.cpp +++ b/ngraph/frontend/onnx_import/src/op/constant.cpp @@ -15,6 +15,7 @@ //***************************************************************************** #include "constant.hpp" +#include "ngraph/log.hpp" #include "ngraph/op/constant.hpp" #include "onnx_import/core/tensor.hpp" #include "onnx_import/default_opset.hpp" @@ -33,8 +34,21 @@ namespace ngraph inline std::shared_ptr __make_ng_constant(const element::Type& type, const Tensor& tensor) { - return std::make_shared( - type, tensor.get_shape(), tensor.get_data()); + std::shared_ptr constant{nullptr}; + try + { + constant = std::make_shared( + type, tensor.get_shape(), tensor.get_data()); + } + catch (const ngraph::ngraph_error& exc) + { + NGRAPH_WARN << "Could not create an nGraph Constant for an ONNX " + "Constant node. Detailed error:\n" + << exc.what(); + constant = std::make_shared(type, Shape{}, 0); + } + + return constant; } template diff --git a/ngraph/frontend/onnx_import/src/op/resize.cpp b/ngraph/frontend/onnx_import/src/op/resize.cpp index 6054740..ff288d8 100644 --- a/ngraph/frontend/onnx_import/src/op/resize.cpp +++ b/ngraph/frontend/onnx_import/src/op/resize.cpp @@ -278,6 +278,8 @@ namespace ngraph if (inputs.size() == 4) // sizes input is provided { + attrs.shape_calculation_mode = + default_opset::Interpolate::ShapeCalcMode::sizes; const auto& sizes = inputs.at(3); const auto& sizes_shape = sizes.get_partial_shape(); @@ -292,6 +294,9 @@ namespace ngraph data, sizes, scales, attrs)}; } + attrs.shape_calculation_mode = + default_opset::Interpolate::ShapeCalcMode::scales; + const auto& scales = inputs.at(2); const auto& scales_shape = scales.get_partial_shape(); diff --git a/ngraph/test/models/onnx/resize11_empty_constant_as_input.prototxt b/ngraph/test/models/onnx/resize11_empty_constant_as_input.prototxt new file mode 100644 index 0000000..b4e397b --- /dev/null +++ b/ngraph/test/models/onnx/resize11_empty_constant_as_input.prototxt @@ -0,0 +1,119 @@ +ir_version: 7 +producer_name: "onnx-importer-test" +graph { + node { + output: "empty_const" + op_type: "Constant" + attribute { + name: "value" + t { + dims: 0 + data_type: 1 + raw_data: "" + } + type: TENSOR + } + } + node { + output: "scales" + op_type: "Constant" + attribute { + name: "value" + t { + dims: 4 + data_type: 1 + float_data: 1.0 + float_data: 1.0 + float_data: 2.0 + float_data: 4.0 + } + type: TENSOR + } + } + node { + input: "X" + input: "empty_const" + input: "scales" + output: "Y" + op_type: "Resize" + attribute { + name: "coordinate_transformation_mode" + s: "asymmetric" + type: STRING + } + attribute { + name: "cubic_coeff_a" + f: -0.75 + type: FLOAT + } + attribute { + name: "exclude_outside" + i: 0 + type: INT + } + attribute { + name: "extrapolation_value" + f: 0 + type: FLOAT + } + attribute { + name: "mode" + s: "linear" + type: STRING + } + attribute { + name: "nearest_mode" + s: "floor" + 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: 2 + } + dim { + dim_value: 2 + } + } + } + } + } + output { + name: "Y" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 1 + } + dim { + dim_value: 2 + } + dim { + dim_value: 4 + } + dim { + dim_value: 8 + } + } + } + } + } +} +opset_import { + domain: "" + version: 11 +} diff --git a/ngraph/test/onnx/onnx_import.in.cpp b/ngraph/test/onnx/onnx_import.in.cpp index f6336db..779de83 100644 --- a/ngraph/test/onnx/onnx_import.in.cpp +++ b/ngraph/test/onnx/onnx_import.in.cpp @@ -1241,6 +1241,30 @@ NGRAPH_TEST(${BACKEND_NAME}, onnx_resize11_scales_nearest_asymmetric_floor) test_case.run(); } +NGRAPH_TEST(${BACKEND_NAME}, onnx_resize11_empty_constant_as_input) +{ + // this model contains a Constant node with an empty underlying tensor + // this node is connected to the "roi" input of the Resize op but this input should be + // ignored since the Resize coordinate_transformation_mode is set to asymmetric + const auto function = onnx_import::import_onnx_model( + file_util::path_join(SERIALIZED_ZOO, "onnx/resize11_empty_constant_as_input.prototxt")); + + auto test_case = test::TestCase(function); + std::vector input_data{1.0f, 3.0f, 4.0f, 8.0f, 6.0f, 2.0f, 7.0f, 11.0f}; + test_case.add_input(input_data); + test_case.add_expected_output( + Shape{1, 2, 4, 8}, + {1.0f, 1.5f, 2.0f, 2.5f, 3.0f, 3.0f, 3.0f, 3.0f, 2.5f, 3.25f, 4.0f, + 4.75f, 5.5f, 5.5f, 5.5f, 5.5f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 8.0f, + 8.0f, 8.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 8.0f, 8.0f, 8.0f, + + 6.0f, 5.0f, 4.0f, 3.0f, 2.0f, 2.0f, 2.0f, 2.0f, 6.5f, 6.5f, 6.5f, + 6.5f, 6.5f, 6.5f, 6.5f, 6.5f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 11.0f, + 11.0f, 11.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 11.0f, 11.0f, 11.0f}); + + test_case.run(); +} + NGRAPH_TEST(${BACKEND_NAME}, onnx_model_shape) { auto function = diff --git a/ngraph/test/runtime/ie/unit_test.manifest b/ngraph/test/runtime/ie/unit_test.manifest index d4b037f..2940058 100644 --- a/ngraph/test/runtime/ie/unit_test.manifest +++ b/ngraph/test/runtime/ie/unit_test.manifest @@ -1095,12 +1095,6 @@ onnx_model_gru_bidirectional onnx_model_gru_fwd_linear_before_reset # Not implemented Interpolate-4: -IE_CPU.onnx_model_resize10_import_only -IE_CPU.onnx_resize10_down_scales_const_nearest -IE_CPU.onnx_resize10_up_scales_const_linear -IE_CPU.onnx_resize10_up_scales_const_nearest -IE_CPU.onnx_resize11_scales_up_linear_asymmetric -IE_CPU.onnx_resize11_scales_nearest_asymmetric_floor IE_CPU.onnx_model_round IE_CPU.onnx_upsample9_scales_const_linear_infer IE_CPU.onnx_upsample9_scales_const_nearest_infer @@ -1110,10 +1104,7 @@ IE_CPU.interpolate_down_scales_const_linear IE_CPU.onnx_upsample8_import_only IE_CPU.onnx_upsample9_scales_const_import_only IE_CPU.onnx_empty_initializers_handling -IE_CPU.onnx_resize11_scales_down_linear IE_CPU.onnx_resize11_scales_nearest_asymmetric_floor_dynamic_sizes -IE_CPU.onnx_resize11_sizes_nearest_asymmetric_floor -IE_CPU.onnx_resize11_sizes_linear # RNNCell operation has a form that is not supported IE_CPU.onnx_model_rnn_defaults_fwd diff --git a/ngraph/test/runtime/interpreter/unit_test.manifest b/ngraph/test/runtime/interpreter/unit_test.manifest index 4502e97..8387297 100644 --- a/ngraph/test/runtime/interpreter/unit_test.manifest +++ b/ngraph/test/runtime/interpreter/unit_test.manifest @@ -12,7 +12,6 @@ reduce_sum_keep_large_1d_to_scalar # Temporarily disabled: INTERPRETER.onnx_resize11_scales_nearest_asymmetric_floor_dynamic_sizes -INTERPRETER.onnx_resize11_scales_down_linear INTERPRETER.interpolate_down_scales_const_linear INTERPRETER.onnx_resize10_up_scales_const_nearest INTERPRETER.onnx_resize10_up_scales_const_linear -- 2.7.4