跳至内容

图像分类

图像分类示例

图像分类是三项任务中最简单的一项,涉及将整幅图像归入一组预定义类别中的某一类。

图像分类器的输出是单一类别标签和置信度分数。当你只需要知道图像属于哪一类,而不需要知道该类对象的位置或确切形状时,图像分类就非常有用。



观看: 探索Ultralytics YOLO 任务:使用Ultralytics HUB 进行图像分类

提示

YOLOv8 分类模型使用 -cls 后缀,即 yolov8n-cls.pt 并对 图像网.

机型

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

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

模型 尺寸
(像素)
acc
top1
acc
top5
速度
CPU ONNX
(毫秒)
速度
A100 TensorRT
(毫秒)
params
(M)
FLOPs
(B) at 640
YOLOv8n-cls 224 69.0 88.3 12.9 0.31 2.7 4.3
YOLOv8s-cls 224 73.8 91.7 23.4 0.35 6.4 13.5
YOLOv8m-cls 224 76.8 93.5 85.4 0.62 17.0 42.7
YOLOv8l-cls 224 76.8 93.5 163.0 0.87 37.5 99.7
YOLOv8x-cls 224 79.0 94.6 232.0 1.01 57.4 154.8
  • 口音 的模型精度。 图像网 数据集验证集。
    复制方式 yolo val classify data=path/to/ImageNet device=0
  • 速度 对 ImageNet val 图像进行平均。 亚马逊 EC2 P4d 实例
    复制方式 yolo val classify data=path/to/ImageNet batch=1 device=0|cpu

火车

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

示例

from ultralytics import YOLO

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

# Train the model
results = model.train(data="mnist160", epochs=100, imgsz=64)
# Build a new model from YAML and start training from scratch
yolo classify train data=mnist160 model=yolov8n-cls.yaml epochs=100 imgsz=64

# Start training from a pretrained *.pt model
yolo classify train data=mnist160 model=yolov8n-cls.pt epochs=100 imgsz=64

# Build a new model from YAML, transfer pretrained weights to it and start training
yolo classify train data=mnist160 model=yolov8n-cls.yaml pretrained=yolov8n-cls.pt epochs=100 imgsz=64

数据集格式

YOLO 分类数据集格式详见数据集指南

瓦尔

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

示例

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n-cls.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.top1  # top1 accuracy
metrics.top5  # top5 accuracy
yolo classify val model=yolov8n-cls.pt  # val official model
yolo classify val model=path/to/best.pt  # val custom model

预测

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

示例

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n-cls.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 classify predict model=yolov8n-cls.pt source='https://ultralytics.com/images/bus.jpg'  # predict with official model
yolo classify predict model=path/to/best.pt source='https://ultralytics.com/images/bus.jpg'  # predict with custom model

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

出口

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

示例

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n-cls.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-cls.pt format=onnx  # export official model
yolo export model=path/to/best.pt format=onnx  # export custom trained model

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

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

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

常见问题

YOLOv8 在图像分类中的作用是什么?

YOLOv8 模型,如 yolov8n-cls.pt是为高效图像分类而设计的。它们为整幅图像分配一个类别标签和置信度分数。这对于只需知道图像的具体类别,而无需识别图像中物体的位置或形状的应用特别有用。

如何训练用于图像分类的YOLOv8 模型?

要训练YOLOv8 模型,可以使用Python 或CLI 命令。例如,要训练一个 yolov8n-cls 模型在 MNIST160 数据集上进行了 100 次历时分析,图像大小为 64:

示例

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n-cls.pt")  # load a pretrained model (recommended for training)

# Train the model
results = model.train(data="mnist160", epochs=100, imgsz=64)
yolo classify train data=mnist160 model=yolov8n-cls.pt epochs=100 imgsz=64

有关更多配置选项,请访问配置页面。

在哪里可以找到经过预训练的YOLOv8 分类模型?

预训练的YOLOv8 分类模型可在 机型 节。机型,如 yolov8n-cls.pt, yolov8s-cls.pt, yolov8m-cls.pt等进行预训练。 图像网 数据集,可轻松下载并用于各种图像分类任务。

如何将训练好的YOLOv8 模型导出为不同格式?

您可以使用Python 或CLI 命令将训练好的YOLOv8 模型导出为各种格式。例如,将模型导出为ONNX 格式:

示例

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n-cls.pt")  # load the trained model

# Export the model to ONNX
model.export(format="onnx")
yolo export model=yolov8n-cls.pt format=onnx  # export the trained model to ONNX format

有关详细的导出选项,请参阅导出页面。

如何验证训练有素的YOLOv8 分类模型?

要在 MNIST160 这样的数据集上验证训练有素的模型的准确性,可以使用以下Python 或CLI 命令:

示例

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n-cls.pt")  # load the trained model

# Validate the model
metrics = model.val()  # no arguments needed, uses the dataset and settings from training
metrics.top1  # top1 accuracy
metrics.top5  # top5 accuracy
yolo classify val model=yolov8n-cls.pt  # validate the trained model

更多信息,请访问验证部分。



创建于 2023-11-12,更新于 2024-07-04
作者:glenn-jocher(18),Burhan-Q(4),RizwanMunawar(2),fcakyon(1),Laughing-q(1)

评论