基于 DINO-V3 7B 模型的缺陷检测微调训练框架,支持变电站等工业场景的缺陷检测。
dinov3/
├── cc/dino_v3_detection/ # 主代码目录
│ ├── data/ # 数据集模块
│ │ ├── __init__.py
│ │ └── dataset.py # COCO 数据集加载 + 数据增强
│ ├── models/ # 模型定义
│ │ ├── __init__.py
│ │ └── model.py # DINOv3Detector
│ ├── train/ # 训练模块
│ │ ├── __init__.py
│ │ └── trainer.py # 训练器、损失函数、匈牙利匹配器
│ ├── eval/ # 评估模块
│ │ ├── __init__.py
│ │ └── metrics.py # mAP/Recall/Precision 计算
│ ├── reason/ # 推理模块
│ │ ├── __init__.py
│ │ ├── inference.py # 主推理脚本
│ │ ├── inference_config.py # 推理配置
│ │ └── README.md
│ ├── train.py # 主训练脚本
│ └── TRAINING.md # 训练快速开始文档
├── configs/
│ └── finetune_config.yaml # 训练配置文件
├── README.md # 本文件
└── .git/
- Python 3.9+
- PyTorch 2.0+
- torchvision
- pycocotools(评估时需要)
- PyYAML
- opencv-python
- Pillow
pip install torch torchvision pycocotools pyyaml opencv-python pillow将标注数据整理为 COCO 格式:
/workspace/substation/
├── images/
│ ├── image_001.jpg
│ └── ...
└── annotations/
├── train.json
└── val.json
编辑 configs/finetune_config.yaml,修改数据路径和模型路径:
data:
train_image_dir: "/workspace/substation/images"
train_annotation_file: "/workspace/substation/annotations/train.json"
val_image_dir: "/workspace/substation/images"
val_annotation_file: "/workspace/substation/annotations/val.json"
target_classes:
- "zhen_kong" # 针孔
- "ca_shang" # 擦伤
- "zang_wu" # 脏污
- "zhe_zhou" # 褶皱
model:
checkpoint_path: "/workspace/ckpts/vit_7b_patch16_dinov3_lvd1689m.pth"cd /Users/xiaoyu/code/dinov3/cc/dino_v3_detection
python train.py --config ../../configs/finetune_config.yamlpython reason/inference.py \
--model /workspace/train_output/checkpoints/checkpoint_best.pth \
--input /workspace/test_images \
--output /workspace/output_images详细文档参考:cc/dino_v3_detection/TRAINING.md
| 类别 ID | 类别名称 | 中文含义 |
|---|---|---|
| 1 | zhen_kong | 针孔 |
| 2 | ca_shang | 擦伤 |
| 3 | zang_wu | 脏污 |
| 4 | zhe_zhou | 褶皱 |
- 当前漏检率: 40% → 目标: <15%
- 当前误报率: 30% → 目标: <10%
configs/finetune_config.yaml 包含所有训练参数:
| 参数 | 默认值 | 说明 |
|---|---|---|
batch_size |
4 | 批次大小 |
learning_rate |
1e-4 | 学习率 |
max_epochs |
100 | 训练轮数 |
input_size |
800 | 输入图像大小 |
use_amp |
true | 混合精度训练 |
python train.py \
--config ../../configs/finetune_config.yaml \
--resume /workspace/train_output/checkpoints/checkpoint_best.pth- mAP@0.5
- mAP@0.5:0.95
- Precision / Recall
- 各类别 AP
- 训练快速开始:
cc/dino_v3_detection/TRAINING.md - 推理使用说明:
cc/dino_v3_detection/reason/README.md - 设计文档:
~/.gstack/projects/xiaoyu-dinov3/dino-v3-finetune-design.md