forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement reference nGraph implementation for the operation "Experime…
…ntalDetectronTopKROIs" (openvinotoolkit#5675) * Started to write nGraph reference implementation of the operation ExperimentalDetectronTopKROIs. * Written cpp-file for the nGraph reference implementation of the operation ExperimentalDetectronTopKROIs. * Code style fixes. * Started to write evaluate() (in evaluates_map.cpp) for the nGraph operation ExperimentalDetectronsTopKROIs. * Code style fixes. * Written evaluate() for ExperimentalDetectronTopKROIs. * Small fixes. * Written test for ExperimentalDetectronTopKROIs evaluation. * Small fix. * Fixed Apache license header. * Some changes. * int64_t was replaced with size_t in the body of ngraph::runtime::reference::experimental_detectron_topk_rois(). * Added more test for evaluation of ExperimentalDetectronTopKROIs.
- Loading branch information
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
ngraph/core/reference/include/ngraph/runtime/reference/experimental_detectron_topk_rois.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (C) 2018-2021 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <cmath> | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <ngraph/runtime/host_tensor.hpp> | ||
#include <vector> | ||
#include "ngraph/node.hpp" | ||
#include "ngraph/op/util/op_types.hpp" | ||
#include "ngraph/ops.hpp" | ||
#include "ngraph/shape_util.hpp" | ||
|
||
namespace ngraph | ||
{ | ||
namespace runtime | ||
{ | ||
namespace reference | ||
{ | ||
template <typename T> | ||
void experimental_detectron_topk_rois(const T* input_rois, | ||
const T* input_probs, | ||
const Shape& input_rois_shape, | ||
const Shape& input_probs_shape, | ||
size_t max_rois, | ||
T* output_rois) | ||
{ | ||
const size_t input_rois_num = input_rois_shape[0]; | ||
const size_t top_rois_num = std::min(max_rois, input_rois_num); | ||
|
||
std::vector<size_t> idx(input_rois_num); | ||
std::iota(idx.begin(), idx.end(), 0); | ||
std::sort(idx.begin(), idx.end(), [&input_probs](size_t i1, size_t i2) { | ||
return input_probs[i1] > input_probs[i2]; | ||
}); | ||
|
||
for (size_t i = 0; i < top_rois_num; ++i) | ||
{ | ||
output_rois[0] = input_rois[4 * idx[i] + 0]; | ||
output_rois[1] = input_rois[4 * idx[i] + 1]; | ||
output_rois[2] = input_rois[4 * idx[i] + 2]; | ||
output_rois[3] = input_rois[4 * idx[i] + 3]; | ||
output_rois += 4; | ||
} | ||
} | ||
} // namespace reference | ||
} // namespace runtime | ||
} // namespace ngraph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
ngraph/test/backend/experimental_detectron_topk_rois.in.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (C) 2018-2021 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "gtest/gtest.h" | ||
#include "ngraph/ngraph.hpp" | ||
#include "ngraph/runtime/tensor.hpp" | ||
#include "runtime/backend.hpp" | ||
#include "util/all_close.hpp" | ||
#include "util/all_close_f.hpp" | ||
#include "util/engine/test_engines.hpp" | ||
#include "util/known_element_types.hpp" | ||
#include "util/ndarray.hpp" | ||
#include "util/test_case.hpp" | ||
#include "util/test_control.hpp" | ||
#include "util/test_tools.hpp" | ||
|
||
using namespace std; | ||
using namespace ngraph; | ||
|
||
static string s_manifest = "${MANIFEST}"; | ||
|
||
using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME}); | ||
|
||
using ExperimentalTopK = op::v6::ExperimentalDetectronTopKROIs; | ||
|
||
NGRAPH_TEST(${BACKEND_NAME}, experimental_detectron_topk_rois_eval) | ||
{ | ||
size_t num_rois = 1; | ||
|
||
const auto input_rois_shape = Shape{2, 4}; | ||
const auto input_probs_shape = Shape{2}; | ||
const auto output_shape = Shape{1, 4}; | ||
|
||
auto input_rois = std::make_shared<op::Parameter>(element::f32, input_rois_shape); | ||
auto input_probs = std::make_shared<op::Parameter>(element::f32, input_probs_shape); | ||
auto topk_rois = std::make_shared<ExperimentalTopK>(input_rois, input_probs, num_rois); | ||
auto f = std::make_shared<Function>(topk_rois, ParameterVector{input_rois, input_probs}); | ||
|
||
auto backend = runtime::Backend::create("${BACKEND_NAME}"); | ||
auto topk_rois_output = backend->create_tensor(element::f32, output_shape); | ||
|
||
std::vector<float> input_rois_data = {1.0f, 1.0f, 3.0f, 4.0f, 2.0f, 1.0f, 5.0f, 7.0f}; | ||
std::vector<float> input_probs_data = {0.5f, 0.3f}; | ||
std::vector<float> expected_result = {1.0, 1.0, 3.0, 4.0}; | ||
|
||
auto backend_input_rois_data = backend->create_tensor(element::f32, input_rois_shape); | ||
copy_data(backend_input_rois_data, input_rois_data); | ||
auto backend_input_probs_data = backend->create_tensor(element::f32, input_probs_shape); | ||
copy_data(backend_input_probs_data, input_probs_data); | ||
|
||
auto handle = backend->compile(f); | ||
handle->call({topk_rois_output}, {backend_input_rois_data, backend_input_probs_data}); | ||
|
||
ASSERT_TRUE(test::all_close_f(read_vector<float>(topk_rois_output), expected_result)); | ||
} | ||
|
||
NGRAPH_TEST(${BACKEND_NAME}, experimental_detectron_topk_rois_eval_2) | ||
{ | ||
size_t num_rois = 2; | ||
|
||
const auto input_rois_shape = Shape{4, 4}; | ||
const auto input_probs_shape = Shape{4}; | ||
const auto output_shape = Shape{2, 4}; | ||
|
||
auto input_rois = std::make_shared<op::Parameter>(element::f32, input_rois_shape); | ||
auto input_probs = std::make_shared<op::Parameter>(element::f32, input_probs_shape); | ||
auto topk_rois = std::make_shared<ExperimentalTopK>(input_rois, input_probs, num_rois); | ||
auto f = std::make_shared<Function>(topk_rois, ParameterVector{input_rois, input_probs}); | ||
|
||
auto backend = runtime::Backend::create("${BACKEND_NAME}"); | ||
auto topk_rois_output = backend->create_tensor(element::f32, output_shape); | ||
|
||
std::vector<float> input_rois_data = {1.0f, 1.0f, 4.0f, 5.0f, 3.0f, 2.0f, 7.0f, 9.0f, | ||
10.0f, 15.0f, 13.0f, 17.0f, 13.0f, 10.0f, 18.0f, 15.0f}; | ||
std::vector<float> input_probs_data = {0.1f, 0.7f, 0.5f, 0.9f}; | ||
std::vector<float> expected_result = {13.0f, 10.0f, 18.0f, 15.0f, 3.0f, 2.0f, 7.0f, 9.0f}; | ||
|
||
auto backend_input_rois_data = backend->create_tensor(element::f32, input_rois_shape); | ||
copy_data(backend_input_rois_data, input_rois_data); | ||
auto backend_input_probs_data = backend->create_tensor(element::f32, input_probs_shape); | ||
copy_data(backend_input_probs_data, input_probs_data); | ||
|
||
auto handle = backend->compile(f); | ||
handle->call({topk_rois_output}, {backend_input_rois_data, backend_input_probs_data}); | ||
|
||
ASSERT_TRUE(test::all_close_f(read_vector<float>(topk_rois_output), expected_result)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters