caffe的底层是c++写的,如果要进行数据可视化,需要借助其它的库或者是接口,如opencv,python或者是matlab,python的环境需要自行配置,因为我使用的都是网管同志已经配置好的深度学习服务器,所以不用管底层的一些配置问题,如果需要自行配置自己的机器,请参照:
http://www.cnblogs.com/denny402/p/5088399.html
当前目录为caffe的根目录,使用python接口读取caffe自带的一张图片,并显示。此处一般来说导入可matplotlib后应该可以正常显示图片,但作者的电脑上一直不能弹出图片对话框,查了好久后,找到了一个对策,导入pylab库,然后在显示图片的语句后加上pylab.show(),这样就可以正常显示了,如果大家电脑上不加这一句仍然可以显示,大可把这一句删了。
图像为三通道,分辨率为360*480,所以打印出来的信息为:
>>>(360,480,3)
打开examples/net_surgery/conv.prototxt文件,修改两个地方。
一是将input_shape由原来的是(1,1,100,100)修改为(1,3,100,100),即由单通道灰度图变为三通道彩色图。
二是将过滤器个数(num_output)由3修改为16,多增加一些filter, 当然保持原来的数不变也行。
其它地方不变,修改后的prototxt如下:只有一个卷积层
# Simple single-layer network to showcase editing model parameters.
name: "convolution"
input: "data"
input_shape {
dim: 1
dim: 3
dim: 100
dim: 100
}
layer {
name: "conv"
type: "Convolution"
bottom: "data"
top: "conv"
convolution_param {
num_output: 16
kernel_size: 5
stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 0
}
}
}
将图片加载到blobs,同样也可以反过来从blob中提取出原始数据进行显示。显示图片的时候要注意各维的顺序,blobs中的顺序是(1,3,360,480),从前往后表示一张图片,三个通道,需要调用transpose改变为(360,480,3)方可正常显示。data[0]表示第一张图片,下标从0开始,使用data[0.0],data[0,1]表示图片的不同通道。
net=caffe.Net('/home/xxx/caffe/examples/net_surgery/conv.prototxt',caffe.TEST)
im_input = im[np.newaxis,:,:,:].transpose(0,3,1,2)
print "data-blobs:" im_input.shape
net.blobs['data'].reshape(*im_input.shape)
net.blobs['data'].data[...] = im_input
plt.imshow(net.blobs['data'].data[0].transpose(1,2,0))
plt.axis('off')
pylab.show()
编写一个show函数来显示数据:
plt.rcParams['image.cmap'] = 'gray'
def show_data(data,head,padsize=1, padval=0):
data -= data.min()
data /= data.max()
# force the number of filters to be square
n = int(np.ceil(np.sqrt(data.shape[0])))
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))
# tile the filters into an image
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
plt.figure()
plt.title(head)
plt.imshow(data)
plt.axis('off')
调用foward()执行卷积操作,blobs数据发生改变,从原来的(1,2,360,480)变成了(1,16,356,476),并且初始化生成了相应权值,权值,最后调用两次show函数显示权值和卷积滤波后的16通道图片。
net.forward()
print "data-blobs:",net.blobs['data'].data.shape
print "conv-blobs:",net.blobs['conv'].data.shape
print "weight-blobs:",net.params['conv'][0span>].data.shape
show_data(net.params['conv'][0].data[:,0],'conv weights(filter)')
show_data(net.blobs['conv'].data[0],'post-conv images')
最后贴上全部的代码:
import numpy as np
import pylab
import matplotlib.pyplot as plt
caffe_root = '/home/lzx/caffe/python/caffe'
import caffe
import PIL
import os,sys
plt.rcParams['image.cmap'] = 'gray'
def show_data(data,head,padsize = 1, padval = 0):
data -= data.min()
print data
data += data.max()
print data
n = int(np.ceil(np.sqrt(data.shape[0])))
print "n = ", n
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))
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
plt.figure()
plt.title(head)
plt.imshow(data)
plt.axis('off')
pylab.show()
if __name__ == '__main__':
os.chdir(caffe_root)
sys.path.append(caffe_root)
im = caffe.io.load_image('/home/lzx/caffe/examples/images/cat.jpg')
print im.shape
plt.imshow(im)
pylab.show()
net = caffe.Net('/home/lzx/caffe/examples/net_surgery/conv.prototxt',caffe.TEST)
im_input = im[np.newaxis,:,:,:].transpose(0,3,1,2)
print "data_blobs:",im_input.shape
net.blobs['data'].reshape(*im_input.shape)
net.blobs['data'].data[...] = im_input
# print "net.blobs['data'].data[0].shape:" net.blobs['data'].data[...].shape
# print "net.blobs['data'].data[0].transpose(1,2,0)" net.blobs['data'].data[0].transpose(1,2,0).shape
plt.imshow(net.blobs['data'].data[0].transpose(1,2,0))
plt.axis('off')
pylab.show()
print 'data-blobs:', net.blobs['data'].data.shape
show_data(net.blobs['data'].data[0],'origin images')
net.forward()
print "data-blobs:", net.blobs['data'].data.shape
print "conv-blobs:", net.blobs['conv'].data.shape
print "weight-blobs", net.params['conv'][0].data.shape
show_data(net.params['conv'][0].data[:,0],'conv weights(filter)')
show_data(net.blobs['conv'].data[0],'post-conv images')