History

dnn_backend_native_layer_mathbinary: add sub support

more math binary operations will be added here

Guo, Yejun committed 6 years ago

Commit ffa15611 parent 2114c427 files · +219 −4Browse files

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

Unified · Split

2 files
  1. tools/python/convert_from_tensorflow.py +52 −3
  2. tools/python/convert_header.py +1 −1
tools/python/convert_from_tensorflow.py+52−3
@@ -70,7 +70,8 @@ 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}
73+ self.op2code = {'Conv2D':1, 'DepthToSpace':2, 'MirrorPad':3, 'Maximum':4, 'MathBinary':5}
74+ self.mathbin2code = {'Sub':0}
7475 self.mirrorpad_mode = {'CONSTANT':0, 'REFLECT':1, 'SYMMETRIC':2}
7576 self.name_operand_dict = {}
7677
@@ -113,6 +114,8 @@ class TFConverter: · 36 unmodified lines
113114 # if activation is None, and BiasAdd.next is the last op which is Identity
114115 if conv2d_scope_name + '/BiasAdd' in self.edges:
115116 anode = self.edges[conv2d_scope_name + '/BiasAdd'][0]
117+ if anode.op not in self.conv_activations:
118+ anode = None
116119 else:
117120 anode = None
118121 return knode, bnode, dnode, anode
@@ -252,14 +255,47 @@ class TFConverter: · 133 unmodified lines
252255 np.array([input_operand_index, output_operand_index], dtype=np.uint32).tofile(f)
253256
254257
258+ def dump_sub_to_file(self, node, f):
259+ assert(node.op == 'Sub')
260+ self.layer_number = self.layer_number + 1
261+ self.converted_nodes.add(node.name)
262+ i0_node = self.name_node_dict[node.input[0]]
263+ i1_node = self.name_node_dict[node.input[1]]
264+ np.array([self.op2code['MathBinary'], self.mathbin2code[node.op]], dtype=np.uint32).tofile(f)
265+ if i0_node.op == 'Const':
266+ 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)
269+ np.array([scalar], dtype=np.float32).tofile(f)
270+ np.array([0], dtype=np.uint32).tofile(f)
271+ input_operand_index = self.add_operand(i1_node.name, Operand.IOTYPE_INPUT)
272+ np.array([input_operand_index], dtype=np.uint32).tofile(f)
273+ elif i1_node.op == 'Const':
274+ 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)
277+ input_operand_index = self.add_operand(i0_node.name, Operand.IOTYPE_INPUT)
278+ np.array([input_operand_index], dtype=np.uint32).tofile(f)
279+ np.array([1], dtype=np.uint32).tofile(f)
280+ np.array([scalar], dtype=np.float32).tofile(f)
281+ else:
282+ np.array([0], dtype=np.uint32).tofile(f)
283+ input_operand_index = self.add_operand(i0_node.name, Operand.IOTYPE_INPUT)
284+ np.array([input_operand_index], dtype=np.uint32).tofile(f)
285+ np.array([0], dtype=np.uint32).tofile(f)
286+ input_operand_index = self.add_operand(i1_node.name, Operand.IOTYPE_INPUT)
287+ np.array([input_operand_index], dtype=np.uint32).tofile(f)
288+ output_operand_index = self.add_operand(node.name, Operand.IOTYPE_OUTPUT)
289+ np.array([output_operand_index], dtype=np.uint32).tofile(f)
290+
291+
255292 def dump_layers_to_file(self, f):
256293 for node in self.nodes:
257294 if node.name in self.converted_nodes:
258295 continue
259296
260297 # conv2d with dilation generates very complex nodes, so handle it in special
261− scope_name = TFConverter.get_scope_name(node.name)
262− if scope_name in self.conv2d_scope_names:
298+ if self.in_conv2d_scope(node.name):
263299 if node.op == 'Conv2D':
264300 self.dump_complex_conv2d_to_file(node, f)
265301 continue
@@ -272,6 +308,8 @@ class TFConverter: · 6 unmodified lines
272308 self.dump_mirrorpad_to_file(node, f)
273309 elif node.op == 'Maximum':
274310 self.dump_maximum_to_file(node, f)
311+ elif node.op == 'Sub':
312+ self.dump_sub_to_file(node, f)
275313
276314
277315 def dump_operands_to_file(self, f):
@@ -352,6 +390,17 @@ class TFConverter: · 74 unmodified lines
352390 return name[0:index]
353391
354392
393+ def in_conv2d_scope(self, name):
394+ inner_scope = TFConverter.get_scope_name(name)
395+ if inner_scope == "":
396+ return False;
397+ for scope in self.conv2d_scope_names:
398+ index = inner_scope.find(scope)
399+ if index == 0:
400+ return True
401+ return False
402+
403+
355404 def generate_conv2d_scope_info(self):
356405 # mostly, conv2d is a sub block in graph, get the scope name
357406 for node in self.nodes:
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 = 0
26+minor = 1

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