【Swin Transformer】Win10使用Swin Transformer做目标检测 (使用自己的数据集 + 图解超详细)

文章目录

Swin Transformer

环境搭建

我的环境:Python 3.6 + VS2019 + CUDA 11.2 + CUDNN 8.1.0

VS2019配置

环境变量
D:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\bin\Hostx86\x64

命令行输入cl

【Swin Transformer】Win10使用Swin Transformer做目标检测 (使用自己的数据集 + 图解超详细)

; conda虚拟环境

创建conda虚拟环境:conda create -n transformer python=3.8

激活虚拟环境:conda activate transformer

Pytorch安装

https://pytorch.org/

mmcv安装

查看匹配版本

https://github.com/open-mmlab/mmdetection/blob/master/docs/en/get_started.md

【Swin Transformer】Win10使用Swin Transformer做目标检测 (使用自己的数据集 + 图解超详细)

; 安装mmcv

我安装的是 mmcv 1.3.1。https://github.com/open-mmlab/mmcv不方便下载的,直接看百度网盘连接。

mmcv 1.3.1版本:
链接:https://pan.baidu.com/s/1Z7eOh7wkrhoKDRoODREHHg?pwd=6mvh
提取码:6mvh

提供令一个版本的,也就是上面github链接的目前最新版本:
链接:https://pan.baidu.com/s/1mKx7w0fjXp9BJhwbg4gtMg?pwd=dbvy
提取码:dbvy

解压之后,使用 conda的power shell

  1. cd 到安装的mmcv1.3.1的目录,推荐放到工程项目中。
  2. 进入虚拟环境: conda activate transformer
  3. 执行 pip install -r requirements.txt
  4. 设置环境变量: $env:TORCH_CUDA_ARCH_LIST="8.6" 8.6为显卡算力,查询位置:英伟达官网(3060看英文文档),3060 查看算力位置:英文算力位置
  5. 设置环境变量: $env:MMCV_WITH_OPS = 1
  6. 设置环境变量: $env:MAX_JOBS = 8 根据CPU核心数设置
  7. 执行 python setup.py build_ext
  8. 执行 python setup.py develop
  9. 使用 pip list查看,mmcv-full 的版本是否为1.3.1
    【Swin Transformer】Win10使用Swin Transformer做目标检测 (使用自己的数据集 + 图解超详细)

; mmdet安装

步骤基本同上。

  1. git clone https://github.com/open-mmlab/mmdetection.git
  2. cd mmdetection
  3. pip install -r requirements/build.txt

apex安装

步骤基本同上。

  1. 下载apex项目:https://github.com/NVIDIA/apex
  2. cd 进入 apex 文件夹
  3. 执行: python setup.py install

下载权重

https://github.com/SwinTransformer/Swin-Transformer-Object-Detection

【Swin Transformer】Win10使用Swin Transformer做目标检测 (使用自己的数据集 + 图解超详细)

; demo测试代码

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

【Swin Transformer】Win10使用Swin Transformer做目标检测 (使用自己的数据集 + 图解超详细)

至此说明 配置没有问题了。

训练自己的数据集

需要修改的部分

修改标签种类数

路径Swin-Transformer-Object-Detection-master\configs\_base_\models\mask_rcnn_swin_fpn.py

将 num_classes 换成自己数据集的种类数

修改标签类别

路径Swin-Transformer-Object-Detection-master\mmdet\datasets\coco.py


    CLASSES = ('dog', 'cat')

配置权重信息

路径Swin-Transformer-Object-Detection-master\configs\_base_\default_runtime.py

  • interval:表示多少个 epoch 验证一次,然后保存一次权重信息
  • loadfrom:加载权重,指向权重的路径

修改尺寸大小

路径Swin-Transformer-Object-Detection-master\configs\swin\mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py

路径Swin-Transformer-Object-Detection-master\configs\_base_\datasets\coco_instance.py

所有的 img_scale 修改为 : img_scale = [(256, 256)] 或者是36的倍数,如480、512等。

配置数据集路径

路径Swin-Transformer-Object-Detection-master\configs\_base_\datasets\coco_instance.py

train=dict(
        type=dataset_type,
        ann_file=data_root + 'annotations/instances_train2017.json',
        img_prefix=data_root + 'train2017/',
        pipeline=train_pipeline),
    val=dict(
        type=dataset_type,
        ann_file=data_root + 'annotations/instances_val2017.json',
        img_prefix=data_root + 'val2017/',
        pipeline=test_pipeline),
    test=dict(
        type=dataset_type,
        ann_file=data_root + 'annotations/instances_test2017.json',
        img_prefix=data_root + 'test2017/',
        pipeline=test_pipeline))

修改 batch size 和 线程数

samples_per_gpu=2,
workers_per_gpu=2,

训练命令

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

【Swin Transformer】Win10使用Swin Transformer做目标检测 (使用自己的数据集 + 图解超详细)

测试命令

可以将测试图片放至 demo 文件夹,执行如下的命令。

python demo/image_demo.py demo/000000444010.jpg configs\swin\mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py work_dirs/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco/latest.pth

【Swin Transformer】Win10使用Swin Transformer做目标检测 (使用自己的数据集 + 图解超详细)

Original: https://blog.csdn.net/qq_44824148/article/details/124099034
Author: 零碎@流年絮语
Title: 【Swin Transformer】Win10使用Swin Transformer做目标检测 (使用自己的数据集 + 图解超详细)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/707510/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球