Skip to main content

Shenzhen, China

YOLOv8 Image Classifier

Using YOLOv8 to classify an entire image into one of a set of predefined classes.

Modelsize (pixels)acc top1acc top5Speed CPU ONNX (ms)Speed A100 TensorRT (ms)params (M)FLOPs (B) at 640
YOLOv8n-cls22466.687.012.90.312.74.3
YOLOv8s-cls22472.391.123.40.356.413.5
YOLOv8m-cls22476.493.285.40.6217.042.7
YOLOv8l-cls22478.094.1163.00.8737.599.7
YOLOv8x-cls22478.494.3232.01.0157.4154.8

Docker Environment

docker run --gpus all -ti --rm \
-v $(pwd):/opt/app -p 8888:8888 \
--name pytorch-jupyter \
pytorch-jupyter:latest

Training Results

top1_acctop5_accvalidation [it/s]inference [ms]prediction preprocess [ms]prediction inference [ms]prediction postprocess [ms]weights [MB]weights (ONNX) [MB]weights (TensorRT) [MB]
YOLOv8n0.7920.95486.500.23.6000002.7333330.0333332.9356995.7313917.929402
YOLOv8s0.8320.96591.720.25.3333334.5000000.1000009.89090019.61817426.030774
YOLOv8m0.8580.96987.900.40.4666675.0000000.06666730.32445560.39778463.700307
YOLOv8l0.8500.97372.360.80.5000007.3000000.06666769.331619138.311538166.345273
YOLOv8x0.8610.97153.221.12.16666714.4000000.100000107.382278214.371352225.140141

YOLOv8 Image Classifier

YOLOv8 Image Classifier

YOLOv8 Image Classifier

YOLOv8 Image Classifier

Model Export Options

Formatformat ArgumentModelMetadataArguments
PyTorch-yolov8n-cls.pt-
TorchScripttorchscriptyolov8n-cls.torchscriptimgsz, optimize
ONNXonnxyolov8n-cls.onnximgsz, half, dynamic, simplify, opset
OpenVINOopenvinoyolov8n-cls_openvino_model/imgsz, half
TensorRTengineyolov8n-cls.engineimgsz, half, dynamic, simplify, workspace
CoreMLcoremlyolov8n-cls.mlpackageimgsz, half, int8, nms
TF SavedModelsaved_modelyolov8n-cls_saved_model/imgsz, keras
TF GraphDefpbyolov8n-cls.pbimgsz
TF Litetfliteyolov8n-cls.tfliteimgsz, half, int8
TF Edge TPUedgetpuyolov8n-cls_edgetpu.tfliteimgsz
TF.jstfjsyolov8n-cls_web_model/imgsz
PaddlePaddlepaddleyolov8n-cls_paddle_model/imgsz
ncnnncnnyolov8n-cls_ncnn_model/imgsz, half
!pip install nvidia-tensorrt sng4onnx onnx_graphsurgeon onnx onnxsim onnxruntime-gpu # tensorflow tflite_support onnx2tf
from ultralytics import YOLO
Modelsize (pixels)acc top1acc top5Speed CPU ONNX (ms)Speed A100 TensorRT (ms)params (M)FLOPs (B) at 640
YOLOv8n-cls22466.687.012.90.312.74.3
YOLOv8s-cls22472.391.123.40.356.413.5
YOLOv8m-cls22476.493.285.40.6217.042.7
YOLOv8l-cls22478.094.1163.00.8737.599.7
YOLOv8x-cls22478.494.3232.01.0157.4154.8

YOLOv8n

model = YOLO('yolov8n-cls.pt')  # load a pretrained model (recommended for training)
# Train the model
results = model.train(data='./data/Flower_Dataset', epochs=20, imgsz=64)

Model Evaluation

YOLOv8 Image Classifier

# Load a model
model_n = YOLO('./runs/classify/train_yolov8n/weights/last.pt') # load a custom model

# Validate the model
metrics_n = model_n.val() # no arguments needed, dataset and settings remembered
print(metrics_n.top1) # top1 accuracy: 0.7915857434272766
print(metrics_n.top5) # top5 accuracy: 0.9543688893318176

YOLOv8 Image Classifier

Model Predictions

# Predict with the model
results_n = model_n('./assets/snapshots/Viola_Tricolor.jpg')

# image 1/1 /opt/app/assets/snapshots/Viola_Tricolor.jpg: 64x64 Viola 1.00, Aquilegia 0.00, Malvaceae 0.00, Helianthus_Annuus 0.00, Plumeria 0.00, 3.6ms
# Speed: 0.6ms preprocess, 3.6ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)
results_n = model_n('./assets/snapshots/Strelitzia.jpg')

# image 1/1 /opt/app/assets/snapshots/Strelitzia.jpg: 64x64 Strelitzia_Reginae 1.00, Alstroemeria 0.00, Guzmania 0.00, Rose 0.00, Anthurium_Andraeanum 0.00, 2.5ms
# Speed: 7.1ms preprocess, 2.5ms inference, 0.0ms postprocess per image at shape (1, 3, 64, 64)
results_n = model_n('./assets/snapshots/Water_Lilly.jpg')

# image 1/1 /opt/app/assets/snapshots/Water_Lilly.jpg: 64x64 Nymphaea_Tetragona 0.78, Dahlia 0.20, Rose 0.01, Alstroemeria 0.01, Antirrhinum 0.00, 3.1ms
# Speed: 6.6ms preprocess, 2.1ms inference, 0.0ms postprocess per image at shape (1, 3, 64, 64)

Model Export

Formatformat ArgumentModelMetadataArguments
PyTorch-yolov8n-cls.pt-
TorchScripttorchscriptyolov8n-cls.torchscriptimgsz, optimize
ONNXonnxyolov8n-cls.onnximgsz, half, dynamic, simplify, opset
OpenVINOopenvinoyolov8n-cls_openvino_model/imgsz, half
TensorRTengineyolov8n-cls.engineimgsz, half, dynamic, simplify, workspace
CoreMLcoremlyolov8n-cls.mlpackageimgsz, half, int8, nms
TF SavedModelsaved_modelyolov8n-cls_saved_model/imgsz, keras
TF GraphDefpbyolov8n-cls.pbimgsz
TF Litetfliteyolov8n-cls.tfliteimgsz, half, int8
TF Edge TPUedgetpuyolov8n-cls_edgetpu.tfliteimgsz
TF.jstfjsyolov8n-cls_web_model/imgsz
PaddlePaddlepaddleyolov8n-cls_paddle_model/imgsz
ncnnncnnyolov8n-cls_ncnn_model/imgsz, half
model_n.export(format='onnx')
model_n.export(format='engine')

YOLOv8s

model = YOLO('yolov8s-cls.pt')
results = model.train(data='./data/Flower_Dataset', epochs=20, imgsz=64)

Model Evaluation

YOLOv8 Image Classifier

# Load a model
model_s = YOLO('./runs/classify/train_yolov8s/weights/last.pt') # load a custom model

# Validate the model
metrics_s = model_s.val() # no arguments needed, dataset and settings remembered
print(metrics_s.top1) # top1 accuracy: 0.8323624134063721
print(metrics_s.top5) # top5 accuracy: 0.9650484919548035

YOLOv8 Image Classifier

Model Predictions

# Predict with the model
results_s = model_s('./assets/snapshots/Viola_Tricolor.jpg')

# image 1/1 /opt/app/assets/snapshots/Viola_Tricolor.jpg: 64x64 Viola 1.00, Primula 0.00, Malvaceae 0.00, Plumeria 0.00, Datura_Metel 0.00, 2.7ms
# Speed: 0.5ms preprocess, 2.7ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)
results_s = model_s('./assets/snapshots/Strelitzia.jpg')

# image 1/1 /opt/app/assets/snapshots/Strelitzia.jpg: 64x64 Strelitzia_Reginae 1.00, Helianthus_Annuus 0.00, Nymphaea_Tetragona 0.00, Plumeria 0.00, Crocus 0.00, 7.6ms
# Speed: 15.1ms preprocess, 7.6ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)
results_s = model_s('./assets/snapshots/Water_Lilly.jpg')

# image 1/1 /opt/app/assets/snapshots/Water_Lilly.jpg: 64x64 Nymphaea_Tetragona 0.99, Alstroemeria 0.01, Passiflora 0.00, Billbergia_Pyramidalis 0.00, Protea_Cynaroides 0.00, 3.2ms
# Speed: 0.4ms preprocess, 3.2ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)

Model Export

model_s.export(format='onnx')
model_s.export(format='engine')

YOLOv8m

model = YOLO('yolov8m-cls.pt')
results = model.train(data='./data/Flower_Dataset', epochs=20, imgsz=64)

Model Evaluation

YOLOv8 Image Classifier

# Load a model
model_m = YOLO('./runs/classify/train_yolov8m/weights/last.pt') # load a custom model

# Validate the model
metrics_m = model_m.val() # no arguments needed, dataset and settings remembered
print(metrics_m.top1) # top1 accuracy: 0.8579287528991699
print(metrics_m.top5) # top5 accuracy: 0.9692556262016296

YOLOv8 Image Classifier

Model Predictions

# Predict with the model
results_m = model_m('./assets/snapshots/Viola_Tricolor.jpg')

# image 1/1 /opt/app/assets/snapshots/Viola_Tricolor.jpg: 64x64 Viola 1.00, Iris_Pseudacorus 0.00, Primula 0.00, Cattleya 0.00, Helianthus_Annuus 0.00, 4.0ms
# Speed: 0.4ms preprocess, 4.0ms inference, 0.0ms postprocess per image at shape (1, 3, 64, 64)
results_m = model_m('./assets/snapshots/Strelitzia.jpg')

# image 1/1 /opt/app/assets/snapshots/Strelitzia.jpg: 64x64 Strelitzia_Reginae 1.00, Alpinia_Purpurata 0.00, Zantedeschia_Aethiopica 0.00, Helianthus_Annuus 0.00, Plumeria 0.00, 6.0ms
# Speed: 0.5ms preprocess, 6.0ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)
results_m = model_m('./assets/snapshots/Water_Lilly.jpg')

# image 1/1 /opt/app/assets/snapshots/Water_Lilly.jpg: 64x64 Nymphaea_Tetragona 1.00, Dahlia 0.00, Alstroemeria 0.00, Passiflora 0.00, Guzmania 0.00, 5.0ms
# Speed: 0.5ms preprocess, 5.0ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)

Model Export

model_m.export(format='onnx')
model_m.export(format='engine')

YOLOv8l

model = YOLO('yolov8l-cls.pt')
results = model.train(data='./data/Flower_Dataset', epochs=20, imgsz=64)

Model Evaluation

YOLOv8 Image Classifier

# Load a model
model_l = YOLO('./runs/classify/train_yolov8l/weights/last.pt') # load a custom model

# Validate the model
metrics_l = model_l.val() # no arguments needed, dataset and settings remembered
print(metrics_l.top1) # top1 accuracy: 0.849838137626648
print(metrics_l.top5) # top5 accuracy: 0.9731391072273254

YOLOv8 Image Classifier

Model Predictions

# Predict with the model
results_l = model_l('./assets/snapshots/Viola_Tricolor.jpg')

# image 1/1 /opt/app/assets/snapshots/Viola_Tricolor.jpg: 64x64 Viola 1.00, Malvaceae 0.00, Iris_Pseudacorus 0.00, Primula 0.00, Paphiopedilum 0.00, 6.3ms
# Speed: 0.5ms preprocess, 6.3ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)
results_l = model_l('./assets/snapshots/Strelitzia.jpg')

# image 1/1 /opt/app/assets/snapshots/Strelitzia.jpg: 64x64 Strelitzia_Reginae 1.00, Anthurium_Andraeanum 0.00, Tropaeolum_Majus 0.00, Alstroemeria 0.00, Zantedeschia_Aethiopica 0.00, 8.0ms
# Speed: 0.6ms preprocess, 8.0ms inference, 0.0ms postprocess per image at shape (1, 3, 64, 64)
results_l = model_l('./assets/snapshots/Water_Lilly.jpg')

# image 1/1 /opt/app/assets/snapshots/Water_Lilly.jpg: 64x64 Nymphaea_Tetragona 1.00, Rose 0.00, Primula 0.00, Dahlia 0.00, Alstroemeria 0.00, 7.6ms
# Speed: 0.4ms preprocess, 7.6ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)

Model Export

model_l.export(format='onnx')
model_l.export(format='engine')

YOLOv8x

model = YOLO('yolov8x-cls.pt')
results = model.train(data='./data/Flower_Dataset', epochs=20, imgsz=64)

Model Evaluation

YOLOv8 Image Classifier

# Load a model
model_x = YOLO('./runs/classify/train_yolov8x/weights/last.pt') # load a custom model

# Validate the model
metrics_x = model_x.val() # no arguments needed, dataset and settings remembered
print(metrics_x.top1) # top1 accuracy: 0.8605177402496338
print(metrics_x.top5) # top5 accuracy: 0.9708737730979919


![YOLOv8 Image Classifier](./model_x_confusion_matrix_normalized.webp)

### Model Predictions

```python
# Predict with the model
results_x = model_x('./assets/snapshots/Viola_Tricolor.jpg')

# image 1/1 /opt/app/assets/snapshots/Viola_Tricolor.jpg: 64x64 Viola 1.00, Dahlia 0.00, Tropaeolum_Majus 0.00, Rose 0.00, Primula 0.00, 10.1ms
# Speed: 0.5ms preprocess, 10.1ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)
results_x = model_x('./assets/snapshots/Strelitzia.jpg')

# image 1/1 /opt/app/assets/snapshots/Strelitzia.jpg: 64x64 Strelitzia_Reginae 1.00, Tropaeolum_Majus 0.00, Alstroemeria 0.00, Rose 0.00, Helianthus_Annuus 0.00, 24.0ms
# Speed: 5.5ms preprocess, 24.0ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)
results_x = model_x('./assets/snapshots/Water_Lilly.jpg')

# image 1/1 /opt/app/assets/snapshots/Water_Lilly.jpg: 64x64 Nymphaea_Tetragona 0.99, Rose 0.00, Alstroemeria 0.00, Primula 0.00, Protea_Cynaroides 0.00, 9.1ms
# Speed: 0.5ms preprocess, 9.1ms inference, 0.1ms postprocess per image at shape (1, 3, 64, 64)

Model Export

model_x.export(format='onnx')
model_x.export(format='engine')