History

lavfi/dnn_backend_tf: Return Specific Error Codes

Switch to returning specific error codes or DNN_GENERIC_ERROR when an error is encountered. For TensorFlow C API errors, currently DNN_GENERIC_ERROR is returned.

Shubhanshu Saxena committed 4 years ago

Commit 3fa89bd1 parent 91af38f2 files · +85 −67Browse files

Showing the changes to libavfilter (2 of 2 files). Show all

Unified · Split

2 files
  1. libavfilter/dnn/dnn_backend_tf.c +83 −65
  2. libavfilter/dnn/dnn_backend_tf.h +2 −2
libavfilter/dnn/dnn_backend_tf.c+83−65
@@ -90,7 +90,7 @@ static const AVOption dnn_tensorflow_options[] = { · 89 unmodified lines
90 90
91AVFILTER_DEFINE_CLASS(dnn_tensorflow);91AVFILTER_DEFINE_CLASS(dnn_tensorflow);
92 92
93static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *lltask_queue);93static int execute_model_tf(TFRequestItem *request, Queue *lltask_queue);
94static void infer_completion_callback(void *args);94static void infer_completion_callback(void *args);
95static inline void destroy_request_item(TFRequestItem **arg);95static inline void destroy_request_item(TFRequestItem **arg);
96 96
@@ -152,9 +152,10 @@ static TFInferRequest *tf_create_inference_request(void) · 55 unmodified lines
152 *152 *
153 * @param request pointer to the TFRequestItem for inference153 * @param request pointer to the TFRequestItem for inference
154 * @retval DNN_SUCCESS if execution is successful154 * @retval DNN_SUCCESS if execution is successful
155 * @retval DNN_ERROR if execution fails155 * @retval AVERROR(EINVAL) if request is NULL
156 * @retval DNN_GENERIC_ERROR if execution fails
156 */157 */
157static DNNReturnType tf_start_inference(void *args)158static int tf_start_inference(void *args)
158{159{
159 TFRequestItem *request = args;160 TFRequestItem *request = args;
160 TFInferRequest *infer_request = request->infer_request;161 TFInferRequest *infer_request = request->infer_request;
@@ -164,7 +165,7 @@ static DNNReturnType tf_start_inference(void *args) · 3 unmodified lines
164 165
165 if (!request) {166 if (!request) {
166 av_log(&tf_model->ctx, AV_LOG_ERROR, "TFRequestItem is NULL\n");167 av_log(&tf_model->ctx, AV_LOG_ERROR, "TFRequestItem is NULL\n");
167 return DNN_ERROR;168 return AVERROR(EINVAL);
168 }169 }
169 170
170 TF_SessionRun(tf_model->session, NULL,171 TF_SessionRun(tf_model->session, NULL,
@@ -178,7 +179,7 @@ static DNNReturnType tf_start_inference(void *args) · 7 unmodified lines
178 if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) {179 if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) {
179 destroy_request_item(&request);180 destroy_request_item(&request);
180 }181 }
181 return DNN_ERROR;182 return DNN_GENERIC_ERROR;
182 }183 }
183 return DNN_SUCCESS;184 return DNN_SUCCESS;
184}185}
@@ -202,14 +203,14 @@ static inline void destroy_request_item(TFRequestItem **arg) { · 17 unmodified lines
202 av_freep(arg);203 av_freep(arg);
203}204}
204 205
205static DNNReturnType extract_lltask_from_task(TaskItem *task, Queue *lltask_queue)206static int extract_lltask_from_task(TaskItem *task, Queue *lltask_queue)
206{207{
207 TFModel *tf_model = task->model;208 TFModel *tf_model = task->model;
208 TFContext *ctx = &tf_model->ctx;209 TFContext *ctx = &tf_model->ctx;
209 LastLevelTaskItem *lltask = av_malloc(sizeof(*lltask));210 LastLevelTaskItem *lltask = av_malloc(sizeof(*lltask));
210 if (!lltask) {211 if (!lltask) {
211 av_log(ctx, AV_LOG_ERROR, "Unable to allocate space for LastLevelTaskItem\n");212 av_log(ctx, AV_LOG_ERROR, "Unable to allocate space for LastLevelTaskItem\n");
212 return DNN_ERROR;213 return AVERROR(ENOMEM);
213 }214 }
214 task->inference_todo = 1;215 task->inference_todo = 1;
215 task->inference_done = 0;216 task->inference_done = 0;
@@ -217,7 +218,7 @@ static DNNReturnType extract_lltask_from_task(TaskItem *task, Queue *lltask_queu · 1 unmodified line
217 if (ff_queue_push_back(lltask_queue, lltask) < 0) {218 if (ff_queue_push_back(lltask_queue, lltask) < 0) {
218 av_log(ctx, AV_LOG_ERROR, "Failed to push back lltask_queue.\n");219 av_log(ctx, AV_LOG_ERROR, "Failed to push back lltask_queue.\n");
219 av_freep(&lltask);220 av_freep(&lltask);
220 return DNN_ERROR;221 return AVERROR(ENOMEM);
221 }222 }
222 return DNN_SUCCESS;223 return DNN_SUCCESS;
223}224}
@@ -277,7 +278,7 @@ static TF_Tensor *allocate_input_tensor(const DNNData *input) · 53 unmodified lines
277 input_dims[1] * input_dims[2] * input_dims[3] * size);278 input_dims[1] * input_dims[2] * input_dims[3] * size);
278}279}
279 280
280static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input_name)281static int get_input_tf(void *model, DNNData *input, const char *input_name)
281{282{
282 TFModel *tf_model = model;283 TFModel *tf_model = model;
283 TFContext *ctx = &tf_model->ctx;284 TFContext *ctx = &tf_model->ctx;
@@ -288,7 +289,7 @@ static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input · 4 unmodified lines
288 tf_output.oper = TF_GraphOperationByName(tf_model->graph, input_name);289 tf_output.oper = TF_GraphOperationByName(tf_model->graph, input_name);
289 if (!tf_output.oper) {290 if (!tf_output.oper) {
290 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name);291 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name);
291 return DNN_ERROR;292 return AVERROR(EINVAL);
292 }293 }
293 294
294 tf_output.index = 0;295 tf_output.index = 0;
@@ -300,7 +301,7 @@ static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input · 5 unmodified lines
300 if (TF_GetCode(status) != TF_OK){301 if (TF_GetCode(status) != TF_OK){
301 TF_DeleteStatus(status);302 TF_DeleteStatus(status);
302 av_log(ctx, AV_LOG_ERROR, "Failed to get input tensor shape: number of dimension incorrect\n");303 av_log(ctx, AV_LOG_ERROR, "Failed to get input tensor shape: number of dimension incorrect\n");
303 return DNN_ERROR;304 return DNN_GENERIC_ERROR;
304 }305 }
305 TF_DeleteStatus(status);306 TF_DeleteStatus(status);
306 307
@@ -313,10 +314,10 @@ static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input · 6 unmodified lines
313 return DNN_SUCCESS;314 return DNN_SUCCESS;
314}315}
315 316
316static DNNReturnType get_output_tf(void *model, const char *input_name, int input_width, int input_height,317static int get_output_tf(void *model, const char *input_name, int input_width, int input_height,
317 const char *output_name, int *output_width, int *output_height)318 const char *output_name, int *output_width, int *output_height)
318{319{
319 DNNReturnType ret;320 int ret;
320 TFModel *tf_model = model;321 TFModel *tf_model = model;
321 TFContext *ctx = &tf_model->ctx;322 TFContext *ctx = &tf_model->ctx;
322 TaskItem task;323 TaskItem task;
@@ -329,20 +330,21 @@ static DNNReturnType get_output_tf(void *model, const char *input_name, int inpu · 6 unmodified lines
329 .out_frame = NULL,330 .out_frame = NULL,
330 };331 };
331 332
332 if (ff_dnn_fill_gettingoutput_task(&task, &exec_params, tf_model, input_height, input_width, ctx) != DNN_SUCCESS) {333 ret = ff_dnn_fill_gettingoutput_task(&task, &exec_params, tf_model, input_height, input_width, ctx);
334 if (ret != DNN_SUCCESS) {
333 goto err;335 goto err;
334 }336 }
335 337
336 if (extract_lltask_from_task(&task, tf_model->lltask_queue) != DNN_SUCCESS) {338 ret = extract_lltask_from_task(&task, tf_model->lltask_queue);
339 if (ret != DNN_SUCCESS) {
337 av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n");340 av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n");
338 ret = DNN_ERROR;
339 goto err;341 goto err;
340 }342 }
341 343
342 request = ff_safe_queue_pop_front(tf_model->request_queue);344 request = ff_safe_queue_pop_front(tf_model->request_queue);
343 if (!request) {345 if (!request) {
344 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");346 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
345 ret = DNN_ERROR;347 ret = AVERROR(EINVAL);
346 goto err;348 goto err;
347 }349 }
348 350
@@ -386,7 +388,7 @@ static int hex_to_data(uint8_t *data, const char *p) · 37 unmodified lines
386 return len;388 return len;
387}389}
388 390
389static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename)391static int load_tf_model(TFModel *tf_model, const char *model_filename)
390{392{
391 TFContext *ctx = &tf_model->ctx;393 TFContext *ctx = &tf_model->ctx;
392 TF_Buffer *graph_def;394 TF_Buffer *graph_def;
@@ -407,7 +409,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename · 14 unmodified lines
407 */409 */
408 if (strncmp(tf_model->ctx.options.sess_config, "0x", 2) != 0) {410 if (strncmp(tf_model->ctx.options.sess_config, "0x", 2) != 0) {
409 av_log(ctx, AV_LOG_ERROR, "sess_config should start with '0x'\n");411 av_log(ctx, AV_LOG_ERROR, "sess_config should start with '0x'\n");
410 return DNN_ERROR;412 return AVERROR(EINVAL);
411 }413 }
412 config = tf_model->ctx.options.sess_config + 2;414 config = tf_model->ctx.options.sess_config + 2;
413 sess_config_length = hex_to_data(NULL, config);415 sess_config_length = hex_to_data(NULL, config);
@@ -415,11 +417,11 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename · 1 unmodified line
415 sess_config = av_mallocz(sess_config_length + AV_INPUT_BUFFER_PADDING_SIZE);417 sess_config = av_mallocz(sess_config_length + AV_INPUT_BUFFER_PADDING_SIZE);
416 if (!sess_config) {418 if (!sess_config) {
417 av_log(ctx, AV_LOG_ERROR, "failed to allocate memory\n");419 av_log(ctx, AV_LOG_ERROR, "failed to allocate memory\n");
418 return DNN_ERROR;420 return AVERROR(ENOMEM);
419 }421 }
420 if (hex_to_data(sess_config, config) < 0) {422 if (hex_to_data(sess_config, config) < 0) {
421 av_log(ctx, AV_LOG_ERROR, "failed to convert hex to data\n");423 av_log(ctx, AV_LOG_ERROR, "failed to convert hex to data\n");
422 return DNN_ERROR;424 return AVERROR(EINVAL);
423 }425 }
424 }426 }
425 427
@@ -427,7 +429,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename · 1 unmodified line
427 if (!graph_def){429 if (!graph_def){
428 av_log(ctx, AV_LOG_ERROR, "Failed to read model \"%s\" graph\n", model_filename);430 av_log(ctx, AV_LOG_ERROR, "Failed to read model \"%s\" graph\n", model_filename);
429 av_freep(&sess_config);431 av_freep(&sess_config);
430 return DNN_ERROR;432 return AVERROR(EINVAL);
431 }433 }
432 tf_model->graph = TF_NewGraph();434 tf_model->graph = TF_NewGraph();
433 tf_model->status = TF_NewStatus();435 tf_model->status = TF_NewStatus();
@@ -440,7 +442,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename · 6 unmodified lines
440 TF_DeleteStatus(tf_model->status);442 TF_DeleteStatus(tf_model->status);
441 av_log(ctx, AV_LOG_ERROR, "Failed to import serialized graph to model graph\n");443 av_log(ctx, AV_LOG_ERROR, "Failed to import serialized graph to model graph\n");
442 av_freep(&sess_config);444 av_freep(&sess_config);
443 return DNN_ERROR;445 return DNN_GENERIC_ERROR;
444 }446 }
445 447
446 init_op = TF_GraphOperationByName(tf_model->graph, "init");448 init_op = TF_GraphOperationByName(tf_model->graph, "init");
@@ -455,7 +457,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename · 8 unmodified lines
455 TF_DeleteSessionOptions(sess_opts);457 TF_DeleteSessionOptions(sess_opts);
456 av_log(ctx, AV_LOG_ERROR, "Failed to set config for sess options with %s\n",458 av_log(ctx, AV_LOG_ERROR, "Failed to set config for sess options with %s\n",
457 tf_model->ctx.options.sess_config);459 tf_model->ctx.options.sess_config);
458 return DNN_ERROR;460 return DNN_GENERIC_ERROR;
459 }461 }
460 }462 }
461 463
@@ -466,7 +468,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename · 4 unmodified lines
466 TF_DeleteGraph(tf_model->graph);468 TF_DeleteGraph(tf_model->graph);
467 TF_DeleteStatus(tf_model->status);469 TF_DeleteStatus(tf_model->status);
468 av_log(ctx, AV_LOG_ERROR, "Failed to create new session with model graph\n");470 av_log(ctx, AV_LOG_ERROR, "Failed to create new session with model graph\n");
469 return DNN_ERROR;471 return DNN_GENERIC_ERROR;
470 }472 }
471 473
472 // Run initialization operation with name "init" if it is present in graph474 // Run initialization operation with name "init" if it is present in graph
@@ -481,7 +483,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename · 8 unmodified lines
481 TF_DeleteGraph(tf_model->graph);483 TF_DeleteGraph(tf_model->graph);
482 TF_DeleteStatus(tf_model->status);484 TF_DeleteStatus(tf_model->status);
483 av_log(ctx, AV_LOG_ERROR, "Failed to run session when initializing\n");485 av_log(ctx, AV_LOG_ERROR, "Failed to run session when initializing\n");
484 return DNN_ERROR;486 return DNN_GENERIC_ERROR;
485 }487 }
486 }488 }
487 489
@@ -490,7 +492,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename · 2 unmodified lines
490 492
491#define NAME_BUFFER_SIZE 256493#define NAME_BUFFER_SIZE 256
492 494
493static DNNReturnType add_conv_layer(TFModel *tf_model, TF_Operation *transpose_op, TF_Operation **cur_op,495static int add_conv_layer(TFModel *tf_model, TF_Operation *transpose_op, TF_Operation **cur_op,
494 ConvolutionalParams* params, const int layer)496 ConvolutionalParams* params, const int layer)
495{497{
496 TFContext *ctx = &tf_model->ctx;498 TFContext *ctx = &tf_model->ctx;
@@ -594,7 +596,7 @@ static DNNReturnType add_conv_layer(TFModel *tf_model, TF_Operation *transpose_o · 97 unmodified lines
594 break;596 break;
595 default:597 default:
596 avpriv_report_missing_feature(ctx, "convolutional activation function %d", params->activation);598 avpriv_report_missing_feature(ctx, "convolutional activation function %d", params->activation);
597 return DNN_ERROR;599 return AVERROR(ENOSYS);
598 }600 }
599 input.oper = *cur_op;601 input.oper = *cur_op;
600 TF_AddInput(op_desc, input);602 TF_AddInput(op_desc, input);
@@ -609,10 +611,10 @@ err: · 8 unmodified lines
609 TF_DeleteTensor(kernel_tensor);611 TF_DeleteTensor(kernel_tensor);
610 TF_DeleteTensor(biases_tensor);612 TF_DeleteTensor(biases_tensor);
611 av_log(ctx, AV_LOG_ERROR, "Failed to add conv layer %d\n", layer);613 av_log(ctx, AV_LOG_ERROR, "Failed to add conv layer %d\n", layer);
612 return DNN_ERROR;614 return DNN_GENERIC_ERROR;
613}615}
614 616
615static DNNReturnType add_depth_to_space_layer(TFModel *tf_model, TF_Operation **cur_op,617static int add_depth_to_space_layer(TFModel *tf_model, TF_Operation **cur_op,
616 DepthToSpaceParams *params, const int layer)618 DepthToSpaceParams *params, const int layer)
617{619{
618 TFContext *ctx = &tf_model->ctx;620 TFContext *ctx = &tf_model->ctx;
@@ -630,13 +632,13 @@ static DNNReturnType add_depth_to_space_layer(TFModel *tf_model, TF_Operation ** · 11 unmodified lines
630 *cur_op = TF_FinishOperation(op_desc, tf_model->status);632 *cur_op = TF_FinishOperation(op_desc, tf_model->status);
631 if (TF_GetCode(tf_model->status) != TF_OK){633 if (TF_GetCode(tf_model->status) != TF_OK){
632 av_log(ctx, AV_LOG_ERROR, "Failed to add depth_to_space to layer %d\n", layer);634 av_log(ctx, AV_LOG_ERROR, "Failed to add depth_to_space to layer %d\n", layer);
633 return DNN_ERROR;635 return DNN_GENERIC_ERROR;
634 }636 }
635 637
636 return DNN_SUCCESS;638 return DNN_SUCCESS;
637}639}
638 640
639static DNNReturnType add_pad_layer(TFModel *tf_model, TF_Operation **cur_op,641static int add_pad_layer(TFModel *tf_model, TF_Operation **cur_op,
640 LayerPadParams *params, const int layer)642 LayerPadParams *params, const int layer)
641{643{
642 TFContext *ctx = &tf_model->ctx;644 TFContext *ctx = &tf_model->ctx;
@@ -666,13 +668,13 @@ static DNNReturnType add_pad_layer(TFModel *tf_model, TF_Operation **cur_op, · 23 unmodified lines
666 if (TF_GetCode(tf_model->status) != TF_OK){668 if (TF_GetCode(tf_model->status) != TF_OK){
667 TF_DeleteTensor(tensor);669 TF_DeleteTensor(tensor);
668 av_log(ctx, AV_LOG_ERROR, "Failed to set value for pad of layer %d\n", layer);670 av_log(ctx, AV_LOG_ERROR, "Failed to set value for pad of layer %d\n", layer);
669 return DNN_ERROR;671 return DNN_GENERIC_ERROR;
670 }672 }
671 op = TF_FinishOperation(op_desc, tf_model->status);673 op = TF_FinishOperation(op_desc, tf_model->status);
672 if (TF_GetCode(tf_model->status) != TF_OK){674 if (TF_GetCode(tf_model->status) != TF_OK){
673 TF_DeleteTensor(tensor);675 TF_DeleteTensor(tensor);
674 av_log(ctx, AV_LOG_ERROR, "Failed to add pad to layer %d\n", layer);676 av_log(ctx, AV_LOG_ERROR, "Failed to add pad to layer %d\n", layer);
675 return DNN_ERROR;677 return DNN_GENERIC_ERROR;
676 }678 }
677 679
678 op_desc = TF_NewOperation(tf_model->graph, "MirrorPad", "mirror_pad");680 op_desc = TF_NewOperation(tf_model->graph, "MirrorPad", "mirror_pad");
@@ -688,13 +690,13 @@ static DNNReturnType add_pad_layer(TFModel *tf_model, TF_Operation **cur_op, · 9 unmodified lines
688 if (TF_GetCode(tf_model->status) != TF_OK){690 if (TF_GetCode(tf_model->status) != TF_OK){
689 TF_DeleteTensor(tensor);691 TF_DeleteTensor(tensor);
690 av_log(ctx, AV_LOG_ERROR, "Failed to add mirror_pad to layer %d\n", layer);692 av_log(ctx, AV_LOG_ERROR, "Failed to add mirror_pad to layer %d\n", layer);
691 return DNN_ERROR;693 return DNN_GENERIC_ERROR;
692 }694 }
693 695
694 return DNN_SUCCESS;696 return DNN_SUCCESS;
695}697}
696 698
697static DNNReturnType add_maximum_layer(TFModel *tf_model, TF_Operation **cur_op,699static int add_maximum_layer(TFModel *tf_model, TF_Operation **cur_op,
698 DnnLayerMaximumParams *params, const int layer)700 DnnLayerMaximumParams *params, const int layer)
699{701{
700 TFContext *ctx = &tf_model->ctx;702 TFContext *ctx = &tf_model->ctx;
@@ -716,13 +718,13 @@ static DNNReturnType add_maximum_layer(TFModel *tf_model, TF_Operation **cur_op, · 15 unmodified lines
716 if (TF_GetCode(tf_model->status) != TF_OK){718 if (TF_GetCode(tf_model->status) != TF_OK){
717 TF_DeleteTensor(tensor);719 TF_DeleteTensor(tensor);
718 av_log(ctx, AV_LOG_ERROR, "Failed to set value for maximum/y of layer %d", layer);720 av_log(ctx, AV_LOG_ERROR, "Failed to set value for maximum/y of layer %d", layer);
719 return DNN_ERROR;721 return DNN_GENERIC_ERROR;
720 }722 }
721 op = TF_FinishOperation(op_desc, tf_model->status);723 op = TF_FinishOperation(op_desc, tf_model->status);
722 if (TF_GetCode(tf_model->status) != TF_OK){724 if (TF_GetCode(tf_model->status) != TF_OK){
723 TF_DeleteTensor(tensor);725 TF_DeleteTensor(tensor);
724 av_log(ctx, AV_LOG_ERROR, "Failed to add maximum/y to layer %d\n", layer);726 av_log(ctx, AV_LOG_ERROR, "Failed to add maximum/y to layer %d\n", layer);
725 return DNN_ERROR;727 return DNN_GENERIC_ERROR;
726 }728 }
727 729
728 snprintf(name_buffer, NAME_BUFFER_SIZE, "maximum%d", layer);730 snprintf(name_buffer, NAME_BUFFER_SIZE, "maximum%d", layer);
@@ -737,13 +739,13 @@ static DNNReturnType add_maximum_layer(TFModel *tf_model, TF_Operation **cur_op, · 8 unmodified lines
737 if (TF_GetCode(tf_model->status) != TF_OK){739 if (TF_GetCode(tf_model->status) != TF_OK){
738 TF_DeleteTensor(tensor);740 TF_DeleteTensor(tensor);
739 av_log(ctx, AV_LOG_ERROR, "Failed to add maximum to layer %d\n", layer);741 av_log(ctx, AV_LOG_ERROR, "Failed to add maximum to layer %d\n", layer);
740 return DNN_ERROR;742 return DNN_GENERIC_ERROR;
741 }743 }
742 744
743 return DNN_SUCCESS;745 return DNN_SUCCESS;
744}746}
745 747
746static DNNReturnType load_native_model(TFModel *tf_model, const char *model_filename)748static int load_native_model(TFModel *tf_model, const char *model_filename)
747{749{
748 TFContext *ctx = &tf_model->ctx;750 TFContext *ctx = &tf_model->ctx;
749 int32_t layer;751 int32_t layer;
@@ -755,14 +757,14 @@ static DNNReturnType load_native_model(TFModel *tf_model, const char *model_file · 5 unmodified lines
755 int32_t *transpose_perm;757 int32_t *transpose_perm;
756 int64_t transpose_perm_shape[] = {4};758 int64_t transpose_perm_shape[] = {4};
757 int64_t input_shape[] = {1, -1, -1, -1};759 int64_t input_shape[] = {1, -1, -1, -1};
758 DNNReturnType layer_add_res;760 int layer_add_res;
759 DNNModel *model = NULL;761 DNNModel *model = NULL;
760 NativeModel *native_model;762 NativeModel *native_model;
761 763
762 model = ff_dnn_load_model_native(model_filename, DFT_PROCESS_FRAME, NULL, NULL);764 model = ff_dnn_load_model_native(model_filename, DFT_PROCESS_FRAME, NULL, NULL);
763 if (!model){765 if (!model){
764 av_log(ctx, AV_LOG_ERROR, "Failed to load native model\n");766 av_log(ctx, AV_LOG_ERROR, "Failed to load native model\n");
765 return DNN_ERROR;767 return AVERROR(EINVAL);
766 }768 }
767 769
768 native_model = model->model;770 native_model = model->model;
@@ -775,7 +777,7 @@ static DNNReturnType load_native_model(TFModel *tf_model, const char *model_file · 6 unmodified lines
775 TF_DeleteGraph(tf_model->graph); \777 TF_DeleteGraph(tf_model->graph); \
776 TF_DeleteStatus(tf_model->status); \778 TF_DeleteStatus(tf_model->status); \
777 av_log(ctx, AV_LOG_ERROR, "Failed to set value or add operator to layer\n"); \779 av_log(ctx, AV_LOG_ERROR, "Failed to set value or add operator to layer\n"); \
778 return DNN_ERROR; \780 return DNN_GENERIC_ERROR; \
779 }781 }
780 782
781 op_desc = TF_NewOperation(tf_model->graph, "Placeholder", "x");783 op_desc = TF_NewOperation(tf_model->graph, "Placeholder", "x");
@@ -942,19 +944,21 @@ err: · 160 unmodified lines
942 return NULL;944 return NULL;
943}945}
944 946
945static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *request) {947static int fill_model_input_tf(TFModel *tf_model, TFRequestItem *request) {
946 DNNData input;948 DNNData input;
947 LastLevelTaskItem *lltask;949 LastLevelTaskItem *lltask;
948 TaskItem *task;950 TaskItem *task;
949 TFInferRequest *infer_request;951 TFInferRequest *infer_request;
950 TFContext *ctx = &tf_model->ctx;952 TFContext *ctx = &tf_model->ctx;
953 int ret = 0;
951 954
952 lltask = ff_queue_pop_front(tf_model->lltask_queue);955 lltask = ff_queue_pop_front(tf_model->lltask_queue);
953 av_assert0(lltask);956 av_assert0(lltask);
954 task = lltask->task;957 task = lltask->task;
955 request->lltask = lltask;958 request->lltask = lltask;
956 959
957 if (get_input_tf(tf_model, &input, task->input_name) != DNN_SUCCESS) {960 ret = get_input_tf(tf_model, &input, task->input_name);
961 if (ret != DNN_SUCCESS) {
958 goto err;962 goto err;
959 }963 }
960 964
@@ -965,12 +969,14 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque · 4 unmodified lines
965 infer_request->tf_input = av_malloc(sizeof(TF_Output));969 infer_request->tf_input = av_malloc(sizeof(TF_Output));
966 if (!infer_request->tf_input) {970 if (!infer_request->tf_input) {
967 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input tensor\n");971 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input tensor\n");
972 ret = AVERROR(ENOMEM);
968 goto err;973 goto err;
969 }974 }
970 975
971 infer_request->tf_input->oper = TF_GraphOperationByName(tf_model->graph, task->input_name);976 infer_request->tf_input->oper = TF_GraphOperationByName(tf_model->graph, task->input_name);
972 if (!infer_request->tf_input->oper){977 if (!infer_request->tf_input->oper){
973 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", task->input_name);978 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", task->input_name);
979 ret = DNN_GENERIC_ERROR;
974 goto err;980 goto err;
975 }981 }
976 infer_request->tf_input->index = 0;982 infer_request->tf_input->index = 0;
@@ -978,6 +984,7 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque · 1 unmodified line
978 infer_request->input_tensor = allocate_input_tensor(&input);984 infer_request->input_tensor = allocate_input_tensor(&input);
979 if (!infer_request->input_tensor){985 if (!infer_request->input_tensor){
980 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input tensor\n");986 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input tensor\n");
987 ret = AVERROR(ENOMEM);
981 goto err;988 goto err;
982 }989 }
983 input.data = (float *)TF_TensorData(infer_request->input_tensor);990 input.data = (float *)TF_TensorData(infer_request->input_tensor);
@@ -1003,12 +1010,14 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque · 19 unmodified lines
1003 infer_request->tf_outputs = av_malloc_array(task->nb_output, sizeof(TF_Output));1010 infer_request->tf_outputs = av_malloc_array(task->nb_output, sizeof(TF_Output));
1004 if (infer_request->tf_outputs == NULL) {1011 if (infer_request->tf_outputs == NULL) {
1005 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for *tf_outputs\n");1012 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for *tf_outputs\n");
1013 ret = AVERROR(ENOMEM);
1006 goto err;1014 goto err;
1007 }1015 }
1008 1016
1009 infer_request->output_tensors = av_calloc(task->nb_output, sizeof(*infer_request->output_tensors));1017 infer_request->output_tensors = av_calloc(task->nb_output, sizeof(*infer_request->output_tensors));
1010 if (!infer_request->output_tensors) {1018 if (!infer_request->output_tensors) {
1011 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output tensor\n");1019 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output tensor\n");
1020 ret = AVERROR(ENOMEM);
1012 goto err;1021 goto err;
1013 }1022 }
1014 1023
@@ -1017,6 +1026,7 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque · 2 unmodified lines
1017 infer_request->tf_outputs[i].oper = TF_GraphOperationByName(tf_model->graph, task->output_names[i]);1026 infer_request->tf_outputs[i].oper = TF_GraphOperationByName(tf_model->graph, task->output_names[i]);
1018 if (!infer_request->tf_outputs[i].oper) {1027 if (!infer_request->tf_outputs[i].oper) {
1019 av_log(ctx, AV_LOG_ERROR, "Could not find output \"%s\" in model\n", task->output_names[i]);1028 av_log(ctx, AV_LOG_ERROR, "Could not find output \"%s\" in model\n", task->output_names[i]);
1029 ret = DNN_GENERIC_ERROR;
1020 goto err;1030 goto err;
1021 }1031 }
1022 infer_request->tf_outputs[i].index = 0;1032 infer_request->tf_outputs[i].index = 0;
@@ -1025,7 +1035,7 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque · 2 unmodified lines
1025 return DNN_SUCCESS;1035 return DNN_SUCCESS;
1026err:1036err:
1027 tf_free_request(infer_request);1037 tf_free_request(infer_request);
1028 return DNN_ERROR;1038 return ret;
1029}1039}
1030 1040
1031static void infer_completion_callback(void *args) {1041static void infer_completion_callback(void *args) {
@@ -1086,12 +1096,13 @@ err: · 54 unmodified lines
1086 }1096 }
1087}1097}
1088 1098
1089static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *lltask_queue)1099static int execute_model_tf(TFRequestItem *request, Queue *lltask_queue)
1090{1100{
1091 TFModel *tf_model;1101 TFModel *tf_model;
1092 TFContext *ctx;1102 TFContext *ctx;
1093 LastLevelTaskItem *lltask;1103 LastLevelTaskItem *lltask;
1094 TaskItem *task;1104 TaskItem *task;
1105 int ret = 0;
1095 1106
1096 if (ff_queue_size(lltask_queue) == 0) {1107 if (ff_queue_size(lltask_queue) == 0) {
1097 destroy_request_item(&request);1108 destroy_request_item(&request);
@@ -1103,7 +1114,8 @@ static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *lltask_queu · 5 unmodified lines
1103 tf_model = task->model;1114 tf_model = task->model;
1104 ctx = &tf_model->ctx;1115 ctx = &tf_model->ctx;
1105 1116
1106 if (fill_model_input_tf(tf_model, request) != DNN_SUCCESS) {1117 ret = fill_model_input_tf(tf_model, request);
1118 if (ret != DNN_SUCCESS) {
1107 goto err;1119 goto err;
1108 }1120 }
1109 1121
@@ -1112,58 +1124,64 @@ static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *lltask_queu · 2 unmodified lines
1112 goto err;1124 goto err;
1113 }1125 }
1114 return DNN_SUCCESS;1126 return DNN_SUCCESS;
1115 } else {1127 }
1116 if (tf_start_inference(request) != DNN_SUCCESS) {1128 else {
1129 ret = tf_start_inference(request);
1130 if (ret != DNN_SUCCESS) {
1117 goto err;1131 goto err;
1118 }1132 }
1119 infer_completion_callback(request);1133 infer_completion_callback(request);
1120 return (task->inference_done == task->inference_todo) ? DNN_SUCCESS : DNN_ERROR;1134 return (task->inference_done == task->inference_todo) ? DNN_SUCCESS : DNN_GENERIC_ERROR;
1121 }1135 }
1122err:1136err:
1123 tf_free_request(request->infer_request);1137 tf_free_request(request->infer_request);
1124 if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) {1138 if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) {
1125 destroy_request_item(&request);1139 destroy_request_item(&request);
1126 }1140 }
1127 return DNN_ERROR;1141 return ret;
1128}1142}
1129 1143
1130DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params)1144int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params)
1131{1145{
1132 TFModel *tf_model = model->model;1146 TFModel *tf_model = model->model;
1133 TFContext *ctx = &tf_model->ctx;1147 TFContext *ctx = &tf_model->ctx;
1134 TaskItem *task;1148 TaskItem *task;
1135 TFRequestItem *request;1149 TFRequestItem *request;
1150 int ret = 0;
1136 1151
1137 if (ff_check_exec_params(ctx, DNN_TF, model->func_type, exec_params) != 0) {1152 ret = ff_check_exec_params(ctx, DNN_TF, model->func_type, exec_params);
1138 return DNN_ERROR;1153 if (ret != 0) {
1154 return ret;
1139 }1155 }
1140 1156
1141 task = av_malloc(sizeof(*task));1157 task = av_malloc(sizeof(*task));
1142 if (!task) {1158 if (!task) {
1143 av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");1159 av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
1144 return DNN_ERROR;1160 return AVERROR(ENOMEM);
1145 }1161 }
1146 1162
1147 if (ff_dnn_fill_task(task, exec_params, tf_model, ctx->options.async, 1) != DNN_SUCCESS) {1163 ret = ff_dnn_fill_task(task, exec_params, tf_model, ctx->options.async, 1);
1164 if (ret != DNN_SUCCESS) {
1148 av_freep(&task);1165 av_freep(&task);
1149 return DNN_ERROR;1166 return ret;
1150 }1167 }
1151 1168
1152 if (ff_queue_push_back(tf_model->task_queue, task) < 0) {1169 if (ff_queue_push_back(tf_model->task_queue, task) < 0) {
1153 av_freep(&task);1170 av_freep(&task);
1154 av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");1171 av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
1155 return DNN_ERROR;1172 return AVERROR(ENOMEM);
1156 }1173 }
1157 1174
1158 if (extract_lltask_from_task(task, tf_model->lltask_queue) != DNN_SUCCESS) {1175 ret = extract_lltask_from_task(task, tf_model->lltask_queue);
1176 if (ret != DNN_SUCCESS) {
1159 av_log(ctx, AV_LOG_ERROR, "unable to extract last level task from task.\n");1177 av_log(ctx, AV_LOG_ERROR, "unable to extract last level task from task.\n");
1160 return DNN_ERROR;1178 return ret;
1161 }1179 }
1162 1180
1163 request = ff_safe_queue_pop_front(tf_model->request_queue);1181 request = ff_safe_queue_pop_front(tf_model->request_queue);
1164 if (!request) {1182 if (!request) {
1165 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");1183 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
1166 return DNN_ERROR;1184 return AVERROR(EINVAL);
1167 }1185 }
1168 return execute_model_tf(request, tf_model->lltask_queue);1186 return execute_model_tf(request, tf_model->lltask_queue);
1169}1187}
@@ -1174,12 +1192,12 @@ DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVF · 4 unmodified lines
1174 return ff_dnn_get_result_common(tf_model->task_queue, in, out);1192 return ff_dnn_get_result_common(tf_model->task_queue, in, out);
1175}1193}
1176 1194
1177DNNReturnType ff_dnn_flush_tf(const DNNModel *model)1195int ff_dnn_flush_tf(const DNNModel *model)
1178{1196{
1179 TFModel *tf_model = model->model;1197 TFModel *tf_model = model->model;
1180 TFContext *ctx = &tf_model->ctx;1198 TFContext *ctx = &tf_model->ctx;
1181 TFRequestItem *request;1199 TFRequestItem *request;
1182 DNNReturnType ret;1200 int ret;
1183 1201
1184 if (ff_queue_size(tf_model->lltask_queue) == 0) {1202 if (ff_queue_size(tf_model->lltask_queue) == 0) {
1185 // no pending task need to flush1203 // no pending task need to flush
@@ -1189,7 +1207,7 @@ DNNReturnType ff_dnn_flush_tf(const DNNModel *model) · 3 unmodified lines
1189 request = ff_safe_queue_pop_front(tf_model->request_queue);1207 request = ff_safe_queue_pop_front(tf_model->request_queue);
1190 if (!request) {1208 if (!request) {
1191 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");1209 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
1192 return DNN_ERROR;1210 return AVERROR(EINVAL);
1193 }1211 }
1194 1212
1195 ret = fill_model_input_tf(tf_model, request);1213 ret = fill_model_input_tf(tf_model, request);
libavfilter/dnn/dnn_backend_tf.h+2−2
@@ -31,9 +31,9 @@ · 30 unmodified lines
31 31
32DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx);32DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx);
33 33
34DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params);34int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params);
35DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out);35DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out);
36DNNReturnType ff_dnn_flush_tf(const DNNModel *model);36int ff_dnn_flush_tf(const DNNModel *model);
37 37
38void ff_dnn_free_model_tf(DNNModel **model);38void ff_dnn_free_model_tf(DNNModel **model);
39 39

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