跳至内容

物体检测

物体检测示例

物体检测是一项涉及识别图像或视频流中物体的位置和类别的任务。

物体检测器的输出是一组包围图像中物体的边框,以及每个边框的类标签和置信度分数。如果您需要识别场景中感兴趣的物体,但又不需要知道物体的具体位置或确切形状,那么物体检测就是一个不错的选择。



观看: 利用预训练的Ultralytics YOLOv8 模型进行物体检测。

提示

YOLOv8 检测模型是默认的YOLOv8 模型,即 yolov8n.pt 并对 COCO.

机型

YOLOv8 这里显示的是经过预训练的检测模型。Detect、Segment 和 Pose 模型是在COCO数据集上预先训练的,而 Classify 模型则是在ImageNet数据集上预先训练的。

首次使用时,模型会自动从最新的Ultralytics 版本下载。

模型 尺寸
(像素)
mAPval
50-95
速度
CPUONNX
(ms)
速度
A100 TensorRT
(毫秒)
params
(M)
FLOPs
(B)
YOLOv8n 640 37.3 80.4 0.99 3.2 8.7
YOLOv8s 640 44.9 128.4 1.20 11.2 28.6
YOLOv8m 640 50.2 234.7 1.83 25.9 78.9
YOLOv8l 640 52.9 375.2 2.39 43.7 165.2
YOLOv8x 640 53.9 479.1 3.53 68.2 257.8
  • mAPval 数值是在 COCO val2017 数据集。
    复制方式 yolo val detect data=coco.yaml device=0
  • 速度 对 COCO val 图像使用 亚马逊 EC2 P4d 实例
    复制方式 yolo val detect data=coco8.yaml batch=1 device=0|cpu

火车

在 COCO8 数据集上对YOLOv8n 进行 100 次历时训练,图像大小为 640。有关可用参数的完整列表,请参阅配置页面。

示例

from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.yaml')  # build a new model from YAML
model = YOLO('yolov8n.pt')  # load a pretrained model (recommended for training)
model = YOLO('yolov8n.yaml').load('yolov8n.pt')  # build from YAML and transfer weights

# Train the model
results = model.train(data='coco8.yaml', epochs=100, imgsz=640)
# Build a new model from YAML and start training from scratch
yolo detect train data=coco8.yaml model=yolov8n.yaml epochs=100 imgsz=640

# Start training from a pretrained *.pt model
yolo detect train data=coco8.yaml model=yolov8n.pt epochs=100 imgsz=640

# Build a new model from YAML, transfer pretrained weights to it and start training
yolo detect train data=coco8.yaml model=yolov8n.yaml pretrained=yolov8n.pt epochs=100 imgsz=640

数据集格式

YOLO 检测数据集格式详见数据集指南。要将现有数据集从其他格式(如 COCO 等)转换为YOLO 格式,请使用JSON2YOLO工具(Ultralytics )。

瓦尔

在 COCO8 数据集上验证训练有素的YOLOv8n 模型的准确性。无需传递参数,因为 model 保留培训 data 和参数作为模型属性。

示例

from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt')  # load an official model
model = YOLO('path/to/best.pt')  # load a custom model

# Validate the model
metrics = model.val()  # no arguments needed, dataset and settings remembered
metrics.box.map    # map50-95
metrics.box.map50  # map50
metrics.box.map75  # map75
metrics.box.maps   # a list contains map50-95 of each category
yolo detect val model=yolov8n.pt  # val official model
yolo detect val model=path/to/best.pt  # val custom model

预测

使用训练有素的YOLOv8n 模型对图像进行预测。

示例

from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt')  # load an official model
model = YOLO('path/to/best.pt')  # load a custom model

# Predict with the model
results = model('https://ultralytics.com/images/bus.jpg')  # predict on an image
yolo detect predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg'  # predict with official model
yolo detect predict model=path/to/best.pt source='https://ultralytics.com/images/bus.jpg'  # predict with custom model

查看全文 predict 模式的详细信息,请参见 预测 page.

出口

将YOLOv8n 模型导出为不同格式,如ONNX,CoreML 等。

示例

from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt')  # load an official model
model = YOLO('path/to/best.pt')  # load a custom trained model

# Export the model
model.export(format='onnx')
yolo export model=yolov8n.pt format=onnx  # export official model
yolo export model=path/to/best.pt format=onnx  # export custom trained model

YOLOv8 可用的导出格式如下表所示。您可以使用 format 参数,即 format='onnx'format='engine'.您可以直接对导出的模型进行预测或验证,即 yolo predict model=yolov8n.onnx.导出完成后会显示模型的使用示例。

格式 format 论据 模型 元数据 论据
PyTorch - yolov8n.pt -
TorchScript torchscript yolov8n.torchscript imgsz, optimize, batch
ONNX onnx yolov8n.onnx imgsz, half, dynamic, simplify, opset, batch
OpenVINO openvino yolov8n_openvino_model/ imgsz, half, int8, batch
TensorRT engine yolov8n.engine imgsz, half, dynamic, simplify, workspace, batch
CoreML coreml yolov8n.mlpackage imgsz, half, int8, nms, batch
TF SavedModel saved_model yolov8n_saved_model/ imgsz, keras, int8, batch
TF GraphDef pb yolov8n.pb imgsz, batch
TF 轻型 tflite yolov8n.tflite imgsz, half, int8, batch
TF 边缘TPU edgetpu yolov8n_edgetpu.tflite imgsz, batch
TF.js tfjs yolov8n_web_model/ imgsz, half, int8, batch
PaddlePaddle paddle yolov8n_paddle_model/ imgsz, batch
NCNN ncnn yolov8n_ncnn_model/ imgsz, half, batch

查看全文 export 中的详细信息 出口 page.



创建于 2023-11-12,更新于 2024-04-27
作者:glenn-jocher(14),Burhan-Q(1),Laughing-q(1),AyushExel(1)

评论