您的位置:首页 > Web前端

caffe学习笔记(8):Net Surgery

2016-05-21 22:17 302 查看
Caffe networks can be transformed to your particular needs by editing the model parameters. The data, diffs, and parameters of a net are all exposed in pycaffe.

load kit

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
caffe_root = './'  # this file is expected to be in {caffe_root}/examples
import sys
sys.path.insert(0, caffe_root+'python')
import caffe

# configure plotting
plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'


Designer Filters

This net has two blobs, data for the input and conv for the convolution output and one parameter conv for the convolution filter weights and biases.

# load net
caffe.set_mode_cpu()
net = caffe.Net('./examples/net_surgery/conv.prototxt', caffe.TEST)
print("blob {}\nparams {}".format(net.blobs.keys(), net.params.keys()))

# load image use np.array with caffe
im = np.array(caffe.io.load_image('./examples/images/cat_gray.jpg', color=False)).squeeze()
plt.title("original image")
plt.imshow(im)
plt.axis('off')
plt.show()
# the code in the next 4 line is very important
im_input = im[np.newaxis, np.newaxis, :, :]
net.blobs['data'].reshape(*im_input.shape)
net.blobs['data'].data[...] = im_input
print im_input.shape




# helper show filter outputs
def show_filters(net):
net.forward()
plt.figure()
filt_min= net.blobs['conv'].data.min()
filt_max= net.blobs['conv'].data.max()
for i in range(6):
plt.subplot(1,6,i+1) # we can change (1, 6, i+1) to (1, 7, i+2)
plt.title("filter #{} output".format(i))
plt.imshow(net.blobs['conv'].data[0, i], vmin=filt_min, vmax=filt_max)
plt.tight_layout()
plt.axis('off')
show_filters(net)
plt.show()




# pick first filter output
conv0 = net.blobs['conv'].data[0, 0]
print("pre-surgery output mean {:.2f}".format(conv0.mean()))
# set first filter bias to 1
net.params['conv'][1].data[0] = 1.
net.forward()
print("post-surgery output mean {:.2f}".format(conv0.mean()))


pre-surgery output mean -0.02

post-surgery output mean 0.98

ksize = net.params['conv'][0].data.shape[2:]
# make Gaussian blur
sigma = 1.
y, x = np.mgrid[-ksize[0]//2 + 1:ksize[0]//2 + 1, -ksize[1]//2 + 1:ksize[1]//2 + 1]
g = np.exp(-((x**2 + y**2)/(2.0*sigma**2)))
gaussian = (g / g.sum()).astype(np.float32)
net.params['conv'][0].data[0] = gaussian
# make Sobel operator for edge detection
net.params['conv'][0].data[1:] = 0.
sobel = np.array((-1, -2, -1, 0, 0, 0, 1, 2, 1), dtype=np.float32).reshape((3,3))
net.params['conv'][0].data[1, 0, 1:-1, 1:-1] = sobel  # horizontal
net.params['conv'][0].data[2, 0, 1:-1, 1:-1] = sobel.T  # vertical
show_filters(net)


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  caffe surgery cnn