!diff imagenet/imagenet_full_conv.prototxt imagenet/imagenet_deploy.prototxt # Make sure that caffe is on the python path: caffe_root = '../' # this file is expected to be in {caffe_root}/examples import sys sys.path.insert(0, caffe_root + 'python') import caffe # Load the original network and extract the fully-connected layers' parameters. net = caffe.Net('imagenet/imagenet_deploy.prototxt', 'imagenet/caffe_reference_imagenet_model') params = ['fc6', 'fc7', 'fc8'] # fc_params = {name: (weights, biases)} fc_params = {pr: (net.params[pr][0].data, net.params[pr][1].data) for pr in params} for fc in params: print '{} weights are {} dimensional and biases are {} dimensional'.format(fc, fc_params[fc][0].shape, fc_params[fc][1].shape) # Load the fully-convolutional network to transplant the parameters. net_full_conv = caffe.Net('imagenet/imagenet_full_conv.prototxt', 'imagenet/caffe_reference_imagenet_model') params_full_conv = ['fc6-conv', 'fc7-conv', 'fc8-conv'] # conv_params = {name: (weights, biases)} conv_params = {pr: (net_full_conv.params[pr][0].data, net_full_conv.params[pr][1].data) for pr in params_full_conv} for conv in params_full_conv: print '{} weights are {} dimensional and biases are {} dimensional'.format(conv, conv_params[conv][0].shape, conv_params[conv][1].shape) for pr, pr_conv in zip(params, params_full_conv): conv_params[pr_conv][1][...] = fc_params[pr][1] for pr, pr_conv in zip(params, params_full_conv): out, in_, h, w = conv_params[pr_conv][0].shape W = fc_params[pr][0].reshape((out, in_, h, w)) conv_params[pr_conv][0][...] = W net_full_conv.save('imagenet/caffe_imagenet_full_conv') # load input and configure preprocessing im = caffe.io.load_image('images/cat.jpg') plt.imshow(im) net_full_conv.set_mean('data', np.load('../python/caffe/imagenet/ilsvrc_2012_mean.npy')) net_full_conv.set_channel_swap('data', (2,1,0)) net_full_conv.set_raw_scale('data', 255.0) # make classification map by forward pass and show top prediction index per location out = net_full_conv.forward_all(data=np.asarray([net_full_conv.preprocess('data', im)])) out['prob'][0].argmax(axis=0)