【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》

论文地址:https://arxiv.org/pdf/2103.05950.pdf
代码地址:https://github.com/megvii-research/FSCE
论文阅读:https://blog.csdn.net/qiankendeNMY/article/details/128390284

我的配置:
Python :3.8(ubuntu18.04)
Pytorch :1.6.0
Cuda :10.1
GPU:RTX 2080 Ti(11GB) * 2

1、依赖安装

requirements.txt文件下载:https://github.com/ucbdrive/few-shot-object-detection

pip install -r requirements.txt

2、 安装pycocoapi

pip install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'

3、安装fvcore库

pip install 'git+https://github.com/facebookresearch/fvcore'

报错:
【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第1张图片

解决参考:
https://blog.csdn.net/weixin_45355608/article/details/116528406

4、Build

python setup.py build develop

4.1、报错1

【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第2张图片

解决:
(1)修改 ./fedet/model_zoo下configs文件的内容,改成configs文件夹所在的路径

【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第3张图片

(2)执行以下命令,创建软链接

ln -sf 【configs文件夹所在的路径】 ./fsdet/model_zoo

例如:

ln -sf /root/autodl-tmp/configs ./fsdet/model_zoo

4.2、报错2

在这里插入图片描述
解决:
降低pytorch的版本,原来我的pytorch版本是1.6,降低到了1.4就可以正常运行了:
https://github.com/megvii-research/FSCE/issues/19

【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第4张图片

build成功

5、下载VOC数据集

cd datasets
wget https://pjreddie.com/media/files/VOCtrainval_11-May-2012.tar
wget https://pjreddie.com/media/files/VOCtrainval_06-Nov-2007.tar
wget https://pjreddie.com/media/files/VOCtest_06-Nov-2007.tar
tar xf VOCtrainval_11-May-2012.tar
tar xf VOCtrainval_06-Nov-2007.tar
tar xf VOCtest_06-Nov-2007.tar

6、训练基础检测器

python tools/train_net.py --num-gpus 2 --config-file configs/PASCAL_VOC/base-training/R101_FPN_base_training_split1.yml

6.1、报错1

【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第5张图片

解决:
手动下载pkl文件:
https://dl.fbaipublicfiles.com/detectron2/ImageNetPretrained/MSRA/R-101.pkl
然后,修改yml.py中pkl文件的路径,改为pkl文件在本机上的存储路径,例如: /path/to/ImageNetPretrained/MSRA/R-101.pkl.

【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第6张图片

6.2、报错2

Traceback (most recent call last):
  File "/root/miniconda3/lib/python3.8/site-packages/torch/multiprocessing/spawn.py", line 20, in _wrap
    fn(i, *args)
  File "/root/autodl-tmp/fsdet/engine/launch.py", line 84, in _distributed_worker
    main_func(*args)
  File "/root/autodl-tmp/tools/train_net.py", line 116, in main
    return trainer.train()
  File "/root/autodl-tmp/fsdet/engine/defaults.py", line 397, in train
    super().train(self.start_iter, self.max_iter)
  File "/root/autodl-tmp/fsdet/engine/train_loop.py", line 132, in train
    self.run_step()
  File "/root/autodl-tmp/fsdet/engine/train_loop.py", line 212, in run_step
    loss_dict = self.model(data)
  File "/root/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/root/miniconda3/lib/python3.8/site-packages/torch/nn/parallel/distributed.py", line 458, in forward
    output = self.module(*inputs[0], **kwargs[0])
  File "/root/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/root/autodl-tmp/fsdet/modeling/meta_arch/rcnn.py", line 166, in forward
    _, detector_losses = self.roi_heads(images, features, proposals, gt_instances)
  File "/root/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/root/autodl-tmp/fsdet/modeling/roi_heads/roi_heads.py", line 443, in forward
    losses = self._forward_box(features_list, proposals)  # get losses from fast_rcnn.py::FastRCNNOutputs
  File "/root/autodl-tmp/fsdet/modeling/roi_heads/roi_heads.py", line 464, in _forward_box
    box_features = self.box_pooler(features, [x.proposal_boxes for x in proposals])  # [None, 256, POOLER_RESOLU, POOLER_RESOLU]
  File "/root/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/root/autodl-tmp/fsdet/modeling/poolers.py", line 233, in forward
    output[inds] = pooler(x_level, pooler_fmt_boxes_level)
  File "/root/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/root/autodl-tmp/fsdet/layers/roi_align.py", line 93, in forward
    return roi_align(
  File "/root/autodl-tmp/fsdet/layers/roi_align.py", line 19, in forward
    output = _C.roi_align_forward(
RuntimeError: Not compiled with GPU support

这个问题死活解决不了,删了所有环境重新配置之后就没有再出现这个问题了。但是花了不少时间,希望有大佬看到的话能够提出解决方案。

基础训练完成:
【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第7张图片

7、随机初始化新类别的权重

python tools/ckpt_surgery.py --src1 checkpoints/voc/faster_rcnn/faster_rcnn_R_101_FPN_base1/model_final.pth --method randinit --save-dir checkpoints/voc/faster_rcnn/faster_rcnn_R_101_FPN_all1

8、数据集split

方法一:

python datasets/prepare_voc_few_shot.py --seeds 1 30

方法二:直接选择下载作者提供的网页数据集

wget -r -R index.html http://dl.yf.io/fs-det/datasets

将下载好的VOC数据集存放在datasets文件夹中的vocsplit文件夹下:

【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第8张图片 【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第9张图片

注:
【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第10张图片

9、微调

修改模型的权重路径为model_resnet_surgery.pth文件的保存路径:
【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第11张图片

python tools/train_net.py --num-gpus 2 \
        --config-file configs/PASCAL_VOC/split1/10shot_CL_IoU.yml

微调结束:
【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第12张图片

10、评估

python tools/test_net.py --num-gpus 2 \
        --config-file configs/PASCAL_VOC/split1/10shot_CL_IoU.yml \
        --eval-only

报错:
【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第13张图片

解决:
参考:https://github.com/megvii-research/FSCE/issues/9

【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第14张图片

评估结束:
【代码调试】《FSCE: Few-Shot Object Detection via Contrastive Proposal Encoding》_第15张图片

如有错误,欢迎指正!

你可能感兴趣的:(小样本目标检测,代码调试,目标检测,深度学习,小样本)