本文主要学习caffe官网例子Classification: Instant Recognition with Caffe,采用caffe数据可视化的总结.
其主要过程包括:
1) 定义图像窗口属性
2) 下载caffe模型,调用caffe.Net网络载入模型配置,模型权值,模型测试
3) 数据转化,变换通道,去除均值,变换尺寸,变换rgb...
4) 定义输入图像大小批次,载入图像,并做预处理
5) 执行前向传播,预测图像类别,载入标签做对比
6) 输出每层输入数据的维度,权值偏值的维度
7) 定义可视化函数,对卷积输入,激活输出可视化
8) 载入自己数据对输出做预测
1) 定义图像窗口属性
# 1) setup
# set up Python environment: numpy for numerical routines, and matplotlib for plotting
import numpy as np
import matplotlib.pyplot as plt
# display plots in this notebook
# %matplotlib inline
import time
# set display defaults
plt.rcParams['figure.figsize'] = (10, 10) # large images (width,height) 窗口大小(inches)
plt.rcParams['image.interpolation'] = 'nearest' # don't interpolate: show square pixels
plt.rcParams['image.cmap'] = 'gray' # use grayscale output rather than a (potentially misleading)
2)下载caffe模型,调用caffe.Net网络载入模型配置,模型权值,模型测试# The caffe module needs to be on the Python path;
# The caffe module needs to be on the Python path;
# we'll add it here explicitly.
import sys
caffe_root = '/home/cc/caffe-master/' # this file should be run from {caffe_root}/examples (otherwise change this line)
# sys.path.insert(0, caffe_root + 'python')
# 可在/etc/profile文件下添加目录.以后调用不在插入.
# sys.path.append(0,'/home/cc/caffe-master/python')
import caffe
# If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.
import os
if os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):
print 'CaffeNet found.'
else:
print 'Downloading pre-trained CaffeNet model...'
# !../scripts/download_model_binary.py ../models/bvlc_reference_caffenet
caffe.set_mode_cpu()
model_def = caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt'
model_weights = caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
# caffe.Net.__init__(self, model_file, pretrained_file, caffe.TEST)
net = caffe.Net(model_def, # defines the structure of the model
model_weights, # contains the trained weights
caffe.TEST) # use test mode (e.g., don't perform dropout)
3) 数据转化,变换通道,去除均值,变换尺寸,变换rgb...
# load the mean ImageNet image (as distributed with Caffe) for subtraction
mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
mu = mu.mean(1).mean(1) # average over pixels to obtain the mean (BGR) pixel values
print 'mean-subtracted values:', zip('BGR', mu)
# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
# print net.blobs['data'].data.shape
transformer.set_transpose('data', (2, 0, 1)) # move image channels to outermost dimension
# changed the [ h, w, channel] to [channel, height, width]
# print net.blobs['data'].data.shape
transformer.set_mean('data', mu) # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2, 1, 0)) # swap channels from RGB to BGR
# mean-subtracted values: [('B', 104.0069879317889), ('G', 116.66876761696767), ('R', 122.6789143406786)]
# print net.blobs['data'].data.shape
4) 定义输入图像大小批次,载入图像,并做预处理
# set the size of the input (we can skip this if we're happy
# with the default; we can also change it later, e.g., for different batch sizes)
net.blobs['data'].reshape(50, # batch size
3, # 3-channel (BGR) images
227, 227) # image size is 227x227
image = caffe.io.load_image(caffe_root + 'examples/images/cat.jpg') #(200,200,3)
# print image.shape # (height,width,channel)
transformed_image = transformer.preprocess('data', image)#将输入图片转发为caffe格式
# print net.blobs['data'].data.shape # (batches, channel,height,width)
plt.imshow(image)
# plt.show()
# copy the image data into the memory allocated for the net
net.blobs['data'].data[...] = transformed_image
5) 执行前向传播,预测图像类别,载入标签做对比
# load ImageNet labels
labels_file = caffe_root + 'data/ilsvrc12/synset_words.txt'if not os.path.exists(labels_file): # !../ data / ilsvrc12 / get_ilsvrc_aux.sh print 'not labels_file'labels = np.loadtxt(labels_file, str, delimiter='\t')# delimiter限定符默认空格# The characters or list of characters used to indicate the start of a comment; default: ‘#’.print 'output label:', labels[output_prob.argmax()] # n02123045 tabby, tabby cat#其它低一些的预测# sort top five predictions from softmax outputtop_inds = output_prob.argsort()[::-1][:5] # reverse sort and take five largest itemsprint "probabilities and labels:\n", zip(output_prob[top_inds], labels[top_inds])start_time = time.time()net.forward()print (time.time()-start_time)# caffe.set_device(0) # if we have multiple GPUs, pick the first one# caffe.set_mode_gpu()# net.forward() # run once before timing to set up memory# print (time.time()-start_time)### perform classification output = net.forward() output_prob = output['prob'][0] # the output probability vector for the first image in the batch print 'predicted class is:', output_prob.argmax() # 281
6)输出每层输入数据的维度,权值偏值的维度
# 6) 可视化 (batch_size,channel_dim,height,width) # for each layer, show the output shape for layer_name, blob in net.blobs.iteritems(): print layer_name + '\t' + str(blob.data.shape) # 输出结果对后面理解数据结构很重要: #data (50, 3, 227, 227) #conv1 (50, 96, 55, 55) #pool1 (50, 96, 27, 27) #norm1 (50, 96, 27, 27) #conv2 (50, 256, 27, 27) #pool2 (50, 256, 13, 13) #norm2 (50, 256, 13, 13) #conv3 (50, 384, 13, 13) #conv4 (50, 384, 13, 13) #conv5 (50, 256, 13, 13) #pool5 (50, 256, 6, 6) #fc6 (50, 4096) #fc7 (50, 4096) #fc8 (50, 1000) # prob (50, 1000)
for layer_name, param in net.params.iteritems(): print layer_name + '\t' + str(param[0].data.shape), str(param[1].data.shape)#conv1 (96, 3, 11, 11) (96,)# 输出结果对后面理解数据结构很重要:
#conv2 (256, 48, 5, 5) (256,)
#conv3 (384, 256, 3, 3) (384,)
#conv4 (384, 192, 3, 3) (384,)
#conv5 (256, 192, 3, 3) (256,)
#fc6 (4096, 9216) (4096,)
#fc7 (4096, 4096) (4096,)
#fc8 (1000, 4096) (1000,)
#7)定义可视化函数,对卷积输入,激活输出可视化
def vis_square(data): """Take an array of shape (n, height, width) or (n, height, width, 3) and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)""" # normalize data for display data = (data - data.min()) / (data.max() - data.min()) # force the number of filters to be square n = int(np.ceil(np.sqrt(data.shape[0]))) #10 #96个滤波器组时 padding = (((0, n ** 2 - data.shape[0]), #((-,4),(0,1),(0,1),(0,0)) (0, 1), (0, 1)) # add some space between filters #给(11,11)添加 + ((0, 0),) * (data.ndim - 3)) # don't pad the last dimension (if there is one) data = np.pad(data, padding, mode='constant', constant_values=1) # pad with ones (white) # np.lib.pad(data,((3,2),(4,6)),,'minimum') #分别上下左右按照不同的数目来填充最小的数字 print 'data,shape', data.shape #(100,12,12,3) print 'data,shape1', data.shape[1:] # (12,12,3) # 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))) # (0,2,1,3)+(4) =(0,2,1,3,4) # print data.shape #(10,12,10,12,3) data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:]) # print 'data.shape2', data.shape #(120,120,3) plt.imshow(data) plt.show() plt.axis('off')
# 8) 载入自己数据对输出做预# the parameters are a list of [weights, biases] filters = net.params['conv1'][0].data #(96,3,11,11)提取卷积核的数据;卷积后n = (227-(ker-sd))/sd=(227-(11-4))/4=55 vis_square(filters.transpose(0, 2, 3, 1)) #转换用于python数据显示 # the first layer output[rectified responses of the filters above] feat = net.blobs['conv1'].data[0, :36] #(36,55,55),截取批次中第一个样本,36个filter可视化 #conv1 (50, 96, 55, 55) vis_square(feat) # the fifth layer after pooling feat = net.blobs['pool5'].data[0] vis_square(feat) #the first fully connetcted layer feat = net.blobs['fc6'].data[0] plt.subplot(2, 1, 1) plt.plot(feat.flat) plt.subplot(2, 1, 2) plt.hist(feat.flat[feat.flat > 0], bins=100) plt.show() # the final probability output, prob feat = net.blobs['prob'].data[0] plt.figure(figsize=(15, 3)) plt.plot(feat.flat)
# 运行自己的图像 # transform it and copy it into the net ImagePath = '/home/cc/temp/' for i in range(0, 20): image = caffe.io.load_image(ImagePath+'%s.jpg' %i) net.blobs['data'].data[...] = transformer.preprocess('data', image) # perform classification net.forward() # obtain the output probabilities output_prob = net.blobs['prob'].data[0] #(1000,) # sort top five predictions from softmax output top_inds = output_prob.argsort()[::-1][:5] #排列从小到大---[::-1]反转从大到小--[:5]截取前5个最大索引 plt.imshow(image) plt.show() print "probabilities and labels:\n", zip(output_prob[top_inds], labels[top_inds])