-
Notifications
You must be signed in to change notification settings - Fork 121
/
prototxt_basic.py
169 lines (151 loc) · 6.07 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
# prototxt_basic
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: 10 dim: 3 dim: 224 dim: 224 }\n') # TODO
txt_file.write(' }\n')
txt_file.write('}\n')
txt_file.write('\n')
def Convolution(txt_file, info):
if info['param']['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['param']['num_filter'])
txt_file.write(' kernel_size: %s\n' % info['param']['kernel'].split('(')[1].split(',')[0]) # TODO
txt_file.write(' pad: %s\n' % info['param']['pad'].split('(')[1].split(',')[0]) # TODO
txt_file.write(' group: %s\n' % info['param']['num_group'])
txt_file.write(' stride: %s\n' % info['param']['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):
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
txt_file.write(' moving_average_fraction: 0.9\n') # TODO
txt_file.write(' eps: 0.001\n') # TODO
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'])
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('}\n')
txt_file.write('\n')
pass
def Pooling(txt_file, info):
pool_type = 'AVE' if info['param']['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
txt_file.write(' kernel_size: %s\n' % info['param']['kernel'].split('(')[1].split(',')[0])
txt_file.write(' stride: %s\n' % info['param']['stride'].split('(')[1].split(',')[0])
txt_file.write(' pad: %s\n' % info['param']['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['param']['num_hidden'])
txt_file.write(' }\n')
txt_file.write('}\n')
txt_file.write('\n')
pass
def Flatten(txt_file, info):
pass
def SoftmaxOutput(txt_file, info):
pass
# ----------------------------------------------------------------
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':
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)
else:
sys.exit("Warning! Unknown mxnet op:{}".format(info['op']))