RTDETRv2 pytorch训练

RTDETRv2 pytorch训练

1. 代码获取

从github上下载官方源码官方源码,将其中的redetrv2_pytorch单独移动到桌面上

python 复制代码
git clone https://github.com/lyuwenyu/RT-DETR.git

2. 数据集制作

使用github仓库中的 yolo2coco_1.py将YOLO标签转化为COCO数据集格式,然后将其转化为如下的存储顺序,最终移动到redetrv2_pytorch中

python 复制代码
dataset
	└── coco
		├── train2017  
		├── val2017 
		├── test2017 
    	└── annotations
    		├── instance_train2017.json
    		├── instance_val2017.json
    		└── instance_test2017.json

3. 环境配置

windows 11 GPU4090

模型训练环境为YOLOv9,然后需要单独安装faster-coco-eval库

python 复制代码
pip install faster-coco-eval

或者直接

python 复制代码
pip install -r requirements.txt

4. 代码修改

1)configs/dataset/coco_detection.yml
python 复制代码
num_classes: 2  # 原始 80  以二标签数据集为例(flame、smoke)

# 如果按照上面的数据集制作数据集格式,则不需要修改内容,以个人数据集为例
img_folder: ./dataset/coco/train2017/
ann_file: ./dataset/coco/annotations/instances_train2017.json
2) configs/src/data/coco_dataset.py

mscoco_category2name修改为自己的标签内容

python 复制代码
# 注意,0、1需要根据自己转化的COCO数据集确定的 由于使用的是yolo2coco_1.py,生成的category_id是从0开始的,所以使用下面的这个
mscoco_category2name = {
    0: 'flame',
    1: 'smoke'
}
# 按照80标签的COCO数据集是从1开始的
mscoco_category2name = {
    1: 'flame',
    2: 'smoke'
}
3)configs/src/core/yaml_utils.py

如果在训练的时候出现读取yaml文件,字体格式的报错,可将代码修改为如下。如果没有出现,可直接跳过本步骤。

python 复制代码
# 添加encoding='utf-8'
with open(file_path, encoding='utf-8') as f:
    file_cfg = yaml.load(f, Loader=yaml.Loader)
    if file_cfg is None:
        return {}
4)configs/rtdeterv2/include/optimizer.yml

如果需要修改epoch,可以修改如下代码

python 复制代码
epoches: 300

此时,对应的configs/rtdeterv2/include/dataloader.yml需要进行如下修改

python 复制代码
train_dataloader: 
  dataset: 
    transforms:
      ops:
        - {type: RandomPhotometricDistort, p: 0.5}
        - {type: RandomZoomOut, fill: 0}
        - {type: RandomIoUCrop, p: 0.8}
        - {type: SanitizeBoundingBoxes, min_size: 1}
        - {type: RandomHorizontalFlip}
        - {type: Resize, size: [640, 640], }
        - {type: SanitizeBoundingBoxes, min_size: 1}
        - {type: ConvertPILImage, dtype: 'float32', scale: True}   
        - {type: ConvertBoxes, fmt: 'cxcywh', normalize: True}
      policy:
        name: stop_epoch
        epoch: 299 # epoch in [71, ~) stop `ops`  原始为71
        ops: ['RandomPhotometricDistort', 'RandomZoomOut', 'RandomIoUCrop']
  
  collate_fn:
    type: BatchImageCollateFuncion
    scales: [480, 512, 544, 576, 608, 640, 640, 640, 672, 704, 736, 768, 800]
    stop_epoch: 299 # epoch in [71, ~) stop `multiscales` 原始为71

5. 代码训练、验证、以及模型参数和FLOPs

1) training
python 复制代码
python tools/train.py -c ./configs/rtdetrv2/rtdetrv2_r50vd_m_7x_coco.yml --use-amp --seed=0
2) tuning
python 复制代码
python tools/train.py -c path/to/config -t path/to/checkpoint --use-amp --seed=0
3)验证
python 复制代码
python tools/train.py -c path/to/config -r path/to/checkpoint --test-only
4) 查看模型参数
python 复制代码
python tools/run_profile.py -c path/to/config
相关推荐
思则变1 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
叶子爱分享1 小时前
计算机视觉与图像处理的关系
图像处理·人工智能·计算机视觉
鱼摆摆拜拜1 小时前
第 3 章:神经网络如何学习
人工智能·神经网络·学习
一只鹿鹿鹿1 小时前
信息化项目验收,软件工程评审和检查表单
大数据·人工智能·后端·智慧城市·软件工程
张较瘦_1 小时前
[论文阅读] 人工智能 | 深度学习系统崩溃恢复新方案:DaiFu框架的原位修复技术
论文阅读·人工智能·深度学习
cver1231 小时前
野生动物检测数据集介绍-5,138张图片 野生动物保护监测 智能狩猎相机系统 生态研究与调查
人工智能·pytorch·深度学习·目标检测·计算机视觉·目标跟踪
漫谈网络1 小时前
WebSocket 在前后端的完整使用流程
javascript·python·websocket
学技术的大胜嗷1 小时前
离线迁移 Conda 环境到 Windows 服务器:用 conda-pack 摆脱硬路径限制
人工智能·深度学习·yolo·目标检测·机器学习
还有糕手1 小时前
西南交通大学【机器学习实验10】
人工智能·机器学习
江瀚视野1 小时前
百度文心大模型4.5系列正式开源,开源会给百度带来什么?
人工智能