History

lavfi/dnn_backend_tf: Separate function for Completion Callback

This commit rearranges the existing code to create a separate function for the completion callback in execute_model_tf.

Shubhanshu Saxena committed 5 years ago

Commit 84e4e601 parent b8492281 file · +61 −48Browse files

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

Unified · Split

libavfilter/dnn/dnn_backend_tf.c+61−48
@@ -915,6 +915,65 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque · 914 unmodified lines
915 return DNN_SUCCESS;915 return DNN_SUCCESS;
916}916}
917 917
918static void infer_completion_callback(void *args) {
919 TFRequestItem *request = args;
920 InferenceItem *inference = request->inference;
921 TaskItem *task = inference->task;
922 DNNData *outputs;
923 TFInferRequest *infer_request = request->infer_request;
924 TFModel *tf_model = task->model;
925 TFContext *ctx = &tf_model->ctx;
926
927 outputs = av_malloc_array(task->nb_output, sizeof(*outputs));
928 if (!outputs) {
929 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for *outputs\n");
930 goto err;
931 }
932
933 for (uint32_t i = 0; i < task->nb_output; ++i) {
934 outputs[i].height = TF_Dim(infer_request->output_tensors[i], 1);
935 outputs[i].width = TF_Dim(infer_request->output_tensors[i], 2);
936 outputs[i].channels = TF_Dim(infer_request->output_tensors[i], 3);
937 outputs[i].data = TF_TensorData(infer_request->output_tensors[i]);
938 outputs[i].dt = TF_TensorType(infer_request->output_tensors[i]);
939 }
940 switch (tf_model->model->func_type) {
941 case DFT_PROCESS_FRAME:
942 //it only support 1 output if it's frame in & frame out
943 if (task->do_ioproc) {
944 if (tf_model->model->frame_post_proc != NULL) {
945 tf_model->model->frame_post_proc(task->out_frame, outputs, tf_model->model->filter_ctx);
946 } else {
947 ff_proc_from_dnn_to_frame(task->out_frame, outputs, ctx);
948 }
949 } else {
950 task->out_frame->width = outputs[0].width;
951 task->out_frame->height = outputs[0].height;
952 }
953 break;
954 case DFT_ANALYTICS_DETECT:
955 if (!tf_model->model->detect_post_proc) {
956 av_log(ctx, AV_LOG_ERROR, "Detect filter needs provide post proc\n");
957 return;
958 }
959 tf_model->model->detect_post_proc(task->out_frame, outputs, task->nb_output, tf_model->model->filter_ctx);
960 break;
961 default:
962 av_log(ctx, AV_LOG_ERROR, "Tensorflow backend does not support this kind of dnn filter now\n");
963 goto err;
964 }
965 task->inference_done++;
966err:
967 tf_free_request(infer_request);
968 av_freep(&outputs);
969
970 if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) {
971 av_freep(&request->infer_request);
972 av_freep(&request);
973 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
974 }
975}
976
918static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *inference_queue)977static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *inference_queue)
919{978{
920 TFModel *tf_model;979 TFModel *tf_model;
@@ -922,7 +981,6 @@ static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *inference_q · 1 unmodified line
922 TFInferRequest *infer_request;981 TFInferRequest *infer_request;
923 InferenceItem *inference;982 InferenceItem *inference;
924 TaskItem *task;983 TaskItem *task;
925 DNNData *outputs;
926 984
927 inference = ff_queue_peek_front(inference_queue);985 inference = ff_queue_peek_front(inference_queue);
928 task = inference->task;986 task = inference->task;
@@ -944,56 +1002,11 @@ static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *inference_q · 15 unmodified lines
944 task->nb_output, NULL, 0, NULL,1002 task->nb_output, NULL, 0, NULL,
945 tf_model->status);1003 tf_model->status);
946 if (TF_GetCode(tf_model->status) != TF_OK) {1004 if (TF_GetCode(tf_model->status) != TF_OK) {
947 tf_free_request(infer_request);
948 av_log(ctx, AV_LOG_ERROR, "Failed to run session when executing model\n");
949 return DNN_ERROR;
950 }
951
952 outputs = av_malloc_array(task->nb_output, sizeof(*outputs));
953 if (!outputs) {
954 tf_free_request(infer_request);
955 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for *outputs\n");
956 return DNN_ERROR;
957 }
958
959 for (uint32_t i = 0; i < task->nb_output; ++i) {
960 outputs[i].height = TF_Dim(infer_request->output_tensors[i], 1);
961 outputs[i].width = TF_Dim(infer_request->output_tensors[i], 2);
962 outputs[i].channels = TF_Dim(infer_request->output_tensors[i], 3);
963 outputs[i].data = TF_TensorData(infer_request->output_tensors[i]);
964 outputs[i].dt = TF_TensorType(infer_request->output_tensors[i]);
965 }
966 switch (tf_model->model->func_type) {
967 case DFT_PROCESS_FRAME:
968 //it only support 1 output if it's frame in & frame out
969 if (task->do_ioproc) {
970 if (tf_model->model->frame_post_proc != NULL) {
971 tf_model->model->frame_post_proc(task->out_frame, outputs, tf_model->model->filter_ctx);
972 } else {
973 ff_proc_from_dnn_to_frame(task->out_frame, outputs, ctx);
974 }
975 } else {
976 task->out_frame->width = outputs[0].width;
977 task->out_frame->height = outputs[0].height;
978 }
979 break;
980 case DFT_ANALYTICS_DETECT:
981 if (!tf_model->model->detect_post_proc) {
982 av_log(ctx, AV_LOG_ERROR, "Detect filter needs provide post proc\n");
983 return DNN_ERROR;
984 }
985 tf_model->model->detect_post_proc(task->out_frame, outputs, task->nb_output, tf_model->model->filter_ctx);
986 break;
987 default:
988 tf_free_request(infer_request);1005 tf_free_request(infer_request);
989 1006 av_log(ctx, AV_LOG_ERROR, "Failed to run session when executing model\n");
990 av_log(ctx, AV_LOG_ERROR, "Tensorflow backend does not support this kind of dnn filter now\n");
991 return DNN_ERROR;1007 return DNN_ERROR;
992 }1008 }
993 task->inference_done++;1009 infer_completion_callback(request);
994 tf_free_request(infer_request);
995 av_freep(&outputs);
996 ff_safe_queue_push_back(tf_model->request_queue, request);
997 return (task->inference_done == task->inference_todo) ? DNN_SUCCESS : DNN_ERROR;1010 return (task->inference_done == task->inference_todo) ? DNN_SUCCESS : DNN_ERROR;
998 }1011 }
999}1012}

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