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

Add 'pad' operator support for ov::preprocess::PrePostProcessor #23093

Merged
merged 21 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions src/core/include/openvino/core/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "openvino/core/preprocess/pre_post_process.hpp"
#include "openvino/core/preprocess/preprocess_steps.hpp"
#include "openvino/core/preprocess/resize_algorithm.hpp"
#include "openvino/core/preprocess/padding_mode.hpp"

// type
#include "openvino/core/type/element_type.hpp"
Expand Down
19 changes: 19 additions & 0 deletions src/core/include/openvino/core/preprocess/padding_mode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

namespace ov {
namespace preprocess {

/// \brief An enum containing all supported padding modes in preprocessing
enum class PaddingMode {
PAD_CONSTANT, //!< padded values are taken from the value/values input
PAD_EDGE, //!< Padded values are copied from the respective edge of the input data tensor
PAD_REFLECT, //!< Padded values are computed by reflecting the input data tensor at the edge. Values on the edges are not duplicated.
PAD_SYMMETRIC //!< Padded values are computed by reflecting the input data tensor at the edge, but values on the edges are duplicated.
};
ccinv marked this conversation as resolved.
Show resolved Hide resolved

} // namespace preprocess
} // namespace ov
27 changes: 27 additions & 0 deletions src/core/include/openvino/core/preprocess/preprocess_steps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "openvino/core/core_visibility.hpp"
#include "openvino/core/preprocess/color_format.hpp"
#include "openvino/core/preprocess/resize_algorithm.hpp"
#include "openvino/core/preprocess/padding_mode.hpp"
#include "openvino/core/type/element_type.hpp"

namespace ov {
Expand Down Expand Up @@ -80,6 +81,32 @@ class OPENVINO_API PreProcessSteps final {
/// \return Reference to 'this' to allow chaining with other calls in a builder-like manner
PreProcessSteps& mean(const std::vector<float>& values);

/// \brief Add pad preprocess operation
/// Extends an input tensor on edges with constants
///
/// \param pads_begin Number of padding elements to add at the beginning of each axis.
/// \param pads_end Number of padding elements to add at the end of each axis.
/// \param value Value to be populated in the padded area
///
/// \return Reference to 'this' to allow chaining with other calls in a builder-like manner
PreProcessSteps& pad(const std::vector<int>& pads_begin,
const std::vector<int>& pads_end,
float value,
PaddingMode mode);

/// \brief Add pad preprocess operation
/// Extends an input tensor on edges with constants
///
/// \param pads_begin Number of padding elements to add at the beginning of each axis.
/// \param pads_end Number of padding elements to add at the end of each axis.
/// \param values Values to be populated in the padded area
///
/// \return Reference to 'this' to allow chaining with other calls in a builder-like manner
PreProcessSteps& pad(const std::vector<int>& pads_begin,
const std::vector<int>& pads_end,
const std::vector<float>& values,
PaddingMode mode);

/// \brief Signature for custom preprocessing operation. Custom preprocessing operation takes one input node and
/// produces one output node. For more advanced cases, client's code can use transformation passes over ov::Model
/// directly
Expand Down
16 changes: 16 additions & 0 deletions src/core/src/preprocess/pre_post_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,22 @@ PreProcessSteps& PreProcessSteps::mean(const std::vector<float>& values) {
return *this;
}

PreProcessSteps& PreProcessSteps::pad(const std::vector<int>& pads_begin,
const std::vector<int>& pads_end,
float value,
PaddingMode mode) {
m_impl->add_pad_impl(pads_begin, pads_end, std::vector<float>{value}, mode);
return *this;
}

PreProcessSteps& PreProcessSteps::pad(const std::vector<int>& pads_begin,
const std::vector<int>& pads_end,
const std::vector<float>& values,
PaddingMode mode) {
m_impl->add_pad_impl(pads_begin, pads_end, values, mode);
return *this;
}

PreProcessSteps& PreProcessSteps::convert_element_type(const element::Type& type) {
m_impl->add_convert_impl(type);
return *this;
Expand Down
56 changes: 56 additions & 0 deletions src/core/src/preprocess/preprocess_steps_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,62 @@ void PreStepsList::add_mean_impl(const std::vector<float>& values) {
"mean " + vector_to_string(values));
}

void PreStepsList::add_pad_impl(const std::vector<int>& pads_begin,
const std::vector<int>& pads_end,
const std::vector<float>& values,
ccinv marked this conversation as resolved.
Show resolved Hide resolved
PaddingMode mode) {
std::string name;
name = "pad(begin " + vector_to_string(pads_begin) +
ccinv marked this conversation as resolved.
Show resolved Hide resolved
", end " + vector_to_string(pads_end);
switch (mode) {
case PaddingMode::PAD_CONSTANT:
name += ", with " + vector_to_string(values) + ")";
break;
case PaddingMode::PAD_EDGE:
name += ", copied from edge";
break;
case PaddingMode::PAD_REFLECT:
name += ", reflected from tensor";
break;
case PaddingMode::PAD_SYMMETRIC:
name += ", symmetrically added from tensor";
break;
}

m_actions.emplace_back(
[pads_begin, pads_end, values, mode](const std::vector<Output<Node>>& nodes,
const std::shared_ptr<Model>& function,
PreprocessingContext& ctxt) {
OPENVINO_ASSERT(!nodes.empty(), "Internal error: Can't add pad for empty input.");
ccinv marked this conversation as resolved.
Show resolved Hide resolved
OPENVINO_ASSERT(nodes.size() == 1,
"Can't pad multi-plane input. Suggesting to convert current image to "
"RGB/BGR color format using 'PreProcessSteps::convert_color'");

const auto to_mode = [](const PaddingMode mode) -> op::PadMode {
ccinv marked this conversation as resolved.
Show resolved Hide resolved
switch (mode) {
case PaddingMode::PAD_CONSTANT:
return op::PadMode::CONSTANT;
case PaddingMode::PAD_EDGE:
return op::PadMode::EDGE;
case PaddingMode::PAD_REFLECT:
return op::PadMode::REFLECT;
case PaddingMode::PAD_SYMMETRIC:
return op::PadMode::SYMMETRIC;
}
};

auto node = nodes[0];
ccinv marked this conversation as resolved.
Show resolved Hide resolved
auto pad_value = opset8::Constant::create(node.get_element_type(), Shape{}, values);

auto npads_begin = opset8::Constant::create(element::i64, Shape{pads_begin.size()}, pads_begin);
auto npads_end = opset8::Constant::create(element::i64, Shape{pads_end.size()}, pads_end);
auto npad_value = opset8::Constant::create(element::f32, Shape{}, values);

auto pad = std::make_shared<opset8::Pad>(node, npads_begin, npads_end, npad_value, to_mode(mode));
return std::make_tuple(std::vector<Output<Node>>{pad}, true);
}, name);
}

void PreStepsList::add_convert_impl(const element::Type& type) {
m_actions.emplace_back(
[type](const std::vector<Output<Node>>& nodes,
Expand Down
4 changes: 4 additions & 0 deletions src/core/src/preprocess/preprocess_steps_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ class PreStepsList {
public:
void add_scale_impl(const std::vector<float>& values);
void add_mean_impl(const std::vector<float>& values);
void add_pad_impl(const std::vector<int>& pads_begin,
const std::vector<int>& pads_end,
const std::vector<float>& values,
PaddingMode mode);
void add_convert_impl(const element::Type& type);
void add_crop_impl(const std::vector<int>& begin, const std::vector<int>& end);
void add_resize_impl(ResizeAlgorithm alg, int dst_height, int dst_width);
Expand Down
16 changes: 16 additions & 0 deletions src/core/tests/preprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,22 @@ TEST(pre_post_process, mean_vector_dynamic_channels_shape) {
EXPECT_EQ(f->get_output_element_type(0), element::f32);
}

TEST(pre_post_process, pad_vector_constant_layout) {
auto f = create_simple_function(element::f32, Shape{1, 3, 199, 199});
auto p = PrePostProcessor(f);

std::stringstream exp_dump;
exp_dump << PartialShape{1, 3, 200, 200};
try{
p.input().preprocess().pad({0, 0, 0, 0}, {0, 0, 1, 1}, 0, PaddingMode::PAD_CONSTANT);
p.build();
Copy link
Contributor

Choose a reason for hiding this comment

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

can we also compare shapes and check that padding is applied?
or even better - compare with reference function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think in this test shapes have been checked(find expected shape in error because it won't match next layer)
Besides I added out_of_range and dim_mismatch check, do I require to incorporate any additional examinations?

} catch (const ov::Exception& err) {
EXPECT_TRUE(std::string(err.what()).find(exp_dump.str())) << err.what();
ccinv marked this conversation as resolved.
Show resolved Hide resolved
} catch (...) {
FAIL() << "Expected ov::Exception";
}
}

TEST(pre_post_process, resize_no_model_layout) {
auto f = create_simple_function(element::f32, Shape{1, 3, 224, 224});
auto p = PrePostProcessor(f);
Expand Down
Loading