This commit rearranges the existing code to create separate function for filling request with execution data.
Unified · Split
| @@ -839,20 +839,16 @@ DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_ · 838 unmodified lines | |||
| 839 | return model; | 839 | return model; |
| 840 | } | 840 | } |
| 841 | 841 | ||
| 842 | static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *inference_queue) | 842 | static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *request) { |
| 843 | { | 843 | DNNData input; |
| 844 | TFModel *tf_model; | ||
| 845 | TFContext *ctx; | ||
| 846 | TFInferRequest *infer_request; | ||
| 847 | InferenceItem *inference; | 844 | InferenceItem *inference; |
| 848 | TaskItem *task; | 845 | TaskItem *task; |
| 849 | DNNData input, *outputs; | 846 | TFInferRequest *infer_request; |
| 847 | TFContext *ctx = &tf_model->ctx; | ||
| 850 | 848 | ||
| 851 | inference = ff_queue_pop_front(inference_queue); | 849 | inference = ff_queue_pop_front(tf_model->inference_queue); |
| 852 | av_assert0(inference); | 850 | av_assert0(inference); |
| 853 | task = inference->task; | 851 | task = inference->task; |
| 854 | tf_model = task->model; | ||
| 855 | ctx = &tf_model->ctx; | ||
| 856 | request->inference = inference; | 852 | request->inference = inference; |
| 857 | 853 | ||
| 858 | if (get_input_tf(tf_model, &input, task->input_name) != DNN_SUCCESS) | 854 | if (get_input_tf(tf_model, &input, task->input_name) != DNN_SUCCESS) |
| @@ -916,63 +912,90 @@ static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *inference_q · 57 unmodified lines | |||
| 916 | infer_request->tf_outputs[i].index = 0; | 912 | infer_request->tf_outputs[i].index = 0; |
| 917 | } | 913 | } |
| 918 | 914 | ||
| 919 | TF_SessionRun(tf_model->session, NULL, | 915 | return DNN_SUCCESS; |
| 920 | infer_request->tf_input, &infer_request->input_tensor, 1, | 916 | } |
| 921 | infer_request->tf_outputs, infer_request->output_tensors, | ||
| 922 | task->nb_output, NULL, 0, NULL, | ||
| 923 | tf_model->status); | ||
| 924 | if (TF_GetCode(tf_model->status) != TF_OK) { | ||
| 925 | tf_free_request(infer_request); | ||
| 926 | av_log(ctx, AV_LOG_ERROR, "Failed to run session when executing model\n"); | ||
| 927 | return DNN_ERROR; | ||
| 928 | } | ||
| 929 | 917 | ||
| 930 | outputs = av_malloc_array(task->nb_output, sizeof(*outputs)); | 918 | static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *inference_queue) |
| 931 | if (!outputs) { | 919 | { |
| 932 | tf_free_request(infer_request); | 920 | TFModel *tf_model; |
| 933 | av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for *outputs\n"); | 921 | TFContext *ctx; |
| 922 | TFInferRequest *infer_request; | ||
| 923 | InferenceItem *inference; | ||
| 924 | TaskItem *task; | ||
| 925 | DNNData *outputs; | ||
| 926 | |||
| 927 | inference = ff_queue_peek_front(inference_queue); | ||
| 928 | task = inference->task; | ||
| 929 | tf_model = task->model; | ||
| 930 | ctx = &tf_model->ctx; | ||
| 931 | |||
| 932 | if (task->async) { | ||
| 933 | avpriv_report_missing_feature(ctx, "Async execution not supported"); | ||
| 934 | return DNN_ERROR; | 934 | return DNN_ERROR; |
| 935 | } | 935 | } else { |
| 936 | if (fill_model_input_tf(tf_model, request) != DNN_SUCCESS) { | ||
| 937 | return DNN_ERROR; | ||
| 938 | } | ||
| 936 | 939 | ||
| 937 | for (uint32_t i = 0; i < task->nb_output; ++i) { | 940 | infer_request = request->infer_request; |
| 938 | outputs[i].height = TF_Dim(infer_request->output_tensors[i], 1); | 941 | TF_SessionRun(tf_model->session, NULL, |
| 939 | outputs[i].width = TF_Dim(infer_request->output_tensors[i], 2); | 942 | infer_request->tf_input, &infer_request->input_tensor, 1, |
| 940 | outputs[i].channels = TF_Dim(infer_request->output_tensors[i], 3); | 943 | infer_request->tf_outputs, infer_request->output_tensors, |
| 941 | outputs[i].data = TF_TensorData(infer_request->output_tensors[i]); | 944 | task->nb_output, NULL, 0, NULL, |
| 942 | outputs[i].dt = TF_TensorType(infer_request->output_tensors[i]); | 945 | tf_model->status); |
| 943 | } | 946 | if (TF_GetCode(tf_model->status) != TF_OK) { |
| 944 | switch (tf_model->model->func_type) { | 947 | tf_free_request(infer_request); |
| 945 | case DFT_PROCESS_FRAME: | 948 | av_log(ctx, AV_LOG_ERROR, "Failed to run session when executing model\n"); |
| 946 | //it only support 1 output if it's frame in & frame out | 949 | return DNN_ERROR; |
| 947 | if (task->do_ioproc) { | 950 | } |
| 948 | if (tf_model->model->frame_post_proc != NULL) { | 951 | |
| 949 | tf_model->model->frame_post_proc(task->out_frame, outputs, tf_model->model->filter_ctx); | 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 | } | ||
| 950 | } else { | 975 | } else { |
| 951 | ff_proc_from_dnn_to_frame(task->out_frame, outputs, ctx); | 976 | task->out_frame->width = outputs[0].width; |
| 977 | task->out_frame->height = outputs[0].height; | ||
| 952 | } | 978 | } |
| 953 | } else { | 979 | break; |
| 954 | task->out_frame->width = outputs[0].width; | 980 | case DFT_ANALYTICS_DETECT: |
| 955 | task->out_frame->height = outputs[0].height; | 981 | if (!tf_model->model->detect_post_proc) { |
| 956 | } | 982 | av_log(ctx, AV_LOG_ERROR, "Detect filter needs provide post proc\n"); |
| 957 | break; | 983 | return DNN_ERROR; |
| 958 | case DFT_ANALYTICS_DETECT: | 984 | } |
| 959 | if (!tf_model->model->detect_post_proc) { | 985 | tf_model->model->detect_post_proc(task->out_frame, outputs, task->nb_output, tf_model->model->filter_ctx); |
| 960 | av_log(ctx, AV_LOG_ERROR, "Detect filter needs provide post proc\n"); | 986 | break; |
| 987 | default: | ||
| 988 | tf_free_request(infer_request); | ||
| 989 | |||
| 990 | av_log(ctx, AV_LOG_ERROR, "Tensorflow backend does not support this kind of dnn filter now\n"); | ||
| 961 | return DNN_ERROR; | 991 | return DNN_ERROR; |
| 962 | } | 992 | } |
| 963 | tf_model->model->detect_post_proc(task->out_frame, outputs, task->nb_output, tf_model->model->filter_ctx); | 993 | task->inference_done++; |
| 964 | break; | ||
| 965 | default: | ||
| 966 | tf_free_request(infer_request); | 994 | tf_free_request(infer_request); |
| 967 | 995 | av_freep(&outputs); | |
| 968 | av_log(ctx, AV_LOG_ERROR, "Tensorflow backend does not support this kind of dnn filter now\n"); | 996 | ff_safe_queue_push_back(tf_model->request_queue, request); |
| 969 | return DNN_ERROR; | 997 | return (task->inference_done == task->inference_todo) ? DNN_SUCCESS : DNN_ERROR; |
| 970 | } | 998 | } |
| 971 | task->inference_done++; | ||
| 972 | tf_free_request(infer_request); | ||
| 973 | av_freep(&outputs); | ||
| 974 | ff_safe_queue_push_back(tf_model->request_queue, request); | ||
| 975 | return (task->inference_done == task->inference_todo) ? DNN_SUCCESS : DNN_ERROR; | ||
| 976 | } | 999 | } |
| 977 | 1000 | ||
| 978 | DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params) | 1001 | DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params) |