From patchwork Mon Jan 11 02:20:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Ting" X-Patchwork-Id: 24891 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 1273344BA65 for ; Mon, 11 Jan 2021 04:28:41 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DA7D968AB3A; Mon, 11 Jan 2021 04:28:40 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1B472689AD1 for ; Mon, 11 Jan 2021 04:28:33 +0200 (EET) IronPort-SDR: U99CcRw5rbk4beeauxGV+DCFdIwH/MtV4PjFiQ49iNfNdwiIFUjc7T5REyPlfW2S4QxOgrpbeP ZKL4qPIayMJQ== X-IronPort-AV: E=McAfee;i="6000,8403,9860"; a="239337962" X-IronPort-AV: E=Sophos;i="5.79,337,1602572400"; d="scan'208";a="239337962" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2021 18:28:31 -0800 IronPort-SDR: bn1FbYkqlYdL1JGX8Tkn1DlrcpcCp5Gc8Js81NZkY+WkkSJruavPt4wI76avhdDQVS0SBRXTg+ Jd3DPoz+3LWA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.79,337,1602572400"; d="scan'208";a="380848662" Received: from semmer-ubuntu.sh.intel.com ([10.239.159.136]) by orsmga008.jf.intel.com with ESMTP; 10 Jan 2021 18:28:30 -0800 From: Ting Fu To: ffmpeg-devel@ffmpeg.org Date: Mon, 11 Jan 2021 10:20:30 +0800 Message-Id: <20210111022032.22334-1-ting.fu@intel.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/3] dnn/openvino: remove unnecessary code X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Ting Fu --- libavfilter/dnn/dnn_backend_openvino.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c index d27e451eea..050be97209 100644 --- a/libavfilter/dnn/dnn_backend_openvino.c +++ b/libavfilter/dnn/dnn_backend_openvino.c @@ -284,14 +284,6 @@ static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input return DNN_ERROR; } - // The order of dims in the openvino is fixed and it is always NCHW for 4-D data. - // while we pass NHWC data from FFmpeg to openvino - status = ie_network_set_input_layout(ov_model->network, input_name, NHWC); - if (status != OK) { - av_log(ctx, AV_LOG_ERROR, "Input \"%s\" does not match layout NHWC\n", input_name); - return DNN_ERROR; - } - input->channels = dims.dims[1]; input->height = dims.dims[2]; input->width = dims.dims[3]; From patchwork Mon Jan 11 02:20:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Ting" X-Patchwork-Id: 24892 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2EFCB44BA65 for ; Mon, 11 Jan 2021 04:28:48 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0F84968AB51; Mon, 11 Jan 2021 04:28:48 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 38E3C68A774 for ; Mon, 11 Jan 2021 04:28:40 +0200 (EET) IronPort-SDR: Nqd8M5W+VYhcAv549pgeOpPDHuaBr1hcEl+FB8i3OqTRYxQFpECLRAfjtY2mSCWaSgtAkCw9Nr qFIRP/nEumug== X-IronPort-AV: E=McAfee;i="6000,8403,9860"; a="239337963" X-IronPort-AV: E=Sophos;i="5.79,337,1602572400"; d="scan'208";a="239337963" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2021 18:28:32 -0800 IronPort-SDR: xTKuOy1If2G+M8RhwJKcz5GZs7YahMl64l8l4fvRP1Dsvpf1nVD32as0jaYdeTr9AivQ+dB53u ebCgKYuthh/g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.79,337,1602572400"; d="scan'208";a="380848667" Received: from semmer-ubuntu.sh.intel.com ([10.239.159.136]) by orsmga008.jf.intel.com with ESMTP; 10 Jan 2021 18:28:31 -0800 From: Ting Fu To: ffmpeg-devel@ffmpeg.org Date: Mon, 11 Jan 2021 10:20:31 +0800 Message-Id: <20210111022032.22334-2-ting.fu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210111022032.22334-1-ting.fu@intel.com> References: <20210111022032.22334-1-ting.fu@intel.com> Subject: [FFmpeg-devel] [PATCH 2/3] dnn/openvino: refine code for better model initialization X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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 --- libavfilter/dnn/dnn_backend_openvino.c | 153 +++++++++++++++---------- 1 file changed, 93 insertions(+), 60 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c index 050be97209..d6e0593a0b 100644 --- a/libavfilter/dnn/dnn_backend_openvino.c +++ b/libavfilter/dnn/dnn_backend_openvino.c @@ -217,6 +217,78 @@ static void infer_completion_callback(void *args) task->done = 1; } +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; + + 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(TaskItem *task, RequestItem *request) { IEStatusCode status; @@ -325,6 +397,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; @@ -347,13 +426,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){ @@ -385,63 +461,6 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, 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++) { - 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; - } - model->get_input = &get_input_ov; model->get_output = &get_output_ov; model->options = options; @@ -479,6 +498,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; @@ -517,6 +543,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; From patchwork Mon Jan 11 02:20:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Ting" X-Patchwork-Id: 24893 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 33AD744BA65 for ; Mon, 11 Jan 2021 04:28:49 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1F0DE68AB5B; Mon, 11 Jan 2021 04:28:49 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8CB9268A774 for ; Mon, 11 Jan 2021 04:28:41 +0200 (EET) IronPort-SDR: /prGuM5ytpMXx7aWaleE9BSBSBLIXRK2ZE8Nsk4kBN8Wl7hxdFpbsVV7BEvcjGkwoYNkgpcFdM jTcmefs2CJaA== X-IronPort-AV: E=McAfee;i="6000,8403,9860"; a="239337965" X-IronPort-AV: E=Sophos;i="5.79,337,1602572400"; d="scan'208";a="239337965" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2021 18:28:33 -0800 IronPort-SDR: Eh0GvlhTZKrtr07mQfvLM7bqrM7HGwD3Uhl9tsixM+IBTD6CsjG6YuU4yVESGqjdriU0xckTi5 CNuh5Z34FmTA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.79,337,1602572400"; d="scan'208";a="380848671" Received: from semmer-ubuntu.sh.intel.com ([10.239.159.136]) by orsmga008.jf.intel.com with ESMTP; 10 Jan 2021 18:28:32 -0800 From: Ting Fu To: ffmpeg-devel@ffmpeg.org Date: Mon, 11 Jan 2021 10:20:32 +0800 Message-Id: <20210111022032.22334-3-ting.fu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210111022032.22334-1-ting.fu@intel.com> References: <20210111022032.22334-1-ting.fu@intel.com> Subject: [FFmpeg-devel] [PATCH 3/3] dnn/openvino: support model input resize X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" OpenVINO APIs require specify input size to run the model, while some OpenVINO model does accept different input size. To enable this feature adding input_resizable option here for easier use. Setting bool variable input_resizable to specify if the input can be resizable or not. input_resizable = 1 means support input resize, aka accept different input size. input_resizable = 0 (default) means do not support input resize. Please make sure the inference model does accept different input size before use this option, otherwise the inference engine may report error(s). eg: ./ffmpeg -i video_name.mp4 -vf dnn_processing=dnn_backend=openvino:\ model=model_name.xml:input=input_name:output=output_name:\ options=device=CPU\&input_resizable=1 -y output_video_name.mp4 Signed-off-by: Ting Fu --- libavfilter/dnn/dnn_backend_openvino.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c index d6e0593a0b..65d74702ff 100644 --- a/libavfilter/dnn/dnn_backend_openvino.c +++ b/libavfilter/dnn/dnn_backend_openvino.c @@ -37,6 +37,7 @@ typedef struct OVOptions{ char *device_type; int nireq; + int input_resizable; } OVOptions; typedef struct OVContext { @@ -83,6 +84,7 @@ typedef struct RequestItem { static const AVOption dnn_openvino_options[] = { { "device", "device to run model", OFFSET(options.device_type), AV_OPT_TYPE_STRING, { .str = "CPU" }, 0, 0, FLAGS }, { "nireq", "number of request", OFFSET(options.nireq), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, + { "input_resizable", "can input be resizable or not", OFFSET(options.input_resizable), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { NULL } }; @@ -334,6 +336,7 @@ static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input size_t model_input_count = 0; dimensions_t dims; precision_e precision; + int input_resizable = ctx->options.input_resizable; status = ie_network_get_inputs_number(ov_model->network, &model_input_count); if (status != OK) { @@ -357,8 +360,8 @@ static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input } input->channels = dims.dims[1]; - input->height = dims.dims[2]; - input->width = dims.dims[3]; + input->height = input_resizable ? -1 : dims.dims[2]; + input->width = input_resizable ? -1 : dims.dims[3]; input->dt = precision_to_datatype(precision); return DNN_SUCCESS; } else { @@ -383,6 +386,8 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu RequestItem request; AVFrame *in_frame = av_frame_alloc(); AVFrame *out_frame = NULL; + IEStatusCode status; + input_shapes_t input_shapes; if (!in_frame) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n"); @@ -397,6 +402,18 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu in_frame->width = input_width; in_frame->height = input_height; + if (ctx->options.input_resizable) { + status = ie_network_get_input_shapes(ov_model->network, &input_shapes); + input_shapes.shapes->shape.dims[2] = input_height; + input_shapes.shapes->shape.dims[3] = input_width; + status |= ie_network_reshape(ov_model->network, input_shapes); + ie_network_input_shapes_free(&input_shapes); + if (status != OK) { + av_log(ctx, AV_LOG_ERROR, "Failed to reshape input size for %s\n", input_name); + 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");