1. 程式人生 > >caffe使用預訓練的模型進行finetune--caffe學習(1)

caffe使用預訓練的模型進行finetune--caffe學習(1)

首先明確預訓練好的模型和自己的網路結構是有差異的,預訓練模型的引數如何跟自己的網路匹配的呢:

–If we provide the weights argument to the caffe train command, the pretrained weights will be loaded into our model, matching layers by name.

意思就是預訓練的模型根據你當前網路的layer 名進行匹配引數,加入預訓練原始網路的第一個卷積層name是conv1,而你自己的第一個卷積層網路name是Convolution1,那麼這個層在預網路中的引數就不會被匹配呼叫,這就沒有實現我們finetune的目的!

因為沒有匹配上的layer會這樣處理:Since there is no layer named that in the bvlc_reference_caffenet, that layer will begin training with random weights.也就是隨機初始化

原來網路結構中的全連線層fc8, 需要改一下名字,如我的改成”re-fc8”. 因為我們做的是微調。微調的意思就是先在別的資料集上進行訓練,把訓練好的權值,作為我們現在資料集的權值初始化,就不再需要隨機初始化了。現在的資料和訓練時的資料不一致,因此有些層數的設定就會有點區別。比如這個例子中,用來訓練模型的資料集是imagenet,分為1000類,而我們的資料集就只有5類,因此在fc8這層上的num_output就會有區別,因此在這一層上就不能用人家的權值了,就需要把這層的名字改得和原來的網路結構不一樣。

因此我們在finetune的時候一般同時使用模型和模型對應的訓練網路結構,保證所有引數被正確載入和呼叫

常見的fintune基礎思路:We will also decrease the overall learning rate base_lr in the solver prototxt, but boost the lr_multon the newly introduced layer. The idea is to have the rest of the model change very slowly with new data, but let the new layer learn fast. Additionally, we set stepsize in the solver to a lower value than if we were training from scratch, since we’re virtually far along in training and therefore want the learning rate to go down faster. Note that we could also entirely prevent fine-tuning of all layers other than fc8_flickr by setting their lr_mult to 0.