diff mbox series

[FFmpeg-devel,V2,2/3] dnn/openvino: refine code for better model initialization

Message ID 20210115084318.5152-2-ting.fu@intel.com
State Superseded
Headers show
Series [FFmpeg-devel,V2,1/3] dnn/openvino: remove unnecessary code
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Fu, Ting Jan. 15, 2021, 8:43 a.m. UTC
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.

Signed-off-by: Ting Fu <ting.fu@intel.com>
---
 libavfilter/dnn/dnn_backend_openvino.c | 196 ++++++++++++++-----------
 1 file changed, 111 insertions(+), 85 deletions(-)

Comments

Guo, Yejun Jan. 18, 2021, 12:49 a.m. UTC | #1
> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of Ting Fu
> Sent: 2021年1月15日 16:43
> To: ffmpeg-devel@ffmpeg.org
> Subject: [FFmpeg-devel] [PATCH V2 2/3] 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.
> 
> Signed-off-by: Ting Fu <ting.fu@intel.com>
> ---
>  libavfilter/dnn/dnn_backend_openvino.c | 196 ++++++++++++++-----------
>  1 file changed, 111 insertions(+), 85 deletions(-)
> 
> -
> -        item->tasks = av_malloc_array(ctx->options.batch_size,
> sizeof(*item->tasks));
> -        if (!item->tasks) {
> -            av_freep(&item);
> -            goto err;
> -        }
> -        item->task_count = 0;

these code are missed in the new added function init_model_ov with rebase
Fu, Ting Jan. 18, 2021, 3:52 a.m. UTC | #2
> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of Guo,
> Yejun
> Sent: Monday, January 18, 2021 08:50 AM
> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
> Subject: Re: [FFmpeg-devel] [PATCH V2 2/3] dnn/openvino: refine code for
> better model initialization
> 
> 
> 
> > -----Original Message-----
> > From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of Ting
> > Fu
> > Sent: 2021年1月15日 16:43
> > To: ffmpeg-devel@ffmpeg.org
> > Subject: [FFmpeg-devel] [PATCH V2 2/3] 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.
> >
> > Signed-off-by: Ting Fu <ting.fu@intel.com>
> > ---
> >  libavfilter/dnn/dnn_backend_openvino.c | 196
> > ++++++++++++++-----------
> >  1 file changed, 111 insertions(+), 85 deletions(-)
> >
> > -
> > -        item->tasks = av_malloc_array(ctx->options.batch_size,
> > sizeof(*item->tasks));
> > -        if (!item->tasks) {
> > -            av_freep(&item);
> > -            goto err;
> > -        }
> > -        item->task_count = 0;
> 
> these code are missed in the new added function init_model_ov with rebase

Hi Yejun,
Thank you for your review.
These codes have been fixed in PATCH V3.

> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org
> with subject "unsubscribe".
diff mbox series

Patch

diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c
index 8476f4fb38..0b125eef65 100644
--- a/libavfilter/dnn/dnn_backend_openvino.c
+++ b/libavfilter/dnn/dnn_backend_openvino.c
@@ -248,6 +248,96 @@  static void infer_completion_callback(void *args)
     }
 }
 
+static DNNReturnType init_model_ov(OVModel *ov_model)
+{
+    OVContext *ctx = &ov_model->ctx;
+    IEStatusCode status;
+    ie_available_devices_t a_dev;
+    ie_config_t config = {NULL, NULL, NULL};
+    char *all_dev_names = NULL;
+
+    // batch size
+    if (ctx->options.batch_size <= 0) {
+        ctx->options.batch_size = 1;
+    }
+
+    if (ctx->options.batch_size > 1) {
+        input_shapes_t input_shapes;
+        status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
+        if (status != OK)
+            goto err;
+        for (int i = 0; i < input_shapes.shape_num; i++)
+            input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
+        status = ie_network_reshape(ov_model->network, input_shapes);
+        ie_network_input_shapes_free(&input_shapes);
+        if (status != OK)
+            goto err;
+    }
+
+    status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
+    if (status != OK) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to load OpenVINO model network\n");
+        status = ie_core_get_available_devices(ov_model->core, &a_dev);
+        if (status != OK) {
+            av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
+            goto err;
+        }
+        for (int i = 0; i < a_dev.num_devices; i++) {
+            APPEND_STRING(all_dev_names, a_dev.devices[i])
+        }
+        av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
+               ctx->options.device_type, all_dev_names);
+        goto err;
+    }
+
+    // create infer_request for sync execution
+    status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
+    if (status != OK)
+        goto err;
+
+    // create infer_requests for async execution
+    if (ctx->options.nireq <= 0) {
+        // the default value is a rough estimation
+        ctx->options.nireq = av_cpu_count() / 2 + 1;
+    }
+
+    ov_model->request_queue = ff_safe_queue_create();
+    if (!ov_model->request_queue) {
+        goto err;
+    }
+
+    for (int i = 0; i < ctx->options.nireq; i++) {
+        ie_infer_request_t *request;
+        RequestItem *item = av_mallocz(sizeof(*item));
+        if (!item) {
+            goto err;
+        }
+        status = ie_exec_network_create_infer_request(ov_model->exe_network, &request);
+        if (status != OK) {
+            av_freep(&item);
+            goto err;
+        }
+        item->infer_request = request;
+        item->callback.completeCallBackFunc = infer_completion_callback;
+        item->callback.args = item;
+        if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
+            av_freep(&item);
+            goto err;
+        }
+    }
+
+    ov_model->task_queue = ff_queue_create();
+    if (!ov_model->task_queue) {
+        goto err;
+    }
+
+    return DNN_SUCCESS;
+
+err:
+    ff_dnn_free_model_ov(&ov_model->model);
+    return DNN_ERROR;
+}
+
 static DNNReturnType execute_model_ov(RequestItem *request)
 {
     IEStatusCode status;
@@ -367,6 +457,13 @@  static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu
     in_frame->width = input_width;
     in_frame->height = input_height;
 
+    if (!ov_model->exe_network) {
+        if (init_model_ov(ov_model) != DNN_SUCCESS) {
+            av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
+            return DNN_ERROR;
+        };
+    }
+
     task.done = 0;
     task.do_ioproc = 0;
     task.async = 0;
@@ -391,13 +488,10 @@  static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu
 
 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, AVFilterContext *filter_ctx)
 {
-    char *all_dev_names = NULL;
     DNNModel *model = NULL;
     OVModel *ov_model = NULL;
     OVContext *ctx = NULL;
     IEStatusCode status;
-    ie_config_t config = {NULL, NULL, NULL};
-    ie_available_devices_t a_dev;
 
     model = av_mallocz(sizeof(DNNModel));
     if (!model){
@@ -429,88 +523,6 @@  DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options,
     if (status != OK)
         goto err;
 
-    // batch size
-    if (ctx->options.batch_size <= 0) {
-        ctx->options.batch_size = 1;
-    }
-
-    if (ctx->options.batch_size > 1) {
-        input_shapes_t input_shapes;
-        status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
-        if (status != OK)
-            goto err;
-        for (int i = 0; i < input_shapes.shape_num; i++)
-            input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
-        status = ie_network_reshape(ov_model->network, input_shapes);
-        ie_network_input_shapes_free(&input_shapes);
-        if (status != OK)
-            goto err;
-    }
-
-    status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
-    if (status != OK) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to init OpenVINO model\n");
-        status = ie_core_get_available_devices(ov_model->core, &a_dev);
-        if (status != OK) {
-            av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
-            goto err;
-        }
-        for (int i = 0; i < a_dev.num_devices; i++) {
-            APPEND_STRING(all_dev_names, a_dev.devices[i])
-        }
-        av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
-               ctx->options.device_type, all_dev_names);
-        goto err;
-    }
-
-    // create infer_request for sync execution
-    status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
-    if (status != OK)
-        goto err;
-
-    // create infer_requests for async execution
-    if (ctx->options.nireq <= 0) {
-        // the default value is a rough estimation
-        ctx->options.nireq = av_cpu_count() / 2 + 1;
-    }
-
-    ov_model->request_queue = ff_safe_queue_create();
-    if (!ov_model->request_queue) {
-        goto err;
-    }
-
-    for (int i = 0; i < ctx->options.nireq; i++) {
-        RequestItem *item = av_mallocz(sizeof(*item));
-        if (!item) {
-            goto err;
-        }
-
-        status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
-        if (status != OK) {
-            av_freep(&item);
-            goto err;
-        }
-
-        item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
-        if (!item->tasks) {
-            av_freep(&item);
-            goto err;
-        }
-        item->task_count = 0;
-
-        item->callback.completeCallBackFunc = infer_completion_callback;
-        item->callback.args = item;
-        if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
-            av_freep(&item);
-            goto err;
-        }
-    }
-
-    ov_model->task_queue = ff_queue_create();
-    if (!ov_model->task_queue) {
-        goto err;
-    }
-
     model->get_input = &get_input_ov;
     model->get_output = &get_output_ov;
     model->options = options;
@@ -554,6 +566,13 @@  DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n
         return DNN_ERROR;
     }
 
+    if (!ov_model->exe_network) {
+        if (init_model_ov(ov_model) != DNN_SUCCESS) {
+            av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
+            return DNN_ERROR;
+        };
+    }
+
     task.done = 0;
     task.do_ioproc = 1;
     task.async = 0;
@@ -594,6 +613,13 @@  DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *i
         return DNN_ERROR;
     }
 
+    if (!ov_model->exe_network) {
+        if (init_model_ov(ov_model) != DNN_SUCCESS) {
+            av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
+            return DNN_ERROR;
+        };
+    }
+
     task->done = 0;
     task->do_ioproc = 1;
     task->async = 1;