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