History

dnn/native: add native support for avg_pool

Not support pooling strides in channel dimension yet.

Ting Fu committed 6 years ago

Commit 91efc411 parent 40597ad7 files · +223 −3Browse files

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

Unified · Split

tools/python/convert_from_tensorflow.py+36−1
@@ -67,10 +67,12 @@ class TFConverter: · 66 unmodified lines
6767 self.edges = {}
6868 self.conv_activations = {'Relu':0, 'Tanh':1, 'Sigmoid':2, 'None':3, 'LeakyRelu':4}
6969 self.conv_paddings = {'VALID':0, 'SAME':1}
70+ self.pool_paddings = {'VALID':0, 'SAME':1}
7071 self.converted_nodes = set()
7172 self.conv2d_scope_names = set()
7273 self.conv2d_scopename_inputname_dict = {}
73− self.op2code = {'Conv2D':1, 'DepthToSpace':2, 'MirrorPad':3, 'Maximum':4, 'MathBinary':5, 'MathUnary':6}
74+ self.op2code = {'Conv2D':1, 'DepthToSpace':2, 'MirrorPad':3, 'Maximum':4,
75+ 'MathBinary':5, 'MathUnary':6, 'AvgPool':7}
7476 self.mathbin2code = {'Sub':0, 'Add':1, 'Mul':2, 'RealDiv':3, 'Minimum':4}
7577 self.mathun2code = {'Abs':0, 'Sin':1, 'Cos':2, 'Tan':3, 'Asin':4,
7678 'Acos':5, 'Atan':6, 'Sinh':7, 'Cosh':8, 'Tanh':9, 'Asinh':10,
@@ -300,6 +302,37 @@ class TFConverter: · 223 unmodified lines
300302 np.array([output_operand_index],dtype=np.uint32).tofile(f)
301303
302304
305+ def dump_avg_pool_to_file(self, node, f):
306+ assert(node.op == 'AvgPool')
307+ self.layer_number = self.layer_number + 1
308+ self.converted_nodes.add(node.name)
309+ node0 = self.name_node_dict[node.input[0]]
310+ strides = node.attr['strides']
311+
312+ # Tensorflow do not support pooling strides in batch dimension and
313+ # current native NN do not support pooling strides in channel dimension, added assert() here.
314+ assert(strides.list.i[1]==strides.list.i[2])
315+ assert(strides.list.i[0]==1)
316+ assert(strides.list.i[3]==1)
317+ strides = strides.list.i[1]
318+ filter_node = node.attr['ksize']
319+ input_name = node.input[0]
320+
321+ # Tensorflow do not support pooling ksize in batch dimension and channel dimension.
322+ assert(filter_node.list.i[0]==1)
323+ assert(filter_node.list.i[3]==1)
324+ filter_height = filter_node.list.i[1]
325+ filter_width = filter_node.list.i[2]
326+
327+ padding = node.attr['padding'].s.decode("utf-8")
328+ np.array([self.op2code[node.op], strides, self.pool_paddings[padding], filter_height],
329+ dtype=np.uint32).tofile(f)
330+
331+ input_operand_index = self.add_operand(input_name, Operand.IOTYPE_INPUT)
332+ output_operand_index = self.add_operand(node.name, Operand.IOTYPE_OUTPUT)
333+ np.array([input_operand_index, output_operand_index],dtype=np.uint32).tofile(f)
334+
335+
303336 def dump_layers_to_file(self, f):
304337 for node in self.nodes:
305338 if node.name in self.converted_nodes:
@@ -313,6 +346,8 @@ class TFConverter: · 7 unmodified lines
313346
314347 if node.op == 'Conv2D':
315348 self.dump_simple_conv2d_to_file(node, f)
349+ if node.op == 'AvgPool':
350+ self.dump_avg_pool_to_file(node, f)
316351 elif node.op == 'DepthToSpace':
317352 self.dump_depth2space_to_file(node, f)
318353 elif node.op == 'MirrorPad':

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