History

lavfi/dnn_backend_tf: Separate function for filling RequestItem

This commit rearranges the existing code to create separate function for filling request with execution data.

Shubhanshu Saxena committed 5 years ago

Commit b8492281 parent 08d8b3b1 file · +80 −57Browse files

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

Unified · Split

libavfilter/dnn/dnn_backend_tf.c+80−57
@@ -839,20 +839,16 @@ DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_ · 838 unmodified lines
839839 return model;
840840 }
841841
842−static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *inference_queue)
843−{
844− TFModel *tf_model;
845− TFContext *ctx;
846− TFInferRequest *infer_request;
842+static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *request) {
843+ DNNData input;
847844 InferenceItem *inference;
848845 TaskItem *task;
849− DNNData input, *outputs;
846+ TFInferRequest *infer_request;
847+ TFContext *ctx = &tf_model->ctx;
850848
851− inference = ff_queue_pop_front(inference_queue);
849+ inference = ff_queue_pop_front(tf_model->inference_queue);
852850 av_assert0(inference);
853851 task = inference->task;
854− tf_model = task->model;
855− ctx = &tf_model->ctx;
856852 request->inference = inference;
857853
858854 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
916912 infer_request->tf_outputs[i].index = 0;
917913 }
918914
919− TF_SessionRun(tf_model->session, NULL,
920− infer_request->tf_input, &infer_request->input_tensor, 1,
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− }
915+ return DNN_SUCCESS;
916+}
929917
930− outputs = av_malloc_array(task->nb_output, sizeof(*outputs));
931− if (!outputs) {
932− tf_free_request(infer_request);
933− av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for *outputs\n");
918+static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *inference_queue)
919+{
920+ TFModel *tf_model;
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");
934934 return DNN_ERROR;
935− }
935+ } else {
936+ if (fill_model_input_tf(tf_model, request) != DNN_SUCCESS) {
937+ return DNN_ERROR;
938+ }
936939
937− for (uint32_t i = 0; i < task->nb_output; ++i) {
938− outputs[i].height = TF_Dim(infer_request->output_tensors[i], 1);
939− outputs[i].width = TF_Dim(infer_request->output_tensors[i], 2);
940− outputs[i].channels = TF_Dim(infer_request->output_tensors[i], 3);
941− outputs[i].data = TF_TensorData(infer_request->output_tensors[i]);
942− outputs[i].dt = TF_TensorType(infer_request->output_tensors[i]);
943− }
944− switch (tf_model->model->func_type) {
945− case DFT_PROCESS_FRAME:
946− //it only support 1 output if it's frame in & frame out
947− if (task->do_ioproc) {
948− if (tf_model->model->frame_post_proc != NULL) {
949− tf_model->model->frame_post_proc(task->out_frame, outputs, tf_model->model->filter_ctx);
940+ infer_request = request->infer_request;
941+ TF_SessionRun(tf_model->session, NULL,
942+ infer_request->tf_input, &infer_request->input_tensor, 1,
943+ infer_request->tf_outputs, infer_request->output_tensors,
944+ task->nb_output, NULL, 0, NULL,
945+ tf_model->status);
946+ 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+ }
950975 } 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;
952978 }
953− } else {
954− task->out_frame->width = outputs[0].width;
955− task->out_frame->height = outputs[0].height;
956− }
957− break;
958− case DFT_ANALYTICS_DETECT:
959− if (!tf_model->model->detect_post_proc) {
960− av_log(ctx, AV_LOG_ERROR, "Detect filter needs provide post proc\n");
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);
989+
990+ av_log(ctx, AV_LOG_ERROR, "Tensorflow backend does not support this kind of dnn filter now\n");
961991 return DNN_ERROR;
962992 }
963− tf_model->model->detect_post_proc(task->out_frame, outputs, task->nb_output, tf_model->model->filter_ctx);
964− break;
965− default:
993+ task->inference_done++;
966994 tf_free_request(infer_request);
967−
968− av_log(ctx, AV_LOG_ERROR, "Tensorflow backend does not support this kind of dnn filter now\n");
969− return DNN_ERROR;
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;
970998 }
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;
976999 }
9771000
9781001 DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params)

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