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

Solve syntax error when using python3 #36

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
48 changes: 24 additions & 24 deletions darknet2caffe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The caffe module needs to be on the Python path;
# we'll add it here explicitly.
caffe_root='/home/chen/caffe/'
caffe_root='/path/to/caffe'
#os.chdir(caffe_root)
import sys
sys.path.insert(0,caffe_root+'python')
Expand Down Expand Up @@ -48,7 +48,7 @@ def darknet2caffe(cfgfile, weightfile, protofile, caffemodel):
continue
elif block['type'] == 'convolutional':
batch_normalize = int(block['batch_normalize'])
if block.has_key('name'):
if 'name' in block:
conv_layer_name = block['name']
bn_layer_name = '%s-bn' % block['name']
scale_layer_name = '%s-scale' % block['name']
Expand All @@ -64,7 +64,7 @@ def darknet2caffe(cfgfile, weightfile, protofile, caffemodel):
layer_id = layer_id+1
elif block['type'] == 'depthwise_convolutional':
batch_normalize = int(block['batch_normalize'])
if block.has_key('name'):
if 'name' in block:
conv_layer_name = block['name']
bn_layer_name = '%s-bn' % block['name']
scale_layer_name = '%s-scale' % block['name']
Expand All @@ -79,7 +79,7 @@ def darknet2caffe(cfgfile, weightfile, protofile, caffemodel):
start = load_conv2caffe(buf, start, params[conv_layer_name])
layer_id = layer_id+1
elif block['type'] == 'connected':
if block.has_key('name'):
if 'name' in block:
fc_layer_name = block['name']
else:
fc_layer_name = 'layer%d-fc' % layer_id
Expand Down Expand Up @@ -173,7 +173,7 @@ def cfg2prototxt(cfgfile):
elif block['type'] == 'convolutional':
conv_layer = OrderedDict()
conv_layer['bottom'] = bottom
if block.has_key('name'):
if 'name' in block:
conv_layer['top'] = block['name']
conv_layer['name'] = block['name']
else:
Expand All @@ -199,7 +199,7 @@ def cfg2prototxt(cfgfile):
bn_layer = OrderedDict()
bn_layer['bottom'] = bottom
bn_layer['top'] = bottom
if block.has_key('name'):
if 'name' in block:
bn_layer['name'] = '%s-bn' % block['name']
else:
bn_layer['name'] = 'layer%d-bn' % layer_id
Expand All @@ -212,7 +212,7 @@ def cfg2prototxt(cfgfile):
scale_layer = OrderedDict()
scale_layer['bottom'] = bottom
scale_layer['top'] = bottom
if block.has_key('name'):
if 'name' in block:
scale_layer['name'] = '%s-scale' % block['name']
else:
scale_layer['name'] = 'layer%d-scale' % layer_id
Expand All @@ -226,7 +226,7 @@ def cfg2prototxt(cfgfile):
activate_layer = OrderedDict()
activate_layer['bottom'] = bottom
activate_layer['top'] = bottom
if block.has_key('name'):
if 'name' in block:
activate_layer['name'] = '%s-act' % block['name']
else:
activate_layer['name'] = 'layer%d-act' % layer_id
Expand All @@ -243,7 +243,7 @@ def cfg2prototxt(cfgfile):
elif block['type'] == 'depthwise_convolutional':
conv_layer = OrderedDict()
conv_layer['bottom'] = bottom
if block.has_key('name'):
if 'name' in block:
conv_layer['top'] = block['name']
conv_layer['name'] = block['name']
else:
Expand All @@ -268,7 +268,7 @@ def cfg2prototxt(cfgfile):
bn_layer = OrderedDict()
bn_layer['bottom'] = bottom
bn_layer['top'] = bottom
if block.has_key('name'):
if 'name' in block:
bn_layer['name'] = '%s-bn' % block['name']
else:
bn_layer['name'] = 'layer%d-bn' % layer_id
Expand All @@ -281,7 +281,7 @@ def cfg2prototxt(cfgfile):
scale_layer = OrderedDict()
scale_layer['bottom'] = bottom
scale_layer['top'] = bottom
if block.has_key('name'):
if 'name' in block:
scale_layer['name'] = '%s-scale' % block['name']
else:
scale_layer['name'] = 'layer%d-scale' % layer_id
Expand All @@ -295,7 +295,7 @@ def cfg2prototxt(cfgfile):
relu_layer = OrderedDict()
relu_layer['bottom'] = bottom
relu_layer['top'] = bottom
if block.has_key('name'):
if 'name' in block:
relu_layer['name'] = '%s-act' % block['name']
else:
relu_layer['name'] = 'layer%d-act' % layer_id
Expand All @@ -310,7 +310,7 @@ def cfg2prototxt(cfgfile):
elif block['type'] == 'maxpool':
max_layer = OrderedDict()
max_layer['bottom'] = bottom
if block.has_key('name'):
if 'name' in block:
max_layer['top'] = block['name']
max_layer['name'] = block['name']
else:
Expand Down Expand Up @@ -338,7 +338,7 @@ def cfg2prototxt(cfgfile):
elif block['type'] == 'avgpool':
avg_layer = OrderedDict()
avg_layer['bottom'] = bottom
if block.has_key('name'):
if 'name' in block:
avg_layer['top'] = block['name']
avg_layer['name'] = block['name']
else:
Expand All @@ -359,7 +359,7 @@ def cfg2prototxt(cfgfile):
if True:
region_layer = OrderedDict()
region_layer['bottom'] = bottom
if block.has_key('name'):
if 'name' in block:
region_layer['top'] = block['name']
region_layer['name'] = block['name']
else:
Expand Down Expand Up @@ -390,7 +390,7 @@ def cfg2prototxt(cfgfile):
bottoms.append(bottom)
route_layer['bottom'] = bottoms

if block.has_key('name'):
if 'name' in block:
route_layer['top'] = block['name']
route_layer['name'] = block['name']
else:
Expand All @@ -405,7 +405,7 @@ def cfg2prototxt(cfgfile):
elif block['type'] == 'upsample':
upsample_layer = OrderedDict()
upsample_layer['bottom'] = bottom
if block.has_key('name'):
if 'name' in block:
upsample_layer['top'] = block['name']
upsample_layer['name'] = block['name']
else:
Expand All @@ -428,7 +428,7 @@ def cfg2prototxt(cfgfile):
bottom2= topnames[prev_layer_id2]
shortcut_layer = OrderedDict()
shortcut_layer['bottom'] = [bottom1, bottom2]
if block.has_key('name'):
if 'name' in block:
shortcut_layer['top'] = block['name']
shortcut_layer['name'] = block['name']
else:
Expand All @@ -445,7 +445,7 @@ def cfg2prototxt(cfgfile):
relu_layer = OrderedDict()
relu_layer['bottom'] = bottom
relu_layer['top'] = bottom
if block.has_key('name'):
if 'name' in block:
relu_layer['name'] = '%s-act' % block['name']
else:
relu_layer['name'] = 'layer%d-act' % layer_id
Expand All @@ -461,7 +461,7 @@ def cfg2prototxt(cfgfile):
elif block['type'] == 'connected':
fc_layer = OrderedDict()
fc_layer['bottom'] = bottom
if block.has_key('name'):
if 'name' in block:
fc_layer['top'] = block['name']
fc_layer['name'] = block['name']
else:
Expand All @@ -478,7 +478,7 @@ def cfg2prototxt(cfgfile):
relu_layer = OrderedDict()
relu_layer['bottom'] = bottom
relu_layer['top'] = bottom
if block.has_key('name'):
if 'name' in block:
relu_layer['name'] = '%s-act' % block['name']
else:
relu_layer['name'] = 'layer%d-act' % layer_id
Expand Down Expand Up @@ -510,9 +510,9 @@ def cfg2prototxt(cfgfile):
exit()

cfgfile = sys.argv[1]
#net_info = cfg2prototxt(cfgfile)
#print_prototxt(net_info)
#save_prototxt(net_info, 'tmp.prototxt')
# net_info = cfg2prototxt(cfgfile)
# print_prototxt(net_info)
# save_prototxt(net_info, 'tmp.prototxt')
weightfile = sys.argv[2]
protofile = sys.argv[3]
caffemodel = sys.argv[4]
Expand Down
28 changes: 14 additions & 14 deletions prototxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
try:
import caffe_pb2
except:
print 'caffe_pb2.py not found. Try:'
print ' protoc caffe.proto --python_out=.'
print('caffe_pb2.py not found. Try:')
print(' protoc caffe.proto --python_out=.')
exit()

def parse_caffemodel(caffemodel):
model = caffe_pb2.NetParameter()
print 'Loading caffemodel: ', caffemodel
print('Loading caffemodel: ', caffemodel)
with open(caffemodel, 'rb') as fp:
model.ParseFromString(fp.read())

Expand Down Expand Up @@ -149,26 +149,26 @@ def format_value(value):

def print_block(block_info, prefix, indent):
blanks = ''.join([' ']*indent)
print >>fp, '%s%s {' % (blanks, prefix)
print('%s%s {' % (blanks, prefix), file=fp)
for key,value in block_info.items():
if type(value) == OrderedDict:
print_block(value, key, indent+4)
elif type(value) == list:
for v in value:
print >> fp, '%s %s: %s' % (blanks, key, format_value(v))
print('%s %s: %s' % (blanks, key, format_value(v)), file=fp)
else:
print >> fp, '%s %s: %s' % (blanks, key, format_value(value))
print >> fp, '%s}' % blanks
print('%s %s: %s' % (blanks, key, format_value(value)), file=fp)
print('%s}' % blanks, file=fp)

props = net_info['props']
layers = net_info['layers']
print >> fp, 'name: \"%s\"' % props['name']
print >> fp, 'input: \"%s\"' % props['input']
print >> fp, 'input_dim: %s' % props['input_dim'][0]
print >> fp, 'input_dim: %s' % props['input_dim'][1]
print >> fp, 'input_dim: %s' % props['input_dim'][2]
print >> fp, 'input_dim: %s' % props['input_dim'][3]
print >> fp, ''
print('name: \"%s\"' % props['name'], file=fp)
print('input: \"%s\"' % props['input'], file=fp)
print('input_dim: %s' % props['input_dim'][0], file=fp)
print('input_dim: %s' % props['input_dim'][1], file=fp)
print('input_dim: %s' % props['input_dim'][2], file=fp)
print('input_dim: %s' % props['input_dim'][3], file=fp)
print('', file=fp)
for layer in layers:
if layer['type'] != 'Region' or region == True:
print_block(layer, 'layer', 0)
Expand Down