使用預訓練模型對圖像進行分類

這裏介紹的是直接用訓練好的分類任務的預訓練模型來測試。當然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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章