History

dnn: convert tf.pad to native model in python script, and load/execute it in the c code.

since tf.pad is enabled, the conv2d(valid) changes back to its original behavior.

Guo, Yejun committed 7 years ago

Commit ccbab411 parent 3805aae3 files · +54 −6Browse files

Showing the changes to tools/python/convert_from_tensorflow.py (1 of 3 files). Show all

Unified · Split

tools/python/convert_from_tensorflow.py+18−5
@@ -23,9 +23,6 @@ import sys, struct · 22 unmodified lines
2323
2424 __all__ = ['convert_from_tensorflow']
2525
26−# as the first step to be compatible with vf_sr, it is not general.
27−# it will be refined step by step.
28−
2926 class TFConverter:
3027 def __init__(self, graph_def, nodes, outfile):
3128 self.graph_def = graph_def
@@ -36,9 +33,10 @@ class TFConverter: · 4 unmodified lines
3633 self.name_node_dict = {}
3734 self.edges = {}
3835 self.conv_activations = {'Relu':0, 'Tanh':1, 'Sigmoid':2, 'LeakyRelu':4}
39− self.conv_paddings = {'VALID':2, 'SAME':1}
36+ self.conv_paddings = {'VALID':0, 'SAME':1}
4037 self.converted_nodes = set()
41− self.op2code = {'Conv2D':1, 'DepthToSpace':2}
38+ self.op2code = {'Conv2D':1, 'DepthToSpace':2, 'MirrorPad':3}
39+ self.mirrorpad_mode = {'CONSTANT':0, 'REFLECT':1, 'SYMMETRIC':2}
4240
4341
4442 def dump_for_tensorboard(self):
@@ -101,6 +99,19 @@ class TFConverter: · 56 unmodified lines
10199 self.converted_nodes.add(node.name)
102100
103101
102+ def dump_mirrorpad_to_file(self, node, f):
103+ assert(node.op == 'MirrorPad')
104+ self.layer_number = self.layer_number + 1
105+ mode = node.attr['mode'].s
106+ mode = self.mirrorpad_mode[mode.decode("utf-8")]
107+ np.array([self.op2code[node.op], mode], dtype=np.uint32).tofile(f)
108+ pnode = self.name_node_dict[node.input[1]]
109+ self.converted_nodes.add(pnode.name)
110+ paddings = pnode.attr['value'].tensor.tensor_content
111+ f.write(paddings)
112+ self.converted_nodes.add(node.name)
113+
114+
104115 def generate_layer_number(self):
105116 # in current hard code implementation, the layer number is the first data written to the native model file
106117 # it is not easy to know it at the beginning time in the general converter, so first do a dry run for compatibility
@@ -118,6 +129,8 @@ class TFConverter: · 11 unmodified lines
118129 self.dump_conv2d_to_file(node, f)
119130 elif node.op == 'DepthToSpace':
120131 self.dump_depth2space_to_file(node, f)
132+ elif node.op == 'MirrorPad':
133+ self.dump_mirrorpad_to_file(node, f)
121134
122135
123136 def dump_to_file(self):

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