forked from GarrickLin/MXNet2Caffe
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mxnet2caffe.py
134 lines (117 loc) · 5.28 KB
/
mxnet2caffe.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
import sys, argparse
import find_caffe
print "start import mxnet"
import mxnet as mx
print "end import mxnet"
import caffe
parser = argparse.ArgumentParser(description='Convert MXNet model to Caffe model')
parser.add_argument('--mx-model', type=str, default='../model-r50-am-lfw/model')
parser.add_argument('--mx-epoch', type=int, default=0)
parser.add_argument('--cf-prototxt', type=str, default='../model-r50-am-lfw/model.prototxt')
# parser.add_argument('--cf-model', type=str, default='../model-r50-am-lfw/model.caffemodel')
args = parser.parse_args()
args.cf_model=args.cf_prototxt.replace(".prototxt",".caffemodel")
# ------------------------------------------
# Load
_, arg_params, aux_params = mx.model.load_checkpoint(args.mx_model, args.mx_epoch)
net = caffe.Net(args.cf_prototxt, caffe.TEST)
# ------------------------------------------
# Convert
all_keys = arg_params.keys() + aux_params.keys()
all_keys.sort()
print('----------------------------------\n')
print('ALL KEYS IN MXNET:')
print(all_keys)
print('%d KEYS' %len(all_keys))
print('----------------------------------\n')
print('VALID KEYS:')
for i_key,key_i in enumerate(all_keys):
try:
if 'data' is key_i:
pass
elif '_weight' in key_i:
key_caffe = key_i.replace('_weight','')
net.params[key_caffe][0].data.flat = arg_params[key_i].asnumpy().flat
elif '_bias' in key_i:
key_caffe = key_i.replace('_bias','')
net.params[key_caffe][1].data.flat = arg_params[key_i].asnumpy().flat
elif '_gamma' in key_i and 'relu' not in key_i:
key_caffe = key_i.replace('_gamma','_scale')
net.params[key_caffe][0].data.flat = arg_params[key_i].asnumpy().flat
# TODO: support prelu
elif '_gamma' in key_i and 'relu' in key_i: # for prelu
key_caffe = key_i.replace('_gamma','')
assert (len(net.params[key_caffe]) == 1)
net.params[key_caffe][0].data.flat = arg_params[key_i].asnumpy().flat
elif '_beta' in key_i:
key_caffe = key_i.replace('_beta','_scale')
net.params[key_caffe][1].data.flat = arg_params[key_i].asnumpy().flat
elif '_moving_mean' in key_i:
key_caffe = key_i.replace('_moving_mean','')
net.params[key_caffe][0].data.flat = aux_params[key_i].asnumpy().flat
net.params[key_caffe][2].data[...] = 1
elif '_moving_var' in key_i:
key_caffe = key_i.replace('_moving_var','')
net.params[key_caffe][1].data.flat = aux_params[key_i].asnumpy().flat
net.params[key_caffe][2].data[...] = 1
elif '_running_mean' in key_i:
key_caffe = key_i.replace('_running_mean','')
net.params[key_caffe][0].data.flat = aux_params[key_i].asnumpy().flat
net.params[key_caffe][2].data[...] = 1
elif '_running_var' in key_i:
key_caffe = key_i.replace('_running_var','')
net.params[key_caffe][1].data.flat = aux_params[key_i].asnumpy().flat
net.params[key_caffe][2].data[...] = 1
else:
sys.exit("Warning! Unknown mxnet:{}".format(key_i))
print("% 3d | %s -> %s, initialized."
%(i_key, key_i.ljust(40), key_caffe.ljust(30)))
except KeyError:
#try gluon style
try:
if 'data' is key_i:
pass
elif '_weight' in key_i:
key_caffe = key_i.replace('_weight', '_fwd')
net.params[key_caffe][0].data.flat = arg_params[key_i].asnumpy().flat
elif '_bias' in key_i:
key_caffe = key_i.replace('_bias', '_fwd')
net.params[key_caffe][1].data.flat = arg_params[key_i].asnumpy().flat
elif '_gamma' in key_i and 'relu' not in key_i:
key_caffe = key_i.replace('_gamma', '_fwd_scale')
net.params[key_caffe][0].data.flat = arg_params[key_i].asnumpy().flat
# TODO: support prelu
elif '_gamma' in key_i and 'relu' in key_i: # for prelu
key_caffe = key_i.replace('_gamma', '')
assert (len(net.params[key_caffe]) == 1)
net.params[key_caffe][0].data.flat = arg_params[key_i].asnumpy().flat
elif '_beta' in key_i:
key_caffe = key_i.replace('_beta', '_fwd_scale')
net.params[key_caffe][1].data.flat = arg_params[key_i].asnumpy().flat
elif '_moving_mean' in key_i:
key_caffe = key_i.replace('_moving_mean', '')
net.params[key_caffe][0].data.flat = aux_params[key_i].asnumpy().flat
net.params[key_caffe][2].data[...] = 1
elif '_moving_var' in key_i:
key_caffe = key_i.replace('_moving_var', '')
net.params[key_caffe][1].data.flat = aux_params[key_i].asnumpy().flat
net.params[key_caffe][2].data[...] = 1
elif '_running_mean' in key_i:
key_caffe = key_i.replace('_running_mean', '_fwd')
net.params[key_caffe][0].data.flat = aux_params[key_i].asnumpy().flat
net.params[key_caffe][2].data[...] = 1
elif '_running_var' in key_i:
key_caffe = key_i.replace('_running_var', '_fwd')
net.params[key_caffe][1].data.flat = aux_params[key_i].asnumpy().flat
net.params[key_caffe][2].data[...] = 1
else:
sys.exit("Warning! Unknown mxnet:{}".format(key_i))
print("% 3d | %s -> %s, initialized."
% (i_key, key_i.ljust(40), key_caffe.ljust(30)))
except KeyError:
print("\nError! key error mxnet:{}".format(key_i))
pass
# ------------------------------------------
# Finish
net.save(args.cf_model)
print("\n- Finished.\n")