inference_engine.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 A header file that provides a set of convenience utility functions and the main include file for all other .h files.
7  * @file inference_engine.hpp
8  */
9 #pragma once
10 
11 #include <vector>
12 #include <numeric>
13 #include <algorithm>
14 #include <memory>
15 
16 #include <ie_blob.h>
17 #include <ie_api.h>
18 #include <ie_error.hpp>
19 #include <ie_layers.h>
20 #include <ie_device.hpp>
21 #include <ie_plugin_dispatcher.hpp>
22 #include <ie_plugin_config.hpp>
23 #include <ie_icnn_network.hpp>
24 #include <ie_icnn_network_stats.hpp>
25 #include <cpp/ie_cnn_net_reader.h>
26 #include <cpp/ie_plugin_cpp.hpp>
28 #include <ie_version.hpp>
29 
30 namespace InferenceEngine {
31 /**
32  * @brief Gets the top n results from a tblob
33  * @param n Top n count
34  * @param input 1D tblob that contains probabilities
35  * @param output Vector of indexes for the top n places
36  */
37 template<class T>
38 inline void TopResults(unsigned int n, TBlob<T> &input, std::vector<unsigned> &output) {
39  size_t input_rank = input.dims().size();
40  if (!input_rank || !input.dims().at(input_rank - 1))
41  THROW_IE_EXCEPTION << "Input blob has incorrect dimensions!";
42  size_t batchSize = input.dims().at(input_rank - 1);
43  std::vector<unsigned> indexes(input.size() / batchSize);
44 
45  n = static_cast<unsigned>(std::min<size_t>((size_t) n, input.size()));
46 
47  output.resize(n * batchSize);
48 
49  for (size_t i = 0; i < batchSize; i++) {
50  size_t offset = i * (input.size() / batchSize);
51  T *batchData = input.data();
52  batchData += offset;
53 
54  std::iota(std::begin(indexes), std::end(indexes), 0);
55  std::partial_sort(std::begin(indexes), std::begin(indexes) + n, std::end(indexes),
56  [&batchData](unsigned l, unsigned r) {
57  return batchData[l] > batchData[r];
58  });
59  for (unsigned j = 0; j < n; j++) {
60  output.at(i * n + j) = indexes.at(j);
61  }
62  }
63 }
64 
65 #define TBLOB_TOP_RESULT(precision)\
66  case InferenceEngine::Precision::precision : {\
67  using myBlobType = InferenceEngine::PrecisionTrait<Precision::precision>::value_type;\
68  TBlob<myBlobType> &tblob = dynamic_cast<TBlob<myBlobType> &>(input);\
69  TopResults(n, tblob, output);\
70  break;\
71  }
72 
73 /**
74  * @brief Gets the top n results from a blob
75  * @param n Top n count
76  * @param input 1D blob that contains probabilities
77  * @param output Vector of indexes for the top n places
78  */
79 inline void TopResults(unsigned int n, Blob &input, std::vector<unsigned> &output) {
80  switch (input.precision()) {
81  TBLOB_TOP_RESULT(FP32);
82  TBLOB_TOP_RESULT(FP16);
83  TBLOB_TOP_RESULT(Q78);
84  TBLOB_TOP_RESULT(I16);
85  TBLOB_TOP_RESULT(U8);
86  TBLOB_TOP_RESULT(I8);
87  TBLOB_TOP_RESULT(U16);
88  TBLOB_TOP_RESULT(I32);
89  default:
90  THROW_IE_EXCEPTION << "cannot locate blob for precision: " << input.precision();
91  }
92 }
93 
94 #undef TBLOB_TOP_RESULT
95 
96 /**
97  * @brief Copies a 8-bit RGB image to the blob.
98  * Throws an exception in case of dimensions or input size mismatch
99  * @tparam data_t Type of the target blob
100  * @param RGB8 8-bit RGB image
101  * @param RGB8_size Size of the image
102  * @param blob Target blob to write image to
103  */
104 template<typename data_t>
105 void copyFromRGB8(uint8_t *RGB8, size_t RGB8_size, InferenceEngine::TBlob<data_t> *blob) {
106  if (4 != blob->dims().size())
107  THROW_IE_EXCEPTION << "Cannot write data to input blob! Blob has incorrect dimensions size "
108  << blob->dims().size();
109  size_t num_channels = blob->dims()[2]; // because RGB
110  size_t num_images = blob->dims()[3];
111  size_t w = blob->dims()[0];
112  size_t h = blob->dims()[1];
113  size_t nPixels = w * h;
114 
115  if (RGB8_size != w * h * num_channels * num_images)
116  THROW_IE_EXCEPTION << "input pixels mismatch, expecting " << w * h * num_channels * num_images
117  << " bytes, got: " << RGB8_size;
118 
119  std::vector<data_t *> dataArray;
120  for (unsigned int n = 0; n < num_images; n++) {
121  for (unsigned int i = 0; i < num_channels; i++) {
122  if (!n && !i && dataArray.empty()) {
123  dataArray.push_back(blob->data());
124  } else {
125  dataArray.push_back(dataArray.at(n * num_channels + i - 1) + nPixels);
126  }
127  }
128  }
129  for (size_t n = 0; n < num_images; n++) {
130  size_t n_num_channels = n * num_channels;
131  size_t n_num_channels_nPixels = n_num_channels * nPixels;
132  for (size_t i = 0; i < nPixels; i++) {
133  size_t i_num_channels = i * num_channels + n_num_channels_nPixels;
134  for (size_t j = 0; j < num_channels; j++) {
135  dataArray.at(n_num_channels + j)[i] = RGB8[i_num_channels + j];
136  }
137  }
138  }
139 }
140 
141 /**
142  * @brief Splits the RGB channels to either I16 Blob or float blob.
143  * The image buffer is assumed to be packed with no support for strides.
144  * @param imgBufRGB8 Packed 24bit RGB image (3 bytes per pixel: R-G-B)
145  * @param lengthbytesSize Size in bytes of the RGB image. It is equal to amount of pixels times 3 (number of channels)
146  * @param input Blob to contain the split image (to 3 channels)
147  */
148 inline void ConvertImageToInput(unsigned char *imgBufRGB8, size_t lengthbytesSize, Blob &input) {
149  TBlob<float> *float_input = dynamic_cast<TBlob<float> *>(&input);
150  if (float_input != nullptr) copyFromRGB8(imgBufRGB8, lengthbytesSize, float_input);
151 
152  TBlob<short> *short_input = dynamic_cast<TBlob<short> *>(&input);
153  if (short_input != nullptr) copyFromRGB8(imgBufRGB8, lengthbytesSize, short_input);
154 
155  TBlob<uint8_t> *byte_input = dynamic_cast<TBlob<uint8_t> *>(&input);
156  if (byte_input != nullptr) copyFromRGB8(imgBufRGB8, lengthbytesSize, byte_input);
157 }
158 
159 /**
160  * @brief Copies data from a certain precision to float
161  * @param dst Pointer to an output float buffer, must be allocated before the call
162  * @param src Source blob to take data from
163  */
164 template<typename T>
165 void copyToFloat(float *dst, const InferenceEngine::Blob *src) {
166  if (!dst) {
167  return;
168  }
169  const InferenceEngine::TBlob<T> *t_blob = dynamic_cast<const InferenceEngine::TBlob<T> *>(src);
170  if (t_blob == nullptr) {
171  THROW_IE_EXCEPTION << "input type is " << src->precision() << " but input is not " << typeid(T).name();
172  }
173 
174  const T *srcPtr = t_blob->readOnly();
175  if (srcPtr == nullptr) {
176  THROW_IE_EXCEPTION << "Input data was not allocated.";
177  }
178  for (size_t i = 0; i < t_blob->size(); i++) dst[i] = srcPtr[i];
179 }
180 
181 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:22
A header file that provides wrapper classes for IExecutableNetwork.
void copyToFloat(float *dst, const InferenceEngine::Blob *src)
Copies data from a certain precision to float.
Definition: inference_engine.hpp:165
const SizeVector dims() const noexcept
Returns the tensor dimensions vector with reversed order.
Definition: ie_blob.h:166
A header file that provides versioning information for the inference engine shared library...
A header file for a plugin logging mechanism.
void TopResults(unsigned int n, TBlob< T > &input, std::vector< unsigned > &output)
Gets the top n results from a tblob.
Definition: inference_engine.hpp:38
This is a header file for the Network reader class (wrapper) used to build networks from a given IR...
Definition: ie_argmax_layer.hpp:11
virtual LockedMemory< T > data() noexcept
Creates an new empty rvalue LockedMemory object.
Definition: ie_blob.h:413
A header file for Blob and generic TBlob<>
A header for a class to handle plugin loading.
This is a header file for the ICNNNetwork class.
size_t size() const noexcept
Returns the total number of elements (a product of all the dims)
Definition: ie_blob.h:180
Represents real host memory allocated for a Tensor/Blob per C type.
Definition: ie_blob.h:266
virtual LockedMemory< const T > readOnly() const noexcept
Creates a new empty rvalue read-only LockedMemory object.
Definition: ie_blob.h:421
a header file for internal Layers structure to describe layers information
a header for advanced hardware related properties for clDNN plugin To use in SetConfig() method of pl...
This is a header file for the ICNNNetworkStats class.
The macro defines a symbol import/export mechanism essential for Microsoft Windows(R) OS...
This class implements a container object that represents a tensor in memory (host and remote/accelera...
Definition: ie_blob.h:33
Precision precision() const noexcept
Returns the tensor precision of the current Blob object.
Definition: ie_blob.h:57
This header file contains aspects of working on different devices like CPU, GEN, FPGA, etc.
void copyFromRGB8(uint8_t *RGB8, size_t RGB8_size, InferenceEngine::TBlob< data_t > *blob)
Copies a 8-bit RGB image to the blob. Throws an exception in case of dimensions or input size mismatc...
Definition: inference_engine.hpp:105
This is a header file for the Inference Engine plugin C++ API.
void ConvertImageToInput(unsigned char *imgBufRGB8, size_t lengthbytesSize, Blob &input)
Splits the RGB channels to either I16 Blob or float blob. The image buffer is assumed to be packed wi...
Definition: inference_engine.hpp:148