Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReorgYolo reference implementation #2384

Merged
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4adb609
Align ReorgYolo to the spec (vector strides -> int stride)
mitruska Sep 21, 2020
fa11af9
ReorgYolo ref impl
mitruska Sep 22, 2020
70be5e8
ReorgYolo evaluate method
mitruska Sep 22, 2020
8b78ebe
ReorgYolo tests
mitruska Sep 22, 2020
93e1043
Tests update
mitruska Sep 22, 2020
9ad6890
Style apply
mitruska Sep 22, 2020
fac48fb
Add some coments
mitruska Sep 22, 2020
874d613
Code refactor
mitruska Sep 22, 2020
2f7f060
Comment update
mitruska Sep 22, 2020
dbedbb9
Merge remote-tracking branch 'origin/master' into mitruska/reorg_yolo…
mitruska Sep 22, 2020
1d4a18a
Style apply
mitruska Sep 22, 2020
39dcf8c
Build fix, mark evaluate as override
mitruska Sep 22, 2020
3a4bb7d
Revert "Align ReorgYolo to the spec (vector strides -> int stride)"
mitruska Sep 23, 2020
7e2bd04
Use int_executable instead of evaluate
mitruska Sep 23, 2020
3dbea04
Use char* instead of templates
mitruska Sep 24, 2020
9b9d4e2
Code refactor
mitruska Sep 24, 2020
3b3d19d
Merge remote-tracking branch 'origin/master' into mitruska/reorg_yolo…
mitruska Sep 24, 2020
0534e1c
Comment update
mitruska Sep 30, 2020
987a42b
Merge remote-tracking branch 'origin/master' into mitruska/reorg_yolo…
mitruska Sep 30, 2020
9c1bab3
Code review comment
postrational Oct 7, 2020
d48dd85
Merge remote-tracking branch 'origin/master' into mitruska/reorg_yolo…
mitruska Oct 8, 2020
b8a23b4
Merge remote-tracking branch 'origin/master' into mitruska/reorg_yolo…
mitruska Oct 14, 2020
6f6d725
Add constructor aligned with spec
mitruska Oct 14, 2020
1c93d96
Update shape validation
mitruska Oct 14, 2020
8d10e7e
Update attributes tests
mitruska Oct 14, 2020
96585d5
Add type_prop tests
mitruska Oct 14, 2020
2b39ebe
Update backend tests
mitruska Oct 14, 2020
664d411
Add single layer tests
mitruska Oct 14, 2020
7ca480e
Update the spec
mitruska Oct 14, 2020
2cdc20b
Merge remote-tracking branch 'origin/master' into mitruska/reorg_yolo…
mitruska Oct 14, 2020
405034f
Remove wrong transformation test
mitruska Oct 14, 2020
cf3fb73
Merge remote-tracking branch 'origin/master' into mitruska/reorg_yolo…
mitruska Oct 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//*****************************************************************************
// Copyright 2017-2020 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************

#pragma once

#include <cmath>
#include <cstddef>

#include "ngraph/shape.hpp"

namespace ngraph
{
namespace runtime
{
namespace reference
{
void reorg_yolo(const char* arg,
char* out,
const Shape& in_shape,
int64_t stride,
const size_t elem_size);
}
}
}
89 changes: 89 additions & 0 deletions ngraph/core/reference/src/runtime/reference/reorg_yolo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//*****************************************************************************
// Copyright 2017-2020 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************

#include <cmath>
#include <stdio.h>

#include "ngraph/runtime/reference/reorg_yolo.hpp"
#include "ngraph/shape.hpp"

using namespace ngraph;

namespace ngraph
{
namespace runtime
{
namespace reference
{
void reorg_yolo(const char* arg,
char* out,
const Shape& in_shape,
int64_t stride,
const size_t elem_size)
{
// [N, C, H, W]
size_t in_N = in_shape[0];
size_t in_C = in_shape[1];
size_t in_H = in_shape[2];
size_t in_W = in_shape[3];

// Inference output shape logic:
// in_shape [N,C,H,W] -> out_shape [N, C*(stride*stride), H/stride, W/stride]
// ReorgYolo implementation calculates new indices like for backprop:
// in_shape [N,C,H,W] -> out_shape [N, C/(stride*stride), H*stride, W*stride]

size_t impl_out_C = in_C / (stride * stride);
if (impl_out_C == 0)
{
throw ngraph_error(
"ReorgYolo. For [N, C, H, W] input shape, C >= (stride*stride) is "
"required.");
}
size_t impl_out_H = in_H * stride;
size_t impl_out_W = in_W * stride;

for (size_t n = 0; n < in_N; ++n)
{
for (size_t c = 0; c < in_C; ++c)
{
for (size_t h = 0; h < in_H; ++h)
{
for (size_t w = 0; w < in_W; ++w)
{
size_t offset = c / impl_out_C;
size_t impl_c = c % impl_out_C;
size_t impl_h = h * stride + offset / stride;
size_t impl_w = w * stride + offset % stride;

size_t arg_index =
((n * impl_out_C + impl_c) * impl_out_H + impl_h) * impl_out_W +
impl_w;
size_t dest_index = ((n * in_C + c) * in_H + h) * in_W + w;

arg_index *= elem_size;
dest_index *= elem_size;

std::copy(arg + arg_index,
arg + (arg_index + elem_size),
out + dest_index);
}
}
}
}
}
}
}
}
1 change: 1 addition & 0 deletions ngraph/core/src/op/reorg_yolo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//*****************************************************************************

#include "ngraph/op/reorg_yolo.hpp"
#include "ngraph/runtime/reference/reorg_yolo.hpp"

using namespace std;
using namespace ngraph;
Expand Down
1 change: 1 addition & 0 deletions ngraph/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ set(MULTI_TEST_SRC
backend/reduce_prod.in.cpp
backend/reduce_sum.in.cpp
backend/relu.in.cpp
backend/reorg_yolo.in.cpp
backend/replace_slice.in.cpp
backend/reshape.in.cpp
backend/reverse_sequence.in.cpp
Expand Down
138 changes: 138 additions & 0 deletions ngraph/test/backend/reorg_yolo.in.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//*****************************************************************************
// Copyright 2017-2020 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdlib>
#include <random>
#include <string>

// clang-format off
#ifdef ${BACKEND_NAME}_FLOAT_TOLERANCE_BITS
#define DEFAULT_FLOAT_TOLERANCE_BITS ${BACKEND_NAME}_FLOAT_TOLERANCE_BITS
#endif

#ifdef ${BACKEND_NAME}_DOUBLE_TOLERANCE_BITS
#define DEFAULT_DOUBLE_TOLERANCE_BITS ${BACKEND_NAME}_DOUBLE_TOLERANCE_BITS
#endif
// clang-format on

#include "gtest/gtest.h"
#include "ngraph/ngraph.hpp"
#include "util/engine/test_engines.hpp"
#include "util/test_case.hpp"
#include "util/test_control.hpp"
#include "util/type_prop.hpp"

using namespace std;
using namespace ngraph;

static string s_manifest = "${MANIFEST}";
using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME});

NGRAPH_TEST(${BACKEND_NAME}, reorg_yolo_stride_2)
{
// in_shape [N,C,H,W]
const auto in_shape = Shape{1, 8, 4, 4};
auto p = make_shared<op::Parameter>(element::f32, in_shape);
size_t stride = 2;
auto reorg_yolo = make_shared<op::v0::ReorgYolo>(p, Strides{stride});
auto fun = make_shared<Function>(OutputVector{reorg_yolo}, ParameterVector{p});

std::vector<float> inputs(128);
std::iota(inputs.begin(), inputs.end(), 0);
std::vector<float> expected_result{
0, 2, 4, 6, 16, 18, 20, 22, 32, 34, 36, 38, 48, 50, 52, 54,
64, 66, 68, 70, 80, 82, 84, 86, 96, 98, 100, 102, 112, 114, 116, 118,
1, 3, 5, 7, 17, 19, 21, 23, 33, 35, 37, 39, 49, 51, 53, 55,
65, 67, 69, 71, 81, 83, 85, 87, 97, 99, 101, 103, 113, 115, 117, 119,
8, 10, 12, 14, 24, 26, 28, 30, 40, 42, 44, 46, 56, 58, 60, 62,
72, 74, 76, 78, 88, 90, 92, 94, 104, 106, 108, 110, 120, 122, 124, 126,
9, 11, 13, 15, 25, 27, 29, 31, 41, 43, 45, 47, 57, 59, 61, 63,
73, 75, 77, 79, 89, 91, 93, 95, 105, 107, 109, 111, 121, 123, 125, 127};
// in_shape [N,C,H,W] -> out_shape [N, C*stride*stride, H/stride, W/stride]
Shape expected_shape = Shape{
in_shape[0], in_shape[1] * stride * stride, in_shape[2] / stride, in_shape[3] / stride};

auto test_case = test::TestCase<TestEngine>(fun);
test_case.add_input<float>(inputs);
test_case.add_expected_output<float>(expected_shape, expected_result);
test_case.run();
}

NGRAPH_TEST(${BACKEND_NAME}, reorg_yolo_stride_3)
{
// in_shape [N,C,H,W]
const auto in_shape = Shape{1, 9, 3, 3};
auto p = make_shared<op::Parameter>(element::f32, in_shape);
size_t stride = 3;
auto reorg_yolo = make_shared<op::v0::ReorgYolo>(p, Strides{stride});
auto fun = make_shared<Function>(OutputVector{reorg_yolo}, ParameterVector{p});

std::vector<float> inputs(81);
std::iota(inputs.begin(), inputs.end(), 0);
std::vector<float> expected_result{
0, 3, 6, 27, 30, 33, 54, 57, 60, 1, 4, 7, 28, 31, 34, 55, 58, 61, 2, 5, 8,
29, 32, 35, 56, 59, 62, 9, 12, 15, 36, 39, 42, 63, 66, 69, 10, 13, 16, 37, 40, 43,
64, 67, 70, 11, 14, 17, 38, 41, 44, 65, 68, 71, 18, 21, 24, 45, 48, 51, 72, 75, 78,
19, 22, 25, 46, 49, 52, 73, 76, 79, 20, 23, 26, 47, 50, 53, 74, 77, 80};
// in_shape [N,C,H,W] -> out_shape [N, C*stride*stride, H/stride, W/stride]
Shape expected_shape = Shape{
in_shape[0], in_shape[1] * stride * stride, in_shape[2] / stride, in_shape[3] / stride};

auto test_case = test::TestCase<TestEngine>(fun);
test_case.add_input<float>(inputs);
test_case.add_expected_output<float>(expected_shape, expected_result);
test_case.run();
}

NGRAPH_TEST(${BACKEND_NAME}, reorg_yolo_catch_small_shape_stride)
{
// Throw error test: For [N, C, H, W] input shape, C >= (stride*stride) is required.
// in_shape [N,C,H,W]
const auto in_shape = Shape{1, 1, 4, 4};
auto p = make_shared<op::Parameter>(element::f32, in_shape);
size_t stride = 2;
auto reorg_yolo = make_shared<op::v0::ReorgYolo>(p, Strides{stride});
auto fun = make_shared<Function>(OutputVector{reorg_yolo}, ParameterVector{p});

std::vector<float> inputs{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
std::vector<float> fake_output(16);

// in_shape [N,C,H,W] -> out_shape [N, C*stride*stride, H/stride, W/stride]
Shape expected_shape = Shape{
in_shape[0], in_shape[1] * stride * stride, in_shape[2] / stride, in_shape[3] / stride};

try
{
auto test_case = test::TestCase<TestEngine>(fun);
test_case.add_input<float>(inputs);
test_case.add_expected_output<float>(expected_shape, fake_output);
test_case.run();

// Should have thrown, so fail if it didn't
FAIL() << "Incompatible stride was not detected.";
jdanieck marked this conversation as resolved.
Show resolved Hide resolved
}
catch (const ngraph_error& error)
{
EXPECT_HAS_SUBSTRING(error.what(), std::string("stride"));
}
catch (...)
{
FAIL() << "Stride size check failed for unexpected reason.";
}
}
3 changes: 3 additions & 0 deletions ngraph/test/runtime/ie/unit_test.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,9 @@ batch_norm_inference_0eps_f64
batch_norm_inference_f64
batch_norm_training_0eps_f64

# Floating point exception (core dumped)
reorg_yolo_catch_small_shape_stride

# Function inputs number differ from number of given inputs
batch_norm_inference_parameters_duplication

Expand Down
11 changes: 11 additions & 0 deletions ngraph/test/runtime/interpreter/int_executable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include "ngraph/runtime/reference/product.hpp"
#include "ngraph/runtime/reference/quantize.hpp"
#include "ngraph/runtime/reference/relu.hpp"
#include "ngraph/runtime/reference/reorg_yolo.hpp"
#include "ngraph/runtime/reference/replace_slice.hpp"
#include "ngraph/runtime/reference/reshape.hpp"
#include "ngraph/runtime/reference/result.hpp"
Expand Down Expand Up @@ -892,6 +893,16 @@ class INTERPRETER_BACKEND_API ngraph::runtime::interpreter::INTExecutable : publ
pbox->get_attrs());
break;
}
case OP_TYPEID::ReorgYolo_v0:
jdanieck marked this conversation as resolved.
Show resolved Hide resolved
{
const op::v0::ReorgYolo* reorg_yolo = static_cast<const op::v0::ReorgYolo*>(&node);
runtime::reference::reorg_yolo(args[0]->get_data_ptr<char>(),
out[0]->get_data_ptr<char>(),
args[0]->get_shape(),
reorg_yolo->get_strides().at(0),
args[0]->get_element_type().size());
break;
}
case OP_TYPEID::Quantize:
{
const op::Quantize* quantize = static_cast<const op::Quantize*>(&node);
Expand Down
1 change: 1 addition & 0 deletions ngraph/test/runtime/interpreter/opset_int_tbl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#define ID_SUFFIX(NAME) NAME##_v0
NGRAPH_OP(DetectionOutput, op::v0)
NGRAPH_OP(ReorgYolo, op::v0)
NGRAPH_OP(RNNCell, op::v0)
#undef ID_SUFFIX

Expand Down