Caffe學習筆記2-用一個預訓練模型提取特征
本文為原創作品,未經本人同意,禁止轉載,禁止用於商業用途!本人對博客使用擁有最終解釋權
歡迎關注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing
http://caffe.berkeleyvision.org/gathered/examples/feature_extraction.html
制作一個數據庫
先做一個臨時文件夾
mkdir examples/_temp
為待處理的文件生成一個文件列表,這些圖片在examples/images文件夾下
find `pwd`/examples/images -type f -exec echo {} \; > examples/_temp/temp.txt
我們將使用預計標簽之后的每一個文件名,故需要在每一行后面加0
sed "s/$/ 0/" examples/_temp/temp.txt > examples/_temp/file_list.txt
定義特征提取的網絡結構
在實際運用中,把分類圖片減去均值圖像,對提高分類准確率具有很重要的意義,下載ILSVRC的均值文件
./data/ilsvrc12/get_ilsvrc_aux.sh
我們將在網絡定義模型中使用data/ilsvrc213/imagenet_mean.binaryproto的文件來定義一個網絡模型
拷貝和修改網絡訓練模型的定義文件,我們將要用的imagedatalayer,這個文件將調整圖片大小
cp examples/feature_extraction/imagenet_val.prototxt examples/_temp
提取特征
./build/tools/extract_features.bin models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel examples/_temp/imagenet_val.prototxt fc7 examples/_temp/features 10 leveldb
其中fc7是最高層的特征,我們也可以提取例如conv5或者pool3等其它層的信息
最后一個參數是圖片的最小批次
特征最后存在了examples/_temp/features,文件夾下面
如果出現"Check failed: status.ok() Failed to open leveldb examples/_temp/features"的錯誤,那是因為examples/_temp/features在上一層運行命令的時候就已經存在了,所以要用命令移除它
rm -rf examples/_temp/features/
最后把臨時的文件刪除不要
rm -r examples/_temp
問題:
-
caffemodel不存在
解決:
http://www.cnblogs.com/denny402/p/5111018.html
https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet
大概233M
sudo ./scripts/download_model_binary.py models/bvlc_reference_caffenet
問題2:
https://github.com/BVLC/caffe/issues/3505
參數設置錯誤,改版之后參數沒有繼續更改,巨坑,最后發現是caffemodel包沒有下載完全,一定要檢查那個包呀,233M,最后運行成功!