caffe学习笔记18-image1000test200数据集分类与检索完整过程
1.準備數(shù)據(jù):數(shù)據(jù)集圖片分10個類,每個類有100個train圖片(train文件夾下,一共1000),20個test圖片(val文件夾下,一共200)
? ? 首先準備自己的數(shù)據(jù)集(都是全路徑),我們需要準備的文件有:
? ? ? 1) 文件夾train:里面放訓練的圖片
? ? ? 2 )文件夾val:里面放val的圖片
? ? ? 3 )train.txt :訓練圖片的文件名和對應的類別, 格式每一行文件名+ 空格 + label
? ? ? 4) val.txt:測試圖片的文件名和對應的類別
2.創(chuàng)建lmdb格式數(shù)據(jù)集
? ?在caffe安裝的根目錄下執(zhí)行下面的腳本可以創(chuàng)建lmdb文件,用于caffe的數(shù)據(jù)輸入;
? ?./examples/image_test/create_imagenet.sh
? ?對于這個腳本,我們打開,根據(jù)自己的路徑做更改;
? ?參考/home/nielsen/caffe-master/examples/image_test/create_imagenet.sh
3. 計算圖像均值
? ?輸入數(shù)據(jù)需要訓練圖片的均值圖像(需要自己修改路徑 路徑最前面千萬不要多加空格,血的教訓)只計算訓練集的均值,測試的時候減去的也是訓練集的均值而不是測試集的
? ?./examples/imagenet/make_imagenet_mean.sh
4. 配置網(wǎng)絡結(jié)構(gòu)文件(檢查train和test不能同時用train_lmdb,會造成死鎖卡在那)
? ?網(wǎng)絡模型文件在models/bvlc_reference_caffenet/train_val.prototxt文件中,復制到image_test文件夾中(統(tǒng)一管理比較方便)
? ?這里我們直接使用caffe提供的這個網(wǎng)絡結(jié)構(gòu),以后我們再根據(jù)自己的想法做更改。
? ?-----提供的數(shù)據(jù)為10類,則最后fc8層的num_output相應的改為10,即fc8為10個節(jié)點
? ?注意:需要打開這個文件,更改里面的路徑
? ?我們對train_val.prototxt 修改后,路徑:/home/nielsen/caffe-master/examples/image_test/train_val.prototxt
5. 配置訓練參數(shù)文件(net路徑也需要修改)
? ?訓練參數(shù)文件在:models/bvlc_reference_caffenet/solver.prototxt中,復制到image_test文件夾中(統(tǒng)一管理比較方便)
? ?我們對solver.prototxt 修改后的參數(shù)如下:
? ?net: "examples/image_test/train_val.prototxt"
? ?test_iter: 4 ? ? ? ? ? # 測試的時候輸入4個batch,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 說明:查看我們train_val.prototxt文件,發(fā)現(xiàn)batch_size: 50;我們的test圖片是200張,200/50=4,所以
? ?test_interval: 1000 ? ?# 每迭代1000次,測試一次
? ?base_lr: 0.01 ? ? ? ? ? ? # 初始的學習率為0.01
? ?lr_policy: "step" ? ? ?
? ?gamma: 0.1
? ?stepsize: 3000 ? ? ? ? # 每迭代3000次,調(diào)整一次學習率
? ?display: 20 ? ? ? ? ? ?# 每迭代20次,顯示一次
? ?max_iter: 12000 ? ? ? ?# 最大迭代12000次
? ?momentum: 0.9 ? ? ? ??
? ?weight_decay: 0.0005
? ?snapshot: 5000 ? ? ? ? # 每迭代5000就存一次快照
? ?snapshot_prefix: "examples/image_test/caffenet_train" ?# 快照存放路徑
? ?solver_mode: CPU ? ? ? # 選擇模式CPU
6. 開始利用自己的數(shù)據(jù)訓練網(wǎng)絡(在caffe的根目錄下執(zhí)行)
? ?./build/tools/caffe train -solver=/home/nielsen/caffe-master/examples/image_test/solver.prototxt
? ?訓練過程中,可以看到正確率在不斷提升;最后的準確率是50%;
? ?我們的樣本數(shù)很少,當圖片到百萬數(shù)量級的時候,效果會很大提升;
7. 改變策略,使用blvc model微調(diào)
? ? fc8層的輸出節(jié)點改為10個,數(shù)據(jù)只有10類,用已經(jīng)訓練好的1000類進行微調(diào)(變動的層fc8需要改名字,不然還是原來的參數(shù))
? (學習率:最后一層fc的lr_mult改為10,20 且 降低base_lr為0.001)? ?caffenet結(jié)構(gòu)的輸出層的類別數(shù)一定要大于我訓練集的類別數(shù)才可以收斂!
? ?比如原來我把圖像類別的label設置成1,2,3,改成0,1,2后,最后全連接層的轉(zhuǎn)出改成3就OK了。
? ?./build/tools/caffe train -solver examples/image_test/solver10.prototxt -weights examples/image_test/caffenet_train_iter_5000.caffemodel
? ?微調(diào)參考:
1)we will change the name of the last layer form fc8 to fc8_flickr in our prototxt, it will begin training with random weights.
2)decrease base_lr(初始學習率,在train_val.protxt文件值中) and boost the lr_mult(局部學習倍率,conv和fc層才有) on the newly introduced layer(fc8_flickr).
3)set stepsize(solver.prototxt,迭代多少次就下降一次學習率) to a lower value. So the learning rate to go down faster
4)So in the solver.prototxt,we can find the base_lr is 0.001 from 0.01,and the stepsize is become to 20000 from 100000.(重要)
? ? ? ? ??base_lr: 0.001~0.01 降低,基礎學習率降低,不然太快的話loss會變成non
? ? ? ? ??lr_mult: w:10,b:20 增大,最后一個fc層(分類層)的局部學習率?
? ? ? ? ??stepsize: 20000~100000 降低,迭代多少次就調(diào)整一次學習率
? ? ? ? ??max_iter: 降低,沒必要迭代那么多次了
? ? 那么在網(wǎng)絡的微調(diào)中,我們的整個流程分為以下幾步:? ? 1)依然是準備好我們的訓練數(shù)據(jù)和測試數(shù)據(jù)
? ? 2)計算數(shù)據(jù)集的均值文件,因為集中特定領域的圖像均值文件會跟ImageNet上比較General的數(shù)據(jù)的均值不太一樣(重要,使用自己數(shù)據(jù)集的均值文件)
? ? 3)修改網(wǎng)絡最后一層的輸出類別,并且需要加快最后一層的參數(shù)學習速率
? ? 4)調(diào)整Solver的配置參數(shù),通常學習速率和步長,迭代次數(shù)都要適當減少
? ? 5)啟動訓練,并且需要加載pretrained模型的參數(shù)(caffemodel)
? ? 總結(jié):
? ? ? ? ?1)需要變動source,meanfile,修改層的名字,基礎學習率,(fc8)分類層局部學習率,步長,最大迭代次數(shù),test_iter(測試圖像個數(shù)/batch_size)
? ? ?? ?2)如果數(shù)據(jù)集比較小,可以增大weight_decay,減少過擬合? ? ? ?3)test_interval也可以相應縮小,比較好觀察accuracy
? ? ?4)display也可以相應縮小,比較好觀察loss變化,畢竟總的迭代次數(shù)減少了嘛
8.caffe寫日志
? ?在train的時候最后加 tee $folder_prefix/caffe.log,就可以重定向到文件夾了。
? ?1)你可以從系統(tǒng) /tmp 文件夾獲取,名字是什么 caffe.ubuntu.username.log.INFO.....之類。(參考output.m)
? ?2)在train的時候最后加 tee $folder_prefix/caffe.log,就可以重定向到文件夾了。 ??
9.test測試模型
? ? # 用 validation set 得到已訓練的 image_test 模型的分數(shù)
? ? ./build/tools/caffe test -model examples/image_test/train_val.prototxt -weights
? ? examples/image_test/caffenet_train/caffenet_train_iter_10000.caffemodel
10.用blvc model微調(diào)10類的自建數(shù)據(jù)庫模型(比自己訓練精度高很多,訓練集的原因,太少了)
? ? ?fine-turning:(采用solver.prototxt + caffemodel)
./build/tools/caffe train -solver examples/image_test/solver10.prototxt -weights examples/image_test/bvlc_reference_caffenet.caffemodel?
? ? ? ?測試結(jié)果,精度很好,自己訓練集訓練12000次,accuracy是0.55,用imagenet模型進行微調(diào)2400次,accuracy達到了0.895(5000次以內(nèi)是最高的)
? ? ?test:(采用train_val.prototxt + caffemodel)
./build/tools/caffe test -model examples/image_test/train_val10.prototxt -weights examples/image_test/caffenet_train/caffenet_train_iter_5000.caffemodel
? ? ? ?[已解決]遇到的問題:訓練accuracy 0.89,測試accurary為0?(model不對,train_val.prototxt的最后一層的(fc8)節(jié)點數(shù)要等于樣本數(shù))
? ? ? ?test的accuracy分數(shù)top1:0.8856(平均batch_size分數(shù)),平均loss分數(shù)為0.419755
? ? ? ?test的accuracy分數(shù)top5:0.9952(平均batch_size分數(shù)),平均loss分數(shù)為0.419755?
? ? ? ? ?accuracy_param{
? ? ? ? ? ? top_k: 5 ? # 默認為1
? ? ? ? ?}
? ? ? ?如果loss一直居高不下(一般大于3),都認為沒有收斂,預測精度都會很低。這個時候就要考慮你的數(shù)據(jù)質(zhì)量問題了,或者調(diào)整初始學習率。
11.單個圖像測試image_test(自己的圖像數(shù)據(jù)數(shù)據(jù)庫),5個輸入?yún)?shù)(deploy.prototxt + caffemodel + mean.binaryproto + synset_words.txt + watch.jpg)
? ? ./build/examples/cpp_classification/classification.bin examples/image_test/deploy10.prototxt examples/image_test/caffenet_train/caffenet_train_iter_5000.caffemodel ? ? ? data/image1000test200/image_test_mean.binaryproto data/image1000test200/synset_words.txt examples/image_test/test_images/150013018467.jpg
? ? ?---------- Prediction for examples/image_test/test_images/150013018467.jpg -----------
? ? ?1.0000 - "0 watch,鐘表"
? ? ?0.0000 - "1 headwear,頭飾"
? ? ?0.0000 - "7 perfume,香水"
? ? ?0.0000 - "8 cosmetic,化妝品"
? ? ?0.0000 - "9 shoe,鞋子"
? ? ?----自己網(wǎng)上找了10類的圖片,測試網(wǎng)絡分類性能-----test0.jpg~test9.jpg
? ? ./build/examples/cpp_classification/classification.bin examples/image_test/deploy10.prototxt examples/image_test/caffenet_train/caffenet_train_iter_5000.caffemodel ? ? ? data/image1000test200/image_test_mean.binaryproto data/image1000test200/synset_words.txt examples/image_test/test_images/test1.jpg
? ? 測試結(jié)果非常好!基本都達到了要求,除了頭飾
test0.jpg ? ? 1.0000 - "0 watch,鐘表" ? ? ? ? ? ? ? ? ? ? 正確
test1.jpg ? ? 0.9982 - "1 headwear,頭飾" ? ? ? ? ? ? 正確
test2.jpg ? ? 1.0000 - "2 sweatpants,運動褲" ? ? ?正確
test3.jpg ? ? 0.9924 - "3 trunk,行李箱" ? ? ? ? ? ? ? ? ?正確
test4.jpg ? ? 1.0000 - "4 chair,椅子" ? ? ? ? ? ? ? ? ? ? ?正確
test5.jpg ? ? 0.9998 - "5 TVwall,電視墻" ? ? ? ? ? ? ? 正確
test6.jpg ? ? 1.0000 - "6 yogurt,酸奶" ? ? ? ? ? ? ? ? ? ?正確
test7.jpg ? ? 0.6424 - "7 perfume,香水" ? ? ? ? ? ? ? ?正確
test8.jpg ? ? 0.9983 - "8 cosmetic,化妝品" ? ? ? ? ?正確
test9.jpg ? ? 1.0000 - "9 shoe,鞋子" ? ? ? ? ? ? ? ? ? ? 正確
結(jié)論:基本都達到了百分之99以上,除了香水的分類低一點,主要是因為香水也屬于化妝品,所以分數(shù)被化妝品占去了一些,實際 (香水+化妝品)的分數(shù)為0.97,這樣看來效果也很好,分類正確率也符合200個測試樣本的accuracy的0.8856分數(shù)。
自帶classification.cpp文件,就是用來分類的。它帶有五個參數(shù),你把這五個參數(shù)準備好,直接調(diào)用就可以了
總結(jié): ?
第一個參數(shù):depoly配置文件
第二個參數(shù):caffemodel文件
第三個參數(shù):均值文件
第四個參數(shù):類別名稱列表文件(需要自己制作synset_words.txt,最后一行不要多加回車(多出一行,返回標簽數(shù)大于輸出層節(jié)點數(shù)的錯誤),血的教訓)
第五個參數(shù):要分類的圖片
其中synset_words.txt里面存放的是類別名稱列表。比如你有三類:dog,cat,pig.你在實際運行的時候是用數(shù)字代替的(如0,1,2),但是預測分類的時候,要把數(shù)字換回名稱,這個txt文件就是做這個用的。有多少類別就有多少行,每行一個名稱:如
labels.txt:
dogcat
pig
12.數(shù)據(jù)集提取(默認是val數(shù)據(jù)集的特征)每層特征,并顯示出來(測試可行)(caffemodel + train_val.prototxt + 需要提取的層名(fc7) + batchsize + 數(shù)據(jù)格式)
? ---使用image_test的數(shù)據(jù)集提取特征---(用第二種方法,比較方便)
? ? ?第一種方法:caffe的cmd接口只能提取特征,特征的可視化需要用python接口,自己根據(jù)extract_feature_example.sh修改第二個部分
./build/tools/extract_features.bin examples/image_test/caffenet_train/caffenet_train_iter_5000.caffemodel examples/image_test/train_val10.prototxt fc7? examples/image_test/temp_features 10 lmdb
? ? ?
? ? ?第二種方法:
? ? ? ? ? ? 1.caffe-master下執(zhí)行:sh extract_feature_example.sh(已寫好,更改路徑和需要提取的層,以及提取特征的圖片個數(shù))默認提取的train里面的?
? ? ? ? ? ? 2.轉(zhuǎn)到lmdb和生成的特征.mat的目錄下,將display_feature.m和display_network.m(都已寫好)拷貝到此目錄下
? ? ? ? ? ? 3.打開matlab,運行display_feature.m,可修改參數(shù),顯示圖片的個數(shù)。
13.若想提取自己定義文件夾的特征,可更改val集(lmdb)路徑(需要先將指定文件夾內(nèi)圖片轉(zhuǎn)為lmdb)
?? ?只要不使用test,則不影響,提取完后(生成了lmdb和.mat),再改回去原來的驗證集。
? ? 1)自己寫個create_my_image_database_lmdb,生成自己文件夾的lmdb
? ? 2)修改train_val.prototxt的val路徑為剛才生成的自己的文件夾,再進行特征提取。
? ? 3)改回原來的val路徑
14.提取單個圖像的特征(這里使用自己的集合,單個就是.mat的某行):
1)制作txt:圖像名 類別號(后面用不到,可隨意寫) 使用create_my_image_database_lmdb.sh
2)使用數(shù)據(jù)庫原來的mean,不是自己圖片的mean。
3)提取自己數(shù)據(jù)集的特征 使用extract_feature_example.sh 修改下.prototxt里面的TEST的source
15.再在哈希桶temp中進行與需要測試的圖像集進行歐式距離的比對,按距離從小到大排序,重排,顯示
? ? topk=1:
? ? 僅用hash:map 0.8900 hamming
? ? 僅用fc7:map 0.0.8950 euclidean ?
? ? 僅用perceptual_fc8_sigmoid:0.8850
? ? topk=5:
? ? 僅用hash:map 0.8900 hamming
? ? 僅用fc7:map 0.9106 euclidean ?
? ? 僅用perceptual_fc8_sigmoid:0.8879
? ? topk=10:
? ? 僅用hash:map 0.8892 hamming
? ? 僅用fc7:map 0.8955 euclidean
? ? 僅用perceptual_fc8_sigmoid:0.8879
? ? topk=20:?
? ? 僅用hash:map 0.8891 hamming
? ? 僅用fc7:map 0.8842 euclidean
? ? 僅用perceptual_fc8_sigmoid:0.8877
? ? topk=50:
? ? 僅用hash:map 0.8893 hamming
? ? 僅用fc7:map 0.8556 euclidean
? ? 僅用perceptual_fc8_sigmoid:0.8893
? ? topk=100(同類全,再大就沒有意義了)
? ? 僅用hash:map 0.8896 hamming
? ? 僅用fc7:map 0.8300 euclidean
? ? 僅用perceptual_fc8_sigmoid:0.8842
總結(jié):1.從MAP角度分析,實質(zhì)判斷的平均topk分類的準確度,也MAP越高也不一定代表最優(yōu),還得看用戶的反饋。
? ? ? ? 當返回的top數(shù)據(jù)越多時,hash48特征性能優(yōu)于fc7
? ? ? ? 當返回的top數(shù)據(jù)比較少時,fc7特征優(yōu)于hash48
? ? ?? ? ? 2.從運行性能上分析
? ? ? ?? hash48維hamming計算速度遠快于fc的4096維euclidean計算(數(shù)據(jù)量越大越明顯)
? ? ?? ? ??3.從存儲和內(nèi)存性能上分析
? ? ? ?? hash48的二進制存儲以及內(nèi)存的占用遠小于fc的float型存儲(數(shù)據(jù)量越大越明顯)
? ? ?? ? ? 4.從特征描述的抽象能力(高層語義)分析
? ? ? ?? hash48的抽象能力(高層語義)比fc的特征的語義更豐富,更抽象
-----接下來的任務是兩步查詢,哈希分桶,實際計算map沒有進行兩步操作,僅用了哈希,但是單張時,在外部可以檢索,縮小范圍。先用hash特征.mat,再用fc7特征.mat
-----查詢圖片的顯示(先完成單張圖像的特征提取(還是得老方法),再單張檢索,參照計算MAP)
16.使用bvlc_reference_caffenet.caffemodel微調(diào)hash48 (log.info)
? ? ./build/tools/caffe train -solver examples/image_test/solver10_hash48.prototxt -weights examples/image_test/bvlc_reference_caffenet.caffemodel
? ? Accuracy:0.88
? ? Test測試微調(diào)好的模型
? ? ./build/tools/caffe test -model examples/image_test/train_val10_hash48.prototxt -weights examples/image_test/caffenet_train/caffenet_fineturning_hash48_iter_2000.caffemodel
? ? test的accuracy分數(shù)top1:0.8804(平均batch_size分數(shù)),平均loss分數(shù)為0.390554
? ? test的accuracy分數(shù)top5:0.9856(平均batch_size分數(shù)),平均loss分數(shù)為0.390554?
? ? test的accuracy分數(shù)top10:1(平均batch_size分數(shù)),平均loss分數(shù)為0.390554
17.使用bvlc_reference_caffenet.caffemodel微調(diào)hash128 (log.info)
? ? ./build/tools/caffe train -solver examples/image_test/solver10_hash128.prototxt -weights examples/image_test/bvlc_reference_caffenet.caffemodel
? ? Accuracy:0.88
? ? Test測試微調(diào)好的模型
? ? ./build/tools/caffe test -model examples/image_test/train_val10_hash128.prototxt -weights ? ? ? ? examples/image_test/caffenet_train/caffenet_fineturning_hash128_iter_1220.caffemodel
? ? test的accuracy分數(shù)top1:0.876(平均batch_size分數(shù)),平均loss分數(shù)為0.379799
? ? test的accuracy分數(shù)top5:1(平均batch_size分數(shù)),平均loss分數(shù)為0.379799
? ? test的accuracy分數(shù)top10:1(平均batch_size分數(shù)),平均loss分數(shù)為0.379799
18.單個檢索流程:caffe-master下:
? ? ? ?1.只計算hash特征:run_one_precision.m (調(diào)用one_precision.m)
? ? ? ?2.顯示hash之后的檢索圖片看一下效果:display_retrieval_images_hash.m?
? ? ? ?3.再計算fc7特征:compute_fc7.m (調(diào)用one_precision.m)
? ? ? ?4.再顯示經(jīng)過hash之后又采用fc7的檢索圖片:display_retrieval_images_hash_fc7.m
19.已完成 檢索結(jié)果可視化ing , 將文件夾下所有圖片變成.mat格式(才能完成上一步)
? ? 第二步,提取第一步的檢索結(jié)果,進行fc7計算,并重排
? ? 已完成,結(jié)果在analysis文件夾
20.整體檢索
準備工作,單個圖像(用文件夾裝)與標簽
兩步檢索流程:
1.該文件夾下:create_my_image_database_lmdb.sh
2.該文件夾下:extract_my_feature.sh (要修改prototxt,圖像集路徑)
3.該文件夾下:display_feature.m 生成哈希的特征 啟動matlab (features_fc8_hash48_sigmoid.mat) fc7特征也打包一下 one_image開頭的.mat
4.caffe-master下:run_precision.m 設置路徑
5.生成的距離,排名,標簽都在caffe-master下
6.查看哈希檢索結(jié)果,display_retrieval_images_hash.m 保存圖片到analysis
7.繼續(xù)計算fc7特征,compute_fc7.m
8.查看fc7檢索結(jié)果,display_retrieval_images_hash_fc7.m 保存圖片到analysis
直接fc7檢索流程:
1.前三步相同
2.caffe-master下:compute_fc7_only.m?
3.查看只用fc7檢索結(jié)果,display_retrieval_images_hash_fc7_only.m 保存圖片到analysis
21.caffe繪制訓練過程的loss和accuracy曲線:
在caffe的訓練過程中,大家難免想圖形化自己的訓練數(shù)據(jù),以便更好的展示結(jié)果。如果自己寫代碼記錄訓練過程的數(shù)據(jù),那就太麻煩了,caffe中其實已經(jīng)自帶了這樣的小工具 :caffe-master/tools/extra/parse_log.sh,caffe-master/tools/extra/extract_seconds.py,caffe-master/tools/extra/plot_training_log.py.example?
使用方法如下:1.記錄訓練日志
在訓練過程中的命令中加入一行參數(shù) ,實現(xiàn)Log日志的記錄,這樣訓練結(jié)束之后,會在Log文件夾中生成每次訓練的Log日志?
TOOLS=./build/tools ?
GLOG_logtostderr=0 GLOG_log_dir=/home/nielsen/caffe-master/examples/cifar10/log/ \ ?
$TOOLS/caffe train -solver=/home/nielsen/caffe-master/examples/cifar10/solver10_hash48.prototxt
2.解析訓練日志
將最上面說的3個腳本文件拷貝到log文件夾下,執(zhí)行:
./parse_log.sh caffe.nielsen-ubuntu.nielsen.log?
后面的參數(shù)為log文件名,這樣就會在當前文件夾下生成一個.train文件和一個.test文件?
3.生成圖片
./plot_training_log.py.example 0 save.png caffe.nielsen-ubuntu.nielsen.log?
就可以生成訓練過程中的Test accuracy ?vs. Iters 曲線,其中0代表曲線類型,save.png代表保存的圖片名稱
caffe中支持很多種曲線繪制,通過指定不同的類型參數(shù)即可,具體參數(shù)如下:
Notes: ?
? ?? 1. Supporting multiple logs. ?
? ?? 2. Log file name must end with the lower-cased ".log". ?
Supported chart types: ?
? ?? 0: Test accuracy ?vs. Iters ?
? ?? 1: Test accuracy ?vs. Seconds ?
? ?? 2: Test loss ?vs. Iters ?
? ?? 3: Test loss ?vs. Seconds ?
? ?? 4: Train learning rate ?vs. Iters ?
? ?? 5: Train learning rate ?vs. Seconds ?
? ?? 6: Train loss ?vs. Iters ?
? ? ?7: Train loss ?vs. Seconds?
總結(jié)
以上是生活随笔為你收集整理的caffe学习笔记18-image1000test200数据集分类与检索完整过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: caffe学习笔记17-find-tun
- 下一篇: caffe学习笔记25-过拟合原因及分析