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。
# 创建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服务
确保数据集结构如下:
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']
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
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
测试不同置信度阈值:
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
python scripts/utils/export_model.py \
--weights best.pt \
--formats onnx \
--simplify
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
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')
# 启动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
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损失权重
错误:
FileNotFoundError: 'levir_ship.yaml' does not exist
解决方案: 增强版脚本会自动查找配置文件,无需手动指定。如果仍然报错,检查文件是否在以下位置之一:
项目根目录/levir_ship.yamldata/processed/LEVIR-Ship-YOLO/levir_ship.yamldata/levir_ship.yamlconfigs/levir_ship.yaml或者手动指定:
python train_levir_ship_enhanced.py \
--mode train \
--data data/processed/LEVIR-Ship-YOLO/levir_ship.yaml
错误:
RuntimeError: CUDA out of memory
解决方案:
# 减小批次大小
--batch 16 # 或 8, 4
# 关闭图像缓存
# 移除 --cache 参数
# 使用更小的模型
--model-size n # 代替 s 或 m
问题: 训练时没有TensorBoard日志
解决方案:
# 安装TensorBoard
pip install tensorboard
# 训练时启用
--tensorboard
# 启动TensorBoard
tensorboard --logdir=artifacts/experiments/levir_ship_detection/实验名称/tensorboard
优化方案:
# 1. 启用图像缓存(需要足够内存)
--cache
# 2. 增加workers
修改脚本中的 workers: 8
# 3. 启用AMP混合精度
已默认启用,train_config中 amp: True
# 4. 使用更小的图像尺寸(不推荐,会降低精度)
--imgsz 416
优化方案:
python train_levir_ship_enhanced.py \
--mode val \
--weights best.pt \
--conf-thresholds 0.001 0.01 0.05 0.1 0.25
--epochs 600 # 或更多
--model-size m # 代替 s
copy_paste: 0.5 # 增加到0.5
mosaic: 1.0
mixup: 0.2
# 批量推理比逐张推理快
results = detector.detect(image_list, conf=0.25)
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
}
检测接口
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 # 实验跟踪
遇到问题?
本项目基于LEVIR-Ship数据集和YOLOv10模型开发。
最后更新: 2026-02-06 版本: 2.0