forked from uhfband/keras2caffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keras2caffe.py
187 lines (134 loc) · 6.71 KB
/
keras2caffe.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
import caffe
from caffe import layers as L, params as P
import numpy as np
def convert(keras_model, caffe_net_file, caffe_params_file):
caffe_net = caffe.NetSpec()
net_params = dict()
outputs=dict()
shape=()
input_str = ''
for layer in keras_model.layers:
name = layer.name
layer_type = type(layer).__name__
config = layer.get_config()
blobs = layer.get_weights()
blobs_num = len(blobs)
if type(layer.output)==list:
raise Exception('Layers with multiply outputs are not supported')
else:
top=layer.output.name
if type(layer.input)!=list:
bottom = layer.input.name
if layer_type=='InputLayer':
name = 'data'
caffe_net[name] = L.Layer()
input_shape=config['batch_input_shape']
input_str = 'input: {}\ninput_dim: {}\ninput_dim: {}\ninput_dim: {}\ninput_dim: {}'.format('"' + name + '"',
1, input_shape[3], 227, 227)#input_shape[1], input_shape[2])
elif layer_type=='Conv2D' or layer_type=='Convolution2D':
strides = config['strides']
kernel_size = config['kernel_size']
filters = config['filters']
padding = config['padding']
use_bias = config['use_bias']
blobs[0] = np.array(blobs[0]).transpose(3,2,0,1)
param = dict(bias_term = use_bias)
if kernel_size[0]==kernel_size[1]:
pad=0
if padding=='same':
pad=kernel_size[0]/2
caffe_net[name] = L.Convolution(caffe_net[outputs[bottom]], num_output=filters,
kernel_size=kernel_size[0], pad=pad, stride=strides[0], convolution_param=param)
else:
pad_h = pad_w = 0
if padding=='same':
pad_h = kernel_size[0]/2
pad_w = kernel_size[1]/2
caffe_net[name] = L.Convolution(caffe_net[outputs[bottom]], num_output=filters,
kernel_h=kernel_size[0], kernel_w=kernel_size[1], pad_h=pad_h, pad_w=pad_w, stride=strides[0], convolution_param=param)
net_params[name] = blobs
elif layer_type=='BatchNormalization':
caffe_net[name] = L.BatchNorm(caffe_net[outputs[bottom]], in_place=True)
variance = np.array(blobs[-1])
mean = np.array(blobs[-2])
param = dict()
if config['scale']:
gamma = np.array(blobs[-3])
else:
gamma = np.ones(mean.shape, dtype=np.float32)
if config['center']:
beta = np.array(blobs[0])
param['bias_term']=True
else:
beta = np.zeros(mean.shape, dtype=np.float32)
param['bias_term']=False
net_params[name] = (mean, variance, np.array(1.0))
name_s = name+'s'
caffe_net[name_s] = L.Scale(caffe_net[name], in_place=True, scale_param=param)
net_params[name_s] = (gamma, beta)
elif layer_type=='Dense':
caffe_net[name] = L.InnerProduct(caffe_net[outputs[bottom]], num_output=config['units'])
if config['use_bias']:
net_params[name] = (np.array(blobs[0]).transpose(1, 0), np.array(blobs[1]))
else:
net_params[name] = (blobs[0])
elif layer_type=='Activation':
if config['activation']!='relu':
raise Exception('Unsupported activation')
caffe_net[name] = L.ReLU(caffe_net[outputs[bottom]], in_place=True)
elif layer_type=='Concatenate':
layers = []
for i in layer.input:
layers.append(caffe_net[outputs[i.name]])
caffe_net[name] = L.Concat(*layers, axis=1)
elif layer_type=='Add':
layers = []
for i in layer.input:
layers.append(caffe_net[outputs[i.name]])
caffe_net[name] = L.Eltwise(*layers)
elif layer_type=='Flatten':
caffe_net[name] = L.Flatten(caffe_net[outputs[bottom]])
elif layer_type=='MaxPooling2D' or layer_type=='AveragePooling2D':
if layer_type=='MaxPooling2D':
pool = P.Pooling.MAX
else:
pool = P.Pooling.AVE
pool_size = config['pool_size']
strides = config['strides']
padding = config['padding']
if pool_size[0]!=pool_size[1]:
raise Exception('Unsupported pool_size')
if strides[0]!=strides[1]:
raise Exception('Unsupported strides')
pad=0
print padding
if padding=='same':
pad=pool_size[0]/2
caffe_net[name] = L.Pooling(caffe_net[outputs[bottom]], kernel_size=pool_size[0],
stride=strides[0], pad=pad, pool=pool)
elif layer_type=='Dropout':
caffe_net[name] = L.Dropout(caffe_net[outputs[bottom]], dropout_param=dict(dropout_ratio=config['rate']))
#TODO
elif layer_type=='GlobalAveragePooling2D':
caffe_net[name] = L.Pooling(caffe_net[outputs[bottom]], pool=P.Pooling.AVE, global_pooling=True)
elif layer_type=='ZeroPadding2D':
padding=config['padding']
caffe_net[name] = L.Convolution(caffe_net[outputs[bottom]], num_output=3, kernel_size=1, stride=1,
pad_h=padding[0][0], pad_w=padding[1][0], convolution_param=dict(bias_term = False))
net_params[name] = np.ones((3,3,1,1))
else:
raise Exception('Unsupported layer type: '+layer_type)
outputs[top]=name
#replace empty layer with input blob
net_proto = input_str + '\n' + 'layer {' + 'layer {'.join(str(caffe_net.to_proto()).split('layer {')[2:])
#print net_proto
f = open(caffe_net_file, 'w')
f.write(net_proto)
f.close()
caffe_model = caffe.Net(caffe_net_file, caffe.TEST)
#print 'hello world'
for layer in caffe_model.params.keys():
for n in range(0, len(caffe_model.params[layer])):
#print layer
caffe_model.params[layer][n].data[...] = net_params[layer][n]
caffe_model.save(caffe_params_file)