win10显卡:1660ti(6G)
cuda:11.1
pytorch版本:1.9.1
python版本:3.8
vs版本:2017
具体实现步骤:
1 首先利用conda新建Detectron2环境
conda create --name Detectron2 pytohn==3.8
2 激活环境,安装1.9.1版本的pytorch
pip install torch==1.9.1+cu111 torchvision==0.10.1+cu111 torchaudio==0.9.1 -f https://download.pytorch.org/whl/torch_stable.html
3 下载detectron2库
https://github.com/facebookresearch/detectron2
4 打开detectron2\detectron2\layers\csrc\nms_rotated路径下的nms_rotated_cuda.cu文件,添加红线这一行代码保存退出
5 编译detectron2
python -m pip install -e detectron2
6 下载AdelaiDet,进行编译即可
git clone https://github.com/aim-uofa/AdelaiDet.git
cd AdelaiDet
python setup.py build develop
7 分别使用blendmask和solov2的预训练权重进行测试,预训练权重和图片均放在AdelaiDe文件夹下,具体位置可自己定义。不同的算法选择其对应的模型和配置文件
blendmask预训练模型图片预测指令
python demo/demo.py --config-file configs/BlendMask/R_101_3x.yaml --input test1.jpg --opts MODEL.WEIGHTS R_101_3x.pth
solov2预测指令:
python demo/demo.py --config-file configs/SOLOv2/R101_3x.yaml --input test1.jpg --opts MODEL.WEIGHTS ./weights/SOLOv2_R101_3x.pth
能够出现检测结果说明配置成功。
8 如何训练自己的数据集
1 首先将数据转换成coco格式,放在dataset下,怎么转换此处不赘述,网上找相关资料即可
2 然后在detectron2下的builtin.py和builtin_meta.py修改注册自己的数据集,主要修改如下位置
1)首先在builtin.py文件下新增自己的数据集路径,参考_PREDEFINED_SPLITS_COCO新增即可
2 在builtin_meta.py下参考COCO_CATEOGIRE制造自己的数据集。注意此处的id从1开始,以及name 必须必须要和json_file相对应。
3 在builtin_meta.py中参考_get_coco_instances_meta函数,将COCO_CATEGORIES换成2中自己注册的数据集名称COCO_CATEGORIES_Stone,将assert len(thing_ids) == 80, len(thing_ids)修改成自己的类别个数即可
4 修改完成后,即可对AdelaiDet中的配置文件yaml进行修改 ,以SOLOv2为例,打开base-SOLOV2.yaml,修改数据集名称,即1)中新增的数据集,然后打开R101_3x.yaml ,修改训练步数。
5 训练
python tools/train_net.py --config-file configs/SOLOv2/R101_Stone_3x.yaml --num-gpus 1
6 预测
python demo/demo.py --config-file configs/SOLOv2/R101_Stone_3x.yaml --input 8.png --output ./test_result --opts MODEL.WEIGHTS ./output/model_final.pth