more math unary operations will be added here It can be tested with the model file generated with below python scripy: import tensorflow as tf import numpy as np import imageio in_img = imageio.imread('input.jpeg') in_img = in_img.astype(np.float32)/255.0 in_data = in_img[np.newaxis, :] x = tf.placeholder(tf.float32, shape=[1, None, None, 3], name='dnn_in') x1 = tf.subtract(x, 0.5) x2 = tf.abs(x1) y = tf.identity(x2, name='dnn_out') sess=tf.Session() sess.run(tf.global_variables_initializer()) graph_def = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, ['dnn_out']) tf.train.write_graph(graph_def, '.', 'image_process.pb', as_text=False) print("image_process.pb generated, please use \ path_to_ffmpeg/tools/python/convert.py to generate image_process.model\n") output = sess.run(y, feed_dict={x: in_data}) imageio.imsave("out.jpg", np.squeeze(output))
Unified · Split
| @@ -6,6 +6,7 @@ OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_con · 5 unmodified lines | ||
| 6 | 6 | OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_depth2space.o |
| 7 | 7 | OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_maximum.o |
| 8 | 8 | OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_mathbinary.o |
| 9 | +OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_mathunary.o | |
| 9 | 10 | |
| 10 | 11 | DNN-OBJS-$(CONFIG_LIBTENSORFLOW) += dnn/dnn_backend_tf.o |
| 11 | 12 | |
| @@ -42,6 +42,7 @@ typedef enum { · 41 unmodified lines | ||
| 42 | 42 | DLT_MIRROR_PAD = 3, |
| 43 | 43 | DLT_MAXIMUM = 4, |
| 44 | 44 | DLT_MATH_BINARY = 5, |
| 45 | + DLT_MATH_UNARY = 6, | |
| 45 | 46 | DLT_COUNT |
| 46 | 47 | } DNNLayerType; |
| 47 | 48 | |
| @@ -0,0 +1,80 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2020 | |
| 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 native backend implementation. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +#include "dnn_backend_native.h" | |
| 27 | +#include "libavutil/avassert.h" | |
| 28 | +#include "dnn_backend_native_layer_mathunary.h" | |
| 29 | + | |
| 30 | +int dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size) | |
| 31 | +{ | |
| 32 | + DnnLayerMathUnaryParams *params; | |
| 33 | + int dnn_size = 0; | |
| 34 | + params = av_malloc(sizeof(*params)); | |
| 35 | + if(!params) | |
| 36 | + return 0; | |
| 37 | + | |
| 38 | + params->un_op = (int32_t)avio_rl32(model_file_context); | |
| 39 | + dnn_size += 4; | |
| 40 | + layer->params = params; | |
| 41 | + layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context); | |
| 42 | + layer->output_operand_index = (int32_t)avio_rl32(model_file_context); | |
| 43 | + dnn_size += 8; | |
| 44 | + | |
| 45 | + return dnn_size; | |
| 46 | + | |
| 47 | +} | |
| 48 | + | |
| 49 | +int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes, | |
| 50 | + int32_t output_operand_index, const void *parameters) | |
| 51 | +{ | |
| 52 | + const DnnOperand *input = &operands[input_operand_indexes[0]]; | |
| 53 | + DnnOperand *output = &operands[output_operand_index]; | |
| 54 | + const DnnLayerMathUnaryParams *params = (const DnnLayerMathUnaryParams *)parameters; | |
| 55 | + int dims_count; | |
| 56 | + const float *src; | |
| 57 | + float *dst; | |
| 58 | + | |
| 59 | + for (int i = 0; i < 4; ++i) | |
| 60 | + output->dims[i] = input->dims[i]; | |
| 61 | + | |
| 62 | + output->data_type = input->data_type; | |
| 63 | + output->length = calculate_operand_data_length(output); | |
| 64 | + output->data = av_realloc(output->data, output->length); | |
| 65 | + if (!output->data) | |
| 66 | + return DNN_ERROR; | |
| 67 | + | |
| 68 | + dims_count = calculate_operand_dims_count(output); | |
| 69 | + src = input->data; | |
| 70 | + dst = output->data; | |
| 71 | + | |
| 72 | + switch (params->un_op) { | |
| 73 | + case DMUO_ABS: | |
| 74 | + for (int i = 0; i < dims_count; ++i) | |
| 75 | + dst[i] = FFABS(src[i]); | |
| 76 | + return 0; | |
| 77 | + default: | |
| 78 | + return -1; | |
| 79 | + } | |
| 80 | +} | |
| @@ -0,0 +1,45 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2020 | |
| 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 inference functions interface for native backend. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +#ifndef AVFILTER_DNN_DNN_BACKEND_NATIVE_LAYER_MATHUNARY_H | |
| 27 | +#define AVFILTER_DNN_DNN_BACKEND_NATIVE_LAYER_MATHUNARY_H | |
| 28 | + | |
| 29 | +#include "libavformat/avio.h" | |
| 30 | +#include "dnn_backend_native.h" | |
| 31 | + | |
| 32 | +typedef enum { | |
| 33 | + DMUO_ABS = 0, | |
| 34 | + DMUO_COUNT | |
| 35 | +} DNNMathUnaryOperation; | |
| 36 | + | |
| 37 | +typedef struct DnnLayerMathUnaryParams{ | |
| 38 | + DNNMathUnaryOperation un_op; | |
| 39 | +} DnnLayerMathUnaryParams; | |
| 40 | + | |
| 41 | +int dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size); | |
| 42 | +int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes, | |
| 43 | + int32_t output_operand_index, const void *parameters); | |
| 44 | + | |
| 45 | +#endif | |
| @@ -25,6 +25,7 @@ · 24 unmodified lines | ||
| 25 | 25 | #include "dnn_backend_native_layer_depth2space.h" |
| 26 | 26 | #include "dnn_backend_native_layer_maximum.h" |
| 27 | 27 | #include "dnn_backend_native_layer_mathbinary.h" |
| 28 | +#include "dnn_backend_native_layer_mathunary.h" | |
| 28 | 29 | |
| 29 | 30 | LayerFunc layer_funcs[DLT_COUNT] = { |
| 30 | 31 | {NULL, NULL}, |
| @@ -33,4 +34,5 @@ LayerFunc layer_funcs[DLT_COUNT] = { · 2 unmodified lines | ||
| 33 | 34 | {dnn_execute_layer_pad, dnn_load_layer_pad}, |
| 34 | 35 | {dnn_execute_layer_maximum, dnn_load_layer_maximum}, |
| 35 | 36 | {dnn_execute_layer_math_binary, dnn_load_layer_math_binary}, |
| 37 | + {dnn_execute_layer_math_unary, dnn_load_layer_math_unary}, | |
| 36 | 38 | }; |
| @@ -70,8 +70,9 @@ class TFConverter: · 69 unmodified lines | ||
| 70 | 70 | self.converted_nodes = set() |
| 71 | 71 | self.conv2d_scope_names = set() |
| 72 | 72 | self.conv2d_scopename_inputname_dict = {} |
| 73 | − self.op2code = {'Conv2D':1, 'DepthToSpace':2, 'MirrorPad':3, 'Maximum':4, 'MathBinary':5} | |
| 73 | + self.op2code = {'Conv2D':1, 'DepthToSpace':2, 'MirrorPad':3, 'Maximum':4, 'MathBinary':5, 'MathUnary':6} | |
| 74 | 74 | self.mathbin2code = {'Sub':0, 'Add':1, 'Mul':2, 'RealDiv':3, 'Minimum':4} |
| 75 | + self.mathun2code = {'Abs':0} | |
| 75 | 76 | self.mirrorpad_mode = {'CONSTANT':0, 'REFLECT':1, 'SYMMETRIC':2} |
| 76 | 77 | self.name_operand_dict = {} |
| 77 | 78 | |
| @@ -286,6 +287,17 @@ class TFConverter: · 208 unmodified lines | ||
| 286 | 287 | np.array([output_operand_index], dtype=np.uint32).tofile(f) |
| 287 | 288 | |
| 288 | 289 | |
| 290 | + def dump_mathunary_to_file(self, node, f): | |
| 291 | + self.layer_number = self.layer_number + 1 | |
| 292 | + self.converted_nodes.add(node.name) | |
| 293 | + i0_node = self.name_node_dict[node.input[0]] | |
| 294 | + np.array([self.op2code['MathUnary'], self.mathun2code[node.op]], dtype=np.uint32).tofile(f) | |
| 295 | + input_operand_index = self.add_operand(i0_node.name, Operand.IOTYPE_INPUT) | |
| 296 | + np.array([input_operand_index], dtype=np.uint32).tofile(f) | |
| 297 | + output_operand_index = self.add_operand(node.name, Operand.IOTYPE_OUTPUT) | |
| 298 | + np.array([output_operand_index],dtype=np.uint32).tofile(f) | |
| 299 | + | |
| 300 | + | |
| 289 | 301 | def dump_layers_to_file(self, f): |
| 290 | 302 | for node in self.nodes: |
| 291 | 303 | if node.name in self.converted_nodes: |
| @@ -307,6 +319,8 @@ class TFConverter: · 15 unmodified lines | ||
| 307 | 319 | self.dump_maximum_to_file(node, f) |
| 308 | 320 | elif node.op in self.mathbin2code: |
| 309 | 321 | self.dump_mathbinary_to_file(node, f) |
| 322 | + elif node.op in self.mathun2code: | |
| 323 | + self.dump_mathunary_to_file(node, f) | |
| 310 | 324 | |
| 311 | 325 | |
| 312 | 326 | def dump_operands_to_file(self, f): |
| @@ -23,4 +23,4 @@ str = 'FFMPEGDNNNATIVE' · 22 unmodified lines | ||
| 23 | 23 | major = 1 |
| 24 | 24 | |
| 25 | 25 | # increase minor when we don't have to re-convert the model file |
| 26 | −minor = 5 | |
| 26 | +minor = 6 | |