32 #include "../internal.h"
37 #include <tensorflow/c/c_api.h>
56 #define OFFSET(x) offsetof(TFContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
66 const char **output_names, uint32_t nb_output,
AVFrame *out_frame,
77 unsigned char *graph_data =
NULL;
79 long size, bytes_read;
94 if (bytes_read !=
size){
99 graph_buf = TF_NewBuffer();
100 graph_buf->data = graph_data;
101 graph_buf->length =
size;
115 size =
sizeof(float);
125 return TF_AllocateTensor(dt, input_dims, 4,
126 input_dims[1] * input_dims[2] * input_dims[3] *
size);
138 tf_output.oper = TF_GraphOperationByName(tf_model->
graph, input_name);
139 if (!tf_output.oper) {
145 dt = TF_OperationOutputType(tf_output);
158 status = TF_NewStatus();
159 TF_GraphGetTensorShape(tf_model->
graph, tf_output, dims, 4, status);
160 if (TF_GetCode(status) != TF_OK){
161 TF_DeleteStatus(status);
165 TF_DeleteStatus(status);
170 input->
width = dims[2];
177 const char *output_name,
int *output_width,
int *output_height)
197 in_frame->
width = input_width;
198 in_frame->
height = input_height;
201 *output_width = out_frame->
width;
202 *output_height = out_frame->
height;
212 TF_Buffer *graph_def;
213 TF_ImportGraphDefOptions *graph_opts;
214 TF_SessionOptions *sess_opts;
215 const TF_Operation *init_op;
217 int sess_config_length = 0;
246 if (sess_config_length % 2 != 0) {
248 "please re-generate the config.\n",
253 sess_config_length -= 2;
254 sess_config_length /= 2;
256 sess_config =
av_malloc(sess_config_length);
262 for (
int i = 0;
i < sess_config_length;
i++) {
263 int index = 2 + (sess_config_length - 1 -
i) * 2;
266 sess_config[
i] = strtol(
tmp,
NULL, 16);
276 tf_model->
graph = TF_NewGraph();
277 tf_model->
status = TF_NewStatus();
278 graph_opts = TF_NewImportGraphDefOptions();
279 TF_GraphImportGraphDef(tf_model->
graph, graph_def, graph_opts, tf_model->
status);
280 TF_DeleteImportGraphDefOptions(graph_opts);
281 TF_DeleteBuffer(graph_def);
282 if (TF_GetCode(tf_model->
status) != TF_OK){
283 TF_DeleteGraph(tf_model->
graph);
284 TF_DeleteStatus(tf_model->
status);
290 init_op = TF_GraphOperationByName(tf_model->
graph,
"init");
291 sess_opts = TF_NewSessionOptions();
294 TF_SetConfig(sess_opts, sess_config, sess_config_length,tf_model->
status);
296 if (TF_GetCode(tf_model->
status) != TF_OK) {
297 TF_DeleteGraph(tf_model->
graph);
298 TF_DeleteStatus(tf_model->
status);
299 TF_DeleteSessionOptions(sess_opts);
307 TF_DeleteSessionOptions(sess_opts);
308 if (TF_GetCode(tf_model->
status) != TF_OK)
310 TF_DeleteGraph(tf_model->
graph);
311 TF_DeleteStatus(tf_model->
status);
322 if (TF_GetCode(tf_model->
status) != TF_OK)
325 TF_DeleteGraph(tf_model->
graph);
326 TF_DeleteStatus(tf_model->
status);
335 #define NAME_BUFFER_SIZE 256
342 TF_OperationDescription *op_desc;
344 int64_t strides[] = {1, 1, 1, 1};
345 TF_Tensor *kernel_tensor =
NULL, *biases_tensor =
NULL;
355 op_desc = TF_NewOperation(tf_model->
graph,
"Const", name_buffer);
356 TF_SetAttrType(op_desc,
"dtype", TF_FLOAT);
362 kernel_tensor = TF_AllocateTensor(TF_FLOAT, dims, dims_len,
size *
sizeof(
float));
363 memcpy(TF_TensorData(kernel_tensor), params->
kernel,
size *
sizeof(
float));
364 TF_SetAttrTensor(op_desc,
"value", kernel_tensor, tf_model->
status);
365 if (TF_GetCode(tf_model->
status) != TF_OK){
368 op = TF_FinishOperation(op_desc, tf_model->
status);
369 if (TF_GetCode(tf_model->
status) != TF_OK){
374 op_desc = TF_NewOperation(tf_model->
graph,
"Transpose", name_buffer);
376 TF_AddInput(op_desc, input);
377 input.oper = transpose_op;
378 TF_AddInput(op_desc, input);
379 TF_SetAttrType(op_desc,
"T", TF_FLOAT);
380 TF_SetAttrType(op_desc,
"Tperm", TF_INT32);
381 op = TF_FinishOperation(op_desc, tf_model->
status);
382 if (TF_GetCode(tf_model->
status) != TF_OK){
387 op_desc = TF_NewOperation(tf_model->
graph,
"Conv2D", name_buffer);
388 input.oper = *cur_op;
389 TF_AddInput(op_desc, input);
391 TF_AddInput(op_desc, input);
392 TF_SetAttrType(op_desc,
"T", TF_FLOAT);
393 TF_SetAttrIntList(op_desc,
"strides", strides, 4);
394 TF_SetAttrString(op_desc,
"padding",
"VALID", 5);
395 *cur_op = TF_FinishOperation(op_desc, tf_model->
status);
396 if (TF_GetCode(tf_model->
status) != TF_OK){
401 op_desc = TF_NewOperation(tf_model->
graph,
"Const", name_buffer);
402 TF_SetAttrType(op_desc,
"dtype", TF_FLOAT);
405 biases_tensor = TF_AllocateTensor(TF_FLOAT, dims, dims_len, params->
output_num *
sizeof(
float));
406 memcpy(TF_TensorData(biases_tensor), params->
biases, params->
output_num *
sizeof(
float));
407 TF_SetAttrTensor(op_desc,
"value", biases_tensor, tf_model->
status);
408 if (TF_GetCode(tf_model->
status) != TF_OK){
411 op = TF_FinishOperation(op_desc, tf_model->
status);
412 if (TF_GetCode(tf_model->
status) != TF_OK){
417 op_desc = TF_NewOperation(tf_model->
graph,
"BiasAdd", name_buffer);
418 input.oper = *cur_op;
419 TF_AddInput(op_desc, input);
421 TF_AddInput(op_desc, input);
422 TF_SetAttrType(op_desc,
"T", TF_FLOAT);
423 *cur_op = TF_FinishOperation(op_desc, tf_model->
status);
424 if (TF_GetCode(tf_model->
status) != TF_OK){
431 op_desc = TF_NewOperation(tf_model->
graph,
"Relu", name_buffer);
434 op_desc = TF_NewOperation(tf_model->
graph,
"Tanh", name_buffer);
437 op_desc = TF_NewOperation(tf_model->
graph,
"Sigmoid", name_buffer);
443 input.oper = *cur_op;
444 TF_AddInput(op_desc, input);
445 TF_SetAttrType(op_desc,
"T", TF_FLOAT);
446 *cur_op = TF_FinishOperation(op_desc, tf_model->
status);
447 if (TF_GetCode(tf_model->
status) != TF_OK){
453 TF_DeleteTensor(kernel_tensor);
454 TF_DeleteTensor(biases_tensor);
463 TF_OperationDescription *op_desc;
468 op_desc = TF_NewOperation(tf_model->
graph,
"DepthToSpace", name_buffer);
469 input.oper = *cur_op;
471 TF_AddInput(op_desc, input);
472 TF_SetAttrType(op_desc,
"T", TF_FLOAT);
473 TF_SetAttrInt(op_desc,
"block_size", params->
block_size);
474 *cur_op = TF_FinishOperation(op_desc, tf_model->
status);
475 if (TF_GetCode(tf_model->
status) != TF_OK){
489 TF_OperationDescription *op_desc;
497 op_desc = TF_NewOperation(tf_model->
graph,
"Const", name_buffer);
498 TF_SetAttrType(op_desc,
"dtype", TF_INT32);
499 tensor = TF_AllocateTensor(TF_INT32, pads_shape, 2, 4 * 2 *
sizeof(
int32_t));
500 pads = (
int32_t *)TF_TensorData(tensor);
509 TF_SetAttrTensor(op_desc,
"value", tensor, tf_model->
status);
510 if (TF_GetCode(tf_model->
status) != TF_OK){
511 TF_DeleteTensor(tensor);
515 op = TF_FinishOperation(op_desc, tf_model->
status);
516 if (TF_GetCode(tf_model->
status) != TF_OK){
517 TF_DeleteTensor(tensor);
522 op_desc = TF_NewOperation(tf_model->
graph,
"MirrorPad",
"mirror_pad");
523 input.oper = *cur_op;
525 TF_AddInput(op_desc, input);
527 TF_AddInput(op_desc, input);
528 TF_SetAttrType(op_desc,
"T", TF_FLOAT);
529 TF_SetAttrType(op_desc,
"Tpaddings", TF_INT32);
530 TF_SetAttrString(op_desc,
"mode",
"SYMMETRIC", 9);
531 *cur_op = TF_FinishOperation(op_desc, tf_model->
status);
532 if (TF_GetCode(tf_model->
status) != TF_OK){
533 TF_DeleteTensor(tensor);
547 TF_OperationDescription *op_desc;
554 op_desc = TF_NewOperation(tf_model->
graph,
"Const", name_buffer);
555 TF_SetAttrType(op_desc,
"dtype", TF_FLOAT);
556 tensor = TF_AllocateTensor(TF_FLOAT,
NULL, 0, TF_DataTypeSize(TF_FLOAT));
557 y = (
float *)TF_TensorData(tensor);
559 TF_SetAttrTensor(op_desc,
"value", tensor, tf_model->
status);
560 if (TF_GetCode(tf_model->
status) != TF_OK){
561 TF_DeleteTensor(tensor);
565 op = TF_FinishOperation(op_desc, tf_model->
status);
566 if (TF_GetCode(tf_model->
status) != TF_OK){
567 TF_DeleteTensor(tensor);
573 op_desc = TF_NewOperation(tf_model->
graph,
"Maximum", name_buffer);
574 input.oper = *cur_op;
576 TF_AddInput(op_desc, input);
578 TF_AddInput(op_desc, input);
579 TF_SetAttrType(op_desc,
"T", TF_FLOAT);
580 *cur_op = TF_FinishOperation(op_desc, tf_model->
status);
581 if (TF_GetCode(tf_model->
status) != TF_OK){
582 TF_DeleteTensor(tensor);
594 TF_OperationDescription *op_desc;
596 TF_Operation *transpose_op;
597 TF_Tensor *tensor =
NULL;
600 int64_t transpose_perm_shape[] = {4};
601 int64_t input_shape[] = {1, -1, -1, -1};
612 native_model = model->
model;
613 tf_model->
graph = TF_NewGraph();
614 tf_model->
status = TF_NewStatus();
616 #define CLEANUP_ON_ERROR(tf_model) \
618 TF_DeleteTensor(tensor); \
619 TF_DeleteGraph(tf_model->graph); \
620 TF_DeleteStatus(tf_model->status); \
621 av_log(ctx, AV_LOG_ERROR, "Failed to set value or add operator to layer\n"); \
625 op_desc = TF_NewOperation(tf_model->
graph,
"Placeholder",
"x");
626 TF_SetAttrType(op_desc,
"dtype", TF_FLOAT);
627 TF_SetAttrShape(op_desc,
"shape", input_shape, 4);
628 op = TF_FinishOperation(op_desc, tf_model->
status);
629 if (TF_GetCode(tf_model->
status) != TF_OK){
633 op_desc = TF_NewOperation(tf_model->
graph,
"Const",
"transpose_perm");
634 TF_SetAttrType(op_desc,
"dtype", TF_INT32);
635 tensor = TF_AllocateTensor(TF_INT32, transpose_perm_shape, 1, 4 *
sizeof(
int32_t));
641 TF_SetAttrTensor(op_desc,
"value", tensor, tf_model->
status);
642 if (TF_GetCode(tf_model->
status) != TF_OK){
645 transpose_op = TF_FinishOperation(op_desc, tf_model->
status);
646 if (TF_GetCode(tf_model->
status) != TF_OK){
650 for (layer = 0; layer < native_model->
layers_num; ++layer){
680 op_desc = TF_NewOperation(tf_model->
graph,
"Identity",
"y");
683 TF_AddInput(op_desc, input);
684 TF_FinishOperation(op_desc, tf_model->
status);
685 if (TF_GetCode(tf_model->
status) != TF_OK){
709 tf_model->
ctx.
class = &dnn_tensorflow_class;
710 tf_model->
model = model;
730 model->
model = tf_model;
741 const char **output_names, uint32_t nb_output,
AVFrame *out_frame,
744 TF_Output *tf_outputs;
748 TF_Tensor **output_tensors;
750 TF_Tensor *input_tensor;
757 tf_input.oper = TF_GraphOperationByName(tf_model->
graph, input_name);
768 input.
data = (
float *)TF_TensorData(input_tensor);
778 if (nb_output != 1) {
781 TF_DeleteTensor(input_tensor);
787 if (tf_outputs ==
NULL) {
788 TF_DeleteTensor(input_tensor);
794 if (!output_tensors) {
795 TF_DeleteTensor(input_tensor);
801 for (
int i = 0;
i < nb_output; ++
i) {
802 tf_outputs[
i].oper = TF_GraphOperationByName(tf_model->
graph, output_names[
i]);
803 if (!tf_outputs[
i].oper) {
804 TF_DeleteTensor(input_tensor);
810 tf_outputs[
i].index = 0;
814 &tf_input, &input_tensor, 1,
815 tf_outputs, output_tensors, nb_output,
817 if (TF_GetCode(tf_model->
status) != TF_OK) {
818 TF_DeleteTensor(input_tensor);
825 for (uint32_t
i = 0;
i < nb_output; ++
i) {
826 output.
height = TF_Dim(output_tensors[
i], 1);
827 output.
width = TF_Dim(output_tensors[
i], 2);
828 output.
channels = TF_Dim(output_tensors[
i], 3);
829 output.
data = TF_TensorData(output_tensors[
i]);
830 output.
dt = TF_TensorType(output_tensors[
i]);
844 for (uint32_t
i = 0;
i < nb_output; ++
i) {
845 if (output_tensors[
i]) {
846 TF_DeleteTensor(output_tensors[
i]);
849 TF_DeleteTensor(input_tensor);
856 const char **output_names, uint32_t nb_output,
AVFrame *out_frame)
871 return execute_model_tf(model, input_name, in_frame, output_names, nb_output, out_frame, 1);
879 tf_model = (*model)->
model;
880 if (tf_model->
graph){
881 TF_DeleteGraph(tf_model->
graph);
888 TF_DeleteStatus(tf_model->
status);
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define AVIO_FLAG_READ
read-only
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
int64_t avio_size(AVIOContext *s)
Get the filesize.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
void ff_dnn_free_model_native(DNNModel **model)
DNNModel * ff_dnn_load_model_native(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
DNN inference functions interface for native backend.
DNN inference functions interface for native backend.
DNN inference functions interface for native backend.
layer pad (equivalent to tf.pad) for native backend.
static DNNReturnType add_conv_layer(TFModel *tf_model, TF_Operation *transpose_op, TF_Operation **cur_op, ConvolutionalParams *params, const int layer)
static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input_name)
static DNNReturnType add_depth_to_space_layer(TFModel *tf_model, TF_Operation **cur_op, DepthToSpaceParams *params, const int layer)
static DNNReturnType execute_model_tf(const DNNModel *model, const char *input_name, AVFrame *in_frame, const char **output_names, uint32_t nb_output, AVFrame *out_frame, int do_ioproc)
static DNNReturnType load_native_model(TFModel *tf_model, const char *model_filename)
DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, const char *input_name, AVFrame *in_frame, const char **output_names, uint32_t nb_output, AVFrame *out_frame)
static TF_Tensor * allocate_input_tensor(const DNNData *input)
DNNModel * ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
#define CLEANUP_ON_ERROR(tf_model)
static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename)
AVFILTER_DEFINE_CLASS(dnn_tensorflow)
static DNNReturnType get_output_tf(void *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
static const AVOption dnn_tensorflow_options[]
static DNNReturnType add_pad_layer(TFModel *tf_model, TF_Operation **cur_op, LayerPadParams *params, const int layer)
static TF_Buffer * read_graph(const char *model_filename)
void ff_dnn_free_model_tf(DNNModel **model)
static DNNReturnType add_maximum_layer(TFModel *tf_model, TF_Operation **cur_op, DnnLayerMaximumParams *params, const int layer)
static void free_buffer(void *data, size_t length)
DNN inference functions interface for TensorFlow backend.
DNNReturnType ff_proc_from_dnn_to_frame(AVFrame *frame, DNNData *output, void *log_ctx)
DNNReturnType ff_proc_from_frame_to_dnn(AVFrame *frame, DNNData *input, DNNFunctionType func_type, void *log_ctx)
DNN input&output process between AVFrame and DNNData.
const OptionDef options[]
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
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.
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
Describe the class of an AVClass context structure.
This structure describes decoded (raw) audio or video data.
DNNActivationFunc activation
int(* pre_proc)(AVFrame *frame_in, DNNData *model_input, AVFilterContext *filter_ctx)
int(* post_proc)(AVFrame *frame_out, DNNData *model_output, AVFilterContext *filter_ctx)
DNNReturnType(* get_output)(void *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
DNNReturnType(* get_input)(void *model, DNNData *input, const char *input_name)
AVFilterContext * filter_ctx
DNNFunctionType func_type
union DnnLayerMaximumParams::@202 val
#define av_malloc_array(a, b)
static FilteringContext * filter_ctx
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: