跳到主要内容CVPR2025 DEIM 目标检测模型训练教程:Windows 环境配置与数据集部署 | 极客日志PythonAI算法
CVPR2025 DEIM 目标检测模型训练教程:Windows 环境配置与数据集部署
CVPR2025 DEIM 目标检测模型在 Windows 系统下的完整训练流程。涵盖源码获取、Conda 虚拟环境配置、PyTorch 安装、VisDrone 数据集准备与配置文件修改、训练参数调整及 Windows 特有 Bug 修复方法。包含模型验证、推理测试及热力图生成步骤,适用于学术研究与实验分析。
晚风叙旧19 浏览 CVPR2025 DEIM 目标检测模型训练教程

一、下载 CVPR2025 DEIM 官方源码
DEIM 项目源码地址:
[CVPR 2025] DEIM: DETR with Improved Matching for Fast Convergence (GitHub)

二、创新 DEIM 项目虚拟环境
第一步:创建一个自己的虚拟环境
conda create -n deim python=3.10
第二步:进入到自己的虚拟环境
conda activate deim
第三步:安装 PyTorch,建议不要安装太新版本
PyTorch 官网:Previous PyTorch Versions
conda install pytorch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 pytorch-cuda=12.1 -c pytorch -c nvidia

第四步:直接复制以下所有命令到控制台'终端里面粘贴回车运行'
安装所需要的包!
pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install torchsummary -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnx==1.14.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxruntime==1.15.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pycocotools==2.0.7 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install faster-coco-eval==1.6.5 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install PyYAML -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tensorboard -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install calflops -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxsim==0.4.36 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxruntime-gpu==1.18.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install timm==1.0.7 thop efficientnet_pytorch==0.7.1 einops grad-cam==1.4.8 dill==0.3.6 albumentations pytorch_wavelets==1.3.0 PyWavelets -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -U openmim -i https://pypi.tuna.tsinghua.edu.cn/simple
mim install mmengine -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install mmcv==2.2.0 -f https://download.openmmlab.com/mmcv/dist/cu121/torch2.3/index.html

三、准备自己的数据集和配置自己数据集步骤
3.1 本文以训练 Visdrone2019 无人机数据集为例
VisDrone 数据集是由天津大学等团队开源的一个大型无人机视角的数据集,官方提供的数据中训练集是 6471、验证集是 548、测试集 1610 张。数据集共提供了以下 12 个类,分别是:'忽略区域', 'pedestrian', 'people', 'bicycle', 'car', 'van','truck', 'tricycle', 'awning-tricycle', 'bus', 'motor', 'others',其中忽略区域、others 是非有效目标区域,本项目中予以忽略,只使用'pedestrian', 'people', 'bicycle', 'car', 'van','truck', 'tricycle', 'awning-tricycle', 'bus', 'motor'10 个类。
3.2 将自己数据集放到 datasets 文件夹里
3.3 配置数据步骤
第一步:在 DEIM/configs/dataset 文件中创建 visdrone_detection.yml 文件
第二步:修改 visdrone_detection.yml 文件
完整 visdrone_detection.yml 文件的代码:
task: detection
evaluator:
type: CocoEvaluator
iou_types: ['bbox', ]
num_classes: 10
remap_mscoco_category: False
train_dataloader:
type: DataLoader
dataset:
type: CocoDetection
img_folder: C:/software/mydemo/Demo/DEIM/datasets/visdrone/train2017
ann_file: C:/software/mydemo/Demo/DEIM/datasets/visdrone/annotations/instances_train2017.json
return_masks: False
transforms:
type: Compose
ops: ~
shuffle: True
num_workers: 4
drop_last: True
collate_fn:
type: BatchImageCollateFunction
val_dataloader:
type: DataLoader
dataset:
type: CocoDetection
img_folder: C:/software/mydemo/Demo/DEIM/datasets/visdrone/val2017
ann_file: C:/software/mydemo/Demo/DEIM/datasets/visdrone/annotations/instances_val2017.json
return_masks: False
transforms:
type: Compose
ops: ~
shuffle: False
num_workers: 4
drop_last: False
collate_fn:
type: BatchImageCollateFunction
第三步:修改 DEIM\configs\deim_dfine\dfine_hgnetv2_n_coco.yml 文件
我们跑 deim_hgnetv2_n_coco.yml 为例子,所以我们就修改 dfine_hgnetv2_n_coco.yml 文件!
接下来只需要修改 dfine_hgnetv2_n_coco.yml 文件中的一行代码即可
__include__: [
'../dataset/coco_detection.yml',
'../dataset/visdrone_detection.yml',
'../runtime.yml',
'../base/dataloader.yml',
'../base/optimizer.yml',
'../base/dfine_hgnetv2.yml',
]
第四步:修改 deim_hgnetv2_n_coco.yml 文件
当自己使用不同数据集训练,保存结果文件夹需要重新创建一下,否则第二次训练后的结果权重文件会覆盖第一次训练后的权重文件。
第五步:训练前,记得修改 batch_size 大小,根据自己设备显存大小去灵活设置。
修改 configs/base/dataloader.yml 中的 total_batch_size
同时也修改 DEIM\configs\deim_dfine\dfine_hgnetv2_n_coco.yml 中的 total_batch_size
第六步: 修改 DEIM\configs\runtime.yml 文件中的参数,方便观察
四、使用 DEIM 训练自己的数据集
4.1 训练前先修改一下 train.py 训练脚本里的参数
4.2 train.py 脚本代码完整代码
""" DEIM: DETR with Improved Matching for Fast Convergence
Copyright (c) 2024 The DEIM Authors. All Rights Reserved.
---------------------------------------------------------------------------------
Modified from RT-DETR (https://github.com/lyuwenyu/RT-DETR)
Copyright (c) 2023 lyuwenyu. All Rights Reserved.
"""
import os
import sys
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
import warnings
warnings.filterwarnings('ignore')
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
import argparse
from engine.misc import dist_utils
from engine.core import YAMLConfig, yaml_utils
from engine.solver import TASKS
debug = False
if debug:
import torch
def custom_repr(self):
return f'{{Tensor:{tuple(self.shape)}}} {original_repr(self)}'
original_repr = torch.Tensor.__repr__
torch.Tensor.__repr__ = custom_repr
def main(args, ) -> None:
"""main """
dist_utils.setup_distributed(args.print_rank, args.print_method, seed=args.seed)
assert not all([args.tuning, args.resume]), \
'Only support from_scrach or resume or tuning at one time'
update_dict = yaml_utils.parse_cli(args.update)
update_dict.update({k: v for k, v in args.__dict__.items() \
if k not in ['update', ] and v is not None})
cfg = YAMLConfig(args.config, **update_dict)
if args.resume or args.tuning:
if 'HGNetv2' in cfg.yaml_cfg:
cfg.yaml_cfg['HGNetv2']['pretrained'] = False
print('cfg: ', cfg.__dict__)
solver = TASKS[cfg.yaml_cfg['task']](cfg)
if args.test_only:
solver.val()
else:
solver.fit()
dist_utils.cleanup()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', default=r'C:\software\mydemo\Demo\DEIM\configs\deim_dfine\deim_hgnetv2_n_coco.yml',type=str)
parser.add_argument('-r', '--resume', type=str, help='resume from checkpoint')
parser.add_argument('-t', '--tuning', type=str, help='tuning from checkpoint')
parser.add_argument('-d', '--device', type=str, help='device',)
parser.add_argument('--seed', default=0,type=int, help='exp reproducibility')
parser.add_argument('--use-amp', action='store_true', help='auto mixed precision training')
parser.add_argument('--output-dir', type=str, help='output directoy')
parser.add_argument('--summary-dir', type=str, help='tensorboard summry')
parser.add_argument('--test-only', action='store_true', default=False,)
parser.add_argument('-u', '--update', nargs='+', help='update yaml config')
parser.add_argument('--print-method', type=str, default='builtin', help='print method')
parser.add_argument('--print-rank', type=int, default=0, help='print rank id')
parser.add_argument('--local-rank', type=int, help='local rank id')
args = parser.parse_args()
main(args)
4.3 Windows 系统上第一次训练,需要解决这个源码 bug!
Windows 系统上第一次训练,100% 会出现这个 bug 问题。解决方法如下:
解决方法:修改这个文件 DEIM\engine\backbone\hgnetv2.py
except Exception as e:
print(f"{str(e)}")
logging.error(RED + "CRITICAL WARNING: Failed to load pretrained HGNetV2 model" + RESET)
logging.error(GREEN + f"Please manually download from {download_url} to {local_model_dir}" + RESET)
到此,现在可以正常训练使用 DEIM 官方模型了。
第一次运行,默认是加载预训练权重,所以需要下载。(建议大家自己离线下载比较快,或是等它自动在线下载完,在线下载慢一些),在此处演示我关闭了加载预训练权重变量。
五、验证自己的模型效果
DEIM 训练部分 epoch,给大家演示一下使用 val.py 在验证集或是测试集上的效果。
六、推理测试自己的模型
七、DEIM 模型生成论文实验热力图
使用 DEIM 生成热力图脚本.py 生成热力图,论文实验部分增加可视化热力图分析,提高论文的质量,增加小论文录用率。
八、本文总结
本文详细介绍了 CVPR2025 DEIM 目标检测模型的完整使用流程。内容包含:1)源码下载与虚拟环境配置;2)Visdrone2019 数据集准备与配置文件修改;3)训练参数设置及 Windows 系统常见问题解决方案;4)模型验证、推理测试及热力图生成方法。特别针对 Windows 用户也提供了本地虚拟环境配置,并详细说明了数据集路径设置、参数调整等关键步骤。通过本文的指导,可以快速掌握 DEIM 模型的使用方法,用于学术论文研究和实验分析。
相关免费在线工具
- 加密/解密文本
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
- RSA密钥对生成器
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
- Mermaid 预览与可视化编辑
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
- 随机西班牙地址生成器
随机生成西班牙地址(支持马德里、加泰罗尼亚、安达卢西亚、瓦伦西亚筛选),支持数量快捷选择、显示全部与下载。 在线工具,随机西班牙地址生成器在线工具,online
- Gemini 图片去水印
基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online
- curl 转代码
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online