Commit 2cf9e4b2 authored by Adam Rogowiec's avatar Adam Rogowiec Committed by Robert Kimball

[ONNX] Fix convolution errors (#2025)

* Unit tests for conv2d causing errors.

* UT for conv3D_bias

* Fix padding order.

`padding below` in nGraph terminology means padding added at the beginning
of the axis. Whereas `padding above` means padding added at the end of
the axis.

* Rename test to sth more descriptive.

* Apply clang-format.

* Fix handling of `SAME_UPPER/LOWER` auto_pads mode for convolution/pooling ops.

* Fix order of padding_below/above.
Signed-off-by: 's avatarAdam Rogowiec <adam.rogowiec@intel.com>

* Fix error in calculating output data shape.
parent 1286fd44
......@@ -31,9 +31,9 @@ namespace ngraph
{
Shape get_kernel_shape(const Node& node)
{
std::size_t input_spacial_dims = node.get_ng_inputs()[0]->get_shape().size() - 2;
std::size_t input_spatial_dims = node.get_ng_inputs().at(0)->get_shape().size() - 2;
return node.get_attribute_value<std::vector<std::size_t>>(
"kernel_shape", std::vector<std::size_t>(input_spacial_dims, 1UL));
"kernel_shape", std::vector<std::size_t>(input_spatial_dims, 1UL));
}
namespace detail
......@@ -64,38 +64,67 @@ namespace ngraph
namespace
{
CoordinateDiff get_auto_pads(const Shape& kernel_shape, const std::string& auto_pad)
Shape get_output_data_shape(const Shape& input, const Strides& strides)
{
CoordinateDiff pads;
Shape output;
for (std::size_t idx = 0; idx < input.size(); ++idx)
{
output.emplace_back(std::ceil(static_cast<float>(input.at(idx)) /
static_cast<float>(strides.at(idx))));
}
return output;
}
// Add padding to the input to match the size of output size.
auto pad_value = [](size_t dim) {
return (static_cast<float>(dim) - 1.f) / 2.f;
};
Shape get_pad_shape(const Shape& input,
const Shape& kernel,
const Shape& strides,
const Shape& output)
{
Shape pad_shape;
for (std::size_t idx = 0; idx < input.size(); ++idx)
{
pad_shape.emplace_back((output.at(idx) - 1) * strides.at(idx) +
kernel.at(idx) - input.at(idx));
}
return pad_shape;
}
CoordinateDiff get_auto_pads(const Shape& input_shape,
const Shape& kernel_shape,
const Strides& strides,
const std::string& auto_pad)
{
CoordinateDiff pads_begin;
CoordinateDiff pads_end;
// Omit {N,C} axes
Shape input_spatial_shape{std::next(std::begin(input_shape), 2),
std::end(input_shape)};
// Assume that all {input_spatial_shape,kernel_shape,strides}.size()
// is the same.
const Shape& output_spatial_shape =
get_output_data_shape(input_spatial_shape, strides);
const Shape& pad_shape = get_pad_shape(
input_spatial_shape, kernel_shape, strides, output_spatial_shape);
if (auto_pad == "SAME_UPPER")
{
for (size_t dim : kernel_shape)
{
pads.emplace_back(std::floor(pad_value(dim)));
}
for (size_t dim : kernel_shape)
for (size_t pad : pad_shape)
{
pads.emplace_back(std::ceil(pad_value(dim)));
// Integer division
pads_begin.emplace_back(pad / 2);
pads_end.emplace_back(pad - pads_begin.back());
}
}
else if (auto_pad == "SAME_LOWER")
{
for (size_t dim : kernel_shape)
for (size_t pad : pad_shape)
{
pads.emplace_back(std::ceil(pad_value(dim)));
}
for (size_t dim : kernel_shape)
{
pads.emplace_back(std::floor(pad_value(dim)));
// Integer division
pads_end.emplace_back(pad / 2);
pads_begin.emplace_back(pad - pads_end.back());
}
}
CoordinateDiff pads{pads_begin};
pads.insert(std::end(pads), std::begin(pads_end), std::end(pads_end));
return pads;
}
......@@ -115,7 +144,10 @@ namespace ngraph
std::string auto_pad{node.get_attribute_value<std::string>("auto_pad", "")};
if (!auto_pad.empty())
{
pads = get_auto_pads(kernel_shape, auto_pad);
pads = get_auto_pads(node.get_ng_inputs().at(0)->get_shape(),
kernel_shape,
get_strides(node),
auto_pad);
}
}
if (pads.empty())
......@@ -130,8 +162,8 @@ namespace ngraph
}
else
{
return {{std::begin(pads) + pads.size() / 2, std::end(pads)},
{std::begin(pads), std::begin(pads) + pads.size() / 2}};
return {{std::begin(pads), std::begin(pads) + pads.size() / 2},
{std::begin(pads) + pads.size() / 2, std::end(pads)}};
}
}
......
......@@ -118,8 +118,8 @@ namespace ngraph
bool count_include_pad = node.get_attribute_value<int64_t>("count_include_pad", 0);
// Convert padding from CoordinateDiff to Shape objects
const CoordinateDiff& padding_above{paddings.first};
const CoordinateDiff& padding_below{paddings.second};
const CoordinateDiff& padding_above{paddings.second};
const CoordinateDiff& padding_below{paddings.first};
Shape padding_below_shape{std::begin(padding_below), std::end(padding_below)};
Shape padding_above_shape{std::begin(padding_above), std::end(padding_above)};
......
ONNXNgraphImporter:
|
A
BC"Conv*
auto_pad*
dilations@@*
group*
kernel_shape@@*
pads@@@@*
strides@@ compute_graphZ
A




Z
B




b
C




B
\ No newline at end of file
ONNXNgraphImporter:

A
B
CD"Conv*
auto_pad*
dilations@@@*
group*
kernel_shape@@@*
pads@@@@@@*
strides@@@ compute_graphZ
A





Z
B





Z
C

b
D





B
\ No newline at end of file
......@@ -1347,6 +1347,160 @@ TEST(onnx, model_custom_op_default_domain)
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
TEST(onnx, model_conv2d_dilation_assymetric_pads_strides)
{
auto function = onnx_import::import_onnx_function(
file_util::path_join(SERIALIZED_ZOO, "onnx/conv2d_dilation_assym_pads_strides.onnx"));
// "", // auto_pad
// vector<int64_t>{1, 1}, // dilations
// 1, // group
// vector<int64_t>{3, 3}, // kernel_shape
// vector<int64_t>{1, 1, 1, 2}, // pads
// vector<int64_t>{3, 1} // strides
Inputs inputs;
// {2, 1, 1, 1}
inputs.emplace_back(
test::NDArray<float, 4>({{{{-0.09103918075561523f}}}, {{{-0.32513630390167236f}}}})
.get_vector());
// {2, 1, 3, 3}
inputs.emplace_back(
test::NDArray<float, 4>(
{{{{0.4312484860420227f, -0.12559029459953308f, 0.44889551401138306f},
{-0.3100617825984955f, 0.13522827625274658f, -0.06791308522224426f},
{0.22671669721603394f, -0.17391827702522278f, -0.31299442052841187f}}},
{{{-0.31545522809028625f, 0.06560015678405762f, 0.2656586766242981f},
{0.41363757848739624f, 0.31231558322906494f, -0.376018226146698f},
{-0.005708813667297363f, 0.34922850131988525f, 0.45095211267471313f}}}})
.get_vector());
// {2, 2, 1, 2}
Outputs expected_output{
test::NDArray<float, 4>({{{{-0.012311071157455444f, 0.02822777070105076f}},
{{-0.028432954102754593f, -0.037657227367162704f}}},
{{{-0.04396762326359749f, 0.10081233829259872f}},
{{-0.10154513269662857f, -0.13448859751224518f}}}})
.get_vector()};
Outputs outputs{execute(function, inputs, "INTERPRETER")};
EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
TEST(onnx, model_conv3d_bias)
{
auto function = onnx_import::import_onnx_function(
file_util::path_join(SERIALIZED_ZOO, "onnx/conv3d_bias.onnx"));
// "", // auto_pad
// vector<int64_t>{2, 2, 2}, // dilations
// 1, // group
// vector<int64_t>{2, 2, 2}, // kernel_shape
// vector<int64_t>{2, 2, 2, 2, 2, 2}, // pads
// vector<int64_t>{2, 2, 2} // strides
Inputs inputs;
// X: {2, 1, 4, 4, 4}
inputs.emplace_back(
std::vector<float>{0.46796226501464844f, -0.4613912105560303f, 0.33512794971466064f,
-0.4010460674762726f, 0.41722816228866577f, -0.048133403062820435f,
0.20415884256362915f, 0.03189706802368164f, -0.04779183864593506f,
-0.0795503556728363f, 0.4987630844116211f, 0.3506373167037964f,
0.48065757751464844f, 0.269855260848999f, -0.2463444471359253f,
0.19044137001037598f, -0.11830493807792664f, -0.2576887905597687f,
-0.33940935134887695f, -0.257951021194458f, -0.08279827237129211f,
0.3513314127922058f, -0.29122066497802734f, -0.43358397483825684f,
-0.13429927825927734f, 0.44032156467437744f, 0.05308258533477783f,
-0.3499870300292969f, -0.28474611043930054f, -0.44209951162338257f,
-0.07418054342269897f, -0.10919415950775146f, 0.2845439314842224f,
0.3498746156692505f, -0.19313520193099976f, 0.32609254121780396f,
0.4880145788192749f, 0.05574071407318115f, -0.46457427740097046f,
-0.02524462342262268f, -0.18780940771102905f, -0.14720159769058228f,
0.207585871219635f, 0.47157740592956543f, -0.05567386746406555f,
-0.49871665239334106f, 0.2274145483970642f, 0.4589425325393677f,
-0.4725189805030823f, -0.4358765780925751f, 0.2841453552246094f,
-0.27037882804870605f, 0.34227508306503296f, 0.33575427532196045f,
-0.19485199451446533f, -0.27679920196533203f, -0.4238079786300659f,
-0.4385119676589966f, 0.43724071979522705f, 0.3065117597579956f,
0.45696544647216797f, 0.05291992425918579f, -0.023618370294570923f,
-0.1860884726047516f, 0.08669537305831909f, 0.32541000843048096f,
0.1846179962158203f, -0.1984834372997284f, -0.2754465937614441f,
0.32004624605178833f, -0.34846532344818115f, 0.0999596118927002f,
-0.11374691128730774f, 0.21225297451019287f, -0.02315312623977661f,
0.1671370267868042f, 0.22319108247756958f, 0.03609824180603027f,
-0.1587022840976715f, 0.059984564781188965f, -0.03951650857925415f,
-0.4841443598270416f, 0.32919085025787354f, -0.23115816712379456f,
0.39441078901290894f, -0.3554944396018982f, -0.17022761702537537f,
-0.055081307888031006f, 0.15856128931045532f, -0.4183449149131775f,
-0.2474445104598999f, 0.03603637218475342f, -0.2836887538433075f,
0.4602506160736084f, 0.29092925786972046f, -0.199321448802948f,
0.380856454372406f, -0.13847029209136963f, -0.238397479057312f,
-0.1907123327255249f, -0.11061936616897583f, -0.08717870712280273f,
0.24449139833450317f, -0.14727482199668884f, 0.1437196135520935f,
0.3955056071281433f, -0.12538021802902222f, 0.11590522527694702f,
0.4598066806793213f, -0.30005723237991333f, -0.46578651666641235f,
-0.33955082297325134f, -0.2671887278556824f, 0.3611910939216614f,
-0.11423084139823914f, -0.08382436633110046f, -0.31819307804107666f,
0.14515334367752075f, 0.3157258629798889f, 0.33179205656051636f,
-0.2558857202529907f, 0.11888682842254639f, 0.12824326753616333f,
-0.33106181025505066f, 0.2549159526824951f, -0.46760573983192444f,
-0.11983257532119751f, 0.1834418773651123f});
// W: {2, 1, 2, 2, 2}
inputs.emplace_back(std::vector<float>{0.388077974319458f,
-0.16366064548492432f,
-0.42871910333633423f,
0.4276432394981384f,
0.21517693996429443f,
0.007908165454864502f,
0.33897721767425537f,
0.21843165159225464f,
0.34095364809036255f,
-0.17043980956077576f,
-0.013571739196777344f,
-0.26793742179870605f,
-0.34863436222076416f,
-0.2672275900840759f,
-0.36691007018089294f,
0.37296557426452637f});
// B: {2}
inputs.emplace_back(std::vector<float>{0.4310183525085449f, -0.4564093053340912f});
// {2, 2, 3, 3, 3}
Outputs expected_output{std::vector<float>{
0.5332361459732056f, 0.6628494262695312f, 0.544619083404541f, 0.4242798388004303f,
0.6271085739135742f, 0.6721994876861572f, 0.43064039945602417f, 0.4246789515018463f,
0.53834068775177f, 0.6932926177978516f, 0.42797625064849854f, 0.2218741625547409f,
0.29522019624710083f, 0.8329390287399292f, 0.37605351209640503f, 0.43735477328300476f,
0.2920728623867035f, 0.6692450046539307f, 0.5527016520500183f, 0.22643595933914185f,
0.5138190984725952f, 0.3041342794895172f, 0.7423423528671265f, 0.26707080006599426f,
0.4617553651332855f, 0.32416003942489624f, 0.511577844619751f, -0.28187549114227295f,
-0.5031181573867798f, -0.5793710947036743f, -0.5992864370346069f, -0.5055556893348694f,
-0.7562476396560669f, -0.44363799691200256f, -0.5730307102203369f, -0.6302952766418457f,
-0.4756688177585602f, -0.728988528251648f, -0.3900943398475647f, -0.6694478988647461f,
-0.38822290301322937f, -0.35774707794189453f, -0.39807581901550293f, -0.547709047794342f,
-0.35872578620910645f, -0.5326492786407471f, -0.40852290391921997f, -0.4537881314754486f,
-0.4545857608318329f, -0.379546195268631f, -0.5250767469406128f, -0.42439910769462585f,
-0.5558245182037354f, -0.38563215732574463f, 0.44995537400245667f, 0.5007325410842896f,
0.49359965324401855f, 0.40685802698135376f, 0.407518208026886f, 0.4628955125808716f,
0.4301188290119171f, 0.40635955333709717f, 0.4260363280773163f, 0.55128413438797f,
0.5498291254043579f, 0.27105778455734253f, 0.40259143710136414f, 0.5747092962265015f,
0.4187920391559601f, 0.4507707953453064f, 0.420598566532135f, 0.3950541913509369f,
0.593889057636261f, 0.16578882932662964f, 0.5332239270210266f, 0.43014785647392273f,
0.50260329246521f, 0.39225444197654724f, 0.4074971079826355f, 0.5073125958442688f,
0.3823610544204712f, -0.4240749180316925f, -0.41936254501342773f, -0.5241475105285645f,
-0.5220003724098206f, -0.502869725227356f, -0.5122783780097961f, -0.4260129928588867f,
-0.4105660617351532f, -0.4483373165130615f, -0.33759188652038574f, -0.735706090927124f,
-0.3714444637298584f, -0.4888814687728882f, -0.6191370487213135f, -0.2640320658683777f,
-0.47542816400527954f, -0.5078460574150085f, -0.4205915927886963f, -0.5584549903869629f,
-0.39770257472991943f, -0.45317384600639343f, -0.5598302483558655f, -0.2542789578437805f,
-0.5359901785850525f, -0.48090484738349915f, -0.38603779673576355f, -0.4991581439971924f}};
Outputs outputs{execute(function, inputs, "INTERPRETER")};
EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
TEST(onnx, model_matmul_vec_ten3d)
{
auto function = onnx_import::import_onnx_function(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment