forked from cypw/MXNet2Caffe
-
Notifications
You must be signed in to change notification settings - Fork 53
/
mxnet2caffe.py
82 lines (72 loc) · 3.01 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
import sys, argparse
import os
os.environ['GLOG_minloglevel'] = '2'
import find_mxnet, find_caffe
import mxnet as mx
import caffe
parser = argparse.ArgumentParser(description='Convert MXNet model to Caffe model')
parser.add_argument('--mx-model', type=str, default='./model_mxnet/model')
parser.add_argument('--mx-epoch', type=int, default=0)
parser.add_argument('--cf-prototxt', type=str, default='./model_caffe/face-mobile.prototxt')
parser.add_argument('--cf-model', type=str, default='./model_caffe/face-mobile.caffemodel')
args = parser.parse_args()
# ------------------------------------------
# Load
_, arg_params, aux_params = mx.model.load_checkpoint(args.mx_model, args.mx_epoch)
print("-------load mxnet model successful")
net = caffe.Net(args.cf_prototxt, caffe.TRAIN)
print("-------load caffe prototxt success")
# ------------------------------------------
# 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','')
if 'fc' in key_i:
print key_i
print arg_params[key_i].shape
key_caffe = 'pre_fc1'
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:
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
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))
# ------------------------------------------
# Finish
net.save(args.cf_model)
print("\n- Finished.\n")