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

[I420 color conversion] I420toRGB/I420toBGR reference implementation #8605

Merged
merged 10 commits into from
Nov 18, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <gtest/gtest.h>

#include <openvino/core/function.hpp>
#include <tuple>
#include <openvino/op/i420_to_rgb.hpp>
#include <openvino/op/i420_to_bgr.hpp>

#include "base_reference_test.hpp"
#include "functional_test_utils/skip_tests_config.hpp"

using namespace ov;
using namespace InferenceEngine;
using namespace reference_tests;

class ReferenceConvertColorI420LayerTest : public testing::Test, public CommonReferenceTest {
public:
void SetUp() override {
SKIP_IF_CURRENT_TEST_IS_DISABLED()
abs_threshold = 1.f; // allow R, G, B absolute deviation to 1 (of max 255)
threshold = 1.f; // Ignore relative comparison (100%)
}

public:
template <typename T>
static std::shared_ptr<Function> CreateFunction(const Tensor& input) {
const auto in = std::make_shared<op::v0::Parameter>(input.type, input.shape);
std::shared_ptr<Node> conv;
conv = std::make_shared<T>(in);
auto res = std::make_shared<op::v0::Result>(conv);
return std::make_shared<Function>(ResultVector{res}, ParameterVector {in});
}

template <typename T>
static std::shared_ptr<Function> CreateFunction3(const Tensor& input1, const Tensor& input2, const Tensor& input3) {
const auto in1 = std::make_shared<op::v0::Parameter>(input1.type, input1.shape);
const auto in2 = std::make_shared<op::v0::Parameter>(input2.type, input2.shape);
const auto in3 = std::make_shared<op::v0::Parameter>(input3.type, input3.shape);
std::shared_ptr<Node> conv;
conv = std::make_shared<T>(in1, in2, in3);
auto res = std::make_shared<op::v0::Result>(conv);
return std::make_shared<Function>(ResultVector{res}, ParameterVector {in1, in2, in3});
}
};

TEST_F(ReferenceConvertColorI420LayerTest, CompareWithHardcodedRefs_r_u8_single_rgb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you can use parametrized tests

auto input = std::vector<uint8_t> {0x51, 0x51, 0x51, 0x51,
0x51, 0x51, 0x51, 0x51,
0x5a, 0x5a, 0xf0, 0xf0};
auto input_shape = Shape{1, 3, 4, 1};
auto exp_out = std::vector<uint8_t> {0xff, 0, 0, 0xff, 0, 0, 0xff, 0, 0, 0xff, 0, 0,
0xff, 0, 0, 0xff, 0, 0, 0xff, 0, 0, 0xff, 0, 0};
auto out_shape = Shape{1, 2, 4, 3};
Tensor inp_tensor(input_shape, element::u8, input);
inputData = {inp_tensor.data};
function = CreateFunction<op::v8::I420toRGB>(inp_tensor);
Tensor exp_tensor_u8(out_shape, element::u8, exp_out);
refOutData = {exp_tensor_u8.data};
Exec();
}

TEST_F(ReferenceConvertColorI420LayerTest, CompareWithHardcodedRefs_color_u8_single_bgr) {
auto input = std::vector<uint8_t> {0x51, 0xeb, 0x51, 0xeb,
0x51, 0xeb, 0x51, 0xeb,
0x6d, 0x6d, 0xb8, 0xb8};
auto input_shape = Shape{1, 6, 2, 1};
auto exp_out = std::vector<uint8_t> {37, 37, 164, 217, 216, 255, 37, 37, 164, 217, 216, 255,
37, 37, 164, 217, 216, 255, 37, 37, 164, 217, 216, 255};
auto out_shape = Shape{1, 4, 2, 3};

Tensor inp_tensor(input_shape, element::u8, input);
inputData = {inp_tensor.data};

Tensor exp_tensor_u8(out_shape, element::u8, exp_out);
refOutData = {exp_tensor_u8.data};

function = CreateFunction<op::v8::I420toBGR>(inp_tensor);

Exec();
}

TEST_F(ReferenceConvertColorI420LayerTest, CompareWithHardcodedRefs_g_fp32_single_rgb) {
auto input = std::vector<float> {145.f, 145.f, 145.f, 145.f,
145.f, 145.f, 145.f, 145.f,
54.f, 54.f, 34.f, 34.f};
auto input_shape = Shape{1, 3, 4, 1};
auto exp_out = std::vector<float> {0, 255.f, 0, 0, 255.f, 0, 0, 255.f, 0, 0, 255.f, 0,
0, 255.f, 0, 0, 255.f, 0, 0, 255.f, 0, 0, 255.f, 0};
auto out_shape = Shape{1, 2, 4, 3};

Tensor inp_tensor(input_shape, element::f32, input);
inputData = {inp_tensor.data};

Tensor exp_tensor(out_shape, element::f32, exp_out);
refOutData = {exp_tensor.data};

function = CreateFunction<op::v8::I420toRGB>(inp_tensor);

Exec();
}

TEST_F(ReferenceConvertColorI420LayerTest, CompareWithHardcodedRefs_batch_fp32_three_bgr) {
auto input_y = std::vector<float> {81.f, 81.f, 81.f, 81.f,
145.f, 145.f, 145.f, 145.f,
41.f, 41.f, 41.f, 41.f};
auto input_shape_y = Shape{3, 2, 2, 1};

auto input_u = std::vector<float> {90.,
54.,
240.};
auto input_shape_u = Shape{3, 1, 1, 1};

auto input_v = std::vector<float> {240.,
34.,
110.};
auto input_shape_v = Shape{3, 1, 1, 1};
auto exp_out = std::vector<float> {0, 0, 255., 0, 0, 255., 0, 0, 255., 0, 0, 255.,
0, 255., 0, 0, 255., 0, 0, 255., 0, 0, 255., 0,
255., 0, 0, 255., 0, 0, 255., 0, 0, 255., 0, 0};
auto out_shape = Shape{3, 2, 2, 3};

Tensor inp_tensor_y(input_shape_y, element::f32, input_y);
Tensor inp_tensor_u(input_shape_u, element::f32, input_u);
Tensor inp_tensor_v(input_shape_v, element::f32, input_v);
inputData = {inp_tensor_y.data, inp_tensor_u.data, inp_tensor_v.data};

Tensor exp_tensor(out_shape, element::f32, exp_out);
refOutData = {exp_tensor.data};

function = CreateFunction3<op::v8::I420toBGR>(inp_tensor_y, inp_tensor_u, inp_tensor_v);

Exec();
}

TEST_F(ReferenceConvertColorI420LayerTest, CompareWithHardcodedRefs_color4x4_f32_three_rgb) {
auto input_y = std::vector<float> {81, 235,
81, 235,
81, 81,
81, 81,
145, 145,
145, 145,
41, 41,
41, 41};
auto input_shape_y = Shape{1, 8, 2, 1};

auto input_u = std::vector<float> {109, 90, 54, 240};
auto input_shape_u = Shape{1, 4, 1, 1};
auto input_v = std::vector<float> {184, 240, 34, 110};
auto input_shape_v = Shape{1, 4, 1, 1};

auto exp_out = std::vector<float> {165, 37, 37, 255, 216, 217, 165, 37, 37, 255, 216, 217,
255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0,
0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0,
0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255};
auto out_shape = Shape{1, 2, 2, 3};

Tensor inp_tensor_y(input_shape_y, element::f32, input_y);
Tensor inp_tensor_u(input_shape_u, element::f32, input_u);
Tensor inp_tensor_v(input_shape_v, element::f32, input_v);
inputData = {inp_tensor_y.data, inp_tensor_u.data, inp_tensor_v.data};

Tensor exp_tensor(out_shape, element::f32, exp_out);
refOutData = {exp_tensor.data};

function = CreateFunction3<op::v8::I420toRGB>(inp_tensor_y, inp_tensor_u, inp_tensor_v);

Exec();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "shared_test_classes/single_layer/convert_color_i420.hpp"

using namespace LayerTestsDefinitions;

namespace {

TEST_P(ConvertColorI420LayerTest, Serialize) {
Serialize();
}

const std::vector<ov::Shape> inShapes_nhwc = {
{1, 10, 10, 1}
};

const std::vector<ov::element::Type> inTypes = {
ov::element::u8, ov::element::f32
};

const auto testCase_values = ::testing::Combine(
::testing::ValuesIn(inShapes_nhwc),
::testing::ValuesIn(inTypes),
::testing::Bool(),
::testing::Bool(),
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

INSTANTIATE_TEST_SUITE_P(smoke_CompareWithRefs, ConvertColorI420LayerTest, testCase_values, ConvertColorI420LayerTest::getTestCaseName);

} // namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <vector>

#include "single_layer_tests/convert_color_i420.hpp"
#include "common_test_utils/test_constants.hpp"

using namespace LayerTestsDefinitions;

namespace {

const std::vector<ov::Shape> inShapes_nhwc = {
{1, 10, 10, 1}
};

const std::vector<ov::element::Type> inTypes = {
ov::element::u8, ov::element::f32
};

const auto testCase_values = ::testing::Combine(
::testing::ValuesIn(inShapes_nhwc),
::testing::ValuesIn(inTypes),
::testing::Bool(),
::testing::Bool(),
::testing::Values(CommonTestUtils::DEVICE_CPU)
);


INSTANTIATE_TEST_SUITE_P(smoke_TestsConvertColorI420, ConvertColorI420LayerTest, testCase_values, ConvertColorI420LayerTest::getTestCaseName);

const auto testCase_accuracy_values = ::testing::Combine(
::testing::Values(ov::Shape{1, 16*6, 16, 1}),
::testing::Values(ov::element::u8),
::testing::Values(false),
::testing::Values(true),
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

INSTANTIATE_TEST_SUITE_P(smoke_TestsConvertColorI420_acc,
ConvertColorI420AccuracyTest,
testCase_accuracy_values,
ConvertColorI420LayerTest::getTestCaseName);

const auto testCase_accuracy_values_nightly = ::testing::Combine(
::testing::Values(ov::Shape{1, 256*256, 256, 1}),
::testing::Values(ov::element::u8),
::testing::Values(false),
::testing::Values(true),
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

INSTANTIATE_TEST_SUITE_P(nightly_TestsConvertColorI420_acc,
ConvertColorI420AccuracyTest,
testCase_accuracy_values_nightly,
ConvertColorI420LayerTest::getTestCaseName);

} // namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <vector>

#include "shared_test_classes/subgraph/preprocess.hpp"

using namespace SubgraphTestsDefinitions;

namespace {

using namespace ov::builder::preprocess;

inline std::vector<preprocess_func> myriad_smoke_preprocess_functions() {
return std::vector<preprocess_func>{
preprocess_func(convert_element_type_and_mean, "convert_element_type_and_mean", 0.01f),
preprocess_func(tensor_element_type_and_mean, "tensor_element_type_and_mean", 0.01f),
preprocess_func(mean_only, "mean_only", 0.01f),
preprocess_func(scale_only, "scale_only", 0.01f),
};
}

INSTANTIATE_TEST_SUITE_P(smoke_PrePostProcess_Myriad, PrePostProcessTest,
::testing::Combine(
::testing::ValuesIn(myriad_smoke_preprocess_functions()),
::testing::Values(CommonTestUtils::DEVICE_MYRIAD)),
PrePostProcessTest::getTestCaseName);

} // namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "shared_test_classes/single_layer/convert_color_i420.hpp"

namespace LayerTestsDefinitions {

TEST_P(ConvertColorI420LayerTest, CompareWithRefs) {
Run();
}

TEST_P(ConvertColorI420AccuracyTest, CompareWithRefs) {
Run();
}

} // namespace LayerTestsDefinitions
Loading