History

dnn_backend_native_layer_mathunary: add abs support

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))

Ting Fu committed 6 years ago

Commit f73cc611 parent b6d65977 files · +145 −2Browse files

Unified · Split

7 files
  1. libavfilter/dnn/Makefile +1 −0
  2. libavfilter/dnn/dnn_backend_native.h +1 −0
  3. libavfilter/dnn/dnn_backend_native_layer_mathunary.c +80 −0
  4. libavfilter/dnn/dnn_backend_native_layer_mathunary.h +45 −0
  5. libavfilter/dnn/dnn_backend_native_layers.c +2 −0
  6. tools/python/convert_from_tensorflow.py +15 −1
  7. tools/python/convert_header.py +1 −1
libavfilter/dnn/Makefile+1−0
@@ -6,6 +6,7 @@ OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_con · 5 unmodified lines
66 OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_depth2space.o
77 OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_maximum.o
88 OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_mathbinary.o
9+OBJS-$(CONFIG_DNN) += dnn/dnn_backend_native_layer_mathunary.o
910
1011 DNN-OBJS-$(CONFIG_LIBTENSORFLOW) += dnn/dnn_backend_tf.o
1112
libavfilter/dnn/dnn_backend_native.h+1−0
@@ -42,6 +42,7 @@ typedef enum { · 41 unmodified lines
4242 DLT_MIRROR_PAD = 3,
4343 DLT_MAXIMUM = 4,
4444 DLT_MATH_BINARY = 5,
45+ DLT_MATH_UNARY = 6,
4546 DLT_COUNT
4647 } DNNLayerType;
4748
libavfilter/dnn/dnn_backend_native_layer_mathunary.c+80−0
@@ -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+}
libavfilter/dnn/dnn_backend_native_layer_mathunary.h+45−0
@@ -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
libavfilter/dnn/dnn_backend_native_layers.c+2−0
@@ -25,6 +25,7 @@ · 24 unmodified lines
2525 #include "dnn_backend_native_layer_depth2space.h"
2626 #include "dnn_backend_native_layer_maximum.h"
2727 #include "dnn_backend_native_layer_mathbinary.h"
28+#include "dnn_backend_native_layer_mathunary.h"
2829
2930 LayerFunc layer_funcs[DLT_COUNT] = {
3031 {NULL, NULL},
@@ -33,4 +34,5 @@ LayerFunc layer_funcs[DLT_COUNT] = { · 2 unmodified lines
3334 {dnn_execute_layer_pad, dnn_load_layer_pad},
3435 {dnn_execute_layer_maximum, dnn_load_layer_maximum},
3536 {dnn_execute_layer_math_binary, dnn_load_layer_math_binary},
37+ {dnn_execute_layer_math_unary, dnn_load_layer_math_unary},
3638 };
tools/python/convert_from_tensorflow.py+15−1
@@ -70,8 +70,9 @@ class TFConverter: · 69 unmodified lines
7070 self.converted_nodes = set()
7171 self.conv2d_scope_names = set()
7272 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}
7474 self.mathbin2code = {'Sub':0, 'Add':1, 'Mul':2, 'RealDiv':3, 'Minimum':4}
75+ self.mathun2code = {'Abs':0}
7576 self.mirrorpad_mode = {'CONSTANT':0, 'REFLECT':1, 'SYMMETRIC':2}
7677 self.name_operand_dict = {}
7778
@@ -286,6 +287,17 @@ class TFConverter: · 208 unmodified lines
286287 np.array([output_operand_index], dtype=np.uint32).tofile(f)
287288
288289
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+
289301 def dump_layers_to_file(self, f):
290302 for node in self.nodes:
291303 if node.name in self.converted_nodes:
@@ -307,6 +319,8 @@ class TFConverter: · 15 unmodified lines
307319 self.dump_maximum_to_file(node, f)
308320 elif node.op in self.mathbin2code:
309321 self.dump_mathbinary_to_file(node, f)
322+ elif node.op in self.mathun2code:
323+ self.dump_mathunary_to_file(node, f)
310324
311325
312326 def dump_operands_to_file(self, f):
tools/python/convert_header.py+1−1
@@ -23,4 +23,4 @@ str = 'FFMPEGDNNNATIVE' · 22 unmodified lines
2323 major = 1
2424
2525 # increase minor when we don't have to re-convert the model file
26−minor = 5
26+minor = 6

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t