1. 程式人生 > >R-CNN:使用自己的資料訓練 Faster R-CNN 的 ResNet-50 模型

R-CNN:使用自己的資料訓練 Faster R-CNN 的 ResNet-50 模型

上次使用 Faster R-CNN 訓練了一個 VGG16 的網路,為了再提升識別的準確率,利用 ResNet 網路在同樣的資料上面訓練了多一次。

一、訓練網路

(一)下載 ResNet-50 的 prototxt 檔案

(二)相關檔案修改

1.cd $FRCN_ROOT/lib/rpn/generate_anchors.py

# 在37行:
def generate_anchors(base_size=16, ratios=[0.5, 1, 2],
                     scales=2**np.arange(3, 6)):
# 修改為:
def generate_anchors
(base_size=16, ratios=[0.5, 1, 2], scales=2**np.arange(1, 6)):

2.cd $FRCN_ROOT/lib/rpn/anchor_target_layer.py

# 在28行:
        anchor_scales = layer_params.get('scales', (8, 16, 32))
# 修改為:
        anchor_scales = layer_params.get('scales', (2, 4, 8, 16, 32))

3.cd $FRCN_ROOT/lib/rpn/proposal_layer.py

# 在29行:
        anchor_scales = layer_params.get('scales', (8, 16, 32))
# 修改為:
        anchor_scales = layer_params.get('scales', (2, 4, 8, 16, 32))

4.pascal_voc .py、imdb.py、train.prototxt、test.prototxt、.pt檔案 的修改參考 使用自己的資料訓練 Faster R-CNN 的 VGG-16 模型

5.因為我們使用了5個尺度的anchors,所以之前的9個anchors變成了3*5=15個。
修改prototxt和pt檔案

,將其中的18換成30。

layer {
  name: "rpn_cls_score"
  type: "Convolution"
  bottom: "rpn/output"
  top: "rpn_cls_score"
  param { lr_mult: 1.0 }
  param { lr_mult: 2.0 }
  convolution_param {
    num_output: 30   # 2(bg/fg) * 9(anchors)    ///將18換成30
    kernel_size: 1 pad: 0 stride: 1
    weight_filler { type: "gaussian" std: 0.01 }
    bias_filler { type: "constant" value: 0 }
  }

(三)下載 ImageNet 模型

下載 ImageNet 預訓練檔案:ResNet-50.v2.caffemodel

(四)清除快取

刪除快取檔案:
$FRCN_ROOT/data/VOCdevkit2007/annotations_cache/annots.pkl
$FRCN_ROOT/data/cache 下的 pkl 檔案
如果不清除快取可能會報錯。

(五)開始訓練

參照 VGG16 的訓練命令:
cd $FRCN_ROOT

./experiments/scripts/faster_rcnn_end2end.sh 0 ResNet-50 pascal_voc

注意:第三個引數 ‘ResNet-50’,一定要和你的資料夾名字對應,比如我的檔案放在$FRCN_ROOT/models/pascal_voc/ResNet-50 裡面,所以我的第三個引數就為我目錄的名稱。

由於 ResNet-50 的網路更深,訓練的時間也需要更久,每一次迭代大約需要 0.5s ,訓練這個網路我用了大概10個小時,但效果會比用 VGG16 的好,主要是對小尺度的物體檢測更加準確了。
這是我訓練時各類的 AP :

如果你想了解更多關於人工智慧的資訊,歡迎掃碼關注微信公眾號以及知乎專欄 「譯智社」,我們為大家提供優質的人工智慧文章、國外優質部落格和論文等資訊喲!