summaryrefslogtreecommitdiff
path: root/mv_inference/inference/include/Inference.h
blob: 6cb25f1040d27137c14a37c21c41150e27962e4a (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * 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.
 */

#ifndef __MEDIA_VISION_INFERENCE_H__
#define __MEDIA_VISION_INFERENCE_H__

#include <string>
#include <map>

#include "mv_common.h"
#include "inference_engine_error.h"
#include "inference_engine_common_impl.h"
#include <mv_inference_type.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>

/**
 * @file Inference.h
 * @brief This file contains the inference class definition which
 *        provides inference interface.
 */
using namespace InferenceEngineInterface::Common;

typedef struct _ImageClassficationResults {
    int number_of_classes;
    std::vector<int> indices;
    std::vector<std::string> names;
    std::vector<float> confidences;
} ImageClassificationResults; /**< structure ImageClassificationResults */

typedef struct _ObjectDetectionResults {
    int number_of_objects;
    std::vector<int> indices;
    std::vector<std::string> names;
    std::vector<float> confidences;
    std::vector<cv::Rect> locations;
} ObjectDetectionResults;  /**< structure ObjectDetectionResults */

typedef struct _FaceDetectionResults {
    int number_of_faces;
    std::vector<float> confidences;
    std::vector<cv::Rect> locations;
} FaceDetectionResults;  /**< structure FaceDetectionResults */

typedef struct _FacialLandMarkDetectionResults {
    int number_of_landmarks;
    std::vector<cv::Point> locations;
} FacialLandMarkDetectionResults;  /**< structure FacialLandMarkDetectionResults */

typedef struct _PoseEstimationResults {
    int number_of_pose_estimation;
    std::vector<cv::Point> locations;
} PoseEstimationResults;  /**< structure PoseEstimationResults */

namespace mediavision {
namespace inference {

struct TensorInfo {
    int width;
    int height;
    int dim;
    int ch;
};

struct InferenceConfig {
	/**
	 * @brief Default constructor for the @ref InferenceConfig
	 *
	 * @since_tizen 5.0
	 */
	InferenceConfig();

	std::string mConfigFilePath; /**< Path of a model configuration file */

	std::string mWeightFilePath; /**< Path of a model weight file */

	std::string mUserFilePath; /**< Path of model user file */

	TensorInfo mTensorInfo; /**< Tensor information */

	mv_inference_backend_type_e mBackedType; /**< Backed type of model files */

	int mTargetTypes; /**< Target type to run inference */

	double mConfidenceThresHold; /**< Confidence threshold value */

	double mMeanValue; /**< The mean value for normalization */

	double mStdValue; /**< The scale factor value for normalization */

	int mMaxOutputNumbers;

	std::vector<std::string> mInputLayerNames; /**< The input layer names */
	std::vector<std::string> mOutputLayerNames; /**< The output layer names */
};


class Inference {
public:
	/**
	 * @brief   Creates an Inference class instance.
	 *
	 * @since_tizen 5.5
	 */
	Inference();

	/**
	 * @brief   Destroys an Inference class instance including
	 *           its all resources.
	 *
	 * @since_tizen 5.5
	 */
	~Inference();

	/**
	 * @brief   Configure modelfiles
	 *
	 * @since_tizen 5.5
	 */
    void ConfigureModelFiles(
                const std::string modelConfigFilePath,
		        const std::string modelWeightFilePath,
		        const std::string modelUserFilePath);

	/**
	 * @brief   Configure input tensor information
	 *
	 * @since_tizen 5.5
	 * @remarks deprecated Replayced by ConfigureInputInfo
	 */
    void ConfigureTensorInfo(int width,
                int height,
                int dim,
                int ch,
				double stdValue,
				double meanValue);

	/**
	 * @brief Configure input infomation
	 *
	 * @since_tizen 6.0
	 */
	void ConfigureInputInfo(int width,
		int height,
		int dim,
		int ch,
		double stdValue,
		double meanValue,
		const std::vector<std::string> names);

	void ConfigureOutputInfo(std::vector<std::string> names);

	/**
	 * @brief   Configure inference backend type.
	 *
	 * @since_tizen 6.0
	 */
	int ConfigureBackendType(const mv_inference_backend_type_e backendType);

	/**
	 * @brief   Configure inference target types such as CPU, GPU or NPU. (one more types can be combined)
	 *
	 * @since_tizen 6.0
	 */
	int ConfigureTargetTypes(const int targetTypes);

	/**
	 * @brief   Configure the maximum number of inference results
	 *
	 * @since_tizen 5.5
	 */
	void ConfigureOutput(const int maxOutputNumbers);

	/**
	 * @brief   Configure the confidence threshold
	 *
	 * @since_tizen 5.5
	 */
	void ConfigureThreshold(const double threshold);

	/**
	 * @brief   Configure the input node name
	 *
	 * @since_tizen 5.5
	 * @remarks deprecated Replayced by ConfigureInputInfo
	 */
	void ConfigureInputNodeName(const std::string nodeName);

	/**
	 * @brief   Configure the output node names
	 *
	 * @since_tizen 5.5
	 * @remarks deprecated Replaced by ConfigureOutputInfo
	 */
	void ConfigureOutputNodeNames(const std::vector<std::string> nodeNames);

	/**
	 * @brief   Bind a backend engine
	 * @details Use this function to bind a backend engine for the inference.
	 * 			This creates a inference engine common class object, and loads a backend
	 *			library which inferfaces with a Neural Network runtime such as TF Lite,
	 *			OpenCV, ARMNN and so on.
	 *
	 *			Ps. The created inference engine common object will be released and its
	 *				corresponding backend library will be unbound when deconstructor
	 *				of Inference class will be called.
	 *
	 * @since_tizen 6.0
	 *
	 * @return @c 0 on success, otherwise a negative error value
	 * @retval #MEDIA_VISION_ERROR_NONE Successful
	 * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
	 */
	int Bind();

	/**
	 * @brief   Set default configuration for the inference
	 * @details Use this function to set default configuration given in json file by user.
	 *
	 * 			Ps. this callback should be called after Bind callback.
	 *
	 * @since_tizen 6.0
	 *
	 * @return @c 0 on success, otherwise a negative error value
	 * @retval #MEDIA_VISION_ERROR_NONE Successful
	 * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
	 */
	int Prepare();

	/**
	 * @brief   Load model files
	 * @details Use this function to load given model files for the inference.
	 *
	 * 			Ps. this callback should be called after Prepare callback.
	 *
	 * @since_tizen 6.0
	 *
	 * @return @c 0 on success, otherwise a negative error value
	 * @retval #MEDIA_VISION_ERROR_NONE Successful
	 * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
	 */
	int Load();

	/**
	 * @brief	Runs inference with a region of a given image
	 * @details Use this function to run forward pass with the given image.
	 *          The given image is preprocessed and the region of the image is
	 *          thrown to neural network. Then, the output tensor is returned.
	 *          If roi is NULL, then full source will be analyzed.
	 *
	 * @since_tizen 5.5
	 * @return @c true on success, otherwise a negative error value
	 */
	int Run(std::vector<mv_source_h> &mvSources, std::vector<mv_rectangle_s> &rects);

	/**
	 * @brief	Gets that given engine is supported or not
	 *
	 * @since_tizen 5.5
	 * @return @c true on success, otherwise a negative error value
	 */
	std::pair<std::string, bool> GetSupportedInferenceBackend(int backend);

	/**
	 * @brief	Gets the ImageClassificationResults
	 *
	 * @since_tizen 5.5
	 * @return @c true on success, otherwise a negative error value
	 */
	int GetClassficationResults(ImageClassificationResults *classificationResults);

	/**
	 * @brief	Gets the ObjectDetectioResults
	 *
	 * @since_tizen 5.5
	 * @return @c true on success, otherwise a negative error value
	 */
	int GetObjectDetectionResults(ObjectDetectionResults *detectionResults);

	/**
	 * @brief	Gets the FaceDetectioResults
	 *
	 * @since_tizen 5.5
	 * @return @c true on success, otherwise a negative error value
	 */
	int GetFaceDetectionResults(FaceDetectionResults *detectionResults);

	/**
	 * @brief	Gets the FacialLandmarkDetectionResults
	 *
	 * @since_tizen 5.5
	 * @return @c true on success, otherwise a negative error value
	 */
	int GetFacialLandMarkDetectionResults(FacialLandMarkDetectionResults* results);

	/**
	 * @brief	Gets the PoseEstimationDetectionResults
	 *
	 * @since_tizen 6.0
	 * @return @c true on success, otherwise a negative error value
	 */
	int GetPoseEstimationDetectionResults(PoseEstimationResults* results);

	int GetResults(std::vector<std::vector<int>>* dimInfo, std::vector<float*> *results);

	mv_engine_config_h GetEngineConfig(void) { return engine_config; }

	void SetEngineConfig(mv_engine_config_h config) { engine_config = config; }

private:
	bool mCanRun; /**< The flag indicating ready to run Inference */
    InferenceConfig mConfig;
	inference_engine_capacity mBackendCapacity;
	std::map<int, std::pair<std::string, bool>> mSupportedInferenceBackend;
	cv::Size mInputSize;
	int mCh;
	int mDim;
	double mDeviation;
	double mMean;
	double mThreshold;
	int mOutputNumbers;
	cv::Size mSourceSize;
	cv::Mat mInputBuffer;

	mv_engine_config_h engine_config;

	InferenceEngineCommon * mBackend;

	std::map<std::string, int> mModelFormats;
    std::vector<std::string> mUserListName;

	std::vector<inference_engine_tensor_buffer> mInputTensorBuffers;
	inference_engine_layer_property mInputLayerProperty;
	std::vector<inference_engine_tensor_buffer> mOutputTensorBuffers;
	inference_engine_layer_property mOutputLayerProperty;

private:
	void CheckSupportedInferenceBackend();
	int ConvertEngineErrorToVisionError(int error);
	int ConvertTargetTypes(int given_types);
	int ConvertDataTypes(int given_type);
	int Preprocess(cv::Mat cvImg, cv::Mat cvDst, int data_type);
	int PrepareTenosrBuffers(void);
	void CleanupTensorBuffers(void);
	int SetUserFile(std::string filename);
	int FillOutputResult(tensor_t &outputData);
};

} /* Inference */
} /* MediaVision */

#endif /* __MEDIA_VISION_INFERENCE_H__ */