本文介绍如何使用Ultralytics yolo做图片检测,如何使用roboflow做数据标注,并导出yolo格式数据集,并训练本地模型,使用训练好的模型做图片检测,并输入程序需要的结果数据。
一、什么是Ultralytics yolo
Ultralytics YOLO 是广受赞誉的 YOLO(You Only Look Once)系列目标检测模型的最新进展,由 Ultralytics 公司开发和维护。YOLO 是一种实时目标检测算法,其核心思想是将目标检测视为一个回归问题,通过单次前向传播即可同时预测图像中物体的位置和类别,具有高效性和高准确性的特点。
Ultralytics YOLO 的主要特点
- 实时性能:YOLO 系列以其高速检测能力著称,适用于需要实时处理的应用,如视频监控、自动驾驶等。
- 多任务支持:Ultralytics YOLO 不仅支持目标检测,还支持图像分割、姿态估计、目标跟踪和分类等多种视觉 AI 任务。
- 版本演进:
- YOLOv5:由 Ultralytics 开发,提供多种模型规模(如 YOLOv5s、YOLOv5m、YOLOv5l、YOLOv5x),以适应不同硬件需求。
- YOLOv8:进一步提升了性能、灵活性和效率,支持全方位的视觉 AI 任务。
- YOLOv9 & YOLOv10:引入了可编程梯度信息(PGI)和广义高效层聚合网络(GELAN)等创新方法。
- YOLOv11:Ultralytics 的最新版本,在检测、分割、姿态估计、跟踪和分类等任务中提供最先进的(SOTA)性能。
易用性:Ultralytics 提供了详细的文档、预训练模型和训练脚本,支持 PyTorch 等深度学习框架,便于开发者快速部署。
应用场景
安防监控:实时检测异常行为或特定目标。
自动驾驶:检测行人、车辆和交通标志。
工业检测:产品质量检测,如电子元件焊接缺陷识别。
医疗影像:辅助诊断和分析。
二、快速入门
1、使用pip安装依赖包
安装或更新 ultralytics
运行 pip install -U ultralytics
# Install the ultralytics package from PyPI
pip install ultralytics
您还可以安装 ultralytics 直接从 Ultralytics GitHub 存储库.如果您需要最新的开发版本,这将非常有用。确保已安装 Git 命令行工具,然后运行
# Install the ultralytics package from GitHub
pip install git+https://github.com/ultralytics/ultralytics.git@main
2、模型训练及使用
from ultralytics import YOLO
# Create a new YOLO model from scratch
model = YOLO("yolo11n.yaml")
# Load a pretrained YOLO model (recommended for training)
model = YOLO("yolo11n.pt")
# Train the model using the 'coco8.yaml' dataset for 3 epochs
results = model.train(data="coco8.yaml", epochs=3)
# Evaluate the model's performance on the validation set
results = model.val()
# Perform object detection on an image using the model
results = model("https://ultralytics.com/images/bus.jpg")
# Export the model to ONNX format
success = model.export(format="onnx")
三、使用roboflow做数据标注
Roboflow 是一个专为计算机视觉开发者设计的平台,旨在简化从数据标注到模型训练和部署的全流程。它提供了一系列工具和服务,帮助用户高效地创建、管理和增强图像数据集,并支持多种主流深度学习框架(如 YOLO、TensorFlow 等)。
1、创建数据标注项目
打开 roboflow官网 注册账号并登录。
输入项目名称后,点击创建项目
2、上传需要标注的图片
3、点击 Annotate 对图片进行标注
4、点击 Version 创建新的版本
可以在 preprocessing 中设置预处理步骤
5、下载指定格式的数据集
点击 “Download Dataset”,下载指定格式的数据集,这里选择的是“YOLOv8”,下载下来是一个zip压缩包,在yolo的Python项目中解压就可以直接用来训练模型了
解压zip压缩包,得到大致如下结构的文件,包含“test、train、valid
”三个数据集。
训练时,只要使用目录中的data.yaml
文件就可以
6、使用yolo训练模型
from ultralytics import YOLO
# 加载基础模型
model = YOLO('yolov8n.pt')
# 训练模型
results = model.train(
data='C:\\workspace\\py\\yolo_demo\\datasets\\Weixin.v7i.yolov8\data.yaml', # 数据集配置文件
epochs=100, # 训练轮次
batch=16, # 批次大小
name='custom_model' # 实验名称
)
执行完成后,大致输出如下日志:
100 epochs completed in 0.994 hours.
Optimizer stripped from runs\detect\custom_model\weights\last.pt, 6.3MB
Optimizer stripped from runs\detect\custom_model\weights\best.pt, 6.3MB
Validating runs\detect\custom_model\weights\best.pt...
Ultralytics 8.3.99 🚀 Python-3.10.1 torch-2.6.0+cpu CPU (11th Gen Intel Core(TM) i7-11800H 2.30GHz)
Model summary (fused): 72 layers, 3,006,623 parameters, 0 gradients, 8.1 GFLOPs
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 1/1 [00:02<00:00, 2.79s/it]
all 25 61 0.879 0.921 0.964 0.784
article 18 40 0.949 0.927 0.977 0.934
card 9 16 0.916 0.678 0.859 0.782
icon_delete 1 1 0.798 1 0.995 0.398
icon_search 3 3 0.776 1 0.995 0.912
icon_soso 1 1 0.958 1 0.995 0.895
Speed: 5.4ms preprocess, 93.0ms inference, 0.0ms loss, 5.5ms postprocess per image
Results saved to runs\detect\custom_model
训练完成后,在目录 runs/detect/custom_model/weights/best.pt
中生成训练好的模型文件
7、使用训练好的模型探测图片
from ultralytics import YOLO
# 加载训练好的模型
model = YOLO('runs/detect/custom_model/weights/best.pt') # 使用最佳权重
# 或使用最后一次训练的权重
# model = YOLO('runs/detect/custom_model/weights/last.pt')
# 进行推理
results = model(['imgs/01.jpg', 'imgs/02.jpg', 'imgs/03.jpg'])
# Process results list
idx = 1
for result in results:
boxes = result.boxes # 包含检测边界框的方框对象
masks = result.masks # 包含检测掩码的掩码对象
keypoints = result.keypoints # 关键点对象,包含每个对象的检测关键点
probs = result.probs # Probs 对象,包含分类任务中每个类别的概率
obb = result.obb # 包含定向包围盒的 OBB 对象
print("-" * 20)
print(result.to_json())
result.show() # 显示带有推理结果注释的图像
filname = f"result/result{idx}.jpg"
result.save(filename=filname) # 将注释的推理结果图像保存到文件并返回文件名
idx = idx + 1
输出的检测结果json文件,大致如下:
[
{
"name": "article",
"class": 0,
"confidence": 0.96316,
"box": {
"x1": 0.0,
"y1": 571.20105,
"x2": 1080.0,
"y2": 936.76697
}
},
{
"name": "article",
"class": 0,
"confidence": 0.93476,
"box": {
"x1": 0.0,
"y1": 939.69043,
"x2": 1080.0,
"y2": 1365.90405
}
}
]
- name:标注的标签名称
- confidence:相识度
- box:方框的左上角和右下角的坐标点
这里的json格式化检测结果,就是我们想要的数据,可以在其他程序中配合使用