History

dnn/native: add native support for 'add'

It can be tested with the model file generated with below python script: import tensorflow as tf import numpy as np import imageio in_img = imageio.imread('input.jpg') 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') z1 = 0.039 + x z2 = x + 0.042 z3 = z1 + z2 z4 = z3 - 0.381 z5 = z4 - x y = tf.math.maximum(z5, 0.0, 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))

Guo, Yejun committed 6 years ago

Commit 6aa7e071 parent 36083454 files · +22 −9Browse files

Showing the changes to tools/python (2 of 4 files). Show all

Unified · Split

2 files
  1. tools/python/convert_from_tensorflow.py +7 −8
  2. tools/python/convert_header.py +1 −1
tools/python/convert_from_tensorflow.py+7−8
@@ -71,7 +71,7 @@ class TFConverter: · 70 unmodified lines
71 self.conv2d_scope_names = set()71 self.conv2d_scope_names = set()
72 self.conv2d_scopename_inputname_dict = {}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}
74 self.mathbin2code = {'Sub':0}74 self.mathbin2code = {'Sub':0, 'Add':1}
75 self.mirrorpad_mode = {'CONSTANT':0, 'REFLECT':1, 'SYMMETRIC':2}75 self.mirrorpad_mode = {'CONSTANT':0, 'REFLECT':1, 'SYMMETRIC':2}
76 self.name_operand_dict = {}76 self.name_operand_dict = {}
77 77
@@ -255,8 +255,7 @@ class TFConverter: · 177 unmodified lines
255 np.array([input_operand_index, output_operand_index], dtype=np.uint32).tofile(f)255 np.array([input_operand_index, output_operand_index], dtype=np.uint32).tofile(f)
256 256
257 257
258 def dump_sub_to_file(self, node, f):258 def dump_mathbinary_to_file(self, node, f):
259 assert(node.op == 'Sub')
260 self.layer_number = self.layer_number + 1259 self.layer_number = self.layer_number + 1
261 self.converted_nodes.add(node.name)260 self.converted_nodes.add(node.name)
262 i0_node = self.name_node_dict[node.input[0]]261 i0_node = self.name_node_dict[node.input[0]]
@@ -264,15 +263,13 @@ class TFConverter: · 1 unmodified line
264 np.array([self.op2code['MathBinary'], self.mathbin2code[node.op]], dtype=np.uint32).tofile(f)263 np.array([self.op2code['MathBinary'], self.mathbin2code[node.op]], dtype=np.uint32).tofile(f)
265 if i0_node.op == 'Const':264 if i0_node.op == 'Const':
266 scalar = i0_node.attr['value'].tensor.float_val[0]265 scalar = i0_node.attr['value'].tensor.float_val[0]
267 assert(i0_node.name.find('sub/x'))266 np.array([1], dtype=np.uint32).tofile(f) # broadcast: 1
268 np.array([1], dtype=np.uint32).tofile(f)
269 np.array([scalar], dtype=np.float32).tofile(f)267 np.array([scalar], dtype=np.float32).tofile(f)
270 np.array([0], dtype=np.uint32).tofile(f)268 np.array([0], dtype=np.uint32).tofile(f) # broadcast: 0
271 input_operand_index = self.add_operand(i1_node.name, Operand.IOTYPE_INPUT)269 input_operand_index = self.add_operand(i1_node.name, Operand.IOTYPE_INPUT)
272 np.array([input_operand_index], dtype=np.uint32).tofile(f)270 np.array([input_operand_index], dtype=np.uint32).tofile(f)
273 elif i1_node.op == 'Const':271 elif i1_node.op == 'Const':
274 scalar = i1_node.attr['value'].tensor.float_val[0]272 scalar = i1_node.attr['value'].tensor.float_val[0]
275 assert(i1_node.name.find('sub/y'))
276 np.array([0], dtype=np.uint32).tofile(f)273 np.array([0], dtype=np.uint32).tofile(f)
277 input_operand_index = self.add_operand(i0_node.name, Operand.IOTYPE_INPUT)274 input_operand_index = self.add_operand(i0_node.name, Operand.IOTYPE_INPUT)
278 np.array([input_operand_index], dtype=np.uint32).tofile(f)275 np.array([input_operand_index], dtype=np.uint32).tofile(f)
@@ -309,7 +306,9 @@ class TFConverter: · 30 unmodified lines
309 elif node.op == 'Maximum':306 elif node.op == 'Maximum':
310 self.dump_maximum_to_file(node, f)307 self.dump_maximum_to_file(node, f)
311 elif node.op == 'Sub':308 elif node.op == 'Sub':
312 self.dump_sub_to_file(node, f)309 self.dump_mathbinary_to_file(node, f)
310 elif node.op == 'Add':
311 self.dump_mathbinary_to_file(node, f)
313 312
314 313
315 def dump_operands_to_file(self, f):314 def dump_operands_to_file(self, f):
tools/python/convert_header.py+1−1
@@ -23,4 +23,4 @@ str = 'FFMPEGDNNNATIVE' · 22 unmodified lines
23major = 123major = 1
24 24
25# increase minor when we don't have to re-convert the model file25# increase minor when we don't have to re-convert the model file
26minor = 126minor = 2

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