Accept ONNX Constants with empty tensors (#2287)

This commit is contained in:
Tomasz Dołbniak 2020-09-18 10:08:29 +02:00 committed by GitHub
parent a34b6e38f3
commit a0da3d360c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 164 additions and 12 deletions

View 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>

View 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();

View File

@ -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
}

View 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 =

View 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

View 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