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