Caffe中c++与python调用图像分类接口

Windows下caffe用fine-tuning训练好的caffemodel来进行图像分类

转载地址 http://blog.csdn.net/xjz18298268521/article/details/52061649

小菜准备了几张验证的图片存放路径为caffe根目录下的 examples/images/, 如果我们想用一个微调训练好的caffemodel来对这张图片进行分类,那该怎么办呢?下面小菜来详细介绍一下这一任务的步骤。一般可以同两种方式进行测试,分别是基于c++接口和python接口。不管是用c++来进行分类,还是用python接口来分类,我们都应该准备这样三个文件: 
1、 caffemodel文件。 
就是之前小菜fine-tuning训练好的caffemodel,小菜用的是result_iter_1000.caffemodel。 
2、均值文件。 
有了caffemodel文件,就需要对应的均值文件,在测试阶段,需要把测试数据减去均值。这个文件我们用脚本来下载,在caffe根目录下执行: 
data/ilsvrc12/get_ilsvrc_aux.sh 
执行并下载后,均值文件放在 data/ilsvrc12/ 文件夹里。不过小菜用的是自己的数据集的均值文件,也就是fine-tuning时生成的均值文件mean.binaryproto。 
3、synset_words.txt文件 
在调用脚本文件下载均值的时候,这个文件也一并下载好了。里面放的是1000个类的名称。小菜自己在examples/myfile/目录下建立一个自己的ynset_words.txt,里面写了根据小菜需要分类的6个类别文本。 
数据准备好了,我们就可以开始分类了,我们给大家提供两个版本的分类方法: 
一、c++方法 
在caffe根目录下的 examples/cpp-classification/ 文件夹下面,有个classification.cpp文件,就是用来分类的。一开始安装caffe时,编译了caffe.sln会在Build/x64/Release/下有

classification.exe,直接条用就行。我们就直接运行命令:
build/x64/examples/cpp_classification.exe(此处有空格)\
examples/myfile/deploy.prototxt (此处有空格) \  
examples/myfile/result_iter_1000.caffemodel  (此处有空格)\
examples/myfile/mean.binaryproto (此处有空格)\
examples/myfile/synset_words.txt (此处有空格)\
examples/images/cat.jpg
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

命令很长,用了很多的\符号来换行。可以看出,从第二行开始就是参数,每行一个,共需要4个参数。为了方便小菜这里把这命令写到脚本文件里了(xxx.sh),在cygwin里直接运行。 
运行成功后,输出top-5结果: 
———- Prediction for examples/images/cat.jpg ———- 
0.3134 - “n02123045 tabby, tabby cat” 
0.2380 - “n02123159 tiger cat” 
0.1235 - “n02124075 Egyptian cat” 
0.1003 - “n02119022 red fox, Vulpes vulpes” 
0.0715 - “n02127052 lynx, catamount” 
即有0.3134的概率为tabby cat, 有0.2380的概率为tiger cat …… 
二、python方法 
python接口可以使用jupyter notebook来进行可视化操作,因此推荐使用这种方法。建立一个test.py格式的脚本代码如下,在test.py根目录下(CMD)通过运行Python test.py来验证测试图片的分类结果,也可以在cygwin中运行。代码如下:

**#coding=utf-8
****#加载必要的库******
import numpy as np

import sys,os,caffe

**#设置当前目录**
caffe_root = 'd:/caffe-master/' 
sys.path.insert(0, caffe_root + 'python')
import caffe
os.chdir(caffe_root)

net_file=caffe_root + 'examples/myfile/deploy.prototxt'

caffe_model=caffe_root + 'examples/myfile/result_iter_1000.caffemodel'
mean_file=caffe_root + 'examples/myfile/mean.npy'
****#python/caffe/imagenet/ilsvrc_2012_mean.npy****

net = caffe.Net(net_file,caffe_model,caffe.TEST)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))
transformer.set_raw_scale('data', 255) 
transformer.set_channel_swap('data', (2,1,0))

im=caffe.io.load_image(caffe_root+'examples/images/2.jpg')
net.blobs['data'].data[...] = transformer.preprocess('data',im)
out = net.forward()
****#imagenet_labels_filename = caffe_root + 'data/ilsvrc12/synset_words.txt'****
imagenet_labels_filename = caffe_root + 'examples/myfile/synset_words.txt'
labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')
top_k = net.blobs['prob'].data[0].flatten().argsort()[-1:-6:-1]
for i in np.arange(top_k.size):
    print top_k[i], labels[top_k[i]]

猜你喜欢

转载自blog.csdn.net/liuxiangxxl/article/details/79382765