ie_preprocess.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief This header file provides structures to store info about pre-processing of network inputs (scale, mean image,
7  * ...)
8  *
9  * @file ie_preprocess.hpp
10  */
11 #pragma once
12 
13 #include <memory>
14 #include <vector>
15 
16 #include "ie_blob.h"
17 
18 namespace InferenceEngine {
19 
20 /**
21  * @brief This structure stores info about pre-processing of network inputs (scale, mean image, ...)
22  */
24  /** @brief Scale parameter for a channel */
25  float stdScale = 1;
26 
27  /** @brief Mean value for a channel */
28  float meanValue = 0;
29 
30  /** @brief Mean data for a channel */
32 
33  /** @brief Smart pointer to an instance */
34  using Ptr = std::shared_ptr<PreProcessChannel>;
35 };
36 
37 /**
38  * @brief Defines available types of mean
39  */
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 */
44 };
45 
46 /**
47  * @enum ResizeAlgorithm
48  * @brief Represents the list of supported resize algorithms.
49  */
50 enum ResizeAlgorithm { NO_RESIZE = 0, RESIZE_BILINEAR, RESIZE_AREA };
51 
52 /**
53  * @brief This class stores pre-process information for the input
54  */
56  // Channel data
57  std::vector<PreProcessChannel::Ptr> _channelsInfo;
58  MeanVariant _variant = NONE;
59 
60  // Resize Algorithm to be applied for input before inference if needed.
61  ResizeAlgorithm _resizeAlg = NO_RESIZE;
62 
63  // Color format to be used in on-demand color conversions applied to input before inference
64  ColorFormat _colorFormat = ColorFormat::RAW;
65 
66 public:
67  /**
68  * @brief Overloaded [] operator to safely get the channel by an index
69  *
70  * Throws an exception if channels are empty
71  *
72  * @param index Index of the channel to get
73  * @return The pre-process channel instance
74  */
76  if (_channelsInfo.empty()) {
77  THROW_IE_EXCEPTION << "accessing pre-process when nothing was set.";
78  }
79  if (index >= _channelsInfo.size()) {
80  THROW_IE_EXCEPTION << "pre process index " << index << " is out of bounds.";
81  }
82  return _channelsInfo[index];
83  }
84 
85  /**
86  * @brief operator [] to safely get the channel preprocessing information by index.
87  *
88  * Throws exception if channels are empty or index is out of border
89  *
90  * @param index Index of the channel to get
91  * @return The const preprocess channel instance
92  */
93  const PreProcessChannel::Ptr& operator[](size_t index) const {
94  if (_channelsInfo.empty()) {
95  THROW_IE_EXCEPTION << "accessing pre-process when nothing was set.";
96  }
97  if (index >= _channelsInfo.size()) {
98  THROW_IE_EXCEPTION << "pre process index " << index << " is out of bounds.";
99  }
100  return _channelsInfo[index];
101  }
102 
103  /**
104  * @brief Returns a number of channels to preprocess
105  *
106  * @return The number of channels
107  */
108  size_t getNumberOfChannels() const {
109  return _channelsInfo.size();
110  }
111 
112  /**
113  * @brief Initializes with given number of channels
114  *
115  * @param numberOfChannels Number of channels to initialize
116  */
117  void init(const size_t numberOfChannels) {
118  _channelsInfo.resize(numberOfChannels);
119  for (auto& channelInfo : _channelsInfo) {
120  channelInfo = std::make_shared<PreProcessChannel>();
121  }
122  }
123 
124  /**
125  * @brief Sets mean image values if operation is applicable.
126  *
127  * Also sets the mean type to MEAN_IMAGE for all channels
128  *
129  * @param meanImage Blob with a mean image
130  */
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();
140  }
141  _variant = MEAN_IMAGE;
142  }
143 
144  /**
145  * @brief Sets mean image values if operation is applicable.
146  *
147  * Also sets the mean type to MEAN_IMAGE for a particular channel
148  *
149  * @param meanImage Blob with a mean image
150  * @param channel Index of a particular channel
151  */
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();
160  }
161  _variant = MEAN_IMAGE;
162  _channelsInfo[channel]->meanData = meanImage;
163  }
164 
165  /**
166  * @brief Sets a type of mean operation
167  *
168  * @param variant Type of mean operation to set
169  */
170  void setVariant(const MeanVariant& variant) {
171  _variant = variant;
172  }
173 
174  /**
175  * @brief Gets a type of mean operation
176  *
177  * @return The type of mean operation
178  */
180  return _variant;
181  }
182 
183  /**
184  * @brief Sets resize algorithm to be used during pre-processing
185  *
186  * @param alg Resize algorithm
187  */
189  _resizeAlg = alg;
190  }
191 
192  /**
193  * @brief Gets preconfigured resize algorithm
194  *
195  * @return Resize algorithm
196  */
198  return _resizeAlg;
199  }
200 
201  /**
202  * @brief Changes the color format of the input data provided by the user
203  *
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)
207  *
208  * @param fmt A new color format associated with the input
209  */
211  _colorFormat = fmt;
212  }
213 
214  /**
215  * @brief Gets a color format associated with the input
216  *
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.
220  */
222  return _colorFormat;
223  }
224 };
225 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
This structure stores info about pre-processing of network inputs (scale, mean image, ...)
Definition: ie_preprocess.hpp:23
void setMeanImageForChannel(const Blob::Ptr &meanImage, const size_t channel)
Sets mean image values if operation is applicable.
Definition: ie_preprocess.hpp:152
Definition: cldnn_config.hpp:16
Definition: ie_preprocess.hpp:42
ResizeAlgorithm
Represents the list of supported resize algorithms.
Definition: ie_preprocess.hpp:50
void setColorFormat(ColorFormat fmt)
Changes the color format of the input data provided by the user.
Definition: ie_preprocess.hpp:210
ResizeAlgorithm getResizeAlgorithm() const
Gets preconfigured resize algorithm.
Definition: ie_preprocess.hpp:197
A header file for Blob and generic TBlob<>
This class stores pre-process information for the input.
Definition: ie_preprocess.hpp:55
const PreProcessChannel::Ptr & operator[](size_t index) const
operator [] to safely get the channel preprocessing information by index.
Definition: ie_preprocess.hpp:93
void setMeanImage(const Blob::Ptr &meanImage)
Sets mean image values if operation is applicable.
Definition: ie_preprocess.hpp:131
PreProcessChannel::Ptr & operator[](size_t index)
Overloaded [] operator to safely get the channel by an index.
Definition: ie_preprocess.hpp:75
std::shared_ptr< PreProcessChannel > Ptr
Smart pointer to an instance.
Definition: ie_preprocess.hpp:34
void init(const size_t numberOfChannels)
Initializes with given number of channels.
Definition: ie_preprocess.hpp:117
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:42
ColorFormat
Extra information about input color format for preprocessing.
Definition: ie_common.h:141
Blob::Ptr meanData
Mean data for a channel.
Definition: ie_preprocess.hpp:31
Definition: ie_preprocess.hpp:41
MeanVariant getMeanVariant() const
Gets a type of mean operation.
Definition: ie_preprocess.hpp:179
size_t getNumberOfChannels() const
Returns a number of channels to preprocess.
Definition: ie_preprocess.hpp:108
void setResizeAlgorithm(const ResizeAlgorithm &alg)
Sets resize algorithm to be used during pre-processing.
Definition: ie_preprocess.hpp:188
Definition: ie_preprocess.hpp:43
ColorFormat getColorFormat() const
Gets a color format associated with the input.
Definition: ie_preprocess.hpp:221
void setVariant(const MeanVariant &variant)
Sets a type of mean operation.
Definition: ie_preprocess.hpp:170
MeanVariant
Defines available types of mean.
Definition: ie_preprocess.hpp:40
float meanValue
Mean value for a channel.
Definition: ie_preprocess.hpp:28
float stdScale
Scale parameter for a channel.
Definition: ie_preprocess.hpp:25