Skip to content

Instantly share code, notes, and snippets.

@zhongwen
Last active August 29, 2015 14:20
Show Gist options
  • Save zhongwen/05829a531e5236ddb8ec to your computer and use it in GitHub Desktop.
Save zhongwen/05829a531e5236ddb8ec to your computer and use it in GitHub Desktop.
caffemodel converter for cxxnet, VGG-16
import os
import sys
import argparse
# caffe root folder
caffe_root = ''
# cxxnet root folder
cxxnet_root = ''
sys.path.insert(0, os.path.join(caffe_root, 'python'))
sys.path.insert(0, os.path.join(cxxnet_root, 'wrapper'))
import caffe
import cxxnet
def main():
parser = argparse.ArgumentParser()
parser.add_argument("caffe_prototxt",
help="caffe prototxt")
parser.add_argument(
"caffe_model", help="caffe model")
parser.add_argument("cxxnet_conf", help="cxxnet conf")
parser.add_argument("to_save", help="to save, in format like 0090.model")
args = parser.parse_args()
caffe_prototxt = args.caffe_prototxt
caffe_model = args.caffe_model
cxxnet_conf = args.cxxnet_conf
to_save = args.to_save
print 'converting {0} and {1} with {2} into {3}'.format(caffe_prototxt, caffe_model, cxxnet_conf, to_save)
caffe.set_mode_cpu()
net_caffe = caffe.Net(caffe_prototxt, caffe_model, caffe.TEST)
print 'creating cxxnet model'
with open(cxxnet_conf, 'r') as f_in:
cfg = f_in.read()
net_cxxnet = cxxnet.Net(dev='cpu', cfg=cfg)
net_cxxnet.set_param('dev', 'cpu')
net_cxxnet.init_model()
layer_names = net_caffe._layer_names
first_conv = True
for layer_idx, layer in enumerate(net_caffe.layers):
layer_name = layer_names[layer_idx]
if layer.type == 'Convolution' or layer.type == 'InnerProduct':
assert(len(layer.blobs) == 2)
wmat = layer.blobs[0].data
bias = layer.blobs[1].data
if first_conv:
print 'Swapping BGR of caffe into RGB in cxxnet'
wmat[:, [0, 2], :, :] = wmat[:, [2, 0], :, :]
assert(wmat.flags['C_CONTIGUOUS'] is True)
assert(bias.flags['C_CONTIGUOUS'] is True)
print 'converting layer {0}, wmat shape = {1}, bias shape = {2}'.format(layer_name, wmat.shape, bias.shape)
wmat = wmat.reshape((wmat.shape[0], -1))
bias = bias.reshape((bias.shape[0], 1))
net_cxxnet.set_weight(wmat, layer_name, 'wmat')
net_cxxnet.set_weight(bias, layer_name, 'bias')
if first_conv and layer.type == 'Convolution':
first_conv = False
net_cxxnet.save_model(to_save)
if __name__ == '__main__':
main()
eval = val
iter = imginst
image_list = "imagenet/val.lst"
image_bin = "val.bin"
#mean_r=123.68
#mean_g=116.779
#mean_b=103.939
mean_value = 103.939,116.779,123.68
#no random crop and mirror in test
iter = end
netconfig = start
layer[0->0.1] = conv:conv1_1
kernel_size = 3
nchannel = 64
pad = 1
stride = 1
layer[0.1->1] = relu
layer[1->1.1] = conv:conv1_2
kernel_size = 3
nchannel = 64
pad = 1
stride = 1
layer[1.1->2] = relu
layer[2->3] = max_pooling:pool1
kernel_size = 2
stride = 2
layer[3->3.1] = conv:conv2_1
kernel_size = 3
nchannel = 128
pad = 1
stride = 1
layer[3.1->4] = relu
layer[4->4.1] = conv:conv2_2
kernel_size = 3
nchannel = 128
pad = 1
stride = 1
layer[4.1->5] = relu
layer[5->6] = max_pooling:pool2
kernel_size = 2
stride = 2
layer[6->6.1] = conv:conv3_1
kernel_size = 3
nchannel = 256
pad = 1
stride = 1
layer[6.1->7] = relu
layer[7->7.1] = conv:conv3_2
kernel_size = 3
nchannel = 256
pad = 1
stride = 1
layer[7.1->8] = relu
layer[8->8.1] = conv:conv3_3
kernel_size = 3
nchannel = 256
pad = 1
stride = 1
layer[8.1->9] = relu
layer[9->10] = max_pooling:pool3
kernel_size = 2
stride = 2
layer[10->10.1] = conv:conv4_1
kernel_size = 3
nchannel = 512
pad = 1
stride = 1
layer[10.1->11] = relu
layer[11->11.1] = conv:conv4_2
kernel_size = 3
nchannel = 512
pad = 1
stride = 1
layer[11.1->12] = relu
layer[12->12.1] = conv:conv4_3
kernel_size = 3
nchannel = 512
pad = 1
stride = 1
layer[12.1->13] = relu
layer[13->14] = max_pooling:pool4
kernel_size = 2
stride = 2
layer[14->14.1] = conv:conv5_1
kernel_size = 3
nchannel = 512
pad = 1
stride = 1
layer[14.1->15] = relu
layer[15->15.1] = conv:conv5_2
kernel_size = 3
nchannel = 512
pad = 1
stride = 1
layer[15.1->16] = relu
layer[16->16.1] = conv:conv5_3
kernel_size = 3
nchannel = 512
pad = 1
stride = 1
layer[16.1->17] = relu
layer[17->18] = max_pooling:pool5
kernel_size = 2
stride = 2
layer[+1] = flatten
layer[+1] = fullc:fc6
nhidden = 4096
layer[+1] = relu
layer[+0] = dropout
threshold = 0.5
layer[+1] = fullc:fc7
nhidden = 4096
layer[+1] = relu
layer[+0] = dropout
threshold = 0.5
layer[+1] = fullc:fc8
nhidden = 1000
layer[+0] = softmax
netconfig = end
input_shape = 3,224,224
batch_size = 64
dev = gpu:0,1,3
model_in = VGG_16/0090.model
metric = rec@1
metric = rec@5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment