Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support conversion with one command #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 31 additions & 36 deletions json2prototxt.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
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='R50v2/R50v2-symbol.json')
parser.add_argument('--cf-prototxt', type=str, default='R50v2/R50v2.prototxt')
args = parser.parse_args()

with open(args.mx_json) as json_file:
jdata = json.load(json_file)
print(jdata)

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
def write_prototxt(json_path, prototx_path):
with open(json_path) as json_file:
jdata = json.load(json_file)
print(jdata)

with open(prototx_path, "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

write_node(prototxt_file, info)

print("*** JSON to PROTOTXT FINISH ***")
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)

print("*** JSON to PROTOTXT FINISH ***")

9 changes: 6 additions & 3 deletions mxnet2caffe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys, argparse
import argparse
import mxnet as mx
import sys
import os
Expand All @@ -11,10 +11,9 @@
sys.path.append(os.path.join(curr_path, "/Users/yujinke/me/caffe/python"))
import caffe


from json2prototxt import write_prototxt

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')
Expand All @@ -23,6 +22,10 @@
parser.add_argument('--cf-model', type=str, default='model_caffe/face/facega2.caffemodel')
args = parser.parse_args()

# ------------------------------------------
# Create prototxt
write_prototxt(args.mx_model + '-symbol.json', args.cf_prototxt)

# ------------------------------------------
# Load
_, arg_params, aux_params = mx.model.load_checkpoint(args.mx_model, args.mx_epoch)
Expand Down