summaryrefslogtreecommitdiff
path: root/mv_machine_learning/object_detection/src/object_detection.cpp
blob: 14681fd84a111ab4da3ac1ccf4ce319abda8688f (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/**
 * Copyright (c) 2022 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.
 */

#include <string.h>
#include <fstream>
#include <map>
#include <memory>
#include <algorithm>

#include "machine_learning_exception.h"
#include "mv_machine_learning_common.h"
#include "mv_object_detection_config.h"
#include "object_detection.h"

using namespace std;
using namespace mediavision::inference;
using namespace MediaVision::Common;
using namespace mediavision::common;
using namespace mediavision::machine_learning::exception;

namespace mediavision
{
namespace machine_learning
{
ObjectDetection::ObjectDetection(ObjectDetectionTaskType task_type)
		: _task_type(task_type), _backendType(), _targetDeviceType()
{
	_inference = make_unique<Inference>();
	_parser = make_unique<ObjectDetectionParser>();
}

void ObjectDetection::preDestroy()
{
	if (_thread_handle) {
		_exit_thread = true;
		_thread_handle->join();
	}
}

bool ObjectDetection::exitThread()
{
	return _exit_thread;
}

ObjectDetectionTaskType ObjectDetection::getTaskType()
{
	return _task_type;
}

void ObjectDetection::getEngineList()
{
	for (auto idx = MV_INFERENCE_BACKEND_NONE + 1; idx < MV_INFERENCE_BACKEND_MAX; ++idx) {
		auto backend = _inference->getSupportedInferenceBackend(idx);
		// TODO. we need to describe what inference engines are supported by each Task API,
		//       and based on it, below inference engine types should be checked
		//       if a given type is supported by this Task API later. As of now, tflite only.
		if (backend.second == true && backend.first.compare("tflite") == 0)
			_valid_backends.push_back(backend.first);
	}
}

void ObjectDetection::getDeviceList(const char *engine_type)
{
	// TODO. add device types available for a given engine type later.
	//       In default, cpu and gpu only.
	_valid_devices.push_back("cpu");
	_valid_devices.push_back("gpu");
}

void ObjectDetection::setEngineInfo(std::string engine_type, std::string device_type)
{
	if (engine_type.empty() || device_type.empty())
		throw InvalidParameter("Invalid engine info.");

	transform(engine_type.begin(), engine_type.end(), engine_type.begin(), ::toupper);
	transform(device_type.begin(), device_type.end(), device_type.begin(), ::toupper);

	_backendType = GetBackendType(engine_type);
	_targetDeviceType = GetDeviceType(device_type);

	LOGI("Engine type : %s => %d, Device type : %s => %d", engine_type.c_str(), GetBackendType(engine_type),
		 device_type.c_str(), GetDeviceType(device_type));

	if (_backendType == MEDIA_VISION_ERROR_INVALID_PARAMETER ||
		_targetDeviceType == MEDIA_VISION_ERROR_INVALID_PARAMETER)
		throw InvalidParameter("backend or target device type not found.");
}

void ObjectDetection::getNumberOfEngines(unsigned int *number_of_engines)
{
	if (!_valid_backends.empty()) {
		*number_of_engines = _valid_backends.size();
		return;
	}

	getEngineList();
	*number_of_engines = _valid_backends.size();
}

void ObjectDetection::getEngineType(unsigned int engine_index, char **engine_type)
{
	if (!_valid_backends.empty()) {
		if (_valid_backends.size() <= engine_index)
			throw InvalidParameter("Invalid engine index.");

		*engine_type = const_cast<char *>(_valid_backends[engine_index].data());
		return;
	}

	getEngineList();

	if (_valid_backends.size() <= engine_index)
		throw InvalidParameter("Invalid engine index.");

	*engine_type = const_cast<char *>(_valid_backends[engine_index].data());
}

void ObjectDetection::getNumberOfDevices(const char *engine_type, unsigned int *number_of_devices)
{
	if (!_valid_devices.empty()) {
		*number_of_devices = _valid_devices.size();
		return;
	}

	getDeviceList(engine_type);
	*number_of_devices = _valid_devices.size();
}

void ObjectDetection::getDeviceType(const char *engine_type, const unsigned int device_index, char **device_type)
{
	if (!_valid_devices.empty()) {
		if (_valid_devices.size() <= device_index)
			throw InvalidParameter("Invalid device index.");

		*device_type = const_cast<char *>(_valid_devices[device_index].data());
		return;
	}

	getDeviceList(engine_type);

	if (_valid_devices.size() <= device_index)
		throw InvalidParameter("Invalid device index.");

	*device_type = const_cast<char *>(_valid_devices[device_index].data());
}

void ObjectDetection::setUserModel(string model_file, string meta_file, string label_file)
{
	_modelFilePath = model_file;
	_modelMetaFilePath = meta_file;
	_modelLabelFilePath = label_file;
}

static bool IsJsonFile(const string &fileName)
{
	return (!fileName.substr(fileName.find_last_of(".") + 1).compare("json"));
}

void ObjectDetection::loadLabel()
{
	ifstream readFile;

	_labels.clear();
	readFile.open(_modelLabelFilePath.c_str());

	if (readFile.fail())
		throw InvalidOperation("Fail to open " + _modelLabelFilePath + " file.");

	string line;

	while (getline(readFile, line))
		_labels.push_back(line);

	readFile.close();
}

void ObjectDetection::parseMetaFile(string meta_file_name)
{
	_config = make_unique<EngineConfig>(string(MV_CONFIG_PATH) + meta_file_name);

	int ret = _config->getIntegerAttribute(string(MV_OBJECT_DETECTION_BACKEND_TYPE), &_backendType);
	if (ret != MEDIA_VISION_ERROR_NONE)
		throw InvalidOperation("Fail to get backend engine type.");

	ret = _config->getIntegerAttribute(string(MV_OBJECT_DETECTION_TARGET_DEVICE_TYPE), &_targetDeviceType);
	if (ret != MEDIA_VISION_ERROR_NONE)
		throw InvalidOperation("Fail to get target device type.");

	ret = _config->getStringAttribute(MV_OBJECT_DETECTION_MODEL_DEFAULT_PATH, &_modelDefaultPath);
	if (ret != MEDIA_VISION_ERROR_NONE)
		throw InvalidOperation("Fail to get model default path");

	if (_modelFilePath.empty()) {
		ret = _config->getStringAttribute(MV_OBJECT_DETECTION_MODEL_FILE_PATH, &_modelFilePath);
		if (ret != MEDIA_VISION_ERROR_NONE)
			throw InvalidOperation("Fail to get model file path");
	}

	_modelFilePath = _modelDefaultPath + _modelFilePath;
	LOGI("model file path = %s", _modelFilePath.c_str());

	if (_modelMetaFilePath.empty()) {
		ret = _config->getStringAttribute(MV_OBJECT_DETECTION_MODEL_META_FILE_PATH, &_modelMetaFilePath);
		if (ret != MEDIA_VISION_ERROR_NONE)
			throw InvalidOperation("Fail to get model meta file path");

		if (_modelMetaFilePath.empty())
			throw InvalidOperation("Model meta file doesn't exist.");

		if (!IsJsonFile(_modelMetaFilePath))
			throw InvalidOperation("Model meta file should be json");
	}

	_modelMetaFilePath = _modelDefaultPath + _modelMetaFilePath;
	LOGI("meta file path = %s", _modelMetaFilePath.c_str());

	_parser->setTaskType(static_cast<int>(_task_type));
	_parser->load(_modelMetaFilePath);

	if (_modelLabelFilePath.empty()) {
		ret = _config->getStringAttribute(MV_OBJECT_DETECTION_LABEL_FILE_NAME, &_modelLabelFilePath);
		if (ret != MEDIA_VISION_ERROR_NONE)
			throw InvalidOperation("Fail to get label file path");

		if (_modelLabelFilePath.empty())
			throw InvalidOperation("Model label file doesn't exist.");
	}

	_modelLabelFilePath = _modelDefaultPath + _modelLabelFilePath;
	LOGI("label file path = %s", _modelLabelFilePath.c_str());

	loadLabel();
}

void ObjectDetection::configure(string configFile)
{
	parseMetaFile(configFile);

	int ret = _inference->bind(_backendType, _targetDeviceType);
	if (ret != MEDIA_VISION_ERROR_NONE)
		throw InvalidOperation("Fail to bind a backend engine.");
}

void ObjectDetection::prepare()
{
	int ret = _inference->configureInputMetaInfo(_parser->getInputMetaMap());
	if (ret != MEDIA_VISION_ERROR_NONE)
		throw InvalidOperation("Fail to configure input tensor info from meta file.");

	ret = _inference->configureOutputMetaInfo(_parser->getOutputMetaMap());
	if (ret != MEDIA_VISION_ERROR_NONE)
		throw InvalidOperation("Fail to configure output tensor info from meta file.");

	_inference->configureModelFiles("", _modelFilePath, "");

	// Request to load model files to a backend engine.
	ret = _inference->load();
	if (ret != MEDIA_VISION_ERROR_NONE)
		throw InvalidOperation("Fail to load model files.");
}

shared_ptr<MetaInfo> ObjectDetection::getInputMetaInfo()
{
	TensorBuffer &tensor_buffer = _inference->getInputTensorBuffer();
	IETensorBuffer &tensor_info_map = tensor_buffer.getIETensorBuffer();

	// TODO. consider using multiple tensors later.
	if (tensor_info_map.size() != 1)
		throw InvalidOperation("Input tensor count not invalid.");

	auto tensor_buffer_iter = tensor_info_map.begin();

	// Get the meta information corresponding to a given input tensor name.
	return _parser->getInputMetaMap()[tensor_buffer_iter->first];
}

template<typename T>
void ObjectDetection::preprocess(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo, vector<T> &inputVector)
{
	LOGI("ENTER");

	PreprocessConfig config = { false,
								metaInfo->colorSpace,
								metaInfo->dataType,
								metaInfo->getChannel(),
								metaInfo->getWidth(),
								metaInfo->getHeight() };

	auto normalization = static_pointer_cast<DecodingNormal>(metaInfo->decodingTypeMap.at(DecodingType::NORMAL));
	if (normalization) {
		config.normalize = normalization->use;
		config.mean = normalization->mean;
		config.std = normalization->std;
	}

	auto quantization =
			static_pointer_cast<DecodingQuantization>(metaInfo->decodingTypeMap.at(DecodingType::QUANTIZATION));
	if (quantization) {
		config.quantize = quantization->use;
		config.scale = quantization->scale;
		config.zeropoint = quantization->zeropoint;
	}

	_preprocess.setConfig(config);
	_preprocess.run<T>(mv_src, inputVector);

	LOGI("LEAVE");
}

template<typename T> void ObjectDetection::inference(vector<vector<T> > &inputVectors)
{
	LOGI("ENTER");

	int ret = _inference->run<T>(inputVectors);
	if (ret != MEDIA_VISION_ERROR_NONE)
		throw InvalidOperation("Fail to run inference");

	LOGI("LEAVE");
}

template<typename T> void ObjectDetection::perform(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo)
{
	vector<T> inputVector;

	preprocess<T>(mv_src, metaInfo, inputVector);

	vector<vector<T> > inputVectors = { inputVector };

	inference<T>(inputVectors);
}

void ObjectDetection::perform(mv_source_h &mv_src)
{
	shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
	if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8)
		perform<unsigned char>(mv_src, metaInfo);
	else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32)
		perform<float>(mv_src, metaInfo);
	else
		throw InvalidOperation("Invalid model data type.");
}

template<typename T> void inferenceThreadLoop(ObjectDetection *object)
{
	// If user called destroy API then this thread loop will be terminated.
	while (!object->exitThread() || !object->isInputQueueEmpty<T>()) {
		// If input queue is empty then skip inference request.
		if (object->isInputQueueEmpty<T>())
			continue;

		ObjectDetectionQueue<T> input = object->popFromInput<T>();

		LOGD("Popped : input index = %lu", input.index);

		object->inference<T>(input.inputs);

		ObjectDetectionResult &result = object->result();

		object->pushToOutput(result);

		input.completion_cb(input.handle, input.user_data);
	}
}

template<typename T> void ObjectDetection::performAsync(ObjectDetectionInput &input, shared_ptr<MetaInfo> metaInfo)
{
	_input_index++;

	if (!isInputQueueEmpty<T>())
		return;

	vector<T> inputVector;

	preprocess<T>(input.inference_src, metaInfo, inputVector);

	vector<vector<T> > inputVectors = { inputVector };
	ObjectDetectionQueue<T> in_queue = { _input_index,		  input.handle, input.inference_src,
										 input.completion_cb, inputVectors, input.user_data };

	pushToInput<T>(in_queue);
	LOGD("Pushed : input index = %lu", in_queue.index);

	if (!_thread_handle)
		_thread_handle = make_unique<thread>(&inferenceThreadLoop<T>, this);
}

void ObjectDetection::performAsync(ObjectDetectionInput &input)
{
	shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();

	if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8) {
		performAsync<unsigned char>(input, metaInfo);
	} else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32) {
		performAsync<float>(input, metaInfo);
		// TODO
	} else {
		throw InvalidOperation("Invalid model data type.");
	}
}

ObjectDetectionResult &ObjectDetection::getOutput()
{
	if (_thread_handle) {
		if (isOutputQueueEmpty())
			throw InvalidOperation("No inference result.");

		_current_result = popFromOutput();
	} else {
		_current_result = result();
	}

	return _current_result;
}

ObjectDetectionResult &ObjectDetection::getOutputCache()
{
	return _current_result;
}

void ObjectDetection::getOutputNames(vector<string> &names)
{
	TensorBuffer &tensor_buffer_obj = _inference->getOutputTensorBuffer();
	IETensorBuffer &ie_tensor_buffer = tensor_buffer_obj.getIETensorBuffer();

	for (IETensorBuffer::iterator it = ie_tensor_buffer.begin(); it != ie_tensor_buffer.end(); it++)
		names.push_back(it->first);
}

void ObjectDetection::getOutputTensor(string target_name, vector<float> &tensor)
{
	TensorBuffer &tensor_buffer_obj = _inference->getOutputTensorBuffer();

	inference_engine_tensor_buffer *tensor_buffer = tensor_buffer_obj.getTensorBuffer(target_name);
	if (!tensor_buffer)
		throw InvalidOperation("Fail to get tensor buffer.");

	auto raw_buffer = static_cast<float *>(tensor_buffer->buffer);

	copy(&raw_buffer[0], &raw_buffer[tensor_buffer->size / sizeof(float)], back_inserter(tensor));
}

template<typename T> void ObjectDetection::pushToInput(ObjectDetectionQueue<T> &input)
{
	lock_guard<mutex> lock(_incoming_queue_mutex);
	_incoming_queue<T>.push(input);
}

template<typename T> ObjectDetectionQueue<T> ObjectDetection::popFromInput()
{
	lock_guard<mutex> lock(_incoming_queue_mutex);
	ObjectDetectionQueue<T> input = _incoming_queue<T>.front();
	_incoming_queue<T>.pop();

	return input;
}

template<typename T> bool ObjectDetection::isInputQueueEmpty()
{
	lock_guard<mutex> lock(_incoming_queue_mutex);
	return _incoming_queue<T>.empty();
}

void ObjectDetection::pushToOutput(ObjectDetectionResult &output)
{
	lock_guard<mutex> lock(_outgoing_queue_mutex);
	_outgoing_queue.push(output);
}

ObjectDetectionResult ObjectDetection::popFromOutput()
{
	lock_guard<mutex> lock(_outgoing_queue_mutex);
	ObjectDetectionResult output = _outgoing_queue.front();

	_outgoing_queue.pop();

	return output;
}

bool ObjectDetection::isOutputQueueEmpty()
{
	lock_guard<mutex> lock(_outgoing_queue_mutex);
	return _outgoing_queue.empty();
}

template<typename T> queue<ObjectDetectionQueue<T> > ObjectDetection::_incoming_queue;

template void ObjectDetection::preprocess<float>(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo,
												 vector<float> &inputVector);
template void ObjectDetection::inference<float>(vector<vector<float> > &inputVectors);
template void ObjectDetection::perform<float>(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo);
template void ObjectDetection::pushToInput<float>(ObjectDetectionQueue<float> &input);
template ObjectDetectionQueue<float> ObjectDetection::popFromInput();
template bool ObjectDetection::isInputQueueEmpty<float>();
template void ObjectDetection::performAsync<float>(ObjectDetectionInput &input, shared_ptr<MetaInfo> metaInfo);

template void ObjectDetection::preprocess<unsigned char>(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo,
														 vector<unsigned char> &inputVector);
template void ObjectDetection::inference<unsigned char>(vector<vector<unsigned char> > &inputVectors);
template void ObjectDetection::perform<unsigned char>(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo);
template void ObjectDetection::pushToInput<unsigned char>(ObjectDetectionQueue<unsigned char> &input);
template ObjectDetectionQueue<unsigned char> ObjectDetection::popFromInput();
template bool ObjectDetection::isInputQueueEmpty<unsigned char>();

template void ObjectDetection::performAsync<unsigned char>(ObjectDetectionInput &input, shared_ptr<MetaInfo> metaInfo);

}
}