forked from cypw/MXNet2Caffe
-
Notifications
You must be signed in to change notification settings - Fork 53
/
prototxt_basic.py
245 lines (224 loc) · 8.78 KB
/
prototxt_basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# prototxt_basic
import sys
import pprint
attrstr = "attrs"
#attrstr = "param"
def data(txt_file, info):
txt_file.write('name: "mxnet-mdoel"\n')
txt_file.write('layer {\n')
txt_file.write(' name: "data"\n')
txt_file.write(' type: "Input"\n')
txt_file.write(' top: "data"\n')
txt_file.write(' input_param {\n')
txt_file.write(' shape: { dim: 1 dim: 3 dim: 112 dim: 112 }\n') # TODO
txt_file.write(' }\n')
txt_file.write('}\n')
txt_file.write('\n')
def fuzzy_haskey(d, key):
for eachkey in d:
if key in eachkey:
return True
return False
def Convolution(txt_file, info):
#if info['attrs']['no_bias'] == 'True':
#bias_term = 'false'
#else:
#bias_term = 'true'
#if info['top'] == 'conv1_1':
#pprint.pprint(info)
if fuzzy_haskey(info['params'], 'bias'):
bias_term = 'true'
elif info[attrstr].has_key('no_bias') and info['attrs']['no_bias'] == 'True':
bias_term = 'false'
else:
bias_term = 'true'
txt_file.write('layer {\n')
txt_file.write(' bottom: "%s"\n' % info['bottom'][0])
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write(' name: "%s"\n' % info['top'])
txt_file.write(' type: "Convolution"\n')
txt_file.write(' convolution_param {\n')
txt_file.write(' num_output: %s\n' % info[attrstr]['num_filter'])
txt_file.write(' kernel_size: %s\n' % info[attrstr]['kernel'].split('(')[1].split(',')[0]) # TODO
if info[attrstr].has_key('pad'):
txt_file.write(' pad: %s\n' % info[attrstr]['pad'].split('(')[1].split(',')[0]) # TODO
if info[attrstr].has_key('num_group') and int(info[attrstr]['num_group']) >1:
txt_file.write(' group: %s\n' % info[attrstr]['num_group'])
txt_file.write(' engine: CAFFE\n')
if info[attrstr].has_key('stride'):
txt_file.write(' stride: %s\n' % info[attrstr]['stride'].split('(')[1].split(',')[0])
txt_file.write(' bias_term: %s\n' % bias_term)
txt_file.write(' }\n')
if 'share' in info.keys() and info['share']:
txt_file.write(' param {\n')
txt_file.write(' name: "%s"\n' % info['params'][0])
txt_file.write(' }\n')
txt_file.write('}\n')
txt_file.write('\n')
def ChannelwiseConvolution(txt_file, info):
Convolution(txt_file, info)
def BatchNorm(txt_file, info):
#pprint.pprint(info)
txt_file.write('layer {\n')
txt_file.write(' bottom: "%s"\n' % info['bottom'][0])
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write(' name: "%s"\n' % info['top'])
txt_file.write(' type: "BatchNorm"\n')
txt_file.write(' batch_norm_param {\n')
txt_file.write(' use_global_stats: true\n') # TODO
if info[attrstr].has_key('momentum'):
txt_file.write(' moving_average_fraction: %s\n' % info[attrstr]['momentum'])
else:
txt_file.write(' moving_average_fraction: 0.9\n')
if info[attrstr].has_key('eps'):
txt_file.write(' eps: %s\n' % info[attrstr]['eps'])
else:
txt_file.write(' eps: 2e-05\n')
txt_file.write(' }\n')
txt_file.write('}\n')
# if info['fix_gamma'] is "False": # TODO
txt_file.write('layer {\n')
txt_file.write(' bottom: "%s"\n' % info['top'])
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write(' name: "%s_scale"\n' % info['top'])
txt_file.write(' type: "Scale"\n')
txt_file.write(' scale_param { bias_term: true }\n')
txt_file.write('}\n')
txt_file.write('\n')
pass
def Activation(txt_file, info):
txt_file.write('layer {\n')
txt_file.write(' bottom: "%s"\n' % info['bottom'][0])
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write(' name: "%s"\n' % info['top'])
if info[attrstr]['act_type']=='sigmoid':
txt_file.write(' type: "Sigmoid"\n')
else:
txt_file.write(' type: "ReLU"\n') # TODO
txt_file.write('}\n')
txt_file.write('\n')
pass
def Concat(txt_file, info):
txt_file.write('layer {\n')
txt_file.write(' name: "%s"\n' % info['top'])
txt_file.write(' type: "Concat"\n')
for bottom_i in info['bottom']:
txt_file.write(' bottom: "%s"\n' % bottom_i)
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write('}\n')
txt_file.write('\n')
pass
def ElementWiseSum(txt_file, info):
txt_file.write('layer {\n')
txt_file.write(' name: "%s"\n' % info['top'])
txt_file.write(' type: "Eltwise"\n')
for bottom_i in info['bottom']:
txt_file.write(' bottom: "%s"\n' % bottom_i)
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write(' eltwise_param { operation: SUM }\n')
txt_file.write('}\n')
txt_file.write('\n')
pass
def Pooling(txt_file, info):
pool_type = 'AVE' if info[attrstr]['pool_type'] == 'avg' else 'MAX'
txt_file.write('layer {\n')
txt_file.write(' bottom: "%s"\n' % info['bottom'][0])
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write(' name: "%s"\n' % info['top'])
txt_file.write(' type: "Pooling"\n')
txt_file.write(' pooling_param {\n')
txt_file.write(' pool: %s\n' % pool_type) # TODO
if info[attrstr].has_key('global_pool') and info[attrstr]['global_pool'] == 'True':
txt_file.write(' global_pooling: true\n')
else:
txt_file.write(' kernel_size: %s\n' % info[attrstr]['kernel'].split('(')[1].split(',')[0])
txt_file.write(' stride: %s\n' % info[attrstr]['stride'].split('(')[1].split(',')[0])
if info[attrstr].has_key('pad'):
txt_file.write(' pad: %s\n' % info[attrstr]['pad'].split('(')[1].split(',')[0])
txt_file.write(' }\n')
txt_file.write('}\n')
txt_file.write('\n')
pass
def FullyConnected(txt_file, info):
txt_file.write('layer {\n')
txt_file.write(' bottom: "%s"\n' % info['bottom'][0])
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write(' name: "%s"\n' % info['top'])
txt_file.write(' type: "InnerProduct"\n')
txt_file.write(' inner_product_param {\n')
txt_file.write(' num_output: %s\n' % info[attrstr]['num_hidden'])
txt_file.write(' }\n')
txt_file.write('}\n')
txt_file.write('\n')
print("fullconnect: ",info[attrstr]['num_hidden'],info['params'])
pass
def Flatten(txt_file, info):
pass
def SoftmaxOutput(txt_file, info):
pass
def LeakyReLU(txt_file, info):
if info[attrstr]['act_type'] == 'elu':
txt_file.write('layer {\n')
txt_file.write(' bottom: "%s"\n' % info['bottom'][0])
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write(' name: "%s"\n' % info['top'])
txt_file.write(' type: "ELU"\n')
txt_file.write(' elu_param { alpha: 0.25 }\n')
txt_file.write('}\n')
txt_file.write('\n')
elif info[attrstr]['act_type'] == 'prelu':
txt_file.write('layer {\n')
txt_file.write(' bottom: "%s"\n' % info['bottom'][0])
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write(' name: "%s"\n' % info['top'])
txt_file.write(' type: "PReLU"\n')
txt_file.write('}\n')
txt_file.write('\n')
else:
raise Exception("unsupported Activation")
def Eltwise(txt_file, info, op):
txt_file.write('layer {\n')
txt_file.write(' type: "Eltwise"\n')
txt_file.write(' top: "%s"\n' % info['top'])
txt_file.write(' name: "%s"\n' % info['top'])
for btom in info['bottom']:
txt_file.write(' bottom: "%s"\n' % btom)
txt_file.write(' eltwise_param { operation: %s }\n' % op)
txt_file.write('}\n')
txt_file.write('\n')
# ----------------------------------------------------------------
def write_node(txt_file, info):
if 'label' in info['name']:
return
if info['op'] == 'null' and info['name'] == 'data':
data(txt_file, info)
elif info['op'] == 'Convolution':
Convolution(txt_file, info)
elif info['op'] == 'ChannelwiseConvolution':
ChannelwiseConvolution(txt_file, info)
elif info['op'] == 'BatchNorm':
BatchNorm(txt_file, info)
elif info['op'] == 'Activation':
Activation(txt_file, info)
elif info['op'] == 'ElementWiseSum': #elemwise_add
ElementWiseSum(txt_file, info)
elif info['op'] == '_Plus':
ElementWiseSum(txt_file, info)
elif info['op'] == 'Concat':
Concat(txt_file, info)
elif info['op'] == 'Pooling':
Pooling(txt_file, info)
elif info['op'] == 'Flatten':
Flatten(txt_file, info)
elif info['op'] == 'FullyConnected':
FullyConnected(txt_file, info)
elif info['op'] == 'SoftmaxOutput':
SoftmaxOutput(txt_file, info)
elif info['op'] == 'LeakyReLU':
LeakyReLU(txt_file, info)
elif info['op'] == 'elemwise_add':
ElementWiseSum(txt_file, info)
else:
#pprint.pprint(info)
#sys.exit("Warning! Unknown mxnet op:{}".format(info['op']))
print "Warning! Skip Unknown mxnet op:{}".format(info['op'])