Accept ONNX Constants with empty tensors (#2287)
authorTomasz Dołbniak <tomasz.dolbniak@intel.com>
Fri, 18 Sep 2020 08:08:29 +0000 (10:08 +0200)
committerGitHub <noreply@github.com>
Fri, 18 Sep 2020 08:08:29 +0000 (10:08 +0200)
ngraph/frontend/onnx_import/src/op/constant.cpp
ngraph/frontend/onnx_import/src/op/resize.cpp
ngraph/test/models/onnx/resize11_empty_constant_as_input.prototxt [new file with mode: 0644]
ngraph/test/onnx/onnx_import.in.cpp
ngraph/test/runtime/ie/unit_test.manifest
ngraph/test/runtime/interpreter/unit_test.manifest

index fdaf65f..3a1718f 100644 (file)
@@ -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<default_opset::Constant>
                         __make_ng_constant(const element::Type& type, const Tensor& tensor)
                     {
-                        return std::make_shared<default_opset::Constant>(
-                            type, tensor.get_shape(), tensor.get_data<T>());
+                        std::shared_ptr<default_opset::Constant> constant{nullptr};
+                        try
+                        {
+                            constant = std::make_shared<default_opset::Constant>(
+                                type, tensor.get_shape(), tensor.get_data<T>());
+                        }
+                        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<default_opset::Constant>(type, Shape{}, 0);
+                        }
+
+                        return constant;
                     }
 
                     template <Tensor::Type>
index 6054740..ff288d8 100644 (file)
@@ -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 (file)
index 0000000..b4e397b
--- /dev/null
@@ -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
+}
index f6336db..779de83 100644 (file)
@@ -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<TestEngine>(function);
+    std::vector<float> input_data{1.0f, 3.0f, 4.0f, 8.0f, 6.0f, 2.0f, 7.0f, 11.0f};
+    test_case.add_input<float>(input_data);
+    test_case.add_expected_output<float>(
+        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 =
index d4b037f..2940058 100644 (file)
@@ -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
index 4502e97..8387297 100644 (file)
@@ -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