Skip to content

Commit

Permalink
Preprocessing tests to new utils (openvinotoolkit#21752)
Browse files Browse the repository at this point in the history
  • Loading branch information
vurusovs authored Dec 20, 2023
1 parent c95a34e commit 8384279
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include <gtest/gtest.h>

#include <openvino/core/preprocess/resize_algorithm.hpp>

#include "shared_test_classes/base/ov_subgraph.hpp"

using TParams = std::tuple<std::string>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
//

#include "preprocessing/yuv_to_grey_tests.hpp"
#include "shared_test_classes/single_layer/convert_color_i420.hpp"
#include "shared_test_classes/single_layer/convert_color_nv12.hpp"
#include "shared_test_classes/base/utils/generate_inputs.hpp"

namespace ov {
namespace preprocess {
Expand Down Expand Up @@ -167,7 +166,7 @@ TEST_P(PreprocessingYUV2GreyTest, convert_single_plane_i420_use_opencv) {
// Test various possible r/g/b values within dimensions
const auto input_yuv_shape = Shape{1, get_full_height() * 3 / 2, width, 1};
const auto input_y_shape = Shape{1, get_full_height(), width, 1};
auto ov20_input_yuv = LayerTestsDefinitions::I420TestUtils::color_test_image(height, width, b_step);
auto ov20_input_yuv = ov::test::utils::color_test_image(height, width, b_step, ColorFormat::I420_SINGLE_PLANE);
auto ov20_input_y =
std::vector<uint8_t>(ov20_input_yuv.begin(), ov20_input_yuv.begin() + shape_size(input_y_shape));

Expand All @@ -188,8 +187,7 @@ TEST_P(PreprocessingYUV2GreyTest, convert_three_plane_i420_use_opencv) {
const auto input_y_shape = Shape{1, get_full_height(), width, 1};
const auto input_u_shape = Shape{1, get_full_height() / 2, width / 2, 1};
const auto input_v_shape = Shape{1, get_full_height() / 2, width / 2, 1};
// const auto input_uv_shape = Shape{1, get_full_height() / 2, width / 2, 2};
auto ov20_input_yuv = LayerTestsDefinitions::I420TestUtils::color_test_image(height, width, b_step);
auto ov20_input_yuv = ov::test::utils::color_test_image(height, width, b_step, ColorFormat::I420_THREE_PLANES);

auto input_yuv_iter = ov20_input_yuv.begin();
auto ov20_input_y = std::vector<uint8_t>(input_yuv_iter, input_yuv_iter + shape_size(input_y_shape));
Expand Down Expand Up @@ -218,7 +216,7 @@ TEST_P(PreprocessingYUV2GreyTest, convert_single_plane_nv12_use_opencv) {
// Test various possible r/g/b values within dimensions
const auto input_yuv_shape = Shape{1, get_full_height() * 3 / 2, width, 1};
const auto input_y_shape = Shape{1, get_full_height(), width, 1};
auto ov20_input_yuv = LayerTestsDefinitions::NV12TestUtils::color_test_image(height, width, b_step);
auto ov20_input_yuv = ov::test::utils::color_test_image(height, width, b_step, ColorFormat::NV12_SINGLE_PLANE);
auto ov20_input_y =
std::vector<uint8_t>(ov20_input_yuv.begin(), ov20_input_yuv.begin() + shape_size(input_y_shape));

Expand All @@ -238,7 +236,7 @@ TEST_P(PreprocessingYUV2GreyTest, convert_two_plane_nv12_use_opencv) {
// Test various possible r/g/b values within dimensions
const auto input_y_shape = Shape{1, get_full_height(), width, 1};
const auto input_uv_shape = Shape{1, get_full_height() / 2, width / 2, 2};
auto ov20_input_yuv = LayerTestsDefinitions::NV12TestUtils::color_test_image(height, width, b_step);
auto ov20_input_yuv = ov::test::utils::color_test_image(height, width, b_step, ColorFormat::NV12_TWO_PLANES);

auto input_yuv_iter = ov20_input_yuv.begin();
auto ov20_input_y = std::vector<uint8_t>(input_yuv_iter, input_yuv_iter + shape_size(input_y_shape));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

#pragma once

#include "ie_core.hpp"
#include "ngraph/node.hpp"
#include "openvino/core/preprocess/color_format.hpp"

#include "shared_test_classes/base/utils/ranges.hpp"

Expand All @@ -16,6 +15,8 @@ namespace utils {
void set_const_ranges(double _min, double _max);
void reset_const_ranges();

std::vector<uint8_t> color_test_image(size_t height, size_t width, int b_step, ov::preprocess::ColorFormat format);

using InputsMap = std::map<ov::NodeTypeInfo, std::function<ov::runtime::Tensor(
const std::shared_ptr<ov::Node>& node,
size_t port,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ void reset_const_ranges() {
const_range.is_defined = false;
}

std::vector<uint8_t> color_test_image(size_t height, size_t width, int b_step, ov::preprocess::ColorFormat format) {
// Test all possible r/g/b values within dimensions
int b_dim = 255 / b_step + 1;
auto input_yuv = std::vector<uint8_t>(height * b_dim * width * 3 / 2);
for (int b = 0; b <= 255; b += b_step) {
for (size_t y = 0; y < height / 2; y++) {
for (size_t x = 0; x < width / 2; x++) {
int r = static_cast<int>(y) * 512 / static_cast<int>(height);
int g = static_cast<int>(x) * 512 / static_cast<int>(width);
// Can't use random y/u/v for testing as this can lead to invalid R/G/B values
int y_val = ((66 * r + 129 * g + 25 * b + 128) / 256) + 16;
int u_val = ((-38 * r - 74 * g + 112 * b + 128) / 256) + 128;
int v_val = ((112 * r - 94 * g + 18 * b + 128) / 256) + 128;

size_t b_offset = height * width * b / b_step * 3 / 2;
if (ov::preprocess::ColorFormat::I420_SINGLE_PLANE == format ||
ov::preprocess::ColorFormat::I420_THREE_PLANES == format) {
size_t u_index = b_offset + height * width + y * width / 2 + x;
size_t v_index = u_index + height * width / 4;
input_yuv[u_index] = u_val;
input_yuv[v_index] = v_val;
} else {
size_t uv_index = b_offset + height * width + y * width + x * 2;
input_yuv[uv_index] = u_val;
input_yuv[uv_index + 1] = v_val;
}
size_t y_index = b_offset + y * 2 * width + x * 2;
input_yuv[y_index] = y_val;
input_yuv[y_index + 1] = y_val;
input_yuv[y_index + width] = y_val;
input_yuv[y_index + width + 1] = y_val;
}
}
}
return input_yuv;
}

namespace {

/**
Expand Down Expand Up @@ -912,48 +949,7 @@ ov::runtime::Tensor generate(const
}

namespace color_conversion {
enum class ColorFormat {
i420,
nv12
};

inline std::vector<uint8_t> color_test_image(size_t height, size_t width, int b_step, ColorFormat format) {
// Test all possible r/g/b values within dimensions
int b_dim = 255 / b_step + 1;
auto input_yuv = std::vector<uint8_t>(height * b_dim * width * 3 / 2);
for (int b = 0; b <= 255; b += b_step) {
for (size_t y = 0; y < height / 2; y++) {
for (size_t x = 0; x < width / 2; x++) {
int r = static_cast<int>(y) * 512 / static_cast<int>(height);
int g = static_cast<int>(x) * 512 / static_cast<int>(width);
// Can't use random y/u/v for testing as this can lead to invalid R/G/B values
int y_val = ((66 * r + 129 * g + 25 * b + 128) / 256) + 16;
int u_val = ((-38 * r - 74 * g + 112 * b + 128) / 256) + 128;
int v_val = ((112 * r - 94 * g + 18 * b + 128) / 256) + 128;

size_t b_offset = height * width * b / b_step * 3 / 2;
if (ColorFormat::i420 == format) {
size_t u_index = b_offset + height * width + y * width / 2 + x;
size_t v_index = u_index + height * width / 4;
input_yuv[u_index] = u_val;
input_yuv[v_index] = v_val;
} else {
size_t uv_index = b_offset + height * width + y * width + x * 2;
input_yuv[uv_index] = u_val;
input_yuv[uv_index + 1] = v_val;
}
size_t y_index = b_offset + y * 2 * width + x * 2;
input_yuv[y_index] = y_val;
input_yuv[y_index + 1] = y_val;
input_yuv[y_index + width] = y_val;
input_yuv[y_index + width + 1] = y_val;
}
}
}
return input_yuv;
}

void fill_tensor(ov::Tensor& tensor, ColorFormat format) {
void fill_tensor(ov::Tensor& tensor, ov::preprocess::ColorFormat format) {
size_t full_height = tensor.get_shape()[1];
size_t full_width = tensor.get_shape()[2];
int b_dim = static_cast<int>(full_height * 2 / (3 * full_width));
Expand All @@ -976,7 +972,7 @@ ov::runtime::Tensor generate(const std::shared_ptr<ov::op::v8::I420toRGB>& node,
if (node->inputs().size() > 1 || b_dim < 2)
return generate(std::static_pointer_cast<ov::Node>(node), port, elemType, targetShape);
ov::Tensor tensor(elemType, targetShape);
color_conversion::fill_tensor(tensor, color_conversion::ColorFormat::i420);
color_conversion::fill_tensor(tensor, ov::preprocess::ColorFormat::I420_SINGLE_PLANE);
return tensor;
}

Expand All @@ -989,7 +985,7 @@ ov::runtime::Tensor generate(const
if (node->inputs().size() > 1 || b_dim < 2)
return generate(std::static_pointer_cast<ov::Node>(node), port, elemType, targetShape);
ov::Tensor tensor(elemType, targetShape);
color_conversion::fill_tensor(tensor, color_conversion::ColorFormat::i420);
color_conversion::fill_tensor(tensor, ov::preprocess::ColorFormat::I420_SINGLE_PLANE);
return tensor;
}

Expand All @@ -1003,7 +999,7 @@ ov::runtime::Tensor generate(const
if (node->inputs().size() > 1 || b_dim < 2)
return generate(std::static_pointer_cast<ov::Node>(node), port, elemType, targetShape);
ov::Tensor tensor(elemType, targetShape);
color_conversion::fill_tensor(tensor, color_conversion::ColorFormat::nv12);
color_conversion::fill_tensor(tensor, ov::preprocess::ColorFormat::NV12_SINGLE_PLANE);
return tensor;
}

Expand All @@ -1016,7 +1012,7 @@ ov::runtime::Tensor generate(const
if (node->inputs().size() > 1 || b_dim < 2)
return generate(std::static_pointer_cast<ov::Node>(node), port, elemType, targetShape);
ov::Tensor tensor(elemType, targetShape);
color_conversion::fill_tensor(tensor, color_conversion::ColorFormat::nv12);
color_conversion::fill_tensor(tensor, ov::preprocess::ColorFormat::NV12_SINGLE_PLANE);
return tensor;
}

Expand Down

0 comments on commit 8384279

Please sign in to comment.