使用预训练模型对图像进行分类

这里介绍的是直接用训练好的分类任务的预训练模型来测试。当然caffe提供了几种方法进行测试。这里介绍两种:

1、直接调用工具

2、基于python接口。


对单个图像进行分类:

第一种:

使用编译好的calssification工具测试,可以用以下命令:

# sudo ./build/examples/cpp_classification/classification.bin \
  models/bvlc_reference_caffenet/deploy.prototxt \
  models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel \
  data/ilsvrc12/imagenet_mean.binaryproto \
  data/ilsvrc12/synset_words.txt \
  examples/images/cat.jpg

解析:

Classification.bin就是classification.cpp的可执行文件。

上面bvlc_reference_caffenet.caffemodel、imagenet_mean.binaryproto和synset_words.txt
需要下载,其中imagenet_mean.binaryproto是均值文件,根据训练时的图像产生,synset_words.txt是类别标签。如果使用自己的数据训练时,需要做均值,则可以根据自己的数据集产生均值文件,当然这样测试时也就一定要做均值。在SSD中,均值处理其实就是数据转换字典中train_transform_param的'mean_value': [104, 117, 123]参数,test_transform_param中也有。

bvlc_reference_caffenet.caffemodel下载:

cd caffe
wget  http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel

imagenet_mean.binaryproto和synset_words.txt下载:
cd data/ilsvrc12
./get_lisvrc_aux.sh

解压后放到相应文件夹,运行就会有top-5的结果

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"


另外:可用其他在imagenet上训练的模型,比如ResNet-101

修改

models/bvlc_reference_caffenet/deploy.prototxt \
models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel \
为
models/bvlc_reference_caffenet/ResNet_101_deploy.prototxt \
models/bvlc_reference_caffenet/ResNet-101-model.caffemodel \

第二种:python接口形式

主要利用caffe/python下的classify.py文件,使用命令

cd python
sudo python classify.py ../examples/images/cat.jpg result.npy

其中result.npy保存在python下,内容就是下面显示的识别结果top-5,但是看不了。

这里注意可能会发生错误,见https://www.cnblogs.com/denny402/p/5111018.html解决办法,还可以将结果显示在窗口上。

结果:

Loading file: ../examples/images/cat.jpg
Classifying 1 inputs.
Done in 1.60 s.
281 n02123045 tabby, tabby cat
282 n02123159 tiger cat
285 n02124075 Egyptian cat
287 n02127052 lynx, catamount
278 n02119789 kit fox, Vulpes macrotis
Saving results into result.npy

-----------------------------------------------------------------------分割线-----------------------------------------------------

参考http://www.cnblogs.com/yyxf1413/p/6339655.html

以python形式对批量图像进行分类:

#coding=utf-8
#作用:可以用来批处理图片进行分类

import os
import caffe
import numpy as np


root='/home/caffe/python/muticlass/' #根目录
deploy='/home/caffe/models/ResNetmodel/ResNet_101_deploy.prototxt'      #deploy文件的路径
caffe_model='/home/caffe/models/ResNetmodel/ResNet-101-model.caffemodel'  #caffe_model的路径
mean_file='/home/caffe/python/caffe/imagenet/ilsvrc_2012_mean.npy'     #mean_file的路径--注意,在python中要将mean.binaryproto转换为mean.npy格式
labels_filename='/home/caffe/data/ilsvrc12/synset_words.txt'  #sysset_words.txt的路径

#预读待分类的图片
import os
dir=root+'images/'
filelist=[]
filenames=os.listdir(dir)  #返回指定目录下的所有文件和目录名
for fn in filenames:
    fullfilename=os.path.join(dir,fn) #os.path.join--拼接路径
    filelist.append(fullfilename) #filelist里存储每个图片的路径
    

net=caffe.Net(deploy,caffe_model,caffe.TEST)  #加载model和network
    
#图片预处理设置
transformer=caffe.io.Transformer({'data':net.blobs['data'].data.shape})  #设定图片的格式(1,3,28,28)
transformer.set_transpose('data',(2,0,1)) #改变维度的顺序,由原始图片(28,28,3)变为(3,28,28)
transformer.set_mean('data',np.load(mean_file).mean(1).mean(1)) #减去均值
transformer.set_raw_scale('data',255)  #缩放到[0,255]之间
transformer.set_channel_swap('data',(2,1,0))  #交换通道,将图片由RGB变成BGR

#加载图片
for i in range(0,len(filelist)):
    img=filelist[i]   #获取当前图片的路径
    print filenames[i]    #打印当前图片的名称
    
    im=caffe.io.load_image(img) #加载图片
    net.blobs['data'].data[...]=transformer.preprocess('data',im) #执行上面的预处理操作,并将图片载入到blob中
    
#执行测试
    out=net.forward()
    
    labels=np.loadtxt(labels_filename,str,delimiter='/t') #读取类别名称文件
    prob=net.blobs['prob'].data[0].flatten()   #取出最后一层(prob)属于某个类标的概率值,'prob'为最后一层的名称
    
    #print prob
    index1=prob.argsort()[-1]  #获取最大概率值对应的index
    index2=prob.argsort()[-2]  #获取第二大概率值对应的index
    index3=prob.argsort()[-3]  #获取第三大概率值对应的index
    index4=prob.argsort()[-4]  #获取第四大概率值对应的index
    
    print labels[index1],'--',prob[index1]   #输出label--prob
    print labels[index2],'--',prob[index2]
    print labels[index3],'--',prob[index3]
    print labels[index4],'--',prob[index4]

首先把模型和网络结构文件,均值文件,标签文件放到你的路径下,然后把路径修改为你自己的路径即可。结果显示:

fish-bike.jpg
n04482393 tricycle, trike, velocipede -- 0.804645
n04509417 unicycle, monocycle -- 0.0884629
n02835271 bicycle-built-for-two, tandem bicycle, tandem -- 0.0245689
n09421951 sandbar, sand bar -- 0.00951305
cat_gray.jpg
n02124075 Egyptian cat -- 0.34318
n02119789 kit fox, Vulpes macrotis -- 0.1278
n02441942 weasel -- 0.077313
n02112018 Pomeranian -- 0.0701563
cat.jpg
n02123159 tiger cat -- 0.226433
n02119789 kit fox, Vulpes macrotis -- 0.195662
n02124075 Egyptian cat -- 0.165634
n02113023 Pembroke, Pembroke Welsh corgi -- 0.147812



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值