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.
pre-surgery output mean -0.02
post-surgery output mean 0.98
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)
相关文章推荐
- CUDA搭建
- 深入理解CNN的细节
- Some Notes of Caffe Installation
- Some Notes of Python Interfaces Pycaffe (Caffe)
- TensorFlow人工智能引擎入门教程之十二 Caffe转换tensorflow并 跨平台调用
- TensorFlow人工智能引擎入门教程所有目录
- 安装caffe过程记录
- py-faster-rcnn训练笔记(ubuntu14.04+cuda7.5+cuDNNv3+Python2.7)
- convolutional neural network
- UFLDL Exercise: Convolutional Neural Network
- 使用深度卷积网络和支撑向量机实现的商标检测与分类的例子
- 对Pedestrian Detection aided by Deep Learning Semantic Tasks的小结
- 准确率, 召回率,mAP
- ubuntu 14.04上配置无GPU的Caffe(A卡机适用)
- caffe term: epoch, itr
- caffe+ubuntu14.04
- caffe中的iteration,batch_size, epochs理解
- 阅读 理解 思考 - Learning to Segment Object Candidates
- 卷积神经网络学习