1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief This header file provides structures to store info about pre-processing of network inputs (scale, mean image,
9 * @file ie_preprocess.hpp
18 namespace InferenceEngine {
21 * @brief This structure stores info about pre-processing of network inputs (scale, mean image, ...)
23 struct PreProcessChannel {
24 /** @brief Scale parameter for a channel */
27 /** @brief Mean value for a channel */
30 /** @brief Mean data for a channel */
33 /** @brief Smart pointer to an instance */
34 using Ptr = std::shared_ptr<PreProcessChannel>;
38 * @brief Defines available types of mean
41 MEAN_IMAGE, /**< mean value is specified for each input pixel */
42 MEAN_VALUE, /**< mean value is specified for each input channel */
43 NONE, /**< no mean value specified */
47 * @enum ResizeAlgorithm
48 * @brief Represents the list of supported resize algorithms.
50 enum ResizeAlgorithm { NO_RESIZE = 0, RESIZE_BILINEAR, RESIZE_AREA };
53 * @brief This class stores pre-process information for the input
55 class PreProcessInfo {
57 std::vector<PreProcessChannel::Ptr> _channelsInfo;
58 MeanVariant _variant = NONE;
60 // Resize Algorithm to be applied for input before inference if needed.
61 ResizeAlgorithm _resizeAlg = NO_RESIZE;
63 // Color format to be used in on-demand color conversions applied to input before inference
64 ColorFormat _colorFormat = ColorFormat::RAW;
68 * @brief Overloaded [] operator to safely get the channel by an index
70 * Throws an exception if channels are empty
72 * @param index Index of the channel to get
73 * @return The pre-process channel instance
75 PreProcessChannel::Ptr& operator[](size_t index) {
76 if (_channelsInfo.empty()) {
77 THROW_IE_EXCEPTION << "accessing pre-process when nothing was set.";
79 if (index >= _channelsInfo.size()) {
80 THROW_IE_EXCEPTION << "pre process index " << index << " is out of bounds.";
82 return _channelsInfo[index];
86 * @brief operator [] to safely get the channel preprocessing information by index.
88 * Throws exception if channels are empty or index is out of border
90 * @param index Index of the channel to get
91 * @return The const preprocess channel instance
93 const PreProcessChannel::Ptr& operator[](size_t index) const {
94 if (_channelsInfo.empty()) {
95 THROW_IE_EXCEPTION << "accessing pre-process when nothing was set.";
97 if (index >= _channelsInfo.size()) {
98 THROW_IE_EXCEPTION << "pre process index " << index << " is out of bounds.";
100 return _channelsInfo[index];
104 * @brief Returns a number of channels to preprocess
106 * @return The number of channels
108 size_t getNumberOfChannels() const {
109 return _channelsInfo.size();
113 * @brief Initializes with given number of channels
115 * @param numberOfChannels Number of channels to initialize
117 void init(const size_t numberOfChannels) {
118 _channelsInfo.resize(numberOfChannels);
119 for (auto& channelInfo : _channelsInfo) {
120 channelInfo = std::make_shared<PreProcessChannel>();
125 * @brief Sets mean image values if operation is applicable.
127 * Also sets the mean type to MEAN_IMAGE for all channels
129 * @param meanImage Blob with a mean image
131 void setMeanImage(const Blob::Ptr& meanImage) {
132 if (meanImage.get() == nullptr) {
133 THROW_IE_EXCEPTION << "Failed to set invalid mean image: nullptr";
134 } else if (meanImage.get()->getTensorDesc().getLayout() != Layout::CHW) {
135 THROW_IE_EXCEPTION << "Mean image layout should be CHW";
136 } else if (meanImage.get()->getTensorDesc().getDims().size() != 3) {
137 THROW_IE_EXCEPTION << "Failed to set invalid mean image: number of dimensions != 3";
138 } else if (meanImage.get()->getTensorDesc().getDims()[0] != getNumberOfChannels()) {
139 THROW_IE_EXCEPTION << "Failed to set invalid mean image: number of channels != " << getNumberOfChannels();
141 _variant = MEAN_IMAGE;
145 * @brief Sets mean image values if operation is applicable.
147 * Also sets the mean type to MEAN_IMAGE for a particular channel
149 * @param meanImage Blob with a mean image
150 * @param channel Index of a particular channel
152 void setMeanImageForChannel(const Blob::Ptr& meanImage, const size_t channel) {
153 if (meanImage.get() == nullptr) {
154 THROW_IE_EXCEPTION << "Failed to set invalid mean image for channel: nullptr";
155 } else if (meanImage.get()->getTensorDesc().getDims().size() != 2) {
156 THROW_IE_EXCEPTION << "Failed to set invalid mean image for channel: number of dimensions != 2";
157 } else if (channel >= _channelsInfo.size()) {
158 THROW_IE_EXCEPTION << "Channel " << channel
159 << " exceed number of PreProcess channels: " << _channelsInfo.size();
161 _variant = MEAN_IMAGE;
162 _channelsInfo[channel]->meanData = meanImage;
166 * @brief Sets a type of mean operation
168 * @param variant Type of mean operation to set
170 void setVariant(const MeanVariant& variant) {
175 * @brief Gets a type of mean operation
177 * @return The type of mean operation
179 MeanVariant getMeanVariant() const {
184 * @brief Sets resize algorithm to be used during pre-processing
186 * @param alg Resize algorithm
188 void setResizeAlgorithm(const ResizeAlgorithm& alg) {
193 * @brief Gets preconfigured resize algorithm
195 * @return Resize algorithm
197 ResizeAlgorithm getResizeAlgorithm() const {
202 * @brief Changes the color format of the input data provided by the user
204 * This function should be called before loading the network to the plugin
205 * Setting color format different from ColorFormat::RAW enables automatic color conversion
206 * (as a part of built-in preprocessing routine)
208 * @param fmt A new color format associated with the input
210 void setColorFormat(ColorFormat fmt) {
215 * @brief Gets a color format associated with the input
217 * @details By default, the color format is ColorFormat::RAW meaning
218 * there is no particular color format assigned to the input
219 * @return Color format.
221 ColorFormat getColorFormat() const {
225 } // namespace InferenceEngine