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