2 * Copyright (c) 2018 Sergey Lavrushkin
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * DNN tensorflow backend implementation.
26 #include "dnn_backend_tf.h"
27 #include "dnn_backend_native.h"
28 #include "dnn_backend_native_layer_conv2d.h"
29 #include "dnn_backend_native_layer_depth2space.h"
30 #include "libavformat/avio.h"
31 #include "libavutil/avassert.h"
32 #include "../internal.h"
33 #include "dnn_backend_native_layer_pad.h"
34 #include "dnn_backend_native_layer_maximum.h"
35 #include "dnn_io_proc.h"
37 #include <tensorflow/c/c_api.h>
39 typedef struct TFOptions{
43 typedef struct TFContext {
48 typedef struct TFModel{
56 #define OFFSET(x) offsetof(TFContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
58 static const AVOption dnn_tensorflow_options[] = {
59 { "sess_config", "config for SessionOptions", OFFSET(options.sess_config), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
63 AVFILTER_DEFINE_CLASS(dnn_tensorflow);
65 static DNNReturnType execute_model_tf(const DNNModel *model, const char *input_name, AVFrame *in_frame,
66 const char **output_names, uint32_t nb_output, AVFrame *out_frame,
69 static void free_buffer(void *data, size_t length)
74 static TF_Buffer *read_graph(const char *model_filename)
77 unsigned char *graph_data = NULL;
78 AVIOContext *model_file_context;
79 long size, bytes_read;
81 if (avio_open(&model_file_context, model_filename, AVIO_FLAG_READ) < 0){
85 size = avio_size(model_file_context);
87 graph_data = av_malloc(size);
89 avio_closep(&model_file_context);
92 bytes_read = avio_read(model_file_context, graph_data, size);
93 avio_closep(&model_file_context);
94 if (bytes_read != size){
95 av_freep(&graph_data);
99 graph_buf = TF_NewBuffer();
100 graph_buf->data = graph_data;
101 graph_buf->length = size;
102 graph_buf->data_deallocator = free_buffer;
107 static TF_Tensor *allocate_input_tensor(const DNNData *input)
111 int64_t input_dims[] = {1, input->height, input->width, input->channels};
115 size = sizeof(float);
122 av_assert0(!"should not reach here");
125 return TF_AllocateTensor(dt, input_dims, 4,
126 input_dims[1] * input_dims[2] * input_dims[3] * size);
129 static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input_name)
131 TFModel *tf_model = model;
132 TFContext *ctx = &tf_model->ctx;
137 tf_output.oper = TF_GraphOperationByName(tf_model->graph, input_name);
138 if (!tf_output.oper) {
139 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name);
144 input->dt = TF_OperationOutputType(tf_output);
146 status = TF_NewStatus();
147 TF_GraphGetTensorShape(tf_model->graph, tf_output, dims, 4, status);
148 if (TF_GetCode(status) != TF_OK){
149 TF_DeleteStatus(status);
150 av_log(ctx, AV_LOG_ERROR, "Failed to get input tensor shape: number of dimension incorrect\n");
153 TF_DeleteStatus(status);
155 // currently only NHWC is supported
156 av_assert0(dims[0] == 1);
157 input->height = dims[1];
158 input->width = dims[2];
159 input->channels = dims[3];
164 static DNNReturnType get_output_tf(void *model, const char *input_name, int input_width, int input_height,
165 const char *output_name, int *output_width, int *output_height)
168 TFModel *tf_model = model;
169 TFContext *ctx = &tf_model->ctx;
170 AVFrame *in_frame = av_frame_alloc();
171 AVFrame *out_frame = NULL;
174 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
178 out_frame = av_frame_alloc();
180 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
181 av_frame_free(&in_frame);
185 in_frame->width = input_width;
186 in_frame->height = input_height;
188 ret = execute_model_tf(tf_model->model, input_name, in_frame, &output_name, 1, out_frame, 0);
189 *output_width = out_frame->width;
190 *output_height = out_frame->height;
192 av_frame_free(&out_frame);
193 av_frame_free(&in_frame);
197 static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename)
199 TFContext *ctx = &tf_model->ctx;
200 TF_Buffer *graph_def;
201 TF_ImportGraphDefOptions *graph_opts;
202 TF_SessionOptions *sess_opts;
203 const TF_Operation *init_op;
204 uint8_t *sess_config = NULL;
205 int sess_config_length = 0;
207 // prepare the sess config data
208 if (tf_model->ctx.options.sess_config != NULL) {
210 tf_model->ctx.options.sess_config is hex to present the serialized proto
211 required by TF_SetConfig below, so we need to first generate the serialized
212 proto in a python script, the following is a script example to generate
213 serialized proto which specifies one GPU, we can change the script to add
216 import tensorflow as tf
217 gpu_options = tf.GPUOptions(visible_device_list='0')
218 config = tf.ConfigProto(gpu_options=gpu_options)
219 s = config.SerializeToString()
220 b = ''.join("%02x" % int(ord(b)) for b in s[::-1])
223 the script output looks like: 0xab...cd, and then pass 0xab...cd to sess_config.
228 if (strncmp(tf_model->ctx.options.sess_config, "0x", 2) != 0) {
229 av_log(ctx, AV_LOG_ERROR, "sess_config should start with '0x'\n");
233 sess_config_length = strlen(tf_model->ctx.options.sess_config);
234 if (sess_config_length % 2 != 0) {
235 av_log(ctx, AV_LOG_ERROR, "the length of sess_config is not even (%s), "
236 "please re-generate the config.\n",
237 tf_model->ctx.options.sess_config);
241 sess_config_length -= 2; //ignore the first '0x'
242 sess_config_length /= 2; //get the data length in byte
244 sess_config = av_malloc(sess_config_length);
246 av_log(ctx, AV_LOG_ERROR, "failed to allocate memory\n");
250 for (int i = 0; i < sess_config_length; i++) {
251 int index = 2 + (sess_config_length - 1 - i) * 2;
252 tmp[0] = tf_model->ctx.options.sess_config[index];
253 tmp[1] = tf_model->ctx.options.sess_config[index + 1];
254 sess_config[i] = strtol(tmp, NULL, 16);
258 graph_def = read_graph(model_filename);
260 av_log(ctx, AV_LOG_ERROR, "Failed to read model \"%s\" graph\n", model_filename);
261 av_freep(&sess_config);
264 tf_model->graph = TF_NewGraph();
265 tf_model->status = TF_NewStatus();
266 graph_opts = TF_NewImportGraphDefOptions();
267 TF_GraphImportGraphDef(tf_model->graph, graph_def, graph_opts, tf_model->status);
268 TF_DeleteImportGraphDefOptions(graph_opts);
269 TF_DeleteBuffer(graph_def);
270 if (TF_GetCode(tf_model->status) != TF_OK){
271 TF_DeleteGraph(tf_model->graph);
272 TF_DeleteStatus(tf_model->status);
273 av_log(ctx, AV_LOG_ERROR, "Failed to import serialized graph to model graph\n");
274 av_freep(&sess_config);
278 init_op = TF_GraphOperationByName(tf_model->graph, "init");
279 sess_opts = TF_NewSessionOptions();
282 TF_SetConfig(sess_opts, sess_config, sess_config_length,tf_model->status);
283 av_freep(&sess_config);
284 if (TF_GetCode(tf_model->status) != TF_OK) {
285 TF_DeleteGraph(tf_model->graph);
286 TF_DeleteStatus(tf_model->status);
287 TF_DeleteSessionOptions(sess_opts);
288 av_log(ctx, AV_LOG_ERROR, "Failed to set config for sess options with %s\n",
289 tf_model->ctx.options.sess_config);
294 tf_model->session = TF_NewSession(tf_model->graph, sess_opts, tf_model->status);
295 TF_DeleteSessionOptions(sess_opts);
296 if (TF_GetCode(tf_model->status) != TF_OK)
298 TF_DeleteGraph(tf_model->graph);
299 TF_DeleteStatus(tf_model->status);
300 av_log(ctx, AV_LOG_ERROR, "Failed to create new session with model graph\n");
304 // Run initialization operation with name "init" if it is present in graph
306 TF_SessionRun(tf_model->session, NULL,
309 &init_op, 1, NULL, tf_model->status);
310 if (TF_GetCode(tf_model->status) != TF_OK)
312 TF_DeleteSession(tf_model->session, tf_model->status);
313 TF_DeleteGraph(tf_model->graph);
314 TF_DeleteStatus(tf_model->status);
315 av_log(ctx, AV_LOG_ERROR, "Failed to run session when initializing\n");
323 #define NAME_BUFFER_SIZE 256
325 static DNNReturnType add_conv_layer(TFModel *tf_model, TF_Operation *transpose_op, TF_Operation **cur_op,
326 ConvolutionalParams* params, const int layer)
328 TFContext *ctx = &tf_model->ctx;
330 TF_OperationDescription *op_desc;
332 int64_t strides[] = {1, 1, 1, 1};
333 TF_Tensor *kernel_tensor = NULL, *biases_tensor = NULL;
336 char name_buffer[NAME_BUFFER_SIZE];
339 size = params->input_num * params->output_num * params->kernel_size * params->kernel_size;
342 snprintf(name_buffer, NAME_BUFFER_SIZE, "conv_kernel%d", layer);
343 op_desc = TF_NewOperation(tf_model->graph, "Const", name_buffer);
344 TF_SetAttrType(op_desc, "dtype", TF_FLOAT);
345 dims[0] = params->output_num;
346 dims[1] = params->kernel_size;
347 dims[2] = params->kernel_size;
348 dims[3] = params->input_num;
350 kernel_tensor = TF_AllocateTensor(TF_FLOAT, dims, dims_len, size * sizeof(float));
351 memcpy(TF_TensorData(kernel_tensor), params->kernel, size * sizeof(float));
352 TF_SetAttrTensor(op_desc, "value", kernel_tensor, tf_model->status);
353 if (TF_GetCode(tf_model->status) != TF_OK){
356 op = TF_FinishOperation(op_desc, tf_model->status);
357 if (TF_GetCode(tf_model->status) != TF_OK){
361 snprintf(name_buffer, NAME_BUFFER_SIZE, "transpose%d", layer);
362 op_desc = TF_NewOperation(tf_model->graph, "Transpose", name_buffer);
364 TF_AddInput(op_desc, input);
365 input.oper = transpose_op;
366 TF_AddInput(op_desc, input);
367 TF_SetAttrType(op_desc, "T", TF_FLOAT);
368 TF_SetAttrType(op_desc, "Tperm", TF_INT32);
369 op = TF_FinishOperation(op_desc, tf_model->status);
370 if (TF_GetCode(tf_model->status) != TF_OK){
374 snprintf(name_buffer, NAME_BUFFER_SIZE, "conv2d%d", layer);
375 op_desc = TF_NewOperation(tf_model->graph, "Conv2D", name_buffer);
376 input.oper = *cur_op;
377 TF_AddInput(op_desc, input);
379 TF_AddInput(op_desc, input);
380 TF_SetAttrType(op_desc, "T", TF_FLOAT);
381 TF_SetAttrIntList(op_desc, "strides", strides, 4);
382 TF_SetAttrString(op_desc, "padding", "VALID", 5);
383 *cur_op = TF_FinishOperation(op_desc, tf_model->status);
384 if (TF_GetCode(tf_model->status) != TF_OK){
388 snprintf(name_buffer, NAME_BUFFER_SIZE, "conv_biases%d", layer);
389 op_desc = TF_NewOperation(tf_model->graph, "Const", name_buffer);
390 TF_SetAttrType(op_desc, "dtype", TF_FLOAT);
391 dims[0] = params->output_num;
393 biases_tensor = TF_AllocateTensor(TF_FLOAT, dims, dims_len, params->output_num * sizeof(float));
394 memcpy(TF_TensorData(biases_tensor), params->biases, params->output_num * sizeof(float));
395 TF_SetAttrTensor(op_desc, "value", biases_tensor, tf_model->status);
396 if (TF_GetCode(tf_model->status) != TF_OK){
399 op = TF_FinishOperation(op_desc, tf_model->status);
400 if (TF_GetCode(tf_model->status) != TF_OK){
404 snprintf(name_buffer, NAME_BUFFER_SIZE, "bias_add%d", layer);
405 op_desc = TF_NewOperation(tf_model->graph, "BiasAdd", name_buffer);
406 input.oper = *cur_op;
407 TF_AddInput(op_desc, input);
409 TF_AddInput(op_desc, input);
410 TF_SetAttrType(op_desc, "T", TF_FLOAT);
411 *cur_op = TF_FinishOperation(op_desc, tf_model->status);
412 if (TF_GetCode(tf_model->status) != TF_OK){
416 snprintf(name_buffer, NAME_BUFFER_SIZE, "activation%d", layer);
417 switch (params->activation){
419 op_desc = TF_NewOperation(tf_model->graph, "Relu", name_buffer);
422 op_desc = TF_NewOperation(tf_model->graph, "Tanh", name_buffer);
425 op_desc = TF_NewOperation(tf_model->graph, "Sigmoid", name_buffer);
428 avpriv_report_missing_feature(ctx, "convolutional activation function %d", params->activation);
431 input.oper = *cur_op;
432 TF_AddInput(op_desc, input);
433 TF_SetAttrType(op_desc, "T", TF_FLOAT);
434 *cur_op = TF_FinishOperation(op_desc, tf_model->status);
435 if (TF_GetCode(tf_model->status) != TF_OK){
441 TF_DeleteTensor(kernel_tensor);
442 TF_DeleteTensor(biases_tensor);
443 av_log(ctx, AV_LOG_ERROR, "Failed to add conv layer %d\n", layer);
447 static DNNReturnType add_depth_to_space_layer(TFModel *tf_model, TF_Operation **cur_op,
448 DepthToSpaceParams *params, const int layer)
450 TFContext *ctx = &tf_model->ctx;
451 TF_OperationDescription *op_desc;
453 char name_buffer[NAME_BUFFER_SIZE];
455 snprintf(name_buffer, NAME_BUFFER_SIZE, "depth_to_space%d", layer);
456 op_desc = TF_NewOperation(tf_model->graph, "DepthToSpace", name_buffer);
457 input.oper = *cur_op;
459 TF_AddInput(op_desc, input);
460 TF_SetAttrType(op_desc, "T", TF_FLOAT);
461 TF_SetAttrInt(op_desc, "block_size", params->block_size);
462 *cur_op = TF_FinishOperation(op_desc, tf_model->status);
463 if (TF_GetCode(tf_model->status) != TF_OK){
464 av_log(ctx, AV_LOG_ERROR, "Failed to add depth_to_space to layer %d\n", layer);
471 static DNNReturnType add_pad_layer(TFModel *tf_model, TF_Operation **cur_op,
472 LayerPadParams *params, const int layer)
474 TFContext *ctx = &tf_model->ctx;
477 TF_OperationDescription *op_desc;
480 int64_t pads_shape[] = {4, 2};
482 char name_buffer[NAME_BUFFER_SIZE];
483 snprintf(name_buffer, NAME_BUFFER_SIZE, "pad%d", layer);
485 op_desc = TF_NewOperation(tf_model->graph, "Const", name_buffer);
486 TF_SetAttrType(op_desc, "dtype", TF_INT32);
487 tensor = TF_AllocateTensor(TF_INT32, pads_shape, 2, 4 * 2 * sizeof(int32_t));
488 pads = (int32_t *)TF_TensorData(tensor);
489 pads[0] = params->paddings[0][0];
490 pads[1] = params->paddings[0][1];
491 pads[2] = params->paddings[1][0];
492 pads[3] = params->paddings[1][1];
493 pads[4] = params->paddings[2][0];
494 pads[5] = params->paddings[2][1];
495 pads[6] = params->paddings[3][0];
496 pads[7] = params->paddings[3][1];
497 TF_SetAttrTensor(op_desc, "value", tensor, tf_model->status);
498 if (TF_GetCode(tf_model->status) != TF_OK){
499 TF_DeleteTensor(tensor);
500 av_log(ctx, AV_LOG_ERROR, "Failed to set value for pad of layer %d\n", layer);
503 op = TF_FinishOperation(op_desc, tf_model->status);
504 if (TF_GetCode(tf_model->status) != TF_OK){
505 TF_DeleteTensor(tensor);
506 av_log(ctx, AV_LOG_ERROR, "Failed to add pad to layer %d\n", layer);
510 op_desc = TF_NewOperation(tf_model->graph, "MirrorPad", "mirror_pad");
511 input.oper = *cur_op;
513 TF_AddInput(op_desc, input);
515 TF_AddInput(op_desc, input);
516 TF_SetAttrType(op_desc, "T", TF_FLOAT);
517 TF_SetAttrType(op_desc, "Tpaddings", TF_INT32);
518 TF_SetAttrString(op_desc, "mode", "SYMMETRIC", 9);
519 *cur_op = TF_FinishOperation(op_desc, tf_model->status);
520 if (TF_GetCode(tf_model->status) != TF_OK){
521 TF_DeleteTensor(tensor);
522 av_log(ctx, AV_LOG_ERROR, "Failed to add mirror_pad to layer %d\n", layer);
529 static DNNReturnType add_maximum_layer(TFModel *tf_model, TF_Operation **cur_op,
530 DnnLayerMaximumParams *params, const int layer)
532 TFContext *ctx = &tf_model->ctx;
535 TF_OperationDescription *op_desc;
539 char name_buffer[NAME_BUFFER_SIZE];
540 snprintf(name_buffer, NAME_BUFFER_SIZE, "maximum/y%d", layer);
542 op_desc = TF_NewOperation(tf_model->graph, "Const", name_buffer);
543 TF_SetAttrType(op_desc, "dtype", TF_FLOAT);
544 tensor = TF_AllocateTensor(TF_FLOAT, NULL, 0, TF_DataTypeSize(TF_FLOAT));
545 y = (float *)TF_TensorData(tensor);
547 TF_SetAttrTensor(op_desc, "value", tensor, tf_model->status);
548 if (TF_GetCode(tf_model->status) != TF_OK){
549 TF_DeleteTensor(tensor);
550 av_log(ctx, AV_LOG_ERROR, "Failed to set value for maximum/y of layer %d", layer);
553 op = TF_FinishOperation(op_desc, tf_model->status);
554 if (TF_GetCode(tf_model->status) != TF_OK){
555 TF_DeleteTensor(tensor);
556 av_log(ctx, AV_LOG_ERROR, "Failed to add maximum/y to layer %d\n", layer);
560 snprintf(name_buffer, NAME_BUFFER_SIZE, "maximum%d", layer);
561 op_desc = TF_NewOperation(tf_model->graph, "Maximum", name_buffer);
562 input.oper = *cur_op;
564 TF_AddInput(op_desc, input);
566 TF_AddInput(op_desc, input);
567 TF_SetAttrType(op_desc, "T", TF_FLOAT);
568 *cur_op = TF_FinishOperation(op_desc, tf_model->status);
569 if (TF_GetCode(tf_model->status) != TF_OK){
570 TF_DeleteTensor(tensor);
571 av_log(ctx, AV_LOG_ERROR, "Failed to add maximum to layer %d\n", layer);
578 static DNNReturnType load_native_model(TFModel *tf_model, const char *model_filename)
580 TFContext *ctx = &tf_model->ctx;
582 TF_OperationDescription *op_desc;
584 TF_Operation *transpose_op;
585 TF_Tensor *tensor = NULL;
587 int32_t *transpose_perm;
588 int64_t transpose_perm_shape[] = {4};
589 int64_t input_shape[] = {1, -1, -1, -1};
590 DNNReturnType layer_add_res;
591 DNNModel *model = NULL;
592 NativeModel *native_model;
594 model = ff_dnn_load_model_native(model_filename, DFT_PROCESS_FRAME, NULL, NULL);
596 av_log(ctx, AV_LOG_ERROR, "Failed to load native model\n");
600 native_model = model->model;
601 tf_model->graph = TF_NewGraph();
602 tf_model->status = TF_NewStatus();
604 #define CLEANUP_ON_ERROR(tf_model) \
606 TF_DeleteTensor(tensor); \
607 TF_DeleteGraph(tf_model->graph); \
608 TF_DeleteStatus(tf_model->status); \
609 av_log(ctx, AV_LOG_ERROR, "Failed to set value or add operator to layer\n"); \
613 op_desc = TF_NewOperation(tf_model->graph, "Placeholder", "x");
614 TF_SetAttrType(op_desc, "dtype", TF_FLOAT);
615 TF_SetAttrShape(op_desc, "shape", input_shape, 4);
616 op = TF_FinishOperation(op_desc, tf_model->status);
617 if (TF_GetCode(tf_model->status) != TF_OK){
618 CLEANUP_ON_ERROR(tf_model);
621 op_desc = TF_NewOperation(tf_model->graph, "Const", "transpose_perm");
622 TF_SetAttrType(op_desc, "dtype", TF_INT32);
623 tensor = TF_AllocateTensor(TF_INT32, transpose_perm_shape, 1, 4 * sizeof(int32_t));
624 transpose_perm = (int32_t *)TF_TensorData(tensor);
625 transpose_perm[0] = 1;
626 transpose_perm[1] = 2;
627 transpose_perm[2] = 3;
628 transpose_perm[3] = 0;
629 TF_SetAttrTensor(op_desc, "value", tensor, tf_model->status);
630 if (TF_GetCode(tf_model->status) != TF_OK){
631 CLEANUP_ON_ERROR(tf_model);
633 transpose_op = TF_FinishOperation(op_desc, tf_model->status);
634 if (TF_GetCode(tf_model->status) != TF_OK){
635 CLEANUP_ON_ERROR(tf_model);
638 for (layer = 0; layer < native_model->layers_num; ++layer){
639 switch (native_model->layers[layer].type){
641 layer_add_res = DNN_SUCCESS;
644 layer_add_res = add_conv_layer(tf_model, transpose_op, &op,
645 (ConvolutionalParams *)native_model->layers[layer].params, layer);
647 case DLT_DEPTH_TO_SPACE:
648 layer_add_res = add_depth_to_space_layer(tf_model, &op,
649 (DepthToSpaceParams *)native_model->layers[layer].params, layer);
652 layer_add_res = add_pad_layer(tf_model, &op,
653 (LayerPadParams *)native_model->layers[layer].params, layer);
656 layer_add_res = add_maximum_layer(tf_model, &op,
657 (DnnLayerMaximumParams *)native_model->layers[layer].params, layer);
660 CLEANUP_ON_ERROR(tf_model);
663 if (layer_add_res != DNN_SUCCESS){
664 CLEANUP_ON_ERROR(tf_model);
668 op_desc = TF_NewOperation(tf_model->graph, "Identity", "y");
671 TF_AddInput(op_desc, input);
672 TF_FinishOperation(op_desc, tf_model->status);
673 if (TF_GetCode(tf_model->status) != TF_OK){
674 CLEANUP_ON_ERROR(tf_model);
677 ff_dnn_free_model_native(&model);
682 DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
684 DNNModel *model = NULL;
685 TFModel *tf_model = NULL;
687 model = av_mallocz(sizeof(DNNModel));
692 tf_model = av_mallocz(sizeof(TFModel));
697 tf_model->ctx.class = &dnn_tensorflow_class;
698 tf_model->model = model;
701 av_opt_set_defaults(&tf_model->ctx);
702 if (av_opt_set_from_string(&tf_model->ctx, options, NULL, "=", "&") < 0) {
703 av_log(&tf_model->ctx, AV_LOG_ERROR, "Failed to parse options \"%s\"\n", options);
709 if (load_tf_model(tf_model, model_filename) != DNN_SUCCESS){
710 if (load_native_model(tf_model, model_filename) != DNN_SUCCESS){
718 model->model = tf_model;
719 model->get_input = &get_input_tf;
720 model->get_output = &get_output_tf;
721 model->options = options;
722 model->filter_ctx = filter_ctx;
723 model->func_type = func_type;
728 static DNNReturnType execute_model_tf(const DNNModel *model, const char *input_name, AVFrame *in_frame,
729 const char **output_names, uint32_t nb_output, AVFrame *out_frame,
732 TF_Output *tf_outputs;
733 TFModel *tf_model = model->model;
734 TFContext *ctx = &tf_model->ctx;
735 DNNData input, output;
736 TF_Tensor **output_tensors;
738 TF_Tensor *input_tensor;
740 if (get_input_tf(tf_model, &input, input_name) != DNN_SUCCESS)
742 input.height = in_frame->height;
743 input.width = in_frame->width;
745 tf_input.oper = TF_GraphOperationByName(tf_model->graph, input_name);
747 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name);
751 input_tensor = allocate_input_tensor(&input);
753 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input tensor\n");
756 input.data = (float *)TF_TensorData(input_tensor);
759 if (tf_model->model->frame_pre_proc != NULL) {
760 tf_model->model->frame_pre_proc(in_frame, &input, tf_model->model->filter_ctx);
762 ff_proc_from_frame_to_dnn(in_frame, &input, tf_model->model->func_type, ctx);
766 if (nb_output != 1) {
767 // currently, the filter does not need multiple outputs,
768 // so we just pending the support until we really need it.
769 TF_DeleteTensor(input_tensor);
770 avpriv_report_missing_feature(ctx, "multiple outputs");
774 tf_outputs = av_malloc_array(nb_output, sizeof(*tf_outputs));
775 if (tf_outputs == NULL) {
776 TF_DeleteTensor(input_tensor);
777 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for *tf_outputs\n"); \
781 output_tensors = av_mallocz_array(nb_output, sizeof(*output_tensors));
782 if (!output_tensors) {
783 TF_DeleteTensor(input_tensor);
784 av_freep(&tf_outputs);
785 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output tensor\n"); \
789 for (int i = 0; i < nb_output; ++i) {
790 tf_outputs[i].oper = TF_GraphOperationByName(tf_model->graph, output_names[i]);
791 if (!tf_outputs[i].oper) {
792 TF_DeleteTensor(input_tensor);
793 av_freep(&tf_outputs);
794 av_freep(&output_tensors);
795 av_log(ctx, AV_LOG_ERROR, "Could not find output \"%s\" in model\n", output_names[i]); \
798 tf_outputs[i].index = 0;
801 TF_SessionRun(tf_model->session, NULL,
802 &tf_input, &input_tensor, 1,
803 tf_outputs, output_tensors, nb_output,
804 NULL, 0, NULL, tf_model->status);
805 if (TF_GetCode(tf_model->status) != TF_OK) {
806 TF_DeleteTensor(input_tensor);
807 av_freep(&tf_outputs);
808 av_freep(&output_tensors);
809 av_log(ctx, AV_LOG_ERROR, "Failed to run session when executing model\n");
813 for (uint32_t i = 0; i < nb_output; ++i) {
814 output.height = TF_Dim(output_tensors[i], 1);
815 output.width = TF_Dim(output_tensors[i], 2);
816 output.channels = TF_Dim(output_tensors[i], 3);
817 output.data = TF_TensorData(output_tensors[i]);
818 output.dt = TF_TensorType(output_tensors[i]);
821 if (tf_model->model->frame_post_proc != NULL) {
822 tf_model->model->frame_post_proc(out_frame, &output, tf_model->model->filter_ctx);
824 ff_proc_from_dnn_to_frame(out_frame, &output, ctx);
827 out_frame->width = output.width;
828 out_frame->height = output.height;
832 for (uint32_t i = 0; i < nb_output; ++i) {
833 if (output_tensors[i]) {
834 TF_DeleteTensor(output_tensors[i]);
837 TF_DeleteTensor(input_tensor);
838 av_freep(&output_tensors);
839 av_freep(&tf_outputs);
843 DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, const char *input_name, AVFrame *in_frame,
844 const char **output_names, uint32_t nb_output, AVFrame *out_frame)
846 TFModel *tf_model = model->model;
847 TFContext *ctx = &tf_model->ctx;
850 av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
855 av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
859 return execute_model_tf(model, input_name, in_frame, output_names, nb_output, out_frame, 1);
862 void ff_dnn_free_model_tf(DNNModel **model)
867 tf_model = (*model)->model;
868 if (tf_model->graph){
869 TF_DeleteGraph(tf_model->graph);
871 if (tf_model->session){
872 TF_CloseSession(tf_model->session, tf_model->status);
873 TF_DeleteSession(tf_model->session, tf_model->status);
875 if (tf_model->status){
876 TF_DeleteStatus(tf_model->status);