这里的代码位于 $caffe-root/examples 下,文件名称为00-classification.ipynb,可以在自己的电脑下用jupyter跑一下,加深记忆。

导入相关的库

# 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  #notebook 使用 matplotlib# set display defaults
plt.rcParams['figure.figsize'] = (10, 10)        # large images
plt.rcParams['image.interpolation'] = 'nearest'  # 插值方式
plt.rcParams['image.cmap'] = 'gray'  # 灰度输出

有关rcParams参数了解可以参考:http://blog.csdn.net/iamzhangzhuping/article/details/50792208

# The caffe module needs to be on the Python path;
#  we'll add it here explicitly.
import sys
caffe_root = '../'  # this file should be run from {caffe_root}/examples (otherwise change this line)
sys.path.insert(0, caffe_root + 'python')import caffe
# If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.

如果import caffe 出现错误,则参考:http://blog.csdn.net/m0_37477175/article/details/78295072重新编译
因为此时文件在 $caffe_root/examples下,所以要把此文件的环境放在$caffe_root/python下才可以。

如果没有相关的模型和模型参数,就下载:

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'net = caffe.Net(model_def,      # defines the structure of the modelmodel_weights,  # contains the trained weightscaffe.TEST)     # use test mode (e.g., don't perform dropout)

注意:此时的网络配置文件并不是原始训练的文件,相比于原始训练的网络,这个test网络去掉了参数初始化,accuracy层,loss层,另外加了porp层(softmax概率),并且修改了数据输入层,数据输入层的修改如下:

原:

layer {name: "data"type: "Data"top: "data"top: "label"include {phase: TRAIN}transform_param {mirror: truecrop_size: 227mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"}
# mean pixel / channel-wise mean instead of mean image
#  transform_param {#    crop_size: 227
#    mean_value: 104
#    mean_value: 117
#    mean_value: 123
#    mirror: true
#  }data_param {source: "examples/imagenet/ilsvrc12_train_lmdb"batch_size: 256backend: LMDB}
}
layer {name: "data"type: "Data"top: "data"top: "label"include {phase: TEST}transform_param {mirror: falsecrop_size: 227mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"}
# mean pixel / channel-wise mean instead of mean image
#  transform_param {#    crop_size: 227
#    mean_value: 104
#    mean_value: 117
#    mean_value: 123
#    mirror: false
#  }data_param {source: "examples/imagenet/ilsvrc12_val_lmdb"batch_size: 50backend: LMDB}
}

改:

layer {name: "data"type: "Input"top: "data"input_param { shape: { dim: 10 dim: 3 dim: 227 dim: 227 } }
}

输入数据的处理

此过程使用caffe.io.Transformer来做处理,当然也可以使用其他的方法,只要最后处理的过程与训练时数据的处理过程一致就行了。

  • 默认的caffenet数据的图像的三色通道读入顺序是BGR
  • 输入的数据范围在[0 255]之间,之后加载平均文件
  • 读入的图片数据,通道数在第三个位置,需要变换到第一个位置,即由[227 227 3]变为[3 227 227]
# 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)
#mean-subtracted values: [('B', 104.0069879317889), ('G', 116.66876761696767), ('R', 122.6789143406786)]# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})transformer.set_transpose('data', (2,0,1))  # move image channels to outermost dimension
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] 数据变换到[0, 255]
transformer.set_channel_swap('data', (2,1,0))  # swap channels from RGB to BGR

注意: RGB ——-> BGR

进行CPU分类

只是分类一张图片。

# 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 size3,         # 3-channel (BGR) images227, 227)  # image size is 227x227

调整输入数据的大小,这部可以跳过,因为网络配置文件中已经有所设置。

image = caffe.io.load_image(caffe_root + 'examples/images/cat.jpg')
transformed_image = transformer.preprocess('data', image)
plt.imshow(image)

# copy the image data into the memory allocated for the net
net.blobs['data'].data[...] = transformed_image### perform classification
output = net.forward()output_prob = output['prob'][0]  # 输出batch中第一张图片的概率向量(总共种类为1000类)print 'predicted class is:', output_prob.argmax()
#输出概率向量中最大值的位置
#predicted class is: 281

对于caffe Python 其他API的详细介绍可以参考:
http://www.cnblogs.com/denny402/p/5686257.html

# load ImageNet labels
labels_file = caffe_root + 'data/ilsvrc12/synset_words.txt'
if not os.path.exists(labels_file):!../data/ilsvrc12/get_ilsvrc_aux.shlabels = np.loadtxt(labels_file, str, delimiter='\t')print 'output label:', labels[output_prob.argmax()]
#output label: n02123045 tabby, tabby cat
#"Tabby cat" is correct! But let's also look at other top (but less confident predictions).

查看分类概率由大到小前五的概率(置信度),以及分类的标签

# sort top five predictions from softmax output
top_inds = output_prob.argsort()[::-1][:5]  # reverse sort and take five largest itemsprint 'probabilities and labels:'
zip(output_prob[top_inds], labels[top_inds])

probabilities and labels:
Out[10]:
[(0.31243637, ‘n02123045 tabby, tabby cat’),
(0.2379719, ‘n02123159 tiger cat’),
(0.12387239, ‘n02124075 Egyptian cat’),
(0.10075711, ‘n02119022 red fox, Vulpes vulpes’),
(0.070957087, ‘n02127052 lynx, catamount’)]

使用GPU模式进行训练

对比CPU和GPU的一次前向运算时间

%timeit net.forward()
## 1 loop, best of 3: 1.42 s per loop
#切换到GPU模式
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
%timeit net.forward()
# 10 loops, best of 3: 70.2 ms per loop
# 速度明显增加

检查中间输出,可视化参数

A net is not just a black box; let’s take a look at some of the parameters and intermediate activations.
First we’ll see how to read out the structure of the net in terms of activation and parameter shapes.
For each layer, let’s look at the activation shapes, which typically have the form (batch_size, channel_dim, height, width).
The activations are exposed as an OrderedDict, net.blobs.

# 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)

权重w和偏置b的shape大小:

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,)

可视化特征图

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 displaydata = (data - data.min()) / (data.max() - data.min())# force the number of filters to be squaren = int(np.ceil(np.sqrt(data.shape[0])))padding = (((0, n ** 2 - data.shape[0]),(0, 1), (0, 1))                 # add some space between filters+ ((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)# tile the filters into an imagedata = 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); plt.axis('off')

对第一层卷积层w参数的可视化

# the parameters are a list of [weights, biases]
filters = net.params['conv1'][0].data
vis_square(filters.transpose(0, 2, 3, 1))

对前36个feature map 的可视化

feat = net.blobs['conv1'].data[0, :36]
vis_square(feat)

对pooling层的feature map的可视化

feat = net.blobs['pool5'].data[0]
vis_square(feat)

对于fc6全卷积层,展示了输出值和正值的直方图

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)

可视化输出概率值:

feat = net.blobs['prob'].data[0]
plt.figure(figsize=(15, 3))
plt.plot(feat.flat)

测试自己的图片

# download an image
my_image_url = "..."  # paste your URL here
# for example:
# my_image_url = "https://upload.wikimedia.org/wikipedia/commons/b/be/Orang_Utan%2C_Semenggok_Forest_Reserve%2C_Sarawak%2C_Borneo%2C_Malaysia.JPG"
!wget -O image.jpg $my_image_url# transform it and copy it into the net
image = caffe.io.load_image('image.jpg')
net.blobs['data'].data[...] = transformer.preprocess('data', image)# perform classification
net.forward()# obtain the output probabilities
output_prob = net.blobs['prob'].data[0]# sort top five predictions from softmax output
top_inds = output_prob.argsort()[::-1][:5]plt.imshow(image)print 'probabilities and labels:'
zip(output_prob[top_inds], labels[top_inds])

caffe 利用Python API做分类预测,以及特征的可视化相关推荐

  1. 网传天猫双十一数据造假?利用Python对其进行预测分析

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 朱小五 PS:如有需要Python学习资料的小伙伴可以加点击下方链接 ...

  2. python数据预测_利用Python编写一个数据预测工具

    利用Python编写一个数据预测工具 发布时间:2020-11-07 17:12:20 来源:亿速云 阅读:96 这篇文章运用简单易懂的例子给大家介绍利用Python编写一个数据预测工具,内容非常详细 ...

  3. ML之kNNC:基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测daiding

    ML之kNNC:基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测 目录 基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测 设计思路 ...

  4. ML之kNNC:基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测

    ML之kNNC:基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测 目录 基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测 设计思路 ...

  5. 数据分享|PYTHON用决策树分类预测糖尿病和可视化实例

    全文下载链接:http://tecdat.cn/?p=23848 在本文中,决策树是对例子进行分类的一种简单表示.它是一种有监督的机器学习技术,数据根据某个参数被连续分割.决策树分析可以帮助解决分类和 ...

  6. ROC原理介绍及利用python实现二分类和多分类的ROC曲线

    对于分类器,或者说分类算法,评价指标主要有precision,recall,F-score1,以及即将要讨论的ROC和AUC.本文通过对这些指标的原理做一个简单的介绍,然后用python分别实现二分类 ...

  7. 初学者笔记(三):利用python列表做一个最简单的垃圾分类

    系列文章目录 初学者笔记(一):利用python求100的因数 初学者笔记(二):利用python输出一个1-100的奇数列表 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目 ...

  8. 利用python语言实现分类算法_使用python实现kNN分类算法

    k-近邻算法是基本的机器学习算法,算法的原理非常简单: 输入样本数据后,计算输入样本和参考样本之间的距离,找出离输入样本距离最近的k个样本,找出这k个样本中出现频率最高的类标签作为输入样本的类标签,很 ...

  9. 利用python进行AdaBoost模型预测

    以信用卡违约数据为例,该数据集来源于UCI网站,一共包30 000条记录和25个变量,其中自变量包含客户的性别.受教育水平.年龄.婚姻状况.信用额度.6个月的历史还款状态.账单金额以及还款金额,因变量 ...

  10. python机器学习之分类预测

    目录 逻辑回归 水位判断案例引入逻辑回归计算原理 逻辑回归 单次项逻辑回归代码示例 二阶项及以上项式的边界函数计算和绘制 二阶多项式逻辑回归案例 尝试用一阶函数画出边界 二阶项逻辑回归 K近邻分类模型 ...

最新文章

  1. eclipse引入svn插件,并将项目同步到svn
  2. Dat.gui 使用教程
  3. CLion 控制台输出内容乱码问题的解决方法
  4. win7更新错误0x800b0109_Win7系统Windows update更新出现错误代码800b0101怎么办
  5. Linux下profile和bashrc四种的区别
  6. boost之对象池使用实例
  7. Vue第一部分(4):表单的双向数据绑定:v-model指令
  8. 计算指数函数的和的对数
  9. python getopt_python 之 分割参数getopt
  10. 搞乱别人精美的代码,一点数没有?
  11. oracle增量备份level0,Oracle备份类型level0、level1,增量、差异备份
  12. laravel框架使用datatables
  13. 地图比例尺、瓦片切片方案、EPSG
  14. doc 和docx的区别
  15. OR1200处理器中Wishbone总线接口模块WB_BIU介绍
  16. 智能物联网之共享单车智能锁是如何接入云端的
  17. 中科大研究人工智能怎么样
  18. js判断客户端访问是安卓还是ios
  19. 【微信小程序】微信小程序读取本地文件--学习微信小程序之路02
  20. 5月青龙面板JD脚本库

热门文章

  1. Javascript实现鼠标框选元素后拖拽被框选的元素
  2. 让Linux脱胎换骨的标致桌面与主题
  3. Dynamics CRM - 不同类型字段在 Plugin 里的赋值方式
  4. 【Spring 核心】装配Bean(一) 自动化装配
  5. win7下cmd常用命令
  6. 灰度变换——反转,对数变换,伽马变换,灰度拉伸,灰度切割,位图切割
  7. 时间序列的归一化方法
  8. 基于.net技术的 Rss 订阅开发
  9. 接口学习心得(Interface)
  10. list 转 json,以及 json 解析