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/convert_from_tensorflow.py (1 of 4 files). Show all

Unified · Split

tools/python/convert_from_tensorflow.py+7−8
@@ -71,7 +71,7 @@ class TFConverter: · 70 unmodified lines
7171 self.conv2d_scope_names = set()
7272 self.conv2d_scopename_inputname_dict = {}
7373 self.op2code = {'Conv2D':1, 'DepthToSpace':2, 'MirrorPad':3, 'Maximum':4, 'MathBinary':5}
74− self.mathbin2code = {'Sub':0}
74+ self.mathbin2code = {'Sub':0, 'Add':1}
7575 self.mirrorpad_mode = {'CONSTANT':0, 'REFLECT':1, 'SYMMETRIC':2}
7676 self.name_operand_dict = {}
7777
@@ -255,8 +255,7 @@ class TFConverter: · 177 unmodified lines
255255 np.array([input_operand_index, output_operand_index], dtype=np.uint32).tofile(f)
256256
257257
258− def dump_sub_to_file(self, node, f):
259− assert(node.op == 'Sub')
258+ def dump_mathbinary_to_file(self, node, f):
260259 self.layer_number = self.layer_number + 1
261260 self.converted_nodes.add(node.name)
262261 i0_node = self.name_node_dict[node.input[0]]
@@ -264,15 +263,13 @@ class TFConverter: · 1 unmodified line
264263 np.array([self.op2code['MathBinary'], self.mathbin2code[node.op]], dtype=np.uint32).tofile(f)
265264 if i0_node.op == 'Const':
266265 scalar = i0_node.attr['value'].tensor.float_val[0]
267− assert(i0_node.name.find('sub/x'))
268− np.array([1], dtype=np.uint32).tofile(f)
266+ np.array([1], dtype=np.uint32).tofile(f) # broadcast: 1
269267 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
271269 input_operand_index = self.add_operand(i1_node.name, Operand.IOTYPE_INPUT)
272270 np.array([input_operand_index], dtype=np.uint32).tofile(f)
273271 elif i1_node.op == 'Const':
274272 scalar = i1_node.attr['value'].tensor.float_val[0]
275− assert(i1_node.name.find('sub/y'))
276273 np.array([0], dtype=np.uint32).tofile(f)
277274 input_operand_index = self.add_operand(i0_node.name, Operand.IOTYPE_INPUT)
278275 np.array([input_operand_index], dtype=np.uint32).tofile(f)
@@ -309,7 +306,9 @@ class TFConverter: · 30 unmodified lines
309306 elif node.op == 'Maximum':
310307 self.dump_maximum_to_file(node, f)
311308 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)
313312
314313
315314 def dump_operands_to_file(self, f):

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