caffe訓練加BN層的網絡時loss爲87.3365的問題解決辦法

Detailed Description about BatchNormLayer

“Normalizes the input to have 0-mean and/or unit (1) variance across the batch.

This layer computes Batch Normalization as described in [1]. For each channel in the data (i.e. axis 1), it subtracts the mean and divides by the variance, where both statistics are computed across both spatial dimensions and across the different examples in the batch.

By default, during training time, the network is computing global mean/variance statistics via a running average, which is then used at test time to allow deterministic outputs for each input. You can manually toggle whether the network is accumulating or using the statistics via the use_global_stats option. For reference, these statistics are kept in the layer’s three blobs: (0) mean, (1) variance, and (2) moving average factor.”

遇見的問題

用的普通卷積層堆疊的網絡,不加BN層時訓練已經有效果並且運行正常,當加上BN層後,出現loss一直爲87.3365不收斂的情況。

加入BN層後loss爲87.3365的解決辦法

BN層中有一個參數use_ global_stats,在訓練時我們需要將其設置爲false,這樣BN層才能更新計算均值和方差,如果設置爲true的話,就是初始固定的了,不會更新。在測試時,需要將其設置爲true。將網絡中該參數修改過來就訓練正常了。

其他可能導致不收斂的問題(如loss爲87.3365,loss居高不下等)解決方案

  1. 可以在solver裏面設置:debug_info: true,看看各個層的data和diff是什麼值,一般這個時候那些值不是NAN(無效數字)就是INF(無窮大);
  2. 檢查數據標籤是否從0開始並且連續;
  3. 把學習率base_lr調低;
  4. 數據問題,lmdb生成有誤;
  5. 中間層沒有歸一化,導致經過幾層後,輸出的值已經很小了,這個時候再計算梯度就比較尷尬了,可以嘗試在各個卷積層後加入BN層和SCALE層;
  6. 把base_lr調低,然後batchsize也調高;
  7. 把data層的輸入圖片進行歸一化,就是從0-255歸一化到0-1,使用的參數是:
 transform_param {  
    scale: 0.00390625//像素歸一化,1/255
  } 
  1. 網絡參數太多,網絡太深,刪掉幾層看看,可能因爲數據少,需要減少中間層的num_output;
  2. 記得要shuffle數據,否則數據不夠隨機,幾個batch之間的數據差異很小。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章