updated readme file due to moving CMake scripts to the root folder
[platform/upstream/dldt.git] / inference-engine / include / inference_engine.hpp
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_plugin_dispatcher.hpp>
21 #include <ie_plugin_config.hpp>
22 #include <ie_icnn_network.hpp>
23 #include <ie_icnn_network_stats.hpp>
24 #include <ie_core.hpp>
25 #include <cpp/ie_cnn_net_reader.h>
26 #include <cpp/ie_plugin_cpp.hpp>
27 #include <cpp/ie_executable_network.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 INFERENCE_ENGINE_DEPRECATED
39 inline void TopResults(unsigned int n, TBlob<T> &input, std::vector<unsigned> &output) {
40     SizeVector dims = input.getTensorDesc().getDims();
41     size_t input_rank = dims.size();
42     if (!input_rank || !dims[0])
43         THROW_IE_EXCEPTION << "Input blob has incorrect dimensions!";
44     size_t batchSize = dims[0];
45     std::vector<unsigned> indexes(input.size() / batchSize);
46
47     n = static_cast<unsigned>(std::min<size_t>((size_t) n, input.size()));
48
49     output.resize(n * batchSize);
50
51     for (size_t i = 0; i < batchSize; i++) {
52         size_t offset = i * (input.size() / batchSize);
53         T *batchData = input.data();
54         batchData += offset;
55
56         std::iota(std::begin(indexes), std::end(indexes), 0);
57         std::partial_sort(std::begin(indexes), std::begin(indexes) + n, std::end(indexes),
58                           [&batchData](unsigned l, unsigned r) {
59                               return batchData[l] > batchData[r];
60                           });
61         for (unsigned j = 0; j < n; j++) {
62             output.at(i * n + j) = indexes.at(j);
63         }
64     }
65 }
66
67 #define TBLOB_TOP_RESULT(precision)\
68     case InferenceEngine::Precision::precision  : {\
69         using myBlobType = InferenceEngine::PrecisionTrait<Precision::precision>::value_type;\
70         TBlob<myBlobType> &tblob = dynamic_cast<TBlob<myBlobType> &>(input);\
71         TopResults(n, tblob, output);\
72         break;\
73     }
74
75 /**
76  * @brief Gets the top n results from a blob
77  * @param n Top n count
78  * @param input 1D blob that contains probabilities
79  * @param output Vector of indexes for the top n places
80  */
81 INFERENCE_ENGINE_DEPRECATED
82 inline void TopResults(unsigned int n, Blob &input, std::vector<unsigned> &output) {
83     IE_SUPPRESS_DEPRECATED_START
84     switch (input.getTensorDesc().getPrecision()) {
85         TBLOB_TOP_RESULT(FP32);
86         TBLOB_TOP_RESULT(FP16);
87         TBLOB_TOP_RESULT(Q78);
88         TBLOB_TOP_RESULT(I16);
89         TBLOB_TOP_RESULT(U8);
90         TBLOB_TOP_RESULT(I8);
91         TBLOB_TOP_RESULT(U16);
92         TBLOB_TOP_RESULT(I32);
93         default:
94             THROW_IE_EXCEPTION << "cannot locate blob for precision: " << input.getTensorDesc().getPrecision();
95     }
96     IE_SUPPRESS_DEPRECATED_END
97 }
98
99 #undef TBLOB_TOP_RESULT
100
101 /**
102  * @brief Copies a 8-bit RGB image to the blob.
103  * Throws an exception in case of dimensions or input size mismatch
104  * @tparam data_t Type of the target blob
105  * @param RGB8 8-bit RGB image
106  * @param RGB8_size Size of the image
107  * @param blob Target blob to write image to
108  */
109 template<typename data_t>
110 INFERENCE_ENGINE_DEPRECATED
111 void copyFromRGB8(uint8_t *RGB8, size_t RGB8_size, InferenceEngine::TBlob<data_t> *blob) {
112     SizeVector dims = blob->getTensorDesc().getDims();
113     if (4 != dims.size())
114         THROW_IE_EXCEPTION << "Cannot write data to input blob! Blob has incorrect dimensions size "
115                            << dims.size();
116     size_t num_channels = dims[1];  // because RGB
117     size_t num_images = dims[0];
118     size_t w = dims[3];
119     size_t h = dims[2];
120     size_t nPixels = w * h;
121
122     if (RGB8_size != w * h * num_channels * num_images)
123         THROW_IE_EXCEPTION << "input pixels mismatch, expecting " << w * h * num_channels * num_images
124                            << " bytes, got: " << RGB8_size;
125
126     std::vector<data_t *> dataArray;
127     for (unsigned int n = 0; n < num_images; n++) {
128         for (unsigned int i = 0; i < num_channels; i++) {
129             if (!n && !i && dataArray.empty()) {
130                 dataArray.push_back(blob->data());
131             } else {
132                 dataArray.push_back(dataArray.at(n * num_channels + i - 1) + nPixels);
133             }
134         }
135     }
136     for (size_t n = 0; n < num_images; n++) {
137         size_t n_num_channels = n * num_channels;
138         size_t n_num_channels_nPixels = n_num_channels * nPixels;
139         for (size_t i = 0; i < nPixels; i++) {
140             size_t i_num_channels = i * num_channels + n_num_channels_nPixels;
141             for (size_t j = 0; j < num_channels; j++) {
142                 dataArray.at(n_num_channels + j)[i] = RGB8[i_num_channels + j];
143             }
144         }
145     }
146 }
147
148 /**
149  * @brief Splits the RGB channels to either I16 Blob or float blob.
150  * The image buffer is assumed to be packed with no support for strides.
151  * @param imgBufRGB8 Packed 24bit RGB image (3 bytes per pixel: R-G-B)
152  * @param lengthbytesSize Size in bytes of the RGB image. It is equal to amount of pixels times 3 (number of channels)
153  * @param input Blob to contain the split image (to 3 channels)
154  */
155 INFERENCE_ENGINE_DEPRECATED
156 inline void ConvertImageToInput(unsigned char *imgBufRGB8, size_t lengthbytesSize, Blob &input) {
157     IE_SUPPRESS_DEPRECATED_START
158     TBlob<float> *float_input = dynamic_cast<TBlob<float> *>(&input);
159     if (float_input != nullptr) copyFromRGB8(imgBufRGB8, lengthbytesSize, float_input);
160
161     TBlob<short> *short_input = dynamic_cast<TBlob<short> *>(&input);
162     if (short_input != nullptr) copyFromRGB8(imgBufRGB8, lengthbytesSize, short_input);
163
164     TBlob<uint8_t> *byte_input = dynamic_cast<TBlob<uint8_t> *>(&input);
165     if (byte_input != nullptr) copyFromRGB8(imgBufRGB8, lengthbytesSize, byte_input);
166     IE_SUPPRESS_DEPRECATED_END
167 }
168
169 /**
170  * @brief Copies data from a certain precision to float
171  * @param dst Pointer to an output float buffer, must be allocated before the call
172  * @param src Source blob to take data from
173  */
174 template<typename T>
175 INFERENCE_ENGINE_DEPRECATED
176 void copyToFloat(float *dst, const InferenceEngine::Blob *src) {
177     if (!dst) {
178         return;
179     }
180     const InferenceEngine::TBlob<T> *t_blob = dynamic_cast<const InferenceEngine::TBlob<T> *>(src);
181     if (t_blob == nullptr) {
182         THROW_IE_EXCEPTION << "input type is " << src->getTensorDesc().getPrecision() << " but input is not " << typeid(T).name();
183     }
184
185     const T *srcPtr = t_blob->readOnly();
186     if (srcPtr == nullptr) {
187         THROW_IE_EXCEPTION << "Input data was not allocated.";
188     }
189     for (size_t i = 0; i < t_blob->size(); i++) dst[i] = srcPtr[i];
190 }
191
192 }  // namespace InferenceEngine