之前的教程我們說了如何使用caffe訓練自己的模型,下面我們來說一下如何fine tune。
所謂fine tune就是用別人訓練好的模型,加上我們自己的數據,來訓練新的模型。fine tune相當於使用別人的模型的前幾層,來提取淺層特征,然后在最后再落入我們自己的分類中。
fine tune的好處在於不用完全重新訓練模型,從而提高效率,因為一般新訓練模型准確率都會從很低的值開始慢慢上升,但是fine tune能夠讓我們在比較少的迭代次數之后得到一個比較好的效果。在數據量不是很大的情況下,fine tune會是一個比較好的選擇。但是如果你希望定義自己的網絡結構的話,就需要從頭開始了。
這里采用一個實際的例子,錢幣分類
1、我們收集了2W張圖片,將其中4000張作為測試集,剩下作為訓練集。
2、接着我們使用上一篇博客中的方法,生成words.txt、train.txt、test.txt三個文件,這里可以不用生成lmdb,因為caffe支持直接指定圖片文件。
3、編輯配置文件,這里我們參考finetune_flickr_style例子(它是用caffenet的訓練結果進行finetune的),拷貝其配置文件:
solver.prototxt
net: "examples/money_test/fine_tune/train_val.prototxt" test_iter: 20 test_interval: 50 base_lr: 0.001 lr_policy: "step" gamma: 0.1 stepsize: 2000 display: 1 max_iter: 10000 momentum: 0.9 weight_decay: 0.0005 snapshot: 1000 snapshot_prefix: "examples/money_test/fine_tune/finetune_money" solver_mode: CPU
train_val.prototxt
其實fine tune使用的網絡跟原有網絡基本一樣,只不過每層調整了一些參數,具體可以參照finetune_flickr_style和caffenet網絡配置的對比
name: "FlickrStyleCaffeNet" layer { name: "data" type: "ImageData" top: "data" top: "label" include { phase: TRAIN } transform_param { mirror: true crop_size: 227 mean_file: "data/ilsvrc12/imagenet_mean.binaryproto" } image_data_param { source: "examples/money_test/data/train.txt" batch_size: 50 new_height: 256 new_width: 256 } } layer { name: "data" type: "ImageData" top: "data" top: "label" include { phase: TEST } transform_param { mirror: false crop_size: 227 mean_file: "data/ilsvrc12/imagenet_mean.binaryproto" } image_data_param { source: "examples/money_test/data/test.txt" batch_size: 50 new_height: 256 new_width: 256 } } .......... layer { name: "fc8_flickr" type: "InnerProduct" bottom: "fc7" top: "fc8_flickr" # lr_mult is set to higher than for other layers, because this layer is starting from random while the others are already trained param { lr_mult: 10 decay_mult: 1 } param { lr_mult: 20 decay_mult: 0 } inner_product_param { num_output: 17 #這里我們的分類數目 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } .....
deploy.prototxt
用於實際分類時的網絡
......... layer { name: "fc8_flickr" type: "InnerProduct" bottom: "fc7" top: "fc8_flickr" # lr_mult is set to higher than for other layers, because this layer is starting from random while the others are already trained param { lr_mult: 10 decay_mult: 1 } param { lr_mult: 20 decay_mult: 0 } inner_product_param { num_output: 17 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } ...........
4、開始訓練
./build/tools/caffe train -solver examples/money_test/fine_tune/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel
其中model指定的是caffenet訓練好的model。
使用fine tune的效果比較好,經過3400多次迭代后,測試集上正確率達到92%,實際測試效果也比較理想。這也許就是深度學習的優勢,不需要仔細地挑選特征,只要數據足夠,也能得到不錯的效果。