History

dnn/openvino: refine code for better model initialization

Move openvino model/inference request creation and initialization steps from ff_dnn_load_model_ov to new function init_model_ov, for later input resize support.

Ting Fu committed 5 years ago

Commit 048d5cc1 parent 946fcd41 file · +118 −85Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/dnn/dnn_backend_openvino.c+118−85
@@ -248,6 +248,103 @@ static void infer_completion_callback(void *args) · 247 unmodified lines
248248 }
249249 }
250250
251+static DNNReturnType init_model_ov(OVModel *ov_model)
252+{
253+ OVContext *ctx = &ov_model->ctx;
254+ IEStatusCode status;
255+ ie_available_devices_t a_dev;
256+ ie_config_t config = {NULL, NULL, NULL};
257+ char *all_dev_names = NULL;
258+
259+ // batch size
260+ if (ctx->options.batch_size <= 0) {
261+ ctx->options.batch_size = 1;
262+ }
263+
264+ if (ctx->options.batch_size > 1) {
265+ input_shapes_t input_shapes;
266+ status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
267+ if (status != OK)
268+ goto err;
269+ for (int i = 0; i < input_shapes.shape_num; i++)
270+ input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
271+ status = ie_network_reshape(ov_model->network, input_shapes);
272+ ie_network_input_shapes_free(&input_shapes);
273+ if (status != OK)
274+ goto err;
275+ }
276+
277+ status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
278+ if (status != OK) {
279+ av_log(ctx, AV_LOG_ERROR, "Failed to load OpenVINO model network\n");
280+ status = ie_core_get_available_devices(ov_model->core, &a_dev);
281+ if (status != OK) {
282+ av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
283+ goto err;
284+ }
285+ for (int i = 0; i < a_dev.num_devices; i++) {
286+ APPEND_STRING(all_dev_names, a_dev.devices[i])
287+ }
288+ av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
289+ ctx->options.device_type, all_dev_names);
290+ goto err;
291+ }
292+
293+ // create infer_request for sync execution
294+ status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
295+ if (status != OK)
296+ goto err;
297+
298+ // create infer_requests for async execution
299+ if (ctx->options.nireq <= 0) {
300+ // the default value is a rough estimation
301+ ctx->options.nireq = av_cpu_count() / 2 + 1;
302+ }
303+
304+ ov_model->request_queue = ff_safe_queue_create();
305+ if (!ov_model->request_queue) {
306+ goto err;
307+ }
308+
309+ for (int i = 0; i < ctx->options.nireq; i++) {
310+ RequestItem *item = av_mallocz(sizeof(*item));
311+ if (!item) {
312+ goto err;
313+ }
314+
315+ status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
316+ if (status != OK) {
317+ av_freep(&item);
318+ goto err;
319+ }
320+
321+ item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
322+ if (!item->tasks) {
323+ av_freep(&item);
324+ goto err;
325+ }
326+ item->task_count = 0;
327+
328+ item->callback.completeCallBackFunc = infer_completion_callback;
329+ item->callback.args = item;
330+ if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
331+ av_freep(&item);
332+ goto err;
333+ }
334+ }
335+
336+ ov_model->task_queue = ff_queue_create();
337+ if (!ov_model->task_queue) {
338+ goto err;
339+ }
340+
341+ return DNN_SUCCESS;
342+
343+err:
344+ ff_dnn_free_model_ov(&ov_model->model);
345+ return DNN_ERROR;
346+}
347+
251348 static DNNReturnType execute_model_ov(RequestItem *request)
252349 {
253350 IEStatusCode status;
@@ -367,6 +464,13 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu · 113 unmodified lines
367464 in_frame->width = input_width;
368465 in_frame->height = input_height;
369466
467+ if (!ov_model->exe_network) {
468+ if (init_model_ov(ov_model) != DNN_SUCCESS) {
469+ av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
470+ return DNN_ERROR;
471+ };
472+ }
473+
370474 task.done = 0;
371475 task.do_ioproc = 0;
372476 task.async = 0;
@@ -391,13 +495,10 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu · 18 unmodified lines
391495
392496 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, AVFilterContext *filter_ctx)
393497 {
394− char *all_dev_names = NULL;
395498 DNNModel *model = NULL;
396499 OVModel *ov_model = NULL;
397500 OVContext *ctx = NULL;
398501 IEStatusCode status;
399− ie_config_t config = {NULL, NULL, NULL};
400− ie_available_devices_t a_dev;
401502
402503 model = av_mallocz(sizeof(DNNModel));
403504 if (!model){
@@ -429,88 +530,6 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, · 25 unmodified lines
429530 if (status != OK)
430531 goto err;
431532
432− // batch size
433− if (ctx->options.batch_size <= 0) {
434− ctx->options.batch_size = 1;
435− }
436−
437− if (ctx->options.batch_size > 1) {
438− input_shapes_t input_shapes;
439− status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
440− if (status != OK)
441− goto err;
442− for (int i = 0; i < input_shapes.shape_num; i++)
443− input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
444− status = ie_network_reshape(ov_model->network, input_shapes);
445− ie_network_input_shapes_free(&input_shapes);
446− if (status != OK)
447− goto err;
448− }
449−
450− status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
451− if (status != OK) {
452− av_log(ctx, AV_LOG_ERROR, "Failed to init OpenVINO model\n");
453− status = ie_core_get_available_devices(ov_model->core, &a_dev);
454− if (status != OK) {
455− av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
456− goto err;
457− }
458− for (int i = 0; i < a_dev.num_devices; i++) {
459− APPEND_STRING(all_dev_names, a_dev.devices[i])
460− }
461− av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
462− ctx->options.device_type, all_dev_names);
463− goto err;
464− }
465−
466− // create infer_request for sync execution
467− status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
468− if (status != OK)
469− goto err;
470−
471− // create infer_requests for async execution
472− if (ctx->options.nireq <= 0) {
473− // the default value is a rough estimation
474− ctx->options.nireq = av_cpu_count() / 2 + 1;
475− }
476−
477− ov_model->request_queue = ff_safe_queue_create();
478− if (!ov_model->request_queue) {
479− goto err;
480− }
481−
482− for (int i = 0; i < ctx->options.nireq; i++) {
483− RequestItem *item = av_mallocz(sizeof(*item));
484− if (!item) {
485− goto err;
486− }
487−
488− status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
489− if (status != OK) {
490− av_freep(&item);
491− goto err;
492− }
493−
494− item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
495− if (!item->tasks) {
496− av_freep(&item);
497− goto err;
498− }
499− item->task_count = 0;
500−
501− item->callback.completeCallBackFunc = infer_completion_callback;
502− item->callback.args = item;
503− if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
504− av_freep(&item);
505− goto err;
506− }
507− }
508−
509− ov_model->task_queue = ff_queue_create();
510− if (!ov_model->task_queue) {
511− goto err;
512− }
513−
514533 model->get_input = &get_input_ov;
515534 model->get_output = &get_output_ov;
516535 model->options = options;
@@ -554,6 +573,13 @@ DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n · 37 unmodified lines
554573 return DNN_ERROR;
555574 }
556575
576+ if (!ov_model->exe_network) {
577+ if (init_model_ov(ov_model) != DNN_SUCCESS) {
578+ av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
579+ return DNN_ERROR;
580+ };
581+ }
582+
557583 task.done = 0;
558584 task.do_ioproc = 1;
559585 task.async = 0;
@@ -594,6 +620,13 @@ DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *i · 34 unmodified lines
594620 return DNN_ERROR;
595621 }
596622
623+ if (!ov_model->exe_network) {
624+ if (init_model_ov(ov_model) != DNN_SUCCESS) {
625+ av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
626+ return DNN_ERROR;
627+ };
628+ }
629+
597630 task->done = 0;
598631 task->do_ioproc = 1;
599632 task->async = 1;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t