环境:caffe, ipython notebook
1. 配置好caffe 的python环境,确保 import caffe 命令成功
2. 在notebook编写程序(或参考参考官网imagenet的实现过程: http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb):
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
caffe_root = '/home/wanghl/CAFFE/'# this file is expected to be in {caffe_root}/examples
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
# Set the right path to your model definition file, pretrained model weights,
#设置好模型路径,和想分类的图像
# and the image you would like to classify.
MODEL_FILE = caffe_root+'examples/imagenet/imagenet_deploy.prototxt'
PRETRAINED = caffe_root+'examples/imagenet/caffe_reference_imagenet_model'
caffe.set_mode_cpu()
net = caffe.Classifier(MODEL_FILE, PRETRAINED, mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1), channel_swap=(2,1,0), raw_scale=255,image_dims=(256, 256))
imagenet_labels_filename = caffe_root + 'data/ilsvrc12/synset_words.txt'
labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')
IMAGE_FILE = caffe_root+'examples/images/cat.jpg'
input_image = caffe.io.load_image(IMAGE_FILE)
#plt.imshow(input_image)
#plt.show()
#分类预测,预测完就可以对每一层特征进行可视化
prediction = net.predict([input_image])
#输出前5个概率最高的类别
print 'predicted class:',prediction[0].argmax()
top_k = net.blobs['prob'].data[0].flatten().argsort()[-1:-6:-1]
print labels[top_k]
[(k, v.data.shape) for k, v in net.blobs.items()]
[(k, v[0].data.shape) for k, v in net.params.items()]
#定义显示函数
#def showimage(input_image):
# if input_image.ndim == 3:
# m = input_image[:, :, ::-1]
# plt.imshow(input_image)
#定义函数vis_square
def vis_square(data, 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))
# 对图像使用滤波器
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.imshow(data)
#conv1层特征显示
conv1 = net.blobs['conv1']
conv1_data = conv1.data[0,:]
vis_square(conv1_data,padval=1)
plt.show()
#显示第85个特征
res = conv1_data[84:85,:]
vis_square(res,padval=1)
plt.show()
# index four is the center crop
# 输出输入的图像
image = net.blobs['data'].data[4].copy()
image -= image.min()
image /= image.max()
plt.imshow(image.transpose(1, 2, 0))
# the parameters are a list of [weights, biases]
filters = net.params['conv1'][0].data
vis_square(filters.transpose(0, 2, 3, 1))
feat = net.blobs['conv1'].data[4, :96]
vis_square(feat, padval=1)
#全连接层每一层的类别直方图
feat = net.blobs['fc6'].data[4]
plt.subplot(2, 1, 1)
plt.plot(feat.flat)
plt.subplot(2, 1, 2)
_ = plt.hist(feat.flat[feat.flat > 0], bins=100)
feat = net.blobs['fc7'].data[4]
plt.subplot(2, 1, 1)
plt.plot(feat.flat)
plt.subplot(2, 1, 2)
_ = plt.hist(feat.flat[feat.flat > 0], bins=100)
feat = net.blobs['prob'].data[4]
plt.subplot(2, 1, 1)
plt.plot(feat.flat)