用到的库
<strong><span style="color:#3366FF;">numpy </span></strong> : <span style="color:#FF6600;">for numerical routines
</span>
<strong><span style="color:#3366FF;">matplotlib</span></strong>: <span style="color:#FF6600;"> for plotting
</span>
import numpy as np
import matplotlib.pyplot as plt
labels_file = caffe_root + 'data/ilsvrc12/synset_words.txt'
labels = np.loadtxt(labels_file, str, delimiter='\t')
<strong><span style="color:#3366FF;">caffe.set_mode_cpu()
</span></strong><pre name="code" class="html"><strong><span style="color:#3366FF;">caffe.set_mode_gpu()</span></strong>
用构建net的函数
<strong><span style="font-size:18px;color:#3366FF;">caffe.Net()</span></strong>
<span style="font-size:18px;"><span style="color:#FF0000;"><span style="font-size:24px;">载入网络与参数</span>
</span>net = caffe.Net(model_def, <span style="color:#FF6600;"># defines the structure of the model</span>
model_weights, <span style="color:#FF6600;"># contains the trained weights</span>
caffe.TEST) <span style="color:#FF6600;"># use test mode (e.g., don't perform dropout)</span>
</span><pre name="code" class="python"><span style="font-size:18px;"><span style="color:#FF0000;"><span style="font-size:24px;">改变输入size</span></span></span>
net.blobs['data'].reshape(50,
# batch size
3, # 3-channel (BGR) images
227, 227) # image size is 227x227
<span style="font-size:18px;"></span><pre name="code" class="python"><span style="font-size:18px;"><span style="color:#FF0000;"><span style="font-size:24px;">前向 和 输出最大及top5</span></span></span>
# copy the image data into the memory allocated for the netnet.blobs['data'].data[...] = transformed_image
### perform classificationoutput = net.forward()output_prob = output['prob'][0]
# the output probability vector for the first image in the batch# sort top five predictions from softmax outputtop_inds = output_prob.argsort()[::-1][:5] # reverse sort and take five largest itemsprint 'probabilities and labels:'zip(output_prob[top_inds], labels[top_inds])
<span style="font-size:18px;"><span style="color:#FF6600;"></span></span><pre name="code" class="python"><pre name="code" class="python"><span style="font-size:18px;"><span style="color:#FF0000;"><span style="font-size:24px;">输出网络超参数及参数</span></span></span>
# for each layer, show the output shape
for layer_name, blob in net.blobs.iteritems():
print layer_name + '\t' + str(blob.data.shape)
for layer_name, param in net.params.iteritems():
print layer_name + '\t' + str(param[0].data.shape), str(param[1].data.shape)
filters = net.params['conv1'][0].data
feat = net.blobs['conv1'].data[0, :96]
用预处理函数<span style="color:#3366FF;">caffe.io.Transformer</span>
<span style="color:#3366FF;">caffe.io.Transformer</span>
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1)) <span style="color:#FF6600;"># move image channels to outermost dimension</span>
transformer.set_mean('data', mu) <span style="color:#FF6600;"># subtract the dataset-mean value in each channel</span>
transformer.set_raw_scale('data', 255) <span style="color:#FF6600;"> # rescale from [0, 1] to [0, 255]</span>
transformer.set_channel_swap('data', (2,1,0)) <span style="color:#FF6600;"># swap channels from RGB to BGR</span>
image = caffe.io.load_image(caffe_root + 'examples/images/cat.jpg')
transformed_image = transformer.preprocess('data', image)
可视化函数
<span style="font-size:18px;"></span><pre name="code" class="python"><span style="font-size:18px;"><span style="color:#FF6600;"><span style="color:#000000;"></span></span></span><pre name="code" class="python"><pre name="code" class="python"><pre name="code" class="python"><span style="font-size:18px;"><span style="color:#FF0000;"><span style="font-size:24px;">可视化 blobs 和 params</span></span></span>
vis_square(data)