[TOC]

requisite: 
1. ubuntu16.04,caffe已成功安装 
2. python2.7 
3. 我本地的caffe_root: /home/sarahzhou/caffe

下载googlenet caffemodel文件

有两种途径可以获取caffemodel文件:

1 使用caffe提供的脚本,注意在caffe_root下执行

sarahzhou@lenovo:~/caffe$ sudo python scripts/download_model_binary.py models/bvlc_googlenet
  • 1

脚本的详细使用说明请参考caffe官网的where to get trained models部分。脚本下载完毕的bvlc_googlenet.caffemodel存储在${caffe_root}/models/bvlc_googlenet目录下。

2 直接到berkeleyvision的网站下载 
http://dl.caffe.berkeleyvision.org/,下载完毕后放到${caffe_root}/models/bvlc_googlenet目录下。

准备数据

因为我们使用的googlenet caffemodel是用imagenet训练好的,所以我们使用这个model的时候,也需要知道相关的imagenet的数据信息:

sarahzhou@lenovo:~/caffe$ ./data/ilsvrc12/get_ilsvrc_aux.sh 
  • 1

执行完毕后,在${caffe_root}/data/ilsvrc12目录下多了以下这些文件:

det_synset_words.txt
imagenet.bet.pickle
synsets.txt             //imagenet数据类别的ID,共有1000个类别
synset_words.txt        //类别ID和类别名字的对应关系
imagenet_mean.binaryproto //imagenet数据均值
train.txt
test.txt
val.txt   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

python代码

用来做测试的图片是: 

图片分类

# coding=utf-8import numpy as np
import matplotlib.pyplot as plt
import pylab
from PIL import Image
import caffecaffe_root = "/home/sarahzhou/caffe/"
net_file = caffe_root + "models/bvlc_googlenet/deploy.prototxt"
model = caffe_root + "models/bvlc_googlenet/bvlc_googlenet.caffemodel"caffe.set_mode_cpu()
net = caffe.Net(net_file, model, caffe.TEST)
image_mean = np.load(caffe_root + "python/caffe/imagenet/ilsvrc_2012_mean.npy").mean(1).mean(1)# print 'mean-subtracted values:', zip('RGB', image_mean)
# 输出结果:mean-subtracted values: [('R', 104.0069879317889), ('G', 116.66876761696767), ('B', 122.6789143406786)]data_shape = net.blobs['data'].data.shape
# print data_shape
# 输出结果:(10, 3, 224, 224) batch_size:10  channels:3  height:224  weight:224transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
transformer.set_mean('data', image_mean)
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2, 1, 0))image = caffe.io.load_image('/home/sarahzhou/software/mydogs/dog.jpg')
transformed_image = transformer.preprocess('data', image)
# plt.imshow(image)
# pylab.show()net.blobs['data'].data[...] = transformed_image
output = net.forward()
output_prob = output['prob'][0]
print 'The predicted class is : ', output_prob.argmax()label_file = caffe_root + "data/ilsvrc12/synset_words.txt"
labels = np.loadtxt(label_file, str, delimiter='\t')
print 'The label is : ', labels[output_prob.argmax()]top_inds = output_prob.argsort()[::-1][:5]
print 'probabilities and labels: ', zip(output_prob[top_inds], labels[top_inds])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

输出结果为:

The predicted class is : 266 
The label is : n02113712 miniature poodle 
probabilities and labels: [(0.32076371, ‘n02113712 miniature poodle’), (0.15544809, ‘n02096051 Airedale, Airedale terrier’), (0.14952005, ‘n02102973 Irish water spaniel’), (0.11370797, ‘n02113624 toy poodle’), (0.04295243, ‘n02093991 Irish terrier’)]

可以看到利用googlenet的caffe model预测出有0.32076371的概率是 miniature poodle(迷你贵宾)

feature map 尺寸

for layer_name, feature_map in net.blobs.iteritems():print layer_name + '\t' + str(feature_map.data.shape)
  • 1
  • 2

上段代码输出每层的名称,和该层输出的feature map尺寸,输入结果为:

data (10, 3, 224, 224) 
conv1/7x7_s2 (10, 64, 112, 112) 
pool1/3x3_s2 (10, 64, 56, 56) 
pool1/norm1 (10, 64, 56, 56) 
conv2/3x3_reduce (10, 64, 56, 56) 
conv2/3x3 (10, 192, 56, 56) 
conv2/norm2 (10, 192, 56, 56) 
pool2/3x3_s2 (10, 192, 28, 28) 
pool2/3x3_s2_pool2/3x3_s2_0_split_0 (10, 192, 28, 28) 
pool2/3x3_s2_pool2/3x3_s2_0_split_1 (10, 192, 28, 28) 
pool2/3x3_s2_pool2/3x3_s2_0_split_2 (10, 192, 28, 28) 
pool2/3x3_s2_pool2/3x3_s2_0_split_3 (10, 192, 28, 28) 
inception_3a/1x1 (10, 64, 28, 28) 
inception_3a/3x3_reduce (10, 96, 28, 28) 
inception_3a/3x3 (10, 128, 28, 28) 
inception_3a/5x5_reduce (10, 16, 28, 28) 
inception_3a/5x5 (10, 32, 28, 28) 
inception_3a/pool (10, 192, 28, 28) 
inception_3a/pool_proj (10, 32, 28, 28) 
inception_3a/output (10, 256, 28, 28) 
inception_3a/output_inception_3a/output_0_split_0 (10, 256, 28, 28) 
inception_3a/output_inception_3a/output_0_split_1 (10, 256, 28, 28) 
inception_3a/output_inception_3a/output_0_split_2 (10, 256, 28, 28) 
inception_3a/output_inception_3a/output_0_split_3 (10, 256, 28, 28) 
inception_3b/1x1 (10, 128, 28, 28) 
inception_3b/3x3_reduce (10, 128, 28, 28) 
inception_3b/3x3 (10, 192, 28, 28) 
inception_3b/5x5_reduce (10, 32, 28, 28) 
inception_3b/5x5 (10, 96, 28, 28) 
inception_3b/pool (10, 256, 28, 28) 
inception_3b/pool_proj (10, 64, 28, 28) 
inception_3b/output (10, 480, 28, 28) 
pool3/3x3_s2 (10, 480, 14, 14) 
pool3/3x3_s2_pool3/3x3_s2_0_split_0 (10, 480, 14, 14) 
pool3/3x3_s2_pool3/3x3_s2_0_split_1 (10, 480, 14, 14) 
pool3/3x3_s2_pool3/3x3_s2_0_split_2 (10, 480, 14, 14) 
pool3/3x3_s2_pool3/3x3_s2_0_split_3 (10, 480, 14, 14) 
inception_4a/1x1 (10, 192, 14, 14) 
inception_4a/3x3_reduce (10, 96, 14, 14) 
inception_4a/3x3 (10, 208, 14, 14) 
inception_4a/5x5_reduce (10, 16, 14, 14) 
inception_4a/5x5 (10, 48, 14, 14) 
inception_4a/pool (10, 480, 14, 14) 
inception_4a/pool_proj (10, 64, 14, 14) 
inception_4a/output (10, 512, 14, 14) 
inception_4a/output_inception_4a/output_0_split_0 (10, 512, 14, 14) 
inception_4a/output_inception_4a/output_0_split_1 (10, 512, 14, 14) 
inception_4a/output_inception_4a/output_0_split_2 (10, 512, 14, 14) 
inception_4a/output_inception_4a/output_0_split_3 (10, 512, 14, 14) 
inception_4b/1x1 (10, 160, 14, 14) 
inception_4b/3x3_reduce (10, 112, 14, 14) 
inception_4b/3x3 (10, 224, 14, 14) 
inception_4b/5x5_reduce (10, 24, 14, 14) 
inception_4b/5x5 (10, 64, 14, 14) 
inception_4b/pool (10, 512, 14, 14) 
inception_4b/pool_proj (10, 64, 14, 14) 
inception_4b/output (10, 512, 14, 14) 
inception_4b/output_inception_4b/output_0_split_0 (10, 512, 14, 14) 
inception_4b/output_inception_4b/output_0_split_1 (10, 512, 14, 14) 
inception_4b/output_inception_4b/output_0_split_2 (10, 512, 14, 14) 
inception_4b/output_inception_4b/output_0_split_3 (10, 512, 14, 14) 
inception_4c/1x1 (10, 128, 14, 14) 
inception_4c/3x3_reduce (10, 128, 14, 14) 
inception_4c/3x3 (10, 256, 14, 14) 
inception_4c/5x5_reduce (10, 24, 14, 14) 
inception_4c/5x5 (10, 64, 14, 14) 
inception_4c/pool (10, 512, 14, 14) 
inception_4c/pool_proj (10, 64, 14, 14) 
inception_4c/output (10, 512, 14, 14) 
inception_4c/output_inception_4c/output_0_split_0 (10, 512, 14, 14) 
inception_4c/output_inception_4c/output_0_split_1 (10, 512, 14, 14) 
inception_4c/output_inception_4c/output_0_split_2 (10, 512, 14, 14) 
inception_4c/output_inception_4c/output_0_split_3 (10, 512, 14, 14) 
inception_4d/1x1 (10, 112, 14, 14) 
inception_4d/3x3_reduce (10, 144, 14, 14) 
inception_4d/3x3 (10, 288, 14, 14) 
inception_4d/5x5_reduce (10, 32, 14, 14) 
inception_4d/5x5 (10, 64, 14, 14) 
inception_4d/pool (10, 512, 14, 14) 
inception_4d/pool_proj (10, 64, 14, 14) 
inception_4d/output (10, 528, 14, 14) 
inception_4d/output_inception_4d/output_0_split_0 (10, 528, 14, 14) 
inception_4d/output_inception_4d/output_0_split_1 (10, 528, 14, 14) 
inception_4d/output_inception_4d/output_0_split_2 (10, 528, 14, 14) 
inception_4d/output_inception_4d/output_0_split_3 (10, 528, 14, 14) 
inception_4e/1x1 (10, 256, 14, 14) 
inception_4e/3x3_reduce (10, 160, 14, 14) 
inception_4e/3x3 (10, 320, 14, 14) 
inception_4e/5x5_reduce (10, 32, 14, 14) 
inception_4e/5x5 (10, 128, 14, 14) 
inception_4e/pool (10, 528, 14, 14) 
inception_4e/pool_proj (10, 128, 14, 14) 
inception_4e/output (10, 832, 14, 14) 
pool4/3x3_s2 (10, 832, 7, 7) 
pool4/3x3_s2_pool4/3x3_s2_0_split_0 (10, 832, 7, 7) 
pool4/3x3_s2_pool4/3x3_s2_0_split_1 (10, 832, 7, 7) 
pool4/3x3_s2_pool4/3x3_s2_0_split_2 (10, 832, 7, 7) 
pool4/3x3_s2_pool4/3x3_s2_0_split_3 (10, 832, 7, 7) 
inception_5a/1x1 (10, 256, 7, 7) 
inception_5a/3x3_reduce (10, 160, 7, 7) 
inception_5a/3x3 (10, 320, 7, 7) 
inception_5a/5x5_reduce (10, 32, 7, 7) 
inception_5a/5x5 (10, 128, 7, 7) 
inception_5a/pool (10, 832, 7, 7) 
inception_5a/pool_proj (10, 128, 7, 7) 
inception_5a/output (10, 832, 7, 7) 
inception_5a/output_inception_5a/output_0_split_0 (10, 832, 7, 7) 
inception_5a/output_inception_5a/output_0_split_1 (10, 832, 7, 7) 
inception_5a/output_inception_5a/output_0_split_2 (10, 832, 7, 7) 
inception_5a/output_inception_5a/output_0_split_3 (10, 832, 7, 7) 
inception_5b/1x1 (10, 384, 7, 7) 
inception_5b/3x3_reduce (10, 192, 7, 7) 
inception_5b/3x3 (10, 384, 7, 7) 
inception_5b/5x5_reduce (10, 48, 7, 7) 
inception_5b/5x5 (10, 128, 7, 7) 
inception_5b/pool (10, 832, 7, 7) 
inception_5b/pool_proj (10, 128, 7, 7) 
inception_5b/output (10, 1024, 7, 7) 
pool5/7x7_s1 (10, 1024, 1, 1) 
loss3/classifier (10, 1000) 
prob (10, 1000)

从上述输出结果中就可以看出每层经过运算之后输出到下一层的数据尺寸,比如inception_4b/3x3 (10, 224, 14, 14),其中batch_size:10 channels:224 height:14 weight:14.

卷积核尺寸

for layer_name, kernel in net.params.iteritems():print layer_name + '\t' + str(kernel[0].data.shape)
  • 1
  • 2

上述代码用来输出每层的卷积核的尺寸:

conv1/7x7_s2 (64, 3, 7, 7) 
conv2/3x3_reduce (64, 64, 1, 1) 
conv2/3x3 (192, 64, 3, 3) 
inception_3a/1x1 (64, 192, 1, 1) 
inception_3a/3x3_reduce (96, 192, 1, 1) 
inception_3a/3x3 (128, 96, 3, 3) 
inception_3a/5x5_reduce (16, 192, 1, 1) 
inception_3a/5x5 (32, 16, 5, 5) 
inception_3a/pool_proj (32, 192, 1, 1) 
inception_3b/1x1 (128, 256, 1, 1) 
inception_3b/3x3_reduce (128, 256, 1, 1) 
inception_3b/3x3 (192, 128, 3, 3) 
inception_3b/5x5_reduce (32, 256, 1, 1) 
inception_3b/5x5 (96, 32, 5, 5) 
inception_3b/pool_proj (64, 256, 1, 1) 
inception_4a/1x1 (192, 480, 1, 1) 
inception_4a/3x3_reduce (96, 480, 1, 1) 
inception_4a/3x3 (208, 96, 3, 3) 
inception_4a/5x5_reduce (16, 480, 1, 1) 
inception_4a/5x5 (48, 16, 5, 5) 
inception_4a/pool_proj (64, 480, 1, 1) 
inception_4b/1x1 (160, 512, 1, 1) 
inception_4b/3x3_reduce (112, 512, 1, 1) 
inception_4b/3x3 (224, 112, 3, 3) 
inception_4b/5x5_reduce (24, 512, 1, 1) 
inception_4b/5x5 (64, 24, 5, 5) 
inception_4b/pool_proj (64, 512, 1, 1) 
inception_4c/1x1 (128, 512, 1, 1) 
inception_4c/3x3_reduce (128, 512, 1, 1) 
inception_4c/3x3 (256, 128, 3, 3) 
inception_4c/5x5_reduce (24, 512, 1, 1) 
inception_4c/5x5 (64, 24, 5, 5) 
inception_4c/pool_proj (64, 512, 1, 1) 
inception_4d/1x1 (112, 512, 1, 1) 
inception_4d/3x3_reduce (144, 512, 1, 1) 
inception_4d/3x3 (288, 144, 3, 3) 
inception_4d/5x5_reduce (32, 512, 1, 1) 
inception_4d/5x5 (64, 32, 5, 5) 
inception_4d/pool_proj (64, 512, 1, 1) 
inception_4e/1x1 (256, 528, 1, 1) 
inception_4e/3x3_reduce (160, 528, 1, 1) 
inception_4e/3x3 (320, 160, 3, 3) 
inception_4e/5x5_reduce (32, 528, 1, 1) 
inception_4e/5x5 (128, 32, 5, 5) 
inception_4e/pool_proj (128, 528, 1, 1) 
inception_5a/1x1 (256, 832, 1, 1) 
inception_5a/3x3_reduce (160, 832, 1, 1) 
inception_5a/3x3 (320, 160, 3, 3) 
inception_5a/5x5_reduce (32, 832, 1, 1) 
inception_5a/5x5 (128, 32, 5, 5) 
inception_5a/pool_proj (128, 832, 1, 1) 
inception_5b/1x1 (384, 832, 1, 1) 
inception_5b/3x3_reduce (192, 832, 1, 1) 
inception_5b/3x3 (384, 192, 3, 3) 
inception_5b/5x5_reduce (48, 832, 1, 1) 
inception_5b/5x5 (128, 48, 5, 5) 
inception_5b/pool_proj (128, 832, 1, 1) 
loss3/classifier (1000, 1024)

卷积核和feature map的可视图

def vis_square(data):# 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);pylab.show()plt.axis('off')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
filters = net.params['conv1/7x7_s2'][0].data
vis_square(filters.transpose(0, 2, 3, 1))feat = net.blobs['conv1/7x7_s2'].data[0, :36]
vis_square(feat)
  • 1
  • 2
  • 3
  • 4
  • 5

http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb

使用训练好的googlenet caffemodel进行图片分类相关推荐

  1. caffe的python接口学习(6):用训练好的模型(caffemodel)来分类新的图片

    经过前面两篇博文的学习,我们已经训练好了一个caffemodel模型,并生成了一个deploy.prototxt文件,现在我们就利用这两个文件来对一个新的图片进行分类预测. 我们从mnist数据集的t ...

  2. 水果数据集(Fruit-Dataset )+水果分类识别训练代码(支持googlenet, resnet, inception_v3, mobilenet_v2)

    水果数据集(Fruit-Dataset )+水果分类识别训练代码(支持googlenet, resnet, inception_v3, mobilenet_v2) 目录 Fruit-Dataset水果 ...

  3. 从一到二:利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试...

    通过从零到一的教程,我们已经得到了通过mnist训练集生成的caffemodel,主要包含下面四个文件: 接下来就可以利用模型进行测试了.关于测试方法按照上篇教程还是选择bat文件,当然python. ...

  4. 如何调用 caffe 训练好的模型对输入图片进行测试

    如何调用 caffe 训练好的模型对输入图片进行测试 该部分包括两篇文章 win10 下 caffe 的第一个测试程序(附带详细讲解) 主要讲解如何利用 caffe 来训练模型. 如何调用 caffe ...

  5. 使用预训练的卷积神经网络(猫狗图片分类)

    本次所用数据来自ImageNet,使用预训练好的数据来预测一个新的数据集:猫狗图片分类.这里,使用VGG模型,这个模型内置在Keras中,直接导入就可以了. from keras.applicatio ...

  6. python狗图像识别_TensorFlow卷积神经网络之使用训练好的模型识别猫狗图片

    本文是Python通过TensorFlow卷积神经网络实现猫狗识别的姊妹篇,是加载上一篇训练好的模型,进行猫狗识别 本文逻辑: 我从网上下载了十几张猫和狗的图片,用于检验我们训练好的模型. 处理我们下 ...

  7. 训练O_net网络,并测试图片

    训练O_net网络,并测试图片 上一篇,我们已经知道如何生成O_net训练集,这次我们开始训练Onet网络. 训练完成后,保存权重,我们随机抽取一张照片,测试一下效果. 代码: train_Onet. ...

  8. PyTorch搭建预训练AlexNet、DenseNet、ResNet、VGG实现猫狗图片分类

    目录 前言 AlexNet DensNet ResNet VGG 前言 在之前的文章中,利用一个简单的三层CNN猫狗图片分类,正确率不高,详见: CNN简单实战:PyTorch搭建CNN对猫狗图片进行 ...

  9. 【tensorflow 深度学习】8.训练图片分类模型

    1.训练图片分类模型的三种方法 (1).从无到有,先确定好算法框架,准备好需要训练的数据集,从头开始训练,参数一开始也是初始化的随机值,一个批次一个批次地进行训练. (2).准备好已经训练好的模型,权 ...

  10. 我写了一个脚本,实现了图片分类问题的全自动化训练

    众所周知,图片分类问题属于计算机视觉中比较容易解决的问题之一 但 这几天被数据集的问题搞得焦头烂额, 照理说分类问题的数据集应该比较好制作 但 如果之前没有现成的数据集 也会变得比较麻烦 直到我偶然发 ...

最新文章

  1. 电脑下载python多少位的在哪看-python64位
  2. string [线段树优化桶排]
  3. html怎么消除打印的进纸,打印机缺纸状态怎么消除?
  4. 解决prometheus k8s.gcr.io/addon-resizer:1.8.4镜像无法下载的问题
  5. 用Socket编写TCP程序(C/C++)(转)
  6. Java 集合 ArrayList 需要知道的几个问题
  7. 12月25晚-12月29日做的两个网页
  8. SVN使用import导入新数据到版本库
  9. 【英语学习】【Daily English】U02 Daily Routine L02 I go to the gym every other day
  10. havlenapetr ffmpeg的移植
  11. 《从问题到程序:用Python学编程和计算》——2.6 简单脚本程序
  12. 电商库存设计mysql redis_基于redis实现的扣减库存
  13. centos安装软件【google浏览器,QQ】【拷贝旧的文件源作为备份】【软件源更换为清华源】
  14. 洛谷——[USACO07OCT]Bessie‘s Secret Pasture S
  15. 给你看一下真实的后浪...
  16. IOS开发—UIGestureRecognizer Tutorial in iOS 5: Pinch
  17. SQLite的使用一
  18. PHP-dede学习:common.ini.php文件
  19. rubyonrails test 小记
  20. Huffman编码之文件的解/压缩

热门文章

  1. $smary模板缓存
  2. 【原创】Kakfa log包源代码分析(一)
  3. 应用:Xbox 360无线大屏幕控制器“WP 7”
  4. 数据结构 - 字符串的模式匹配
  5. SSM集成activiti6.0错误集锦(二)
  6. 反射方式,获取出集合ArrayList类的class文件对象
  7. 【转】【Linux】Linux 命令行快捷键
  8. Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 分块
  9. 【转】安卓开发经验分享:资源、UI、函数库、测试、构建一个都不能少
  10. 又遇到jqGrid在chrome下宽度不正常有滚动条