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_srcnn.h"
28 #include "dnn_espcn.h"
29 #include "libavformat/avio.h"
31 #include <tensorflow/c/c_api.h>
33 typedef struct TFModel{
37 TF_Output input, output;
38 TF_Tensor* input_tensor;
42 static void free_buffer(void* data, size_t length)
47 static TF_Buffer* read_graph(const char* model_filename)
50 unsigned char* graph_data = NULL;
51 AVIOContext* model_file_context;
52 long size, bytes_read;
54 if (avio_open(&model_file_context, model_filename, AVIO_FLAG_READ) < 0){
58 size = avio_size(model_file_context);
60 graph_data = av_malloc(size);
62 avio_closep(&model_file_context);
65 bytes_read = avio_read(model_file_context, graph_data, size);
66 avio_closep(&model_file_context);
67 if (bytes_read != size){
68 av_freep(&graph_data);
72 graph_buf = TF_NewBuffer();
73 graph_buf->data = (void*)graph_data;
74 graph_buf->length = size;
75 graph_buf->data_deallocator = free_buffer;
80 static DNNReturnType set_input_output_tf(void* model, DNNData* input, DNNData* output)
82 TFModel* tf_model = (TFModel*)model;
83 int64_t input_dims[] = {1, input->height, input->width, input->channels};
84 TF_SessionOptions* sess_opts;
85 const TF_Operation* init_op = TF_GraphOperationByName(tf_model->graph, "init");
86 TF_Tensor* output_tensor;
88 // Input operation should be named 'x'
89 tf_model->input.oper = TF_GraphOperationByName(tf_model->graph, "x");
90 if (!tf_model->input.oper){
93 tf_model->input.index = 0;
94 if (tf_model->input_tensor){
95 TF_DeleteTensor(tf_model->input_tensor);
97 tf_model->input_tensor = TF_AllocateTensor(TF_FLOAT, input_dims, 4,
98 input_dims[1] * input_dims[2] * input_dims[3] * sizeof(float));
99 if (!tf_model->input_tensor){
102 input->data = (float*)TF_TensorData(tf_model->input_tensor);
104 // Output operation should be named 'y'
105 tf_model->output.oper = TF_GraphOperationByName(tf_model->graph, "y");
106 if (!tf_model->output.oper){
109 tf_model->output.index = 0;
111 if (tf_model->session){
112 TF_CloseSession(tf_model->session, tf_model->status);
113 TF_DeleteSession(tf_model->session, tf_model->status);
116 sess_opts = TF_NewSessionOptions();
117 tf_model->session = TF_NewSession(tf_model->graph, sess_opts, tf_model->status);
118 TF_DeleteSessionOptions(sess_opts);
119 if (TF_GetCode(tf_model->status) != TF_OK)
124 // Run initialization operation with name "init" if it is present in graph
126 TF_SessionRun(tf_model->session, NULL,
129 &init_op, 1, NULL, tf_model->status);
130 if (TF_GetCode(tf_model->status) != TF_OK)
136 // Execute network to get output height, width and number of channels
137 TF_SessionRun(tf_model->session, NULL,
138 &tf_model->input, &tf_model->input_tensor, 1,
139 &tf_model->output, &output_tensor, 1,
140 NULL, 0, NULL, tf_model->status);
141 if (TF_GetCode(tf_model->status) != TF_OK){
145 output->height = TF_Dim(output_tensor, 1);
146 output->width = TF_Dim(output_tensor, 2);
147 output->channels = TF_Dim(output_tensor, 3);
148 output->data = av_malloc(output->height * output->width * output->channels * sizeof(float));
152 tf_model->output_data = output;
153 TF_DeleteTensor(output_tensor);
159 DNNModel* ff_dnn_load_model_tf(const char* model_filename)
161 DNNModel* model = NULL;
162 TFModel* tf_model = NULL;
163 TF_Buffer* graph_def;
164 TF_ImportGraphDefOptions* graph_opts;
166 model = av_malloc(sizeof(DNNModel));
171 tf_model = av_malloc(sizeof(TFModel));
176 tf_model->session = NULL;
177 tf_model->input_tensor = NULL;
178 tf_model->output_data = NULL;
180 graph_def = read_graph(model_filename);
186 tf_model->graph = TF_NewGraph();
187 tf_model->status = TF_NewStatus();
188 graph_opts = TF_NewImportGraphDefOptions();
189 TF_GraphImportGraphDef(tf_model->graph, graph_def, graph_opts, tf_model->status);
190 TF_DeleteImportGraphDefOptions(graph_opts);
191 TF_DeleteBuffer(graph_def);
192 if (TF_GetCode(tf_model->status) != TF_OK){
193 TF_DeleteGraph(tf_model->graph);
194 TF_DeleteStatus(tf_model->status);
200 model->model = (void*)tf_model;
201 model->set_input_output = &set_input_output_tf;
206 DNNModel* ff_dnn_load_default_model_tf(DNNDefaultModel model_type)
208 DNNModel* model = NULL;
209 TFModel* tf_model = NULL;
210 TF_Buffer* graph_def;
211 unsigned char* graph_data = NULL;
212 TF_ImportGraphDefOptions* graph_opts;
214 graph_def = TF_NewBuffer();
217 graph_data = av_malloc(srcnn_tf_size);
219 TF_DeleteBuffer(graph_def);
222 memcpy(graph_data, srcnn_tf_model, srcnn_tf_size);
223 graph_def->data = (void*)graph_data;
224 graph_def->length = srcnn_tf_size;
225 graph_def->data_deallocator = free_buffer;
228 graph_data = av_malloc(espcn_tf_size);
230 TF_DeleteBuffer(graph_def);
233 memcpy(graph_data, espcn_tf_model, espcn_tf_size);
234 graph_def->data = (void*)graph_data;
235 graph_def->length = espcn_tf_size;
236 graph_def->data_deallocator = free_buffer;
239 TF_DeleteBuffer(graph_def);
243 model = av_malloc(sizeof(DNNModel));
245 TF_DeleteBuffer(graph_def);
249 tf_model = av_malloc(sizeof(TFModel));
251 TF_DeleteBuffer(graph_def);
255 tf_model->session = NULL;
256 tf_model->input_tensor = NULL;
257 tf_model->output_data = NULL;
259 tf_model->graph = TF_NewGraph();
260 tf_model->status = TF_NewStatus();
261 graph_opts = TF_NewImportGraphDefOptions();
262 TF_GraphImportGraphDef(tf_model->graph, graph_def, graph_opts, tf_model->status);
263 TF_DeleteImportGraphDefOptions(graph_opts);
264 TF_DeleteBuffer(graph_def);
265 if (TF_GetCode(tf_model->status) != TF_OK){
266 TF_DeleteGraph(tf_model->graph);
267 TF_DeleteStatus(tf_model->status);
273 model->model = (void*)tf_model;
274 model->set_input_output = &set_input_output_tf;
279 DNNReturnType ff_dnn_execute_model_tf(const DNNModel* model)
281 TFModel* tf_model = (TFModel*)model->model;
282 TF_Tensor* output_tensor;
284 TF_SessionRun(tf_model->session, NULL,
285 &tf_model->input, &tf_model->input_tensor, 1,
286 &tf_model->output, &output_tensor, 1,
287 NULL, 0, NULL, tf_model->status);
289 if (TF_GetCode(tf_model->status) != TF_OK){
293 memcpy(tf_model->output_data->data, TF_TensorData(output_tensor),
294 tf_model->output_data->height * tf_model->output_data->width *
295 tf_model->output_data->channels * sizeof(float));
296 TF_DeleteTensor(output_tensor);
302 void ff_dnn_free_model_tf(DNNModel** model)
307 tf_model = (TFModel*)(*model)->model;
308 if (tf_model->graph){
309 TF_DeleteGraph(tf_model->graph);
311 if (tf_model->session){
312 TF_CloseSession(tf_model->session, tf_model->status);
313 TF_DeleteSession(tf_model->session, tf_model->status);
315 if (tf_model->status){
316 TF_DeleteStatus(tf_model->status);
318 if (tf_model->input_tensor){
319 TF_DeleteTensor(tf_model->input_tensor);
321 av_freep(&tf_model->output_data->data);