使用Caffe的Python接口进行Cifar10可视化

根据训练好的cifar10数据的model,从测试图片中选出一张进行测试,并进行网络模型、卷积结果及参数可视化
注意:本文中代码运行在windows+ipython notebook下,已事先配置好caffe的python接口

导入必需的包

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import caffe
%matplotlib inline
plt.rcParams['figure.figsize'] = (8, 8)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

载入网络模型

1
2
3
4
5
6
# 载入模型,并显示各层数据信息
caffe.set_mode_gpu()
net = caffe.Net('examples/cifar10/cifar10_quick.prototxt',
'examples/cifar10/cifar10_quick_iter_5000.caffemodel.h5',
caffe.TEST)
[(k, v.data.shape) for k, v in net.blobs.items()]
[('data', (1L, 3L, 32L, 32L)),
 ('conv1', (1L, 32L, 32L, 32L)),
 ('pool1', (1L, 32L, 16L, 16L)),
 ('conv2', (1L, 32L, 16L, 16L)),
 ('pool2', (1L, 32L, 8L, 8L)),
 ('conv3', (1L, 64L, 8L, 8L)),
 ('pool3', (1L, 64L, 4L, 4L)),
 ('ip1', (1L, 64L)),
 ('ip2', (1L, 10L)),
 ('prob', (1L, 10L))]

可视化网络模型

使用GraphViz+Caffe的draw_net.py来可视化网络模型

1
2
3
Rem 运行以下命令前必需先安装配置GraphViz
Rem --rankdir参数为网络方向,BT代表图片上网络从底至顶绘出
python ./Build/x64/Release/pycaffe/draw_net.py examples/cifar10/cifar10_quick_train_test.prototxt examples/cifar10/cifar-quick.png --rankdir=BT
Drawing net to examples/cifar10/cifar-quick.png
1
2
3
4
#显示模型图片
net_im = mpimg.imread('examples/cifar10/cifar-quick.png')
plt.imshow(net_im)
plt.axis('off')
(-0.5, 904.5, 2079.5, -0.5)
output_7_1.png

加载测试图片

1
2
3
4
5
#加载测试图片,并显示
im = caffe.io.load_image('examples/cifar10/cat.jpg')
print im.shape
plt.imshow(im)
plt.axis('off')
(1200L, 1600L, 3L)

(-0.5, 1599.5, 1199.5, -0.5)
output_8_2.jpg

转换均值

1
2
3
4
5
6
7
8
# 编写一个函数,将二进制的均值转换为python的均值
def convert_mean(binMean,npyMean):
blob = caffe.proto.caffe_pb2.BlobProto()
bin_mean = open(binMean, 'rb' ).read()
blob.ParseFromString(bin_mean)
arr = np.array( caffe.io.blobproto_to_array(blob) )
npy_mean = arr[0]
np.save(npyMean, npy_mean )
1
2
3
4
# 调用函数转换均值
binMean='examples/cifar10/mean.binaryproto'
npyMean='examples/cifar10/mean.npy'
convert_mean(binMean,npyMean)

将图片载入Blob

1
2
3
4
5
6
7
8
#将图片载入blob中,并减去均值
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load(npyMean).mean(1).mean(1)) # 减去均值
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2,1,0))
net.blobs['data'].data[...] = transformer.preprocess('data',im)
inputData=net.blobs['data'].data
1
2
3
4
5
6
7
8
#显示减去均值前后的数据
plt.figure()
plt.subplot(1,2,1),plt.title("origin")
plt.imshow(im)
plt.axis('off')
plt.subplot(1,2,2),plt.title("subtract mean")
plt.imshow(transformer.deprocess('data', inputData[0]))
plt.axis('off')
(-0.5, 31.5, 31.5, -0.5)
output_12_1.jpg

编写用于参数/卷积结果可视化的函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 编写一个函数,用于显示各层数据
def show_data(data, padsize=1, padval=0):
# data归一化
data -= data.min()
data /= data.max()
# 根据data中图片数量data.shape[0],计算最后输出时每行每列图片数n
n = int(np.ceil(np.sqrt(data.shape[0])))
# padding = ((图片个数维度的padding),(图片高的padding), (图片宽的padding), ....)
padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3)
data = np.pad(data, padding, mode='constant', constant_values=(padval, padval))
# 先将padding后的data分成n*n张图像
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
# 再将(n, W, n, H)变换成(n*w, n*H)
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
plt.figure()
plt.imshow(data,cmap='gray')
plt.axis('off')

可视化各层数据

1
2
3
4
5
6
# 运行模型并显示第一个卷积层的输出数据和权值(filter)
net.forward()
print net.blobs['conv1'].data[0].shape
show_data(net.blobs['conv1'].data[0])
print net.params['conv1'][0].data.shape
show_data(net.params['conv1'][0].data.reshape(32*3,5,5))
(32L, 32L, 32L)
(32L, 3L, 5L, 5L)
output_14_1.png output_14_2.png
1
2
3
# 显示第一次pooling后的输出数据
show_data(net.blobs['pool1'].data[0])
net.blobs['pool1'].data.shape
(1L, 32L, 16L, 16L)
output_15_1.png
1
2
3
4
5
# 显示第二次卷积后的输出数据以及相应的权值(filter)
show_data(net.blobs['conv2'].data[0],padval=0.5)
print net.blobs['conv2'].data.shape
show_data(net.params['conv2'][0].data.reshape(32**2,5,5))
print net.params['conv2'][0].data.shape
(1L, 32L, 16L, 16L)
(32L, 32L, 5L, 5L)
output_16_1.png output_16_2.png
1
2
3
4
5
# 显示第三次卷积后的输出数据以及相应的权值(filter),取前1024个进行显示
show_data(net.blobs['conv3'].data[0],padval=0.5)
print net.blobs['conv3'].data.shape
show_data(net.params['conv3'][0].data.reshape(64*32,5,5)[:1024])
print net.params['conv3'][0].data.shape
(1L, 64L, 8L, 8L)
(64L, 32L, 5L, 5L)
output_17_1.png output_17_2.png
1
2
3
# 显示第三次池化后的输出数据
show_data(net.blobs['pool3'].data[0],padval=0.2)
print net.blobs['pool3'].data.shape
(1L, 64L, 4L, 4L)
output_18_1.png
1
2
3
4
# 最后一层输入属于某个类的概率
feat = net.blobs['prob'].data[0]
print feat
plt.plot(feat.flat)
[ 0.00170287  0.00115923  0.0225699   0.60395384  0.00453733  0.14171894
  0.00307363  0.01260873  0.15008588  0.05858969]

[<matplotlib.lines.Line2D at 0x3bd38080>]

与cifar10中的10种类型名称进行对比:

airplane、automobile、bird、cat、deer、dog、frog、horse、ship、truck

根据测试结果,判断为Cat。