aboutsummaryrefslogtreecommitdiff
path: root/modules/dnn/dnn_inference_engine.h
blob: ed8ed3628dbbac0ee498c504ba482370fa01c3eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * dnn_inference_engine.h -  dnn inference engine
 *
 *  Copyright (c) 2019 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Zong Wei <wei.zong@intel.com>
 */

#ifndef _XCMA_DNN_INFERENCE_ENGINE_H_
#define _XCMA_DNN_INFERENCE_ENGINE_H_

#pragma once

#include <vector>
#include <string>
#include <inference_engine.hpp>

#include <xcam_std.h>

namespace XCam {

enum DnnInferTargetDeviceType {
    DnnInferDeviceDefault = 0,
    DnnInferDeviceBalanced = 1,
    DnnInferDeviceCPU = 2,
    DnnInferDeviceGPU = 3,
    DnnInferDeviceFPGA = 4,
    DnnInferDeviceMyriad = 5,
    DnnInferDeviceHetero = 8
};

enum DnnInferPrecisionType {
    DnnInferPrecisionMixed = 0,
    DnnInferPrecisionFP32 = 10,
    DnnInferPrecisionFP16 = 11,
    DnnInferPrecisionQ78 = 20,
    DnnInferPrecisionI16 = 30,
    DnnInferPrecisionU8 = 40,
    DnnInferPrecisionI8 = 50,
    DnnInferPrecisionU16 = 60,
    DnnInferPrecisionI32 = 70,
    DnnInferPrecisionCustom = 80,
    DnnInferPrecisionUnspecified = 255
};

enum DnnInferLayoutType {
    DnnInferLayoutAny = 0,
    DnnInferLayoutNCHW = 1,
    DnnInferLayoutNHWC = 2,
    DnnInferLayoutOIHW = 64,
    DnnInferLayoutC = 96,
    DnnInferLayoutCHW = 128,
    DnnInferLayoutHW = 192,
    DnnInferLayoutNC = 193,
    DnnInferLayoutCN = 194,
    DnnInferLayoutBlocked = 200
};

enum DnnInferMemoryType {
    DnnInferMemoryDefault = 0,
    DnnInferMemoryHost = 1,
    DnnInferMemoryGPU = 2,
    DnnInferMemoryMYRIAD = 3,
    DnnInferMemoryShared = 4
};

enum DnnInferImageFormatType {
    DnnInferImageFormatBGRPacked = 0,
    DnnInferImageFormatBGRPlanar,
    DnnInferImageFormatRGBPacked,
    DnnInferImageFormatRGBPlanar,
    DnnInferImageFormatGrayPlanar,
    DnnInferImageFormatGeneric1D,
    DnnInferImageFormatGeneric2D,
    DnnInferImageFormatUnknown = -1
};

enum DnnInferMode {
    DnnInferModeSync = 0,
    DnnInferModeAsync = 1
};

enum DnnInferDataType {
    DnnInferDataTypeNonImage = 0,
    DnnInferDataTypeImage = 1
};

enum DnnInferLogLevel {
    DnnInferLogLevelNone = 0x0,
    DnnInferLogLevelEngine = 0x1,
    DnnInferLogLevelLayer = 0x2
};

enum DnnInferInfoType {
    DnnInferInfoEngine = 0x0,
    DnnInferInfoPlugin = 0x1,
    DnnInferInfoNetwork = 0x2
};

struct DnnInferImageSize {
    uint32_t image_width;
    uint32_t image_height;

    DnnInferImageSize () {
        image_width = 0;
        image_height = 0;
    }
};

struct DnnInferenceEngineInfo {
    DnnInferInfoType type;
    int32_t major;
    int32_t minor;
    const char* desc;
    const char* name;

    DnnInferenceEngineInfo () {
        type = DnnInferInfoEngine;
        major = 0;
        minor = 0;
        desc = NULL;
        name = NULL;
    };
};

#define DNN_INFER_MAX_INPUT_OUTPUT 10
struct DnnInferInputOutputInfo {
    uint32_t width[DNN_INFER_MAX_INPUT_OUTPUT];
    uint32_t height[DNN_INFER_MAX_INPUT_OUTPUT];
    uint32_t channels[DNN_INFER_MAX_INPUT_OUTPUT];
    uint32_t object_size[DNN_INFER_MAX_INPUT_OUTPUT];
    DnnInferPrecisionType precision[DNN_INFER_MAX_INPUT_OUTPUT];
    DnnInferLayoutType layout[DNN_INFER_MAX_INPUT_OUTPUT];
    DnnInferDataType data_type[DNN_INFER_MAX_INPUT_OUTPUT];
    uint32_t batch_size;
    uint32_t numbers;
};

struct DnnInferData {
    void * buffer;
    uint32_t size;
    uint32_t width;
    uint32_t height;
    uint32_t width_stride;
    uint32_t height_stride;
    uint32_t channel_num;
    uint32_t batch_idx;
    DnnInferPrecisionType precision;
    DnnInferMemoryType mem_type;
    DnnInferImageFormatType image_format;
    DnnInferDataType data_type;

    DnnInferData () {
        buffer = NULL;
    };
};

struct DnnInferConfig {
    DnnInferTargetDeviceType target_id;
    DnnInferInputOutputInfo input_infos;
    DnnInferInputOutputInfo output_infos;

    char * plugin_path;
    char * cpu_ext_path;
    char * cldnn_ext_path;
    char * model_filename;
    char * output_layer_name;
    uint32_t  perf_counter;
    uint32_t infer_req_num;

    DnnInferConfig () {
        plugin_path = NULL;
        cpu_ext_path = NULL;
        cldnn_ext_path = NULL;
        model_filename = NULL;
        output_layer_name = NULL;
    };
};

class DnnInferenceEngine {
public:
    explicit DnnInferenceEngine (DnnInferConfig& config);
    virtual ~DnnInferenceEngine ();

    XCamReturn create_model (DnnInferConfig& config);
    XCamReturn load_model (DnnInferConfig& config);

    XCamReturn get_info (DnnInferenceEngineInfo& info, DnnInferInfoType type);

    XCamReturn set_batch_size (const size_t size);
    size_t get_batch_size ();

    bool ready_to_start ()  const {
        return _model_created && _model_loaded;
    };

    XCamReturn start (bool sync = true);

    size_t get_input_size ();
    size_t get_output_size ();

    XCamReturn set_input_presion (uint32_t idx, DnnInferPrecisionType precision);
    DnnInferPrecisionType get_input_presion (uint32_t idx);
    XCamReturn set_output_presion (uint32_t idx, DnnInferPrecisionType precision);
    DnnInferPrecisionType get_output_presion (uint32_t idx);

    XCamReturn set_input_layout (uint32_t idx, DnnInferLayoutType layout);
    XCamReturn set_output_layout (uint32_t idx, DnnInferLayoutType layout);

    uint32_t get_input_image_height () const {
        return _input_image_height;
    };
    uint32_t get_input_image_width () const {
        return _input_image_width;
    };

    virtual XCamReturn set_model_input_info (DnnInferInputOutputInfo& info) = 0;
    virtual XCamReturn get_model_input_info (DnnInferInputOutputInfo& info) = 0;

    virtual XCamReturn set_model_output_info (DnnInferInputOutputInfo& info) = 0;
    virtual XCamReturn get_model_output_info (DnnInferInputOutputInfo& info) = 0;

    virtual XCamReturn set_inference_data (std::vector<std::string> images) = 0;
    virtual void* get_inference_results (uint32_t idx, uint32_t& size) = 0;

    std::shared_ptr<uint8_t> read_inference_image (std::string image);
    void print_log (uint32_t flag);

protected:

    InferenceEngine::TargetDevice get_device_from_string (const std::string& device_name);
    InferenceEngine::TargetDevice get_device_from_id (DnnInferTargetDeviceType device);

    InferenceEngine::Layout estimate_layout_type (const int ch_num);
    InferenceEngine::Layout convert_layout_type (DnnInferLayoutType layout);
    DnnInferLayoutType convert_layout_type (InferenceEngine::Layout layout);

    InferenceEngine::Precision convert_precision_type (DnnInferPrecisionType precision);
    DnnInferPrecisionType convert_precision_type (InferenceEngine::Precision precision);

    std::string get_filename_prefix (const std::string &file_path);

    void print_performance_counts (const std::map<std::string, InferenceEngine::InferenceEngineProfileInfo>& performance_map);

    XCamReturn set_input_blob (uint32_t idx, DnnInferData& data);

private:
    template <typename T> XCamReturn copy_image_to_blob (const DnnInferData& data, InferenceEngine::Blob::Ptr& blob, int batch_index);
    template <typename T> XCamReturn copy_data_to_blob (const DnnInferData& data, InferenceEngine::Blob::Ptr& blob, int batch_index);

protected:

    bool _model_created;
    bool _model_loaded;

    uint32_t _input_image_width;
    uint32_t _input_image_height;

    InferenceEngine::InferencePlugin _plugin;
    InferenceEngine::CNNNetReader _network_reader;
    InferenceEngine::CNNNetwork _network;
    InferenceEngine::InferRequest _infer_request;
    std::vector<InferenceEngine::CNNLayerPtr> _layers;
};

}  // namespace XCam

#endif //_XCMA_DNN_INFERENCE_ENGINE_H_