yolov10-levir-ship

YOLOv10 + LEVIR-Ship 舰船检测项目

🎯 项目特色

📁 项目结构

yolov10-levir-ship/
├── data/                        # 数据集与 YOLO 标注
├── se_yolov10/                  # SE-YOLOv10 核心算法包
├── scripts/
│   ├── data_preparation/        # 数据准备脚本
│   ├── training/                # 训练入口脚本
│   ├── evaluation/              # 评估脚本
│   ├── inference/               # 推理/API 脚本
│   └── utils/                   # 导出等工具脚本
├── models/
│   ├── pretrained/              # 预训练与对比权重
│   └── trained/                 # 训练后模型存放位
├── results/                     # 评估/预测/可视化结果
├── artifacts/
│   ├── experiments/             # 历史训练实验输出
│   ├── exports/                 # 导出产物
│   ├── tracking/                # runs / wandb 等跟踪产物
│   └── legacy/                  # 从旧目录迁移的历史产物
├── configs/                     # 配置和示例 YAML
├── docs/                        # 文档和结构快照
└── train_se_yolov10.py          # 兼容入口,转发到 scripts/training/

当前结构按“算法实现 / 可执行脚本 / 模型权重 / 实验产物”分层。根目录的 train_se_yolov10.py 仅保留为兼容入口,实际实现位于 scripts/training/train_se_yolov10.py

🚀 快速开始

1. 环境设置

# 创建conda环境
conda create -n yolov10 python=3.9
conda activate yolov10

# 安装依赖
pip install ultralytics
pip install tensorboard  # 可选,用于可视化
pip install wandb        # 可选,用于实验跟踪
pip install flask flask-cors  # 可选,用于API服务

2. 数据准备

确保数据集结构如下:

data/processed/LEVIR-Ship-YOLO/
├── images/
│   ├── train/
│   └── val/
├── labels/
│   ├── train/
│   └── val/
└── levir_ship.yaml

levir_ship.yaml 内容:

path: /path/to/data/processed/LEVIR-Ship-YOLO
train: images/train
val: images/val

nc: 1
names: ['ship']

3. 训练模型

基础训练(推荐)

python scripts/training/train_levir_ship_enhanced.py \
  --mode train \
  --model-size s \
  --epochs 500 \
  --batch 8 \
  --pretrained \
  --cache \
  --tensorboard

高精度训练

python scripts/training/train_levir_ship_enhanced.py \
  --mode train \
  --model-size m \
  --epochs 600 \
  --batch 24 \
  --pretrained \
  --cache \
  --tensorboard \
  --wandb

轻量级训练

python scripts/training/train_levir_ship_enhanced.py \
  --mode train \
  --model-size n \
  --epochs 400 \
  --batch 48 \
  --pretrained \
  --cache

4. 查看训练过程

TensorBoard可视化

tensorboard --logdir=artifacts/experiments/levir_ship_detection/yolov10s_YYYYMMDD_HHMMSS/tensorboard

然后在浏览器打开 http://localhost:6006

训练曲线

训练过程中会自动生成训练曲线图:

artifacts/experiments/levir_ship_detection/yolov10s_YYYYMMDD_HHMMSS/training_curves.png

5. 模型验证

测试不同置信度阈值:

python scripts/training/train_levir_ship_enhanced.py \
  --mode val \
  --weights artifacts/experiments/levir_ship_detection/yolov10s_YYYYMMDD_HHMMSS/weights/best.pt \
  --conf-thresholds 0.001 0.01 0.05 0.1 0.25

6. 模型导出

导出ONNX

python scripts/utils/export_model.py \
  --weights best.pt \
  --formats onnx \
  --simplify

导出TensorRT(GPU加速)

python scripts/utils/export_model.py \
  --weights best.pt \
  --formats engine \
  --half

导出多种格式

python scripts/utils/export_model.py \
  --weights best.pt \
  --formats onnx engine torchscript

7. 推理使用

方法1: Python类接口

from ship_detector import ShipDetector

# 初始化检测器
detector = ShipDetector('best.pt', device='cuda')

# 单图检测
result = detector.detect_single('ship.jpg', conf=0.25)
print(f"检测到 {result['num_detections']} 艘舰船")

# 批量检测
results = detector.detect(['img1.jpg', 'img2.jpg'], conf=0.25)

# 可视化
detector.visualize_results(results, save_dir='outputs')

# 导出结果
detector.export_results(results, 'results.json', format='json')

方法2: REST API

# 启动API服务
python scripts/inference/inference_api.py \
  --weights best.pt \
  --device cuda \
  --port 5000

# 测试API (另一个终端)
curl -X POST \
  -F "image=@ship.jpg" \
  -F "conf=0.25" \
  http://localhost:5000/api/detect

方法3: 命令行

yolo predict model=best.pt source=ship.jpg conf=0.25

📊 训练配置说明

模型规模选择

模型 参数量 速度 精度 推荐场景
YOLOv10-N 2.3M 最快 较低 边缘设备、实时检测
YOLOv10-S 7.2M 中等 推荐,平衡性能
YOLOv10-M 15.4M 中等 高精度需求
YOLOv10-B 19.1M 很高 离线分析
YOLOv10-L 24.4M 很慢 极高 研究用途
YOLOv10-X 29.5M 极慢 最高 追求极限精度

关键超参数

# 优化器
optimizer: 'AdamW'        # 小目标检测推荐AdamW
lr0: 0.001               # 初始学习率
weight_decay: 0.0005     # 权重衰减

# 数据增强(小目标优化)
mosaic: 1.0              # 马赛克增强(重要!)
copy_paste: 0.3          # 复制粘贴增强(关键!)
mixup: 0.15              # Mixup增强
scale: 0.8               # 尺度变化(±20%)

# 训练策略
multi_scale: True        # 多尺度训练
close_mosaic: 50         # 最后50轮关闭mosaic
patience: 100            # 早停耐心值

# 损失权重
box: 7.5                 # 边框损失权重
cls: 0.5                 # 分类损失权重(单类别降低)
dfl: 1.5                 # DFL损失权重

🔧 常见问题解决

1. 找不到levir_ship.yaml

错误:

FileNotFoundError: 'levir_ship.yaml' does not exist

解决方案: 增强版脚本会自动查找配置文件,无需手动指定。如果仍然报错,检查文件是否在以下位置之一:

或者手动指定:

python train_levir_ship_enhanced.py \
  --mode train \
  --data data/processed/LEVIR-Ship-YOLO/levir_ship.yaml

2. CUDA内存不足

错误:

RuntimeError: CUDA out of memory

解决方案:

# 减小批次大小
--batch 16  # 或 8, 4

# 关闭图像缓存
# 移除 --cache 参数

# 使用更小的模型
--model-size n  # 代替 s 或 m

3. TensorBoard未显示

问题: 训练时没有TensorBoard日志

解决方案:

# 安装TensorBoard
pip install tensorboard

# 训练时启用
--tensorboard

# 启动TensorBoard
tensorboard --logdir=artifacts/experiments/levir_ship_detection/实验名称/tensorboard

4. 训练速度慢

优化方案:

# 1. 启用图像缓存(需要足够内存)
--cache

# 2. 增加workers
修改脚本中的 workers: 8

# 3. 启用AMP混合精度
已默认启用,train_config中 amp: True

# 4. 使用更小的图像尺寸(不推荐,会降低精度)
--imgsz 416

5. 验证精度低

优化方案:

  1. 调整置信度阈值
    python train_levir_ship_enhanced.py \
      --mode val \
      --weights best.pt \
      --conf-thresholds 0.001 0.01 0.05 0.1 0.25
    
  2. 增加训练轮数
    --epochs 600  # 或更多
    
  3. 使用更大的模型
    --model-size m  # 代替 s
    
  4. 增强数据增强 修改脚本中的数据增强参数:
    copy_paste: 0.5   # 增加到0.5
    mosaic: 1.0
    mixup: 0.2
    

📈 性能优化建议

训练阶段

  1. 使用预训练权重 - 显著提升收敛速度
  2. 图像缓存 - 512×512图像可以缓存,加速训练
  3. 多尺度训练 - 提升对不同尺度目标的检测能力
  4. Copy-Paste增强 - 对小目标检测非常有效

推理阶段

  1. 模型导出
    • ONNX: 通用性好,支持多平台
    • TensorRT: NVIDIA GPU上速度最快
    • CoreML: iOS/macOS设备
  2. 批量推理
    # 批量推理比逐张推理快
    results = detector.detect(image_list, conf=0.25)
    
  3. 降低置信度阈值
    • 提高召回率: conf=0.01
    • 平衡精确率和召回率: conf=0.25

📚 API文档

Python类接口

from ship_detector import ShipDetector

# 初始化
detector = ShipDetector(
    model_path='best.pt',
    device='cuda',  # 'cpu', 'cuda', '0', '1'
    verbose=True
)

# 检测
result = detector.detect_single(
    'ship.jpg',
    conf=0.25,        # 置信度阈值
    iou=0.5,          # NMS IOU阈值
    max_det=300       # 最大检测数
)

# 返回格式
{
    'image_path': 'ship.jpg',
    'image_shape': [1024, 1024],
    'num_detections': 3,
    'detections': [
        {
            'bbox': [x1, y1, x2, y2],
            'confidence': 0.85,
            'class_id': 0,
            'class_name': 'ship'
        },
        ...
    ],
    'inference_time': 0.015
}

REST API接口

检测接口

POST /api/detect

参数:
- image: 图像文件或base64
- conf: 置信度阈值 (默认0.25)
- iou: NMS阈值 (默认0.5)
- max_det: 最大检测数 (默认300)
- visualize: 是否返回可视化图像 (默认true)

返回:
{
  "success": true,
  "num_detections": 3,
  "detections": [...],
  "inference_time": 0.015,
  "image_base64": "..."  // 如果visualize=true
}

健康检查

GET /api/health

返回:
{
  "status": "healthy",
  "model_loaded": true,
  "model_info": {...}
}

🎓 训练建议

初学者

# 使用默认配置,快速开始
python train_levir_ship_enhanced.py \
  --mode train \
  --model-size s \
  --epochs 300 \
  --pretrained \
  --cache \
  --tensorboard

进阶用户

# 自定义配置,追求高精度
python train_levir_ship_enhanced.py \
  --mode train \
  --model-size m \
  --epochs 600 \
  --batch 24 \
  --pretrained \
  --cache \
  --tensorboard \
  --wandb  # 实验跟踪

研究用途

  1. 修改脚本中的训练配置
  2. 尝试不同的数据增强策略
  3. 调整损失函数权重
  4. 使用ensemble方法

📞 技术支持

遇到问题?

  1. 检查本README的”常见问题解决”部分
  2. 查看训练日志和TensorBoard
  3. 验证数据集格式是否正确
  4. 确保环境依赖已正确安装

📄 许可证

本项目基于LEVIR-Ship数据集和YOLOv10模型开发。

🙏 致谢


最后更新: 2026-02-06 版本: 2.0