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.
Showing the changes to libavfilter (1 of 1 file). Show all
Unified · Split
| @@ -248,6 +248,103 @@ static void infer_completion_callback(void *args) · 247 unmodified lines | ||
| 248 | 248 | } |
| 249 | 249 | } |
| 250 | 250 | |
| 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 | + | |
| 251 | 348 | static DNNReturnType execute_model_ov(RequestItem *request) |
| 252 | 349 | { |
| 253 | 350 | IEStatusCode status; |
| @@ -367,6 +464,13 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu · 113 unmodified lines | ||
| 367 | 464 | in_frame->width = input_width; |
| 368 | 465 | in_frame->height = input_height; |
| 369 | 466 | |
| 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 | + | |
| 370 | 474 | task.done = 0; |
| 371 | 475 | task.do_ioproc = 0; |
| 372 | 476 | task.async = 0; |
| @@ -391,13 +495,10 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu · 18 unmodified lines | ||
| 391 | 495 | |
| 392 | 496 | DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, AVFilterContext *filter_ctx) |
| 393 | 497 | { |
| 394 | − char *all_dev_names = NULL; | |
| 395 | 498 | DNNModel *model = NULL; |
| 396 | 499 | OVModel *ov_model = NULL; |
| 397 | 500 | OVContext *ctx = NULL; |
| 398 | 501 | IEStatusCode status; |
| 399 | − ie_config_t config = {NULL, NULL, NULL}; | |
| 400 | − ie_available_devices_t a_dev; | |
| 401 | 502 | |
| 402 | 503 | model = av_mallocz(sizeof(DNNModel)); |
| 403 | 504 | if (!model){ |
| @@ -429,88 +530,6 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, · 25 unmodified lines | ||
| 429 | 530 | if (status != OK) |
| 430 | 531 | goto err; |
| 431 | 532 | |
| 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 | − | |
| 514 | 533 | model->get_input = &get_input_ov; |
| 515 | 534 | model->get_output = &get_output_ov; |
| 516 | 535 | model->options = options; |
| @@ -554,6 +573,13 @@ DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n · 37 unmodified lines | ||
| 554 | 573 | return DNN_ERROR; |
| 555 | 574 | } |
| 556 | 575 | |
| 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 | + | |
| 557 | 583 | task.done = 0; |
| 558 | 584 | task.do_ioproc = 1; |
| 559 | 585 | task.async = 0; |
| @@ -594,6 +620,13 @@ DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *i · 34 unmodified lines | ||
| 594 | 620 | return DNN_ERROR; |
| 595 | 621 | } |
| 596 | 622 | |
| 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 | + | |
| 597 | 630 | task->done = 0; |
| 598 | 631 | task->do_ioproc = 1; |
| 599 | 632 | task->async = 1; |