ARTICLE DETAIL

资讯详情

深耕网站SEO优化与搜索引擎排名提升的一线实战洞察。

Labelme标注转YOLOv5训练数据的全流程实战

Labelme标注转YOLOv5训练数据的全流程实战 1. 项目概述从Labelme标注到YOLOv5训练的全流程解析在计算机视觉项目中数据标注到模型训练是一个完整的工作流。Labelme作为一款开源的图像标注工具以其多边形标注能力和JSON格式输出著称而YOLOv5则是当前最流行的目标检测框架之一。将Labelme标注数据转换为YOLOv5可用的格式是许多实际项目中的必经之路。这个转换过程看似简单实则暗藏诸多技术细节。从Labelme的JSON标注结构解析到YOLOv5所需的TXT格式转换再到类别ID映射和坐标归一化处理每个环节都需要精确处理。我在多个工业检测和安防项目中实践过这套流程本文将分享从标注到训练的全套实战经验。2. 核心工具与环境配置2.1 Labelme的安装与配置Labelme推荐通过Python虚拟环境安装conda create -n labelme python3.8 conda activate labelme pip install labelme中文显示问题的解决方案找到Labelme配置文件通常位于~/.labelmerc修改font项为系统中已安装的中文字体路径示例配置{ flags: {}, labels: [], lineColor: [0,255,0,128], fillColor: [255,0,0,128], font: /usr/share/fonts/truetype/wqy/wqy-microhei.ttc }2.2 YOLOv5环境准备官方推荐使用PyTorch 1.7环境git clone https://github.com/ultralytics/yolov5 cd yolov5 pip install -r requirements.txt3. Labelme标注规范与技巧3.1 标注文件结构解析Labelme生成的JSON文件包含以下关键字段{ version: 4.5.6, flags: {}, shapes: [ { label: person, points: [[x1,y1], [x2,y2], ...], shape_type: polygon } ], imagePath: example.jpg, imageData: base64编码的图片数据 }3.2 高效标注实践使用快捷键加速标注Ctrl鼠标滚轮缩放图像空格键完成当前多边形CtrlZ撤销上一步批量标注建议保持同类物体使用相同标签名称复杂物体建议用多边形而非矩形标注对遮挡物体进行分层标注4. 格式转换核心技术实现4.1 坐标转换算法YOLOv5需要中心坐标宽高的相对坐标格式转换公式为x_center (x_min x_max) / 2 / image_width y_center (y_min y_max) / 2 / image_height width (x_max - x_min) / image_width height (y_max - y_min) / image_height4.2 完整转换脚本import json import os from pathlib import Path def labelme2yolo(json_file, class_map): with open(json_file) as f: data json.load(f) img_width data[imageWidth] img_height data[imageHeight] txt_lines [] for shape in data[shapes]: label shape[label] points shape[points] # 获取边界框坐标 x_coords [p[0] for p in points] y_coords [p[1] for p in points] x_min, x_max min(x_coords), max(x_coords) y_min, y_max min(y_coords), max(y_coords) # 转换为YOLO格式 x_center (x_min x_max) / 2 / img_width y_center (y_min y_max) / 2 / img_height width (x_max - x_min) / img_width height (y_max - y_min) / img_height class_id class_map[label] txt_lines.append(f{class_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}) # 保存为同名txt文件 txt_path json_file.replace(.json, .txt) with open(txt_path, w) as f: f.write(\n.join(txt_lines))5. YOLOv5数据集配置5.1 目录结构规范dataset/ ├── images/ │ ├── train/ │ └── val/ └── labels/ ├── train/ └── val/5.2 数据集YAML配置# dataset.yaml train: ../dataset/images/train val: ../dataset/images/val nc: 3 # 类别数量 names: [person, car, dog] # 类别名称6. 训练与验证6.1 启动训练命令python train.py --img 640 --batch 16 --epochs 100 \ --data dataset.yaml --weights yolov5s.pt6.2 关键参数解析--img输入图像尺寸必须为32的倍数--batch根据GPU显存调整建议从8开始尝试--epochs通常50-300之间--weights预训练模型选择s/m/l/x7. 常见问题与解决方案7.1 标注转换问题问题1转换后坐标超出[0,1]范围原因标注时超出图像边界解决在Labelme中修正标注或添加边界检查代码问题2类别ID不匹配原因class_map定义与YAML文件不一致解决保持class_map与dataset.yaml中的names顺序一致7.2 训练问题问题1Loss不下降检查项标注质量使用verify_labels.py脚本学习率设置尝试--lr0 0.01数据增强配置适当减少--hsv_h参数问题2CUDA out of memory解决方案减小--batch-size降低--img-size使用更小的模型如yolov5s8. 高级技巧与优化8.1 数据增强策略在data/hyps/hyp.scratch-low.yaml中调整hsv_h: 0.015 # 色相增强幅度 hsv_s: 0.7 # 饱和度增强幅度 hsv_v: 0.4 # 明度增强幅度 degrees: 5 # 旋转角度范围 translate: 0.1 # 平移比例8.2 模型微调技巧冻结骨干网络前10个epoch# 在train.py中添加 for k, v in model.named_parameters(): if backbone in k: v.requires_grad False自适应锚框计算python train.py --autoanchor9. 部署与应用9.1 模型导出导出为ONNX格式python export.py --weights runs/train/exp/weights/best.pt \ --include onnx --img 6409.2 推理测试使用转换后的模型进行预测import torch model torch.hub.load(ultralytics/yolov5, custom, pathruns/train/exp/weights/best.pt) results model(test.jpg) results.show()10. 项目实战建议标注质量控制建议至少标注1000张图像作为起点复杂场景建议3人交叉验证标注迭代优化流程graph LR A[初始标注] -- B[训练模型] B -- C[测试发现bad case] C -- D[补充标注] D -- B性能评估指标mAP0.5基础指标mAP0.5:0.95严格指标推理速度(FPS)实际部署关键这套流程在工业缺陷检测项目中帮助我们将识别准确率从初始的78%提升到94%。关键点在于持续迭代标注数据和调整模型参数。当遇到性能瓶颈时建议优先检查标注质量而非盲目调整模型结构。
返回列表