[卷积神经网络]YOLOv11实战:从零构建自定义数据集训练流程

张开发
2026/4/10 8:30:05 15 分钟阅读

分享文章

[卷积神经网络]YOLOv11实战:从零构建自定义数据集训练流程
1. 数据集准备与格式转换第一次用YOLOv11训练自己的模型时最头疼的就是数据集处理。我的数据是VOC格式的但YOLO需要特定的文本标注格式。这里分享一个我验证过的完整转换方案包含几个容易踩坑的细节。VOC转YOLO格式的核心是坐标归一化处理。VOC使用绝对坐标(xmin,ymin,xmax,ymax)而YOLO需要转换为相对坐标(x_center,y_center,width,height)。我写了个Python脚本自动处理import xml.etree.ElementTree as ET def convert_box(size, box): dw 1./size[0] dh 1./size[1] x (box[0] box[2])/2.0 y (box[1] box[3])/2.0 w box[2] - box[0] h box[3] - box[1] x x * dw w w * dw y y * dh h h * dh return (x,y,w,h) def convert_annotation(xml_file, txt_file, classes): tree ET.parse(xml_file) root tree.getroot() size root.find(size) w int(size.find(width).text) h int(size.find(height).text) with open(txt_file, w) as f: for obj in root.iter(object): cls obj.find(name).text if cls not in classes: continue cls_id classes.index(cls) xmlbox obj.find(bndbox) b (float(xmlbox.find(xmin).text), float(xmlbox.find(ymin).text), float(xmlbox.find(xmax).text), float(xmlbox.find(ymax).text)) bb convert_box((w,h), b) f.write(f{cls_id} { .join([str(a) for a in bb])}\n)实际使用时要注意三点类别顺序必须固定建议用列表不要用字典图像尺寸要从XML里读取不能假设都是统一尺寸路径处理建议用os.path.join避免跨平台问题2. 数据集划分策略数据集划分直接影响模型效果。我习惯用8:1:1的比例分割训练集、验证集和测试集。这里推荐使用sklearn的train_test_split分层抽样from sklearn.model_selection import train_test_split def split_dataset(image_dir, label_dir, test_size0.2): images [f for f in os.listdir(image_dir) if f.endswith(.jpg)] labels [f.replace(.jpg, .txt) for f in images] # 先分训练集和临时集 train_img, temp_img, train_lbl, temp_lbl train_test_split( images, labels, test_sizetest_size, random_state42) # 再分验证集和测试集 val_img, test_img, val_lbl, test_lbl train_test_split( temp_img, temp_lbl, test_size0.5, random_state42) return { train: (train_img, train_lbl), val: (val_img, val_lbl), test: (test_img, test_lbl) }关键点保持图像和标注文件同步分割随机种子固定保证可复现测试集要完全隔离只在最终评估使用3. 配置文件定制YOLOv11需要两个核心配置文件数据配置和模型配置。数据配置示例# VOC.yaml path: ../datasets/VOC train: images/train val: images/val test: images/test nc: 6 # 类别数 names: [person, car, dog, cat, bicycle, motorcycle]模型配置我推荐从官方yolo11s.yaml开始修改# yolo11-custom.yaml nc: 6 # 必须与数据配置一致 scales: s: depth: 0.33 width: 0.50 max_channels: 1024 backbone: # [from, repeats, module, args] - [-1, 1, Conv, [64, 3, 2]] # 0-P1/2 - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4 # ...保持其他默认配置特别注意修改nc后要同步调整最后一层Detect的参数小数据集建议用s版本减少过拟合风险输入尺寸保持640x640除非有特殊需求4. 训练过程优化启动训练的核心代码很简单from ultralytics import YOLO model YOLO(yolo11-custom.yaml) results model.train( dataVOC.yaml, epochs100, imgsz640, batch16, device0 )但有几个提升效果的关键技巧学习率预热添加warmup_epochs3参数早停机制patience10当验证集指标不再提升时停止数据增强默认的augment已经很充分不建议新手修改混合精度ampTrue能显著减少显存占用训练过程中要重点关注三个指标train/box_loss检测框回归损失train/cls_loss分类损失val/mAP0.5验证集平均精度5. 模型评估与导出训练完成后用测试集进行最终评估model YOLO(runs/detect/train/weights/best.pt) metrics model.val( dataVOC.yaml, splittest, conf0.5 # 置信度阈值 ) print(fmAP0.5: {metrics.box.map})模型导出为ONNX格式便于部署model.export(formatonnx, dynamicTrue)常见问题处理如果显存不足减小batch_size或imgsz如果过拟合增加数据增强或减少模型规模如果训练不稳定检查数据标注质量6. 实际应用建议在工业项目中我发现这些实践特别有用使用wandb或tensorboard记录训练过程对困难样本进行针对性数据增强尝试不同anchor box设置尤其非常规目标测试时使用TTA(Test Time Augmentation)提升稳定性对于边缘设备部署建议导出时进行量化int8使用TensorRT加速对输出做后处理过滤

更多文章