Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
120 lines (104 sloc) 5.3 KB

Detectron Transfer Learning with PASCAL VOC 2007 dataset

** Detectron implemented several object detecton algorithms. All the algorithms are trained on coco 2014 data set which has 80 categories. I want to fine tune the faster-rcnn with FPN on pascal voc 2007 dataset which has only 20 categories. The same way can be used to fine tune your own model on a new dataset **

1. Setup caffe2 and Detectron and run the Detectron demo successfully.

I will refer the Detectron directory as $DETECTRON

2. Download the pre-trained model

The code will download the models automatically. But my internet is slow and I'd like to download them before I run the code. Becase I'm gonna using the ResNet-50 as the backbone, so I need to download the ResNet and faster_rcnn_R-50-FPN model.

wget /tmp/detectron/detectron-download-cache/ImageNetPretrained/MSRA/R-50.pkl

3. Prepare configuration file.

a. Copy the sample configure file from $DETECTRON/configs/getting_started

mkdir experiments && cd experiments
cp ../configs/getting_started/tutorial_1gpu_e2e_faster_rcnn_R-50-FPN.yaml e2e_faster_rcnn_resnet-50-FPN_pascal2007.yaml

b. Change the configuration file

  TYPE: generalized_rcnn
  CONV_BODY: FPN.add_fpn_ResNet50_conv5_body

The pascal voc 2007 has only 20 classes plus one background class. So the NUM_CLASSES is set to 21.

  WEIGHTS: /tmp/detectron/detectron-download-cache/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
  DATASETS: ('voc_2007_train',)

Change the WEIGHTS value to where you just place in step 2.

4. Download pascal voc2007 and coco format annotations.

Refer data readme file to prepare the pascal data set.

The code support pascal data set has bug, the code in $DETECTRON/lib/datasets/ should be changed as following:

    'voc_2007_train': {
            _DATA_DIR + '/VOC2007/JPEGImages',
            _DATA_DIR + '/VOC2007/annotations/pascal_train2007.json',
            _DATA_DIR + '/VOC2007/VOCdevkit2007'
    'voc_2007_test': {
            _DATA_DIR + '/VOC2007/JPEGImages',
            _DATA_DIR + '/VOC2007/annotations/pascal_test2007.json',
            _DATA_DIR + '/VOC2007/VOCdevkit2007'

5. Change the cls_score and bbox_pred name to prevent error when load weights

In lib/modeling/, change all cls_score to cls_score_voc, bbox_pred to bbox_pred_voc.

6. Run command to begin training.

python2 tools/ --cfg experiments/e2e_faster_rcnn_resnet-50-FPN_pascal2007.yaml  OUTPUT_DIR experiments/output

7. Copy the final model just trained.

mkdir -p /tmp/detectron-download-cache/voc2007/
cp experiments/output/train/voc_2007_train/generalized_rcnn/model_iter49999.pkl /tmp/detectron-download-cache/voc2007/model_final.pkl

8. Infer some images psacal 2007 from test dataset

python2 tools/ --cfg experiments/e2e_faster_rcnn_resnet-50-FPN_pascal2007.yaml \
    --output-dir /tmp/detectron-visualizations --wts /tmp/detectron-download-cache/voc2007/model_final.pkl \

Unfortunately, I found all persons are labeld as bird. This may be caused by the json datasets which are not corrrectly converted.

9. Run on pascal 2007 test dataset.

python2 tools/ \
    --cfg experiments/e2e_faster_rcnn_resnet-50-FPN_pascal2007.yaml \
    TEST.WEIGHTS /tmp/detectron-download-cache/voc2007/model_final.pkl \
    NUM_GPUS 1

The test report the AP and mAP:

INFO 127: AP for aeroplane = 0.8095
INFO 127: AP for bicycle = 0.8042
INFO 127: AP for bird = 0.7086
INFO 127: AP for boat = 0.6418
INFO 127: AP for bottle = 0.6861
INFO 127: AP for bus = 0.8822
INFO 127: AP for car = 0.8794
INFO 127: AP for cat = 0.8621
INFO 127: AP for chair = 0.5876
INFO 127: AP for cow = 0.7799
INFO 127: AP for diningtable = 0.7404
INFO 127: AP for dog = 0.8497
INFO 127: AP for horse = 0.8855
INFO 127: AP for motorbike = 0.7912
INFO 127: AP for person = 0.7931
INFO 127: AP for pottedplant = 0.5142
INFO 127: AP for sheep = 0.7950
INFO 127: AP for sofa = 0.7457
INFO 127: AP for train = 0.7956
INFO 127: AP for tvmonitor = 0.6960
INFO 130: Mean AP = 0.7624

You can’t perform that action at this time.