-
Notifications
You must be signed in to change notification settings - Fork 18
/
mxnet2caffe.py
154 lines (131 loc) · 5.59 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import sys, argparse
import mxnet as mx
import sys
import os
try:
import caffe
except ImportError:
import os, sys
curr_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(curr_path, "/Users/yujinke/me/caffe/python"))
import caffe
from find import *
import time
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '4'
parser = argparse.ArgumentParser(description='Convert MXNet model to Caffe model')
parser.add_argument('--mx-model', type=str, default='model_mxnet/face/facega2')
parser.add_argument('--mx-epoch', type=int, default=0)
parser.add_argument('--cf-prototxt', type=str, default='model_caffe/face/facega2.prototxt')
parser.add_argument('--cf-model', type=str, default='model_caffe/face/facega2.caffemodel')
args = parser.parse_args()
# ------------------------------------------
# Load
_, arg_params, aux_params = mx.model.load_checkpoint(args.mx_model, args.mx_epoch)
#net = caffe.Net(args.cf_prototxt, caffe.TRAIN)
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:')
# backbone = "hstage1"
backbone = find_backbone(args.mx_model + '-symbol.json')
for i_key,key_i in enumerate(all_keys):
# try:
if 'data' is key_i:
pass
elif '_weight' in key_i:
if key_i.find(backbone)!=-1 or key_i.find("dense") != -1:
key_caffe = key_i.replace('_weight', '_fwd')
else:
key_caffe = key_i.replace('_weight','')
# if
# key_caffe = key_i.replace('_weight', '_fwd')
# else:
# key_caffe = key_i.replace('_weight', '')
print(key_i,key_caffe)
print("{}: {}->{}".format(key_i, arg_params[key_i].shape, net.params[key_caffe][0].data.shape))
# if 'fc' in key_i:
# print key_i
# print arg_params[key_i].shape
# print net.params[key_caffe][0].data.shape
net.params[key_caffe][0].data.flat = arg_params[key_i].asnumpy().flat
elif '_bias' in key_i:
if key_i.find(backbone)!=-1:
key_caffe = key_i.replace('_bias', '_fwd')
else:
key_caffe = key_i.replace('_bias','')
if key_i.find("dense") != -1:
key_caffe = key_i.replace('_bias', '_fwd')
else:
key_caffe = key_i.replace('_bias', '')
print("{}: {}->{}".format(key_i, arg_params[key_i].shape, net.params[key_caffe][0].data.shape))
net.params[key_caffe][1].data.flat = arg_params[key_i].asnumpy().flat
elif '_gamma' in key_i and 'relu' not in key_i:
if key_i.find(backbone)!=-1:
key_caffe = key_i.replace('_gamma', '_fwd_scale')
else:
key_caffe = key_i.replace('_gamma','_scale')
print("{}: {}->{}".format(key_i, arg_params[key_i].shape, net.params[key_caffe][0].data.shape))
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','')
print("key_i",key_i)
print("{}: {}->{}".format(key_i, arg_params[key_i].shape, net.params[key_caffe][0].data.shape))
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:
if key_i.find(backbone)!=-1:
key_caffe = key_i.replace('_beta', '_fwd_scale')
else:
key_caffe = key_i.replace('_beta','_scale')
print("key in mxnet",key_i,key_i in arg_params.keys())
print("key in caffe",key_caffe,key_caffe in net.params.keys())
print("{}: {}->{}".format(key_i, arg_params[key_i].shape, net.params[key_caffe][0].data.shape))
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','')
print("{}: {}->{}".format(key_i, aux_params[key_i].shape, net.params[key_caffe][0].data.shape))
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','')
print("{}: {}->{}".format(key_i, aux_params[key_i].shape, net.params[key_caffe][0].data.shape))
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:
exit()
key_caffe = key_i.replace('_running_mean', '_fwd')
print("{}: {}->{}".format(key_i, aux_params[key_i].shape, net.params[key_caffe][0].data.shape))
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:
exit()
key_caffe = key_i.replace('_running_var', '_fwd')
print("{}: {}->{}".format(key_i, aux_params[key_i].shape, net.params[key_caffe][0].data.shape))
net.params[key_caffe][1].data.flat = aux_params[key_i].asnumpy().flat
net.params[key_caffe][2].data[...] = 1
else:
# pass
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:
# pass
#
# import traceback
# print(traceback.print_exc())
# print("\nError! key error mxnet:{}".format(key_i))
# break
#
# ------------------------------------------
# Finish
net.save(args.cf_model)
print("\n*** PARAMS to CAFFEMODEL Finished. ***\n")