caffe2 基于faster rcnn做transfer learning

参考链接:https://medium.com/@royhuang_87663/detectron-transfer-learning-with-pascal-voc-2007-dataset-73bacf43dfbd

前言:

原文是fine turn pascal voc 2007,但是截至2018年9月18日为止,detectron并不支持voc格式的验证AP值的计算,默认输出结果为-1

因此本文进行了修改,改为fine turn coco2014

一.将模型下载到本地

resnet50

wget https://s3-us-west-2.amazonaws.com/detectron/ImageNetPretrained/MSRA/R-50.pkl /tmp/detectron/detectron-download-cache/ImageNetPretrained/MSRA/R-50.pkl

faster-rcnn-R50-FPN

wget https://s3-us-west-2.amazonaws.com/detectron/35857389/12_2017_baselines/e2e_faster_rcnn_R-50-FPN_2x.yaml.01_37_22.KSeq0b5q/output/train/coco_2014_train%3Acoco_2014_valminusminival/generalized_rcnn/model_final.pkl 

二.准备配置文件

cd $DETECTORN
mkdir experiments && cd experiments
cp ../configs/getting_started/tutorial_1gpu_e2e_faster_rcnn_R-50-FPN.yaml e2e_faster_rcnn_resnet-50-FPN_coco2014.yaml

 修改配置文件e2e_faster_rcnn_resnet-50-FPN_coco2014.yaml

 

MODEL:
  TYPE: generalized_rcnn
  CONV_BODY: FPN.add_fpn_ResNet50_conv5_body
#coco2014一共80分类+背景
  NUM_CLASSES: 81
  FASTER_RCNN: True
NUM_GPUS: 1
SOLVER:
  WEIGHT_DECAY: 0.0001
  LR_POLICY: steps_with_decay
  BASE_LR: 0.0025
  GAMMA: 0.1
  MAX_ITER: 6000
  STEPS: [0, 30000, 40000]
  # Equivalent schedules with...
  # 1 GPU:
  #   BASE_LR: 0.0025
  #   MAX_ITER: 60000
  #   STEPS: [0, 30000, 40000]
  # 2 GPUs:
  #   BASE_LR: 0.005
  #   MAX_ITER: 30000
  #   STEPS: [0, 15000, 20000]
  # 4 GPUs:
  #   BASE_LR: 0.01
  #   MAX_ITER: 15000
  #   STEPS: [0, 7500, 10000]
  # 8 GPUs:
  #   BASE_LR: 0.02
  #   MAX_ITER: 7500
  #   STEPS: [0, 3750, 5000]
FPN:
  FPN_ON: True
  MULTILEVEL_ROIS: True
  MULTILEVEL_RPN: True
FAST_RCNN:
  ROI_BOX_HEAD: fast_rcnn_heads.add_roi_2mlp_head
  ROI_XFORM_METHOD: RoIAlign
  ROI_XFORM_RESOLUTION: 7
  ROI_XFORM_SAMPLING_RATIO: 2
TRAIN:
#下载faster-rcnn的预训练模型进行fine-turn
  WEIGHTS: /home/test/caffe2_model/faster_rcnn_r50/model_final.pkl
#这个datasets是dataset_category.py字典的key
  DATASETS: ('coco_2014_train',)
  SCALES: (500,)
  MAX_SIZE: 833
  BATCH_SIZE_PER_IM: 256
  RPN_PRE_NMS_TOP_N: 2000  # Per FPN level
TEST:
#这个datasets是dataset_category.py字典的key
  DATASETS: ('coco_2014_minival',)
  SCALE: 500
  MAX_SIZE: 833
  NMS: 0.5
  RPN_PRE_NMS_TOP_N: 1000  # Per FPN level
  RPN_POST_NMS_TOP_N: 1000
OUTPUT_DIR: .

 

 三.改配置参数,防止在训练的时候报错

1)因为是fine-turn,因此需要改一下网络层的名字,将detectron/modeling/fast_rcnn_heads.py当中的

cls_score => cls_score_voc,bbox_pred => bbox_pred_voc

2)将detectron/dectron/core/test.py文件173行改为:否则会报错,找不到bbox_pred_voc层

#bbox_pred =>bbox_pred_voc

box_deltas = workspace.FetchBlob(core.ScopedName('bbox_pred_voc')).squeeze()

 

四.开始训练

python2 tools/train_net.py --cfg experiments/e2e_faster_rcnn_r50_RPN_coco2014.yaml OUTPUT_DIR experiments/coco_fineturn_output/

 

训练结果:

caffe2 基于faster rcnn做transfer learning_第1张图片

 

五.测试demo

python2 tools/infer_simple.py --cfg experiments/e2e_faster_rcnn_r50_RPN_coco2014.yaml --output-dir /tmp/detectron-visualizations --wts /home/test/detectron/experiments/coco_fineturn_output/train/coco_2014_train/generalized_rcnn/model_final.pkl demo/

 

 

caffe2 基于faster rcnn做transfer learning_第2张图片

 

你可能感兴趣的:(caffe2)