FFmpeg
dnn_backend_tf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Sergey Lavrushkin
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * DNN tensorflow backend implementation.
24  */
25 
26 #include "dnn_backend_tf.h"
27 #include "dnn_backend_native.h"
30 #include "libavformat/avio.h"
31 #include "libavutil/avassert.h"
33 
34 #include <tensorflow/c/c_api.h>
35 
36 typedef struct TFModel{
37  TF_Graph *graph;
38  TF_Session *session;
39  TF_Status *status;
40  TF_Output input;
41  TF_Tensor *input_tensor;
42  TF_Output *outputs;
43  TF_Tensor **output_tensors;
44  uint32_t nb_output;
45 } TFModel;
46 
47 static void free_buffer(void *data, size_t length)
48 {
49  av_freep(&data);
50 }
51 
52 static TF_Buffer *read_graph(const char *model_filename)
53 {
54  TF_Buffer *graph_buf;
55  unsigned char *graph_data = NULL;
56  AVIOContext *model_file_context;
57  long size, bytes_read;
58 
59  if (avio_open(&model_file_context, model_filename, AVIO_FLAG_READ) < 0){
60  return NULL;
61  }
62 
63  size = avio_size(model_file_context);
64 
65  graph_data = av_malloc(size);
66  if (!graph_data){
67  avio_closep(&model_file_context);
68  return NULL;
69  }
70  bytes_read = avio_read(model_file_context, graph_data, size);
71  avio_closep(&model_file_context);
72  if (bytes_read != size){
73  av_freep(&graph_data);
74  return NULL;
75  }
76 
77  graph_buf = TF_NewBuffer();
78  graph_buf->data = (void *)graph_data;
79  graph_buf->length = size;
80  graph_buf->data_deallocator = free_buffer;
81 
82  return graph_buf;
83 }
84 
85 static TF_Tensor *allocate_input_tensor(const DNNInputData *input)
86 {
87  TF_DataType dt;
88  size_t size;
89  int64_t input_dims[] = {1, input->height, input->width, input->channels};
90  switch (input->dt) {
91  case DNN_FLOAT:
92  dt = TF_FLOAT;
93  size = sizeof(float);
94  break;
95  case DNN_UINT8:
96  dt = TF_UINT8;
97  size = sizeof(char);
98  break;
99  default:
100  av_assert0(!"should not reach here");
101  }
102 
103  return TF_AllocateTensor(dt, input_dims, 4,
104  input_dims[1] * input_dims[2] * input_dims[3] * size);
105 }
106 
107 static DNNReturnType set_input_output_tf(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output)
108 {
109  TFModel *tf_model = (TFModel *)model;
110  TF_SessionOptions *sess_opts;
111  const TF_Operation *init_op = TF_GraphOperationByName(tf_model->graph, "init");
112 
113  // Input operation
114  tf_model->input.oper = TF_GraphOperationByName(tf_model->graph, input_name);
115  if (!tf_model->input.oper){
116  return DNN_ERROR;
117  }
118  tf_model->input.index = 0;
119  if (tf_model->input_tensor){
120  TF_DeleteTensor(tf_model->input_tensor);
121  }
122  tf_model->input_tensor = allocate_input_tensor(input);
123  if (!tf_model->input_tensor){
124  return DNN_ERROR;
125  }
126  input->data = (float *)TF_TensorData(tf_model->input_tensor);
127 
128  // Output operation
129  if (nb_output == 0)
130  return DNN_ERROR;
131 
132  av_freep(&tf_model->outputs);
133  tf_model->outputs = av_malloc_array(nb_output, sizeof(*tf_model->outputs));
134  if (!tf_model->outputs)
135  return DNN_ERROR;
136  for (int i = 0; i < nb_output; ++i) {
137  tf_model->outputs[i].oper = TF_GraphOperationByName(tf_model->graph, output_names[i]);
138  if (!tf_model->outputs[i].oper){
139  av_freep(&tf_model->outputs);
140  return DNN_ERROR;
141  }
142  tf_model->outputs[i].index = 0;
143  }
144 
145  if (tf_model->output_tensors) {
146  for (uint32_t i = 0; i < tf_model->nb_output; ++i) {
147  if (tf_model->output_tensors[i]) {
148  TF_DeleteTensor(tf_model->output_tensors[i]);
149  tf_model->output_tensors[i] = NULL;
150  }
151  }
152  }
153  av_freep(&tf_model->output_tensors);
154  tf_model->output_tensors = av_mallocz_array(nb_output, sizeof(*tf_model->output_tensors));
155  if (!tf_model->output_tensors) {
156  av_freep(&tf_model->outputs);
157  return DNN_ERROR;
158  }
159 
160  tf_model->nb_output = nb_output;
161 
162  if (tf_model->session){
163  TF_CloseSession(tf_model->session, tf_model->status);
164  TF_DeleteSession(tf_model->session, tf_model->status);
165  }
166 
167  sess_opts = TF_NewSessionOptions();
168  tf_model->session = TF_NewSession(tf_model->graph, sess_opts, tf_model->status);
169  TF_DeleteSessionOptions(sess_opts);
170  if (TF_GetCode(tf_model->status) != TF_OK)
171  {
172  return DNN_ERROR;
173  }
174 
175  // Run initialization operation with name "init" if it is present in graph
176  if (init_op){
177  TF_SessionRun(tf_model->session, NULL,
178  NULL, NULL, 0,
179  NULL, NULL, 0,
180  &init_op, 1, NULL, tf_model->status);
181  if (TF_GetCode(tf_model->status) != TF_OK)
182  {
183  return DNN_ERROR;
184  }
185  }
186 
187  return DNN_SUCCESS;
188 }
189 
190 static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename)
191 {
192  TF_Buffer *graph_def;
193  TF_ImportGraphDefOptions *graph_opts;
194 
195  graph_def = read_graph(model_filename);
196  if (!graph_def){
197  return DNN_ERROR;
198  }
199  tf_model->graph = TF_NewGraph();
200  tf_model->status = TF_NewStatus();
201  graph_opts = TF_NewImportGraphDefOptions();
202  TF_GraphImportGraphDef(tf_model->graph, graph_def, graph_opts, tf_model->status);
203  TF_DeleteImportGraphDefOptions(graph_opts);
204  TF_DeleteBuffer(graph_def);
205  if (TF_GetCode(tf_model->status) != TF_OK){
206  TF_DeleteGraph(tf_model->graph);
207  TF_DeleteStatus(tf_model->status);
208  return DNN_ERROR;
209  }
210 
211  return DNN_SUCCESS;
212 }
213 
214 #define NAME_BUFFER_SIZE 256
215 
216 static DNNReturnType add_conv_layer(TFModel *tf_model, TF_Operation *transpose_op, TF_Operation **cur_op,
217  ConvolutionalParams* params, const int layer)
218 {
219  TF_Operation *op;
220  TF_OperationDescription *op_desc;
221  TF_Output input;
222  int64_t strides[] = {1, 1, 1, 1};
223  TF_Tensor *tensor;
224  int64_t dims[4];
225  int dims_len;
226  char name_buffer[NAME_BUFFER_SIZE];
227  int32_t size;
228 
229  size = params->input_num * params->output_num * params->kernel_size * params->kernel_size;
230  input.index = 0;
231 
232  snprintf(name_buffer, NAME_BUFFER_SIZE, "conv_kernel%d", layer);
233  op_desc = TF_NewOperation(tf_model->graph, "Const", name_buffer);
234  TF_SetAttrType(op_desc, "dtype", TF_FLOAT);
235  dims[0] = params->output_num;
236  dims[1] = params->kernel_size;
237  dims[2] = params->kernel_size;
238  dims[3] = params->input_num;
239  dims_len = 4;
240  tensor = TF_AllocateTensor(TF_FLOAT, dims, dims_len, size * sizeof(float));
241  memcpy(TF_TensorData(tensor), params->kernel, size * sizeof(float));
242  TF_SetAttrTensor(op_desc, "value", tensor, tf_model->status);
243  if (TF_GetCode(tf_model->status) != TF_OK){
244  return DNN_ERROR;
245  }
246  op = TF_FinishOperation(op_desc, tf_model->status);
247  if (TF_GetCode(tf_model->status) != TF_OK){
248  return DNN_ERROR;
249  }
250 
251  snprintf(name_buffer, NAME_BUFFER_SIZE, "transpose%d", layer);
252  op_desc = TF_NewOperation(tf_model->graph, "Transpose", name_buffer);
253  input.oper = op;
254  TF_AddInput(op_desc, input);
255  input.oper = transpose_op;
256  TF_AddInput(op_desc, input);
257  TF_SetAttrType(op_desc, "T", TF_FLOAT);
258  TF_SetAttrType(op_desc, "Tperm", TF_INT32);
259  op = TF_FinishOperation(op_desc, tf_model->status);
260  if (TF_GetCode(tf_model->status) != TF_OK){
261  return DNN_ERROR;
262  }
263 
264  snprintf(name_buffer, NAME_BUFFER_SIZE, "conv2d%d", layer);
265  op_desc = TF_NewOperation(tf_model->graph, "Conv2D", name_buffer);
266  input.oper = *cur_op;
267  TF_AddInput(op_desc, input);
268  input.oper = op;
269  TF_AddInput(op_desc, input);
270  TF_SetAttrType(op_desc, "T", TF_FLOAT);
271  TF_SetAttrIntList(op_desc, "strides", strides, 4);
272  TF_SetAttrString(op_desc, "padding", "VALID", 5);
273  *cur_op = TF_FinishOperation(op_desc, tf_model->status);
274  if (TF_GetCode(tf_model->status) != TF_OK){
275  return DNN_ERROR;
276  }
277 
278  snprintf(name_buffer, NAME_BUFFER_SIZE, "conv_biases%d", layer);
279  op_desc = TF_NewOperation(tf_model->graph, "Const", name_buffer);
280  TF_SetAttrType(op_desc, "dtype", TF_FLOAT);
281  dims[0] = params->output_num;
282  dims_len = 1;
283  tensor = TF_AllocateTensor(TF_FLOAT, dims, dims_len, params->output_num * sizeof(float));
284  memcpy(TF_TensorData(tensor), params->biases, params->output_num * sizeof(float));
285  TF_SetAttrTensor(op_desc, "value", tensor, tf_model->status);
286  if (TF_GetCode(tf_model->status) != TF_OK){
287  return DNN_ERROR;
288  }
289  op = TF_FinishOperation(op_desc, tf_model->status);
290  if (TF_GetCode(tf_model->status) != TF_OK){
291  return DNN_ERROR;
292  }
293 
294  snprintf(name_buffer, NAME_BUFFER_SIZE, "bias_add%d", layer);
295  op_desc = TF_NewOperation(tf_model->graph, "BiasAdd", name_buffer);
296  input.oper = *cur_op;
297  TF_AddInput(op_desc, input);
298  input.oper = op;
299  TF_AddInput(op_desc, input);
300  TF_SetAttrType(op_desc, "T", TF_FLOAT);
301  *cur_op = TF_FinishOperation(op_desc, tf_model->status);
302  if (TF_GetCode(tf_model->status) != TF_OK){
303  return DNN_ERROR;
304  }
305 
306  snprintf(name_buffer, NAME_BUFFER_SIZE, "activation%d", layer);
307  switch (params->activation){
308  case RELU:
309  op_desc = TF_NewOperation(tf_model->graph, "Relu", name_buffer);
310  break;
311  case TANH:
312  op_desc = TF_NewOperation(tf_model->graph, "Tanh", name_buffer);
313  break;
314  case SIGMOID:
315  op_desc = TF_NewOperation(tf_model->graph, "Sigmoid", name_buffer);
316  break;
317  default:
318  return DNN_ERROR;
319  }
320  input.oper = *cur_op;
321  TF_AddInput(op_desc, input);
322  TF_SetAttrType(op_desc, "T", TF_FLOAT);
323  *cur_op = TF_FinishOperation(op_desc, tf_model->status);
324  if (TF_GetCode(tf_model->status) != TF_OK){
325  return DNN_ERROR;
326  }
327 
328  return DNN_SUCCESS;
329 }
330 
331 static DNNReturnType add_depth_to_space_layer(TFModel *tf_model, TF_Operation **cur_op,
332  DepthToSpaceParams *params, const int layer)
333 {
334  TF_OperationDescription *op_desc;
335  TF_Output input;
336  char name_buffer[NAME_BUFFER_SIZE];
337 
338  snprintf(name_buffer, NAME_BUFFER_SIZE, "depth_to_space%d", layer);
339  op_desc = TF_NewOperation(tf_model->graph, "DepthToSpace", name_buffer);
340  input.oper = *cur_op;
341  input.index = 0;
342  TF_AddInput(op_desc, input);
343  TF_SetAttrType(op_desc, "T", TF_FLOAT);
344  TF_SetAttrInt(op_desc, "block_size", params->block_size);
345  *cur_op = TF_FinishOperation(op_desc, tf_model->status);
346  if (TF_GetCode(tf_model->status) != TF_OK){
347  return DNN_ERROR;
348  }
349 
350  return DNN_SUCCESS;
351 }
352 
353 static DNNReturnType add_pad_layer(TFModel *tf_model, TF_Operation **cur_op,
354  LayerPadParams *params, const int layer)
355 {
356  TF_Operation *op;
357  TF_Tensor *tensor;
358  TF_OperationDescription *op_desc;
359  TF_Output input;
360  int32_t *pads;
361  int64_t pads_shape[] = {4, 2};
362 
363  char name_buffer[NAME_BUFFER_SIZE];
364  snprintf(name_buffer, NAME_BUFFER_SIZE, "pad%d", layer);
365 
366  op_desc = TF_NewOperation(tf_model->graph, "Const", name_buffer);
367  TF_SetAttrType(op_desc, "dtype", TF_INT32);
368  tensor = TF_AllocateTensor(TF_INT32, pads_shape, 2, 4 * 2 * sizeof(int32_t));
369  pads = (int32_t *)TF_TensorData(tensor);
370  pads[0] = params->paddings[0][0];
371  pads[1] = params->paddings[0][1];
372  pads[2] = params->paddings[1][0];
373  pads[3] = params->paddings[1][1];
374  pads[4] = params->paddings[2][0];
375  pads[5] = params->paddings[2][1];
376  pads[6] = params->paddings[3][0];
377  pads[7] = params->paddings[3][1];
378  TF_SetAttrTensor(op_desc, "value", tensor, tf_model->status);
379  if (TF_GetCode(tf_model->status) != TF_OK){
380  return DNN_ERROR;
381  }
382  op = TF_FinishOperation(op_desc, tf_model->status);
383  if (TF_GetCode(tf_model->status) != TF_OK){
384  return DNN_ERROR;
385  }
386 
387  op_desc = TF_NewOperation(tf_model->graph, "MirrorPad", "mirror_pad");
388  input.oper = *cur_op;
389  input.index = 0;
390  TF_AddInput(op_desc, input);
391  input.oper = op;
392  TF_AddInput(op_desc, input);
393  TF_SetAttrType(op_desc, "T", TF_FLOAT);
394  TF_SetAttrType(op_desc, "Tpaddings", TF_INT32);
395  TF_SetAttrString(op_desc, "mode", "SYMMETRIC", 9);
396  *cur_op = TF_FinishOperation(op_desc, tf_model->status);
397  if (TF_GetCode(tf_model->status) != TF_OK){
398  return DNN_ERROR;
399  }
400 
401  return DNN_SUCCESS;
402 }
403 
404 static DNNReturnType load_native_model(TFModel *tf_model, const char *model_filename)
405 {
406  int32_t layer;
407  TF_OperationDescription *op_desc;
408  TF_Operation *op;
409  TF_Operation *transpose_op;
410  TF_Tensor *tensor;
411  TF_Output input;
413  int64_t transpose_perm_shape[] = {4};
414  int64_t input_shape[] = {1, -1, -1, -1};
415  DNNReturnType layer_add_res;
416  DNNModel *native_model = NULL;
417  ConvolutionalNetwork *conv_network;
418 
419  native_model = ff_dnn_load_model_native(model_filename);
420  if (!native_model){
421  return DNN_ERROR;
422  }
423 
424  conv_network = (ConvolutionalNetwork *)native_model->model;
425  tf_model->graph = TF_NewGraph();
426  tf_model->status = TF_NewStatus();
427 
428 #define CLEANUP_ON_ERROR(tf_model) \
429  { \
430  TF_DeleteGraph(tf_model->graph); \
431  TF_DeleteStatus(tf_model->status); \
432  return DNN_ERROR; \
433  }
434 
435  op_desc = TF_NewOperation(tf_model->graph, "Placeholder", "x");
436  TF_SetAttrType(op_desc, "dtype", TF_FLOAT);
437  TF_SetAttrShape(op_desc, "shape", input_shape, 4);
438  op = TF_FinishOperation(op_desc, tf_model->status);
439  if (TF_GetCode(tf_model->status) != TF_OK){
440  CLEANUP_ON_ERROR(tf_model);
441  }
442 
443  op_desc = TF_NewOperation(tf_model->graph, "Const", "transpose_perm");
444  TF_SetAttrType(op_desc, "dtype", TF_INT32);
445  tensor = TF_AllocateTensor(TF_INT32, transpose_perm_shape, 1, 4 * sizeof(int32_t));
446  transpose_perm = (int32_t *)TF_TensorData(tensor);
447  transpose_perm[0] = 1;
448  transpose_perm[1] = 2;
449  transpose_perm[2] = 3;
450  transpose_perm[3] = 0;
451  TF_SetAttrTensor(op_desc, "value", tensor, tf_model->status);
452  if (TF_GetCode(tf_model->status) != TF_OK){
453  CLEANUP_ON_ERROR(tf_model);
454  }
455  transpose_op = TF_FinishOperation(op_desc, tf_model->status);
456 
457  for (layer = 0; layer < conv_network->layers_num; ++layer){
458  switch (conv_network->layers[layer].type){
459  case INPUT:
460  layer_add_res = DNN_SUCCESS;
461  break;
462  case CONV:
463  layer_add_res = add_conv_layer(tf_model, transpose_op, &op,
464  (ConvolutionalParams *)conv_network->layers[layer].params, layer);
465  break;
466  case DEPTH_TO_SPACE:
467  layer_add_res = add_depth_to_space_layer(tf_model, &op,
468  (DepthToSpaceParams *)conv_network->layers[layer].params, layer);
469  break;
470  case MIRROR_PAD:
471  layer_add_res = add_pad_layer(tf_model, &op,
472  (LayerPadParams *)conv_network->layers[layer].params, layer);
473  break;
474  default:
475  CLEANUP_ON_ERROR(tf_model);
476  }
477 
478  if (layer_add_res != DNN_SUCCESS){
479  CLEANUP_ON_ERROR(tf_model);
480  }
481  }
482 
483  op_desc = TF_NewOperation(tf_model->graph, "Identity", "y");
484  input.oper = op;
485  input.index = 0;
486  TF_AddInput(op_desc, input);
487  TF_FinishOperation(op_desc, tf_model->status);
488  if (TF_GetCode(tf_model->status) != TF_OK){
489  CLEANUP_ON_ERROR(tf_model);
490  }
491 
492  ff_dnn_free_model_native(&native_model);
493 
494  return DNN_SUCCESS;
495 }
496 
497 DNNModel *ff_dnn_load_model_tf(const char *model_filename)
498 {
499  DNNModel *model = NULL;
500  TFModel *tf_model = NULL;
501 
502  model = av_malloc(sizeof(DNNModel));
503  if (!model){
504  return NULL;
505  }
506 
507  tf_model = av_mallocz(sizeof(TFModel));
508  if (!tf_model){
509  av_freep(&model);
510  return NULL;
511  }
512 
513  if (load_tf_model(tf_model, model_filename) != DNN_SUCCESS){
514  if (load_native_model(tf_model, model_filename) != DNN_SUCCESS){
515  av_freep(&tf_model);
516  av_freep(&model);
517 
518  return NULL;
519  }
520  }
521 
522  model->model = (void *)tf_model;
524 
525  return model;
526 }
527 
528 
529 
531 {
532  TFModel *tf_model = (TFModel *)model->model;
533  uint32_t nb = FFMIN(nb_output, tf_model->nb_output);
534  if (nb == 0)
535  return DNN_ERROR;
536 
537  av_assert0(tf_model->output_tensors);
538  for (uint32_t i = 0; i < tf_model->nb_output; ++i) {
539  if (tf_model->output_tensors[i]) {
540  TF_DeleteTensor(tf_model->output_tensors[i]);
541  tf_model->output_tensors[i] = NULL;
542  }
543  }
544 
545  TF_SessionRun(tf_model->session, NULL,
546  &tf_model->input, &tf_model->input_tensor, 1,
547  tf_model->outputs, tf_model->output_tensors, nb,
548  NULL, 0, NULL, tf_model->status);
549 
550  if (TF_GetCode(tf_model->status) != TF_OK){
551  return DNN_ERROR;
552  }
553 
554  for (uint32_t i = 0; i < nb; ++i) {
555  outputs[i].height = TF_Dim(tf_model->output_tensors[i], 1);
556  outputs[i].width = TF_Dim(tf_model->output_tensors[i], 2);
557  outputs[i].channels = TF_Dim(tf_model->output_tensors[i], 3);
558  outputs[i].data = TF_TensorData(tf_model->output_tensors[i]);
559  }
560 
561  return DNN_SUCCESS;
562 }
563 
565 {
566  TFModel *tf_model;
567 
568  if (*model){
569  tf_model = (TFModel *)(*model)->model;
570  if (tf_model->graph){
571  TF_DeleteGraph(tf_model->graph);
572  }
573  if (tf_model->session){
574  TF_CloseSession(tf_model->session, tf_model->status);
575  TF_DeleteSession(tf_model->session, tf_model->status);
576  }
577  if (tf_model->status){
578  TF_DeleteStatus(tf_model->status);
579  }
580  if (tf_model->input_tensor){
581  TF_DeleteTensor(tf_model->input_tensor);
582  }
583  if (tf_model->output_tensors) {
584  for (uint32_t i = 0; i < tf_model->nb_output; ++i) {
585  if (tf_model->output_tensors[i]) {
586  TF_DeleteTensor(tf_model->output_tensors[i]);
587  tf_model->output_tensors[i] = NULL;
588  }
589  }
590  }
591  av_freep(&tf_model->outputs);
592  av_freep(&tf_model->output_tensors);
593  av_freep(&tf_model);
594  av_freep(model);
595  }
596 }
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:1187
void * model
Definition: dnn_interface.h:50
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:339
Buffered I/O operations.
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static TF_Buffer * read_graph(const char *model_filename)
int channels
Definition: dnn_interface.h:45
DNN inference functions interface for native backend.
#define AVIO_FLAG_READ
read-only
Definition: avio.h:674
DNNModel * ff_dnn_load_model_tf(const char *model_filename)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
DNNDataType dt
Definition: dnn_interface.h:39
uint32_t nb_output
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define av_malloc(s)
TF_Status * status
DNN inference functions interface for TensorFlow backend.
ptrdiff_t size
Definition: opengl_enc.c:100
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:650
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
static TF_Tensor * allocate_input_tensor(const DNNInputData *input)
int height
Definition: dnn_interface.h:45
static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename)
GLenum GLint * params
Definition: opengl_enc.c:113
simple assert() macros that are a bit more flexible than ISO C assert().
GLsizei GLsizei * length
Definition: opengl_enc.c:114
TF_Tensor * input_tensor
TF_Output input
static DNNReturnType load_native_model(TFModel *tf_model, const char *model_filename)
#define FFMIN(a, b)
Definition: common.h:96
#define NAME_BUFFER_SIZE
int32_t
DNN inference functions interface for native backend.
DNNReturnType
Definition: dnn_interface.h:31
void ff_dnn_free_model_native(DNNModel **model)
if(ret)
static DNNReturnType add_conv_layer(TFModel *tf_model, TF_Operation *transpose_op, TF_Operation **cur_op, ConvolutionalParams *params, const int layer)
DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, DNNData *outputs, uint32_t nb_output)
static void free_buffer(void *data, size_t length)
static DNNReturnType add_depth_to_space_layer(TFModel *tf_model, TF_Operation **cur_op, DepthToSpaceParams *params, const int layer)
layer pad (equivalent to tf.pad) for native backend.
DNNReturnType(* set_input_output)(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output)
Definition: dnn_interface.h:53
TF_Tensor ** output_tensors
#define snprintf
Definition: snprintf.h:34
DNNLayerType type
TF_Session * session
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:78
TF_Output * outputs
static DNNReturnType set_input_output_tf(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output)
TF_Graph * graph
void ff_dnn_free_model_tf(DNNModel **model)
float * data
Definition: dnn_interface.h:44
void * params
static void transpose_perm(int16_t *out, int16_t *in, int num_vect, const uint8_t line_len[2], int length_div)
Interpret the input data as in the following table:
Definition: twinvq.c:630
DNNModel * ff_dnn_load_model_native(const char *model_filename)
#define av_freep(p)
static DNNReturnType add_pad_layer(TFModel *tf_model, TF_Operation **cur_op, LayerPadParams *params, const int layer)
#define av_malloc_array(a, b)
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:1242
#define CLEANUP_ON_ERROR(tf_model)
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191