ubuntu18 Swin-Transformer-Object-Detection

1、目标检测:https://github.com/SwinTransformer/Swin-Transformer-Object-Detection
原文地址:https://arxiv.org/abs/2103.14030
代码地址:https://github.com/microsoft/Swin-Transformer
注:Swin-Transformer-Object-Detection中mmdet可能会发生报错,说目标文件不存在,先不用急,先装好环境,自然就不会报错了
安装参考链接:
链接1
链接2
链接3

2、环境安装
1)安装mmcv
可自行百度mmcv安装方式,按版本安装,教程很多

 pip install mmcv-full==1.2.2 -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.1/index.html  
 # 注意安装版本、cuda版本、torch版本

2)安装mmdetction
安装包网址

pip install -r requirements.txt
python setup.py develop

pip install -r requirements/build.txt
pip install -v -e .

注意mmdetcton与mmcv版本的对应

3)安装Apex
安装包网址

cd apex-master
pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" .

git clone https://github.com/NVIDIA/apex
cd apex
pip install -r requirements.txt
python setup.py install --cpp_ext

安装成功:

Processing dependencies for apex==0.1
Finished processing dependencies for apex==0.1

3、测试环境
参考命令:

python demo/image_demo.py demo/demo.jpg configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py mask_rcnn_swin_tiny_patch4_window7.pth

用终端命令可能会报错,直接去pycharm下面跑,直接给出文件的地址。
把image_dome.py 放到根目录下

def main():
    parser = ArgumentParser()
    # parser.add_argument('img', help='Image file')
    # parser.add_argument('config', help='Config file')
    # parser.add_argument('checkpoint', help='Checkpoint file')
    parser.add_argument(
        '--device', default='cuda:0', help='Device used for inference')
    parser.add_argument(
        '--score-thr', type=float, default=0.3, help='bbox score threshold')
    args = parser.parse_args()
    # device1 = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

    condfig = 'configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py'
    # condfig = 'work_dirs/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco/latest.pth'
    # 选择下载好的checkpoint
    checkpoint = 'moby_mask_rcnn_swin_tiny_patch4_window7_3x_2.pth'
    # checkpoint = 'work_dirs/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco/latest.pth'
    # img = 'demo/demo.jpg'
    img = 'img.png'

    # build the model from a config file and a checkpoint file
    # model = init_detector(args.config, args.checkpoint, device=args.device)
    model = init_detector(condfig, checkpoint, device=args.device)
    # test a single image
    # result = inference_detector(model, args.img)
    result = inference_detector(model, img)
    # show the results
    show_result_pyplot(model, img, result, score_thr=args.score_thr)

预训练模型mask_rcnn_swin_tiny_patch4_window7.pth可在代码地址处下载,记得修改名字

4、训练
1)准备coco数据集 中的(数据集准备部分)
在准备coco数据集的过程中,我在Ubuntu下生成不了coco训练集的文件,一直会出现以下报错,无论是安装Pyside还是Qt5都解决不了这个问题,希望有大佬可以指点一下。
在这里插入图片描述
于是我就转到win10下面生成
COCO API的Windows安装
有可能需要安装vs,不用怕麻烦,直接去网上找安装VS的教程,等待安装好就行,重启电脑后,COCO API安装会很顺利。
生成coco数据集的代码
生成的.json文件中,可能需要修改图片的地址

2) 修改changemaskrcnn.py中num_class并运行
3) 修改configs_base_\models\mask_rcnn_swin_fpn.py中num_classes
4) 修改configs_base_\default_runtime.py中interval,load_from
5) 修改configs\swin\mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py中的max_epochs、lr
6) 修改configs_base_\datasets\coco_instance.py中samples_per_gpu和workers_per_gpu
7) 修改mmdet\datasets\coco.py中CLASSES
8) python tools/train.py configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py
如果出现:
RuntimeError: cuda runtime error (30) : unknown error at /opt/conda/conda-bld/pytorch_160730172917/work/aten/Thc/THCBlas.cu:255
这种报错,直接运行

sudo python tools/train.py configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py

9)测试模型:和3、测试环境的命令相同,注意修改一些文件
随便打了一些标签的测试结果
在这里插入图片描述

参考视频1
参考视频2

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值