-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathkeras_to_caffe.py
35 lines (30 loc) · 989 Bytes
/
keras_to_caffe.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
import keras
import Caffe.net as caffe
import numpy as np
from funcs import *
def convert_filter(numpy_filter_weight):
return np.transpose(numpy_filter_weight,(3,2,1,0))
def convert_fc(numpy_fc_weight):
return np.transpose(numpy_fc_weight,(1,0))
def keras_weights_to_caffemodel(keras_model):
"""
Only Implement the conv layer and fc layer
:param keras_model:
:return:
"""
net=caffe.Net()
layers=keras_model.layers
for layer in layers:
if type(layer)==keras.layers.Convolution2D:
w,b=layer.get_weights()
w=convert_filter(w)
param=caffe.Layer_param(layer.name,'Convolution')
net.add_layer_with_data(param,[w,b])
if type(layer)==keras.layers.Dense:
w, b = layer.get_weights()
w = convert_fc(w)
param = caffe.Layer_param(layer.name, 'InnerProduct')
net.add_layer_with_data(param, [w, b])
return net
if __name__=='__main__':
pass