forked from cypw/MXNet2Caffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2prototxt.py
39 lines (32 loc) · 1.46 KB
/
json2prototxt.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
import sys
import argparse
import json
from prototxt_basic import *
parser = argparse.ArgumentParser(description='Convert MXNet jason to Caffe prototxt')
parser.add_argument('--mx-json', type=str, default='model_mxnet/residual-symbol.json')
parser.add_argument('--cf-prototxt', type=str, default='model_caffe/deploy.prototxt')
args = parser.parse_args()
with open(args.mx_json) as json_file:
jdata = json.load(json_file)
with open(args.cf_prototxt, "w") as prototxt_file:
for i_node in range(0,len(jdata['nodes'])):
node_i = jdata['nodes'][i_node]
if str(node_i['op']) == 'null' and str(node_i['name']) != 'data':
continue
print('{}, \top:{}, name:{} -> {}'.format(i_node,node_i['op'].ljust(20),
node_i['name'].ljust(30),
node_i['name']).ljust(20))
info = node_i
info['top'] = info['name']
info['bottom'] = []
info['params'] = []
for input_idx_i in node_i['inputs']:
input_i = jdata['nodes'][input_idx_i[0]]
if str(input_i['op']) != 'null' or (str(input_i['name']) == 'data'):
info['bottom'].append(str(input_i['name']))
if str(input_i['op']) == 'null':
info['params'].append(str(input_i['name']))
if not str(input_i['name']).startswith(str(node_i['name'])):
print(' use shared weight -> %s'% str(input_i['name']))
info['share'] = True
write_node(prototxt_file, info)