Image classification is the simplest of the three tasks and involves classifying an entire image into one of a set of predefined classes.
图像分类是这三项任务中最简单的一项,涉及将整个图像分类为一组预定义类别中的一个。
The output of an image classifier is a single class label and a confidence score. Image classification is useful when you need to know only what class an image belongs to and don't need to know where objects of that class are located or what their exact shape is.
图像分类器的输出是单个类别标签和置信度得分。当您只需要知道图像属于哪个类别,而不需要知道该类别的对象位于何处或其确切形状时,图像分类非常有用。
Watch: Explore Ultralytics YOLO Tasks: Image Classification using Ultralytics HUB
观看:探索Ultralytics YOLO任务:使用Ultralytics HUB进行图像分类
Tip 尖端
YOLOv8 Classify models use the -cls
suffix, i.e. yolov8n-cls.pt
and are pretrained on ImageNet.
YOLOv 8 Classify模型使用-cls
后缀,即yolov8n-cls.pt
,并在ImageNet上进行预训练。
Models 模型
YOLOv8 pretrained Classify models are shown here. Detect, Segment and Pose models are pretrained on the COCO dataset, while Classify models are pretrained on the ImageNet dataset.
这里显示了YOLOv 8预训练的Classify模型。检测、分割和姿态模型在COCO数据集上进行预训练,而分类模型在ImageNet数据集上进行预训练。
Models download automatically from the latest Ultralytics release on first use.
模型在首次使用时自动从最新的Ultralytics版本下载。
Model | size 大小 (pixels) (像素) | acc top1 | acc top5 | Speed 速度 CPU ONNX (ms) (毫秒) | Speed 速度 A100 TensorRT (ms) (毫秒) | params (M) (百万) | FLOPs (B) at 640 (B)在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 |
- acc values are model accuracies on the ImageNet dataset validation set.
acc值是ImageNet数据集验证集上的模型精度。
Reproduce byyolo val classify data=path/to/ImageNet device=0
复制者yolo val classify data=path/to/ImageNet device=0
- Speed averaged over ImageNet val images using an Amazon EC2 P4d instance.
使用Amazon EC2 P4 d实例的ImageNet瓦尔图像的平均速度。
Reproduce byyolo val classify data=path/to/ImageNet batch=1 device=0|cpu
复制者yolo val classify data=path/to/ImageNet batch=1 device=0|cpu
Train 火车
Train YOLOv8n-cls on the MNIST160 dataset for 100 epochs at image size 64. For a full list of available arguments see the Configuration page.
在MNIST 160数据集上训练YOLOv 8 n-cls 100个epoch,图像大小为64。有关可用参数的完整列表,请参见“配置”页。
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)
Dataset format 数据集格式
YOLO classification dataset format can be found in detail in the Dataset Guide.
YOLO分类数据集格式可以在数据集指南中找到详细信息。
Val 瓦尔
Validate trained YOLOv8n-cls model accuracy on the MNIST160 dataset. No arguments are needed as the model
retains its training data
and arguments as model attributes.
在MNIST 160数据集上训练YOLOv 8 n-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
# Validate the model
metrics = model.val() # no arguments needed, dataset and settings remembered
metrics.top1 # top1 accuracy
metrics.top5 # top5 accuracy
Predict 预测
Use a trained YOLOv8n-cls model to run predictions on images.
使用经过训练的YOLOv 8 n-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
See full predict
mode details in the Predict page.
在Predict页面中查看完整的预测
模式详情。
Export 出口
Export a YOLOv8n-cls model to a different format like ONNX, CoreML, etc.
将YOLOv 8 n-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")
Available YOLOv8-cls export formats are in the table below. You can export to any format using the format
argument, i.e. format='onnx'
or format='engine'
. You can predict or validate directly on exported models, i.e. yolo predict model=yolov8n-cls.onnx
. Usage examples are shown for your model after export completes.
可用的YOLOv 8-cls导出格式如下表所示。您可以使用format
参数导出为任何格式,即format ='onnx'
或format ='engine'
。您可以直接在导出的模型上进行预测或验证,即 yolo predict model=yolov8n-cls.onnx
。导出完成后,将显示模型的使用示例。
Format 格式 | format Argument格式 参数 | Model | Metadata 元数据 | Arguments 论点 |
---|---|---|---|---|
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 IMGSZ ,批次 |
TF Lite | tflite 特弗利特 | yolov8n-cls.tflite | ✅ | imgsz , half , int8 , batch |
TF Edge TPU | edgetpu 埃杰普 | yolov8n-cls_edgetpu.tflite | ✅ | imgsz 伊姆斯 |
TF.js | tfjs | yolov8n-cls_web_model/ | ✅ | imgsz , half , int8 , batch |
PaddlePaddle | paddle 桨 | yolov8n-cls_paddle_model/ yolov 8 n-cls_桨_模型/ | ✅ | imgsz , batch IMGSZ ,批次 |
NCNN | ncnn | yolov8n-cls_ncnn_model/ | ✅ | imgsz , half , batch |
See full export
details in the Export page.
在导出页面中查看完整的 export
详细信息。
FAQ
What is the purpose of YOLOv8 in image classification?
YOLOv 8在图像分类中的作用是什么?
YOLOv8 models, such as yolov8n-cls.pt
, are designed for efficient image classification. They assign a single class label to an entire image along with a confidence score. This is particularly useful for applications where knowing the specific class of an image is sufficient, rather than identifying the location or shape of objects within the image.
YOLOv 8模型,如yolov8n-cls.pt
,是为高效的图像分类而设计的。他们为整个图像分配一个单独的类别标签,沿着一个置信度得分。这对于知道图像的特定类别就足够的应用特别有用,而不是识别图像中对象的位置或形状。
How do I train a YOLOv8 model for image classification?
如何训练YOLOv 8模型进行图像分类?
To train a YOLOv8 model, you can use either Python or CLI commands. For example, to train a yolov8n-cls
model on the MNIST160 dataset for 100 epochs at an image size of 64:
要训练YOLOv 8模型,您可以使用Python或CLI命令。例如,要在MNIST 160数据集上训练yolov 8 n-cls
模型100个epoch,图像大小为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)
For more configuration options, visit the Configuration page.
有关更多配置选项,请访问配置页面。
Where can I find pretrained YOLOv8 classification models?
哪里可以找到预训练的YOLOv 8分类模型?
Pretrained YOLOv8 classification models can be found in the Models section. Models like yolov8n-cls.pt
, yolov8s-cls.pt
, yolov8m-cls.pt
, etc., are pretrained on the ImageNet dataset and can be easily downloaded and used for various image classification tasks.
预训练的YOLOv8分类模型可以在模型部分找到。例如yolov8n-cls.pt
、yolov8s-cls.pt
、yolov8m-cls.pt
等模型,在ImageNet数据集上进行预训练,可以轻松下载并用于各种图像分类任务。
How can I export a trained YOLOv8 model to different formats?
如何将经过训练的YOLOv 8模型导出为不同的格式?
You can export a trained YOLOv8 model to various formats using Python or CLI commands. For instance, to export a model to ONNX format:
您可以使用Python或CLI命令将经过训练的YOLOv8模型导出为各种格式。例如,要将模型导出为ONNX格式: