caffe学习笔记(8):Net Surgery

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)

这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值