Skip to content

TensorRT Export for YOLOv8 Models

Deploying computer vision models in high-performance environments can require a format that maximizes speed and efficiency. This is especially true when you are deploying your model on NVIDIA GPUs.

By using the TensorRT export format, you can enhance your Ultralytics YOLOv8 models for swift and efficient inference on NVIDIA hardware. This guide will give you easy-to-follow steps for the conversion process and help you make the most of NVIDIA's advanced technology in your deep learning projects.

TensorRT

TensorRT Overview

TensorRT, developed by NVIDIA, is an advanced software development kit (SDK) designed for high-speed deep learning inference. It's well-suited for real-time applications like object detection.

This toolkit optimizes deep learning models for NVIDIA GPUs and results in faster and more efficient operations. TensorRT models undergo TensorRT optimization, which includes techniques like layer fusion, precision calibration (INT8 and FP16), dynamic tensor memory management, and kernel auto-tuning. Converting deep learning models into the TensorRT format allows developers to realize the potential of NVIDIA GPUs fully.

TensorRT is known for its compatibility with various model formats, including TensorFlow, PyTorch, and ONNX, providing developers with a flexible solution for integrating and optimizing models from different frameworks. This versatility enables efficient model deployment across diverse hardware and software environments.

Key Features of TensorRT Models

TensorRT models offer a range of key features that contribute to their efficiency and effectiveness in high-speed deep learning inference:

  • Precision Calibration: TensorRT supports precision calibration, allowing models to be fine-tuned for specific accuracy requirements. This includes support for reduced precision formats like INT8 and FP16, which can further boost inference speed while maintaining acceptable accuracy levels.

  • Layer Fusion: The TensorRT optimization process includes layer fusion, where multiple layers of a neural network are combined into a single operation. This reduces computational overhead and improves inference speed by minimizing memory access and computation.

TensorRT Layer Fusion

  • Dynamic Tensor Memory Management: TensorRT efficiently manages tensor memory usage during inference, reducing memory overhead and optimizing memory allocation. This results in more efficient GPU memory utilization.

  • Automatic Kernel Tuning: TensorRT applies automatic kernel tuning to select the most optimized GPU kernel for each layer of the model. This adaptive approach ensures that the model takes full advantage of the GPU's computational power.

Deployment Options in TensorRT

Before we look at the code for exporting YOLOv8 models to the TensorRT format, let's understand where TensorRT models are normally used.

TensorRT offers several deployment options, and each option balances ease of integration, performance optimization, and flexibility differently:

  • Deploying within TensorFlow: This method integrates TensorRT into TensorFlow, allowing optimized models to run in a familiar TensorFlow environment. It's useful for models with a mix of supported and unsupported layers, as TF-TRT can handle these efficiently.

TensorRT Overview

  • Standalone TensorRT Runtime API: Offers granular control, ideal for performance-critical applications. It's more complex but allows for custom implementation of unsupported operators.

  • NVIDIA Triton Inference Server: An option that supports models from various frameworks. Particularly suited for cloud or edge inference, it provides features like concurrent model execution and model analysis.

Exporting YOLOv8 Models to TensorRT

You can improve execution efficiency and optimize performance by converting YOLOv8 models to TensorRT format.

Installation

To install the required package, run:

Installation

# Install the required package for YOLOv8
pip install ultralytics

For detailed instructions and best practices related to the installation process, check our YOLOv8 Installation guide. While installing the required packages for YOLOv8, if you encounter any difficulties, consult our Common Issues guide for solutions and tips.

Usage

Before diving into the usage instructions, be sure to check out the range of YOLOv8 models offered by Ultralytics. This will help you choose the most appropriate model for your project requirements.

Usage

from ultralytics import YOLO

# Load the YOLOv8 model
model = YOLO("yolov8n.pt")

# Export the model to TensorRT format
model.export(format="engine")  # creates 'yolov8n.engine'

# Load the exported TensorRT model
tensorrt_model = YOLO("yolov8n.engine")

# Run inference
results = tensorrt_model("https://ultralytics.com/images/bus.jpg")
# Export a YOLOv8n PyTorch model to TensorRT format
yolo export model=yolov8n.pt format=engine  # creates 'yolov8n.engine''

# Run inference with the exported model
yolo predict model=yolov8n.engine source='https://ultralytics.com/images/bus.jpg'

For more details about the export process, visit the Ultralytics documentation page on exporting.

Exporting TensorRT with INT8 Quantization

Exporting Ultralytics YOLO models using TensorRT with INT8 precision executes post-training quantization (PTQ). TensorRT uses calibration for PTQ, which measures the distribution of activations within each activation tensor as the YOLO model processes inference on representative input data, and then uses that distribution to estimate scale values for each tensor. Each activation tensor that is a candidate for quantization has an associated scale that is deduced by a calibration process.

When processing implicitly quantized networks TensorRT uses INT8 opportunistically to optimize layer execution time. If a layer runs faster in INT8 and has assigned quantization scales on its data inputs and outputs, then a kernel with INT8 precision is assigned to that layer, otherwise TensorRT selects a precision of either FP32 or FP16 for the kernel based on whichever results in faster execution time for that layer.

Tip

It is critical to ensure that the same device that will use the TensorRT model weights for deployment is used for exporting with INT8 precision, as the calibration results can vary across devices.

Configuring INT8 Export

The arguments provided when using export for an Ultralytics YOLO model will greatly influence the performance of the exported model. They will also need to be selected based on the device resources available, however the default arguments should work for most Ampere (or newer) NVIDIA discrete GPUs. The calibration algorithm used is "ENTROPY_CALIBRATION_2" and you can read more details about the options available in the TensorRT Developer Guide. Ultralytics tests found that "ENTROPY_CALIBRATION_2" was the best choice and exports are fixed to using this algorithm.

  • workspace : Controls the size (in GiB) of the device memory allocation while converting the model weights.

    • Adjust the workspace value according to your calibration needs and resource availability. While a larger workspace may increase calibration time, it allows TensorRT to explore a wider range of optimization tactics, potentially enhancing model performance and accuracy. Conversely, a smaller workspace can reduce calibration time but may limit the optimization strategies, affecting the quality of the quantized model.

    • Default is workspace=4 (GiB), this value may need to be increased if calibration crashes (exits without warning).

    • TensorRT will report UNSUPPORTED_STATE during export if the value for workspace is larger than the memory available to the device, which means the value for workspace should be lowered.

    • If workspace is set to max value and calibration fails/crashes, consider reducing the values for imgsz and batch to reduce memory requirements.

    • Remember calibration for INT8 is specific to each device, borrowing a "high-end" GPU for calibration, might result in poor performance when inference is run on another device.

  • batch : The maximum batch-size that will be used for inference. During inference smaller batches can be used, but inference will not accept batches any larger than what is specified.

Note

During calibration, twice the batch size provided will be used. Using small batches can lead to inaccurate scaling during calibration. This is because the process adjusts based on the data it sees. Small batches might not capture the full range of values, leading to issues with the final calibration, so the batch size is doubled automatically. If no batch size is specified batch=1, calibration will be run at batch=1 * 2 to reduce calibration scaling errors.

Experimentation by NVIDIA led them to recommend using at least 500 calibration images that are representative of the data for your model, with INT8 quantization calibration. This is a guideline and not a hard requirement, and you will need to experiment with what is required to perform well for your dataset. Since the calibration data is required for INT8 calibration with TensorRT, make certain to use the data argument when int8=True for TensorRT and use data="my_dataset.yaml", which will use the images from validation to calibrate with. When no value is passed for data with export to TensorRT with INT8 quantization, the default will be to use one of the "small" example datasets based on the model task instead of throwing an error.

Example

from ultralytics import YOLO

model = YOLO("yolov8n.pt")
model.export(
    format="engine",
    dynamic=True,  # (1)!
    batch=8,  # (2)!
    workspace=4,  # (3)!
    int8=True,
    data="coco.yaml",  # (4)!
)

# Load the exported TensorRT INT8 model
model = YOLO("yolov8n.engine", task="detect")

# Run inference
result = model.predict("https://ultralytics.com/images/bus.jpg")
  1. Exports with dynamic axes, this will be enabled by default when exporting with int8=True even when not explicitly set. See export arguments for additional information.
  2. Sets max batch size of 8 for exported model, which calibrates with batch = 2 * 8 to avoid scaling errors during calibration.
  3. Allocates 4 GiB of memory instead of allocating the entire device for conversion process.
  4. Uses COCO dataset for calibration, specifically the images used for validation (5,000 total).
# Export a YOLOv8n PyTorch model to TensorRT format with INT8 quantization
yolo export model=yolov8n.pt format=engine batch=8 workspace=4 int8=True data=coco.yaml  # creates 'yolov8n.engine''

# Run inference with the exported TensorRT quantized model
yolo predict model=yolov8n.engine source='https://ultralytics.com/images/bus.jpg'
Calibration Cache

TensorRT will generate a calibration .cache which can be re-used to speed up export of future model weights using the same data, but this may result in poor calibration when the data is vastly different or if the batch value is changed drastically. In these circumstances, the existing .cache should be renamed and moved to a different directory or deleted entirely.

Advantages of using YOLO with TensorRT INT8

  • Reduced model size: Quantization from FP32 to INT8 can reduce the model size by 4x (on disk or in memory), leading to faster download times. lower storage requirements, and reduced memory footprint when deploying a model.

  • Lower power consumption: Reduced precision operations for INT8 exported YOLO models can consume less power compared to FP32 models, especially for battery-powered devices.

  • Improved inference speeds: TensorRT optimizes the model for the target hardware, potentially leading to faster inference speeds on GPUs, embedded devices, and accelerators.

Note on Inference Speeds

The first few inference calls with a model exported to TensorRT INT8 can be expected to have longer than usual preprocessing, inference, and/or postprocessing times. This may also occur when changing imgsz during inference, especially when imgsz is not the same as what was specified during export (export imgsz is set as TensorRT "optimal" profile).

Drawbacks of using YOLO with TensorRT INT8

  • Decreases in evaluation metrics: Using a lower precision will mean that mAP, Precision, Recall or any other metric used to evaluate model performance is likely to be somewhat worse. See the Performance results section to compare the differences in mAP50 and mAP50-95 when exporting with INT8 on small sample of various devices.

  • Increased development times: Finding the "optimal" settings for INT8 calibration for dataset and device can take a significant amount of testing.

  • Hardware dependency: Calibration and performance gains could be highly hardware dependent and model weights are less transferable.

Ultralytics YOLO TensorRT Export Performance

NVIDIA A100

Performance

Tested with Ubuntu 22.04.3 LTS, python 3.10.12, ultralytics==8.2.4, tensorrt==8.6.1.post1

See Detection Docs for usage examples with these models trained on COCO, which include 80 pre-trained classes.

Note

Inference times shown for mean, min (fastest), and max (slowest) for each test using pre-trained weights yolov8n.engine

PrecisionEval testmean
(ms)
min | max
(ms)
mAPval
50(B)
mAPval
50-95(B)
batchsize
(pixels)
FP32Predict0.520.51 | 0.568640
FP32COCOval0.520.520.371640
FP16Predict0.340.34 | 0.418640
FP16COCOval0.330.520.371640
INT8Predict0.280.27 | 0.318640
INT8COCOval0.290.470.331640

See Segmentation Docs for usage examples with these models trained on COCO, which include 80 pre-trained classes.

Note

Inference times shown for mean, min (fastest), and max (slowest) for each test using pre-trained weights yolov8n-seg.engine

PrecisionEval testmean
(ms)
min | max
(ms)
mAPval
50(B)
mAPval
50-95(B)
mAPval
50(M)
mAPval
50-95(M)
batchsize
(pixels)
FP32Predict0.620.61 | 0.688640
FP32COCOval0.630.520.360.490.311640
FP16Predict0.400.39 | 0.448640
FP16COCOval0.430.520.360.490.301640
INT8Predict0.340.33 | 0.378640
INT8COCOval0.360.460.320.430.271640

See Classification Docs for usage examples with these models trained on ImageNet, which include 1000 pre-trained classes.

Note

Inference times shown for mean, min (fastest), and max (slowest) for each test using pre-trained weights yolov8n-cls.engine

PrecisionEval testmean
(ms)
min | max
(ms)
top-1top-5batchsize
(pixels)
FP32Predict0.260.25 | 0.288640
FP32ImageNetval0.260.350.611640
FP16Predict0.180.17 | 0.198640
FP16ImageNetval0.180.350.611640
INT8Predict0.160.15 | 0.578640
INT8ImageNetval0.150.320.591640

See Pose Estimation Docs for usage examples with these models trained on COCO, which include 1 pre-trained class, "person".

Note

Inference times shown for mean, min (fastest), and max (slowest) for each test using pre-trained weights yolov8n-pose.engine

PrecisionEval testmean
(ms)
min | max
(ms)
mAPval
50(B)
mAPval
50-95(B)
mAPval
50(P)
mAPval
50-95(P)
batchsize
(pixels)
FP32Predict0.540.53 | 0.588640
FP32COCOval0.550.910.690.800.511640
FP16Predict0.370.35 | 0.418640
FP16COCOval0.360.910.690.800.511640
INT8Predict0.290.28 | 0.338640
INT8COCOval0.300.900.680.780.471640

See Oriented Detection Docs for usage examples with these models trained on DOTAv1, which include 15 pre-trained classes.

Note

Inference times shown for mean, min (fastest), and max (slowest) for each test using pre-trained weights yolov8n-obb.engine

PrecisionEval testmean
(ms)
min | max
(ms)
mAPval
50(B)
mAPval
50-95(B)
batchsize
(pixels)
FP32Predict0.520.51 | 0.598640
FP32DOTAv1val0.760.500.361640
FP16Predict0.340.33 | 0.428640
FP16DOTAv1val0.590.500.361640
INT8Predict0.290.28 | 0.338640
INT8DOTAv1val0.320.450.321640

Consumer GPUs

Detection Performance (COCO)

Tested with Windows 10.0.19045, python 3.10.9, ultralytics==8.2.4, tensorrt==10.0.0b6

Note

Inference times shown for mean, min (fastest), and max (slowest) for each test using pre-trained weights yolov8n.engine

PrecisionEval testmean
(ms)
min | max
(ms)
mAPval
50(B)
mAPval
50-95(B)
batchsize
(pixels)
FP32Predict1.060.75 | 1.888640
FP32COCOval1.370.520.371640
FP16Predict0.620.75 | 1.138640
FP16COCOval0.850.520.371640
INT8Predict0.520.38 | 1.008640
INT8COCOval0.740.470.331640

Tested with Windows 10.0.22631, python 3.11.9, ultralytics==8.2.4, tensorrt==10.0.1

Note

Inference times shown for mean, min (fastest), and max (slowest) for each test using pre-trained weights yolov8n.engine

PrecisionEval testmean
(ms)
min | max
(ms)
mAPval
50(B)
mAPval
50-95(B)
batchsize
(pixels)
FP32Predict1.761.69 | 1.878640
FP32COCOval1.940.520.371640
FP16Predict0.860.75 | 1.008640
FP16COCOval1.430.520.371640
INT8Predict0.800.75 | 1.008640
INT8COCOval1.350.470.331640

Tested with Pop!_OS 22.04 LTS, python 3.10.12, ultralytics==8.2.4, tensorrt==8.6.1.post1

Note

Inference times shown for mean, min (fastest), and max (slowest) for each test using pre-trained weights yolov8n.engine

PrecisionEval testmean
(ms)
min | max
(ms)
mAPval
50(B)
mAPval
50-95(B)
batchsize
(pixels)
FP32Predict2.842.84 | 2.858640
FP32COCOval2.940.520.371640
FP16Predict1.091.09 | 1.108640
FP16COCOval1.200.520.371640
INT8Predict0.750.74 | 0.758640
INT8COCOval0.760.470.331640

Embedded Devices

Detection Performance (COCO)

Tested with JetPack 6.0 (L4T 36.3) Ubuntu 22.04.4 LTS, python 3.10.12, ultralytics==8.2.16, tensorrt==10.0.1

Note

Inference times shown for mean, min (fastest), and max (slowest) for each test using pre-trained weights yolov8n.engine

PrecisionEval testmean
(ms)
min | max
(ms)
mAPval
50(B)
mAPval
50-95(B)
batchsize
(pixels)
FP32Predict6.116.10 | 6.298640
FP32COCOval6.170.520.371640
FP16Predict3.183.18 | 3.208640
FP16COCOval3.190.520.371640
INT8Predict2.302.29 | 2.358640
INT8COCOval2.320.460.321640

Info

See our quickstart guide on NVIDIA Jetson with Ultralytics YOLO to learn more about setup and configuration.

Evaluation methods

Expand sections below for information on how these models were exported and tested.

Export configurations

See export mode for details regarding export configuration arguments.

from ultralytics import YOLO

model = YOLO("yolov8n.pt")

# TensorRT FP32
out = model.export(format="engine", imgsz=640, dynamic=True, verbose=False, batch=8, workspace=2)

# TensorRT FP16
out = model.export(format="engine", imgsz=640, dynamic=True, verbose=False, batch=8, workspace=2, half=True)

# TensorRT INT8 with calibration `data` (i.e. COCO, ImageNet, or DOTAv1 for appropriate model task)
out = model.export(
    format="engine", imgsz=640, dynamic=True, verbose=False, batch=8, workspace=2, int8=True, data="coco8.yaml"
)
Predict loop

See predict mode for additional information.

import cv2

from ultralytics import YOLO

model = YOLO("yolov8n.engine")
img = cv2.imread("path/to/image.jpg")

for _ in range(100):
    result = model.predict(
        [img] * 8,  # batch=8 of the same image
        verbose=False,
        device="cuda",
    )
Validation configuration

See val mode to learn more about validation configuration arguments.

from ultralytics import YOLO

model = YOLO("yolov8n.engine")
results = model.val(
    data="data.yaml",  # COCO, ImageNet, or DOTAv1 for appropriate model task
    batch=1,
    imgsz=640,
    verbose=False,
    device="cuda",
)

Deploying Exported YOLOv8 TensorRT Models

Having successfully exported your Ultralytics YOLOv8 models to TensorRT format, you're now ready to deploy them. For in-depth instructions on deploying your TensorRT models in various settings, take a look at the following resources:

Summary

In this guide, we focused on converting Ultralytics YOLOv8 models to NVIDIA's TensorRT model format. This conversion step is crucial for improving the efficiency and speed of YOLOv8 models, making them more effective and suitable for diverse deployment environments.

For more information on usage details, take a look at the TensorRT official documentation.

If you're curious about additional Ultralytics YOLOv8 integrations, our integration guide page provides an extensive selection of informative resources and insights.

FAQ

How do I convert YOLOv8 models to TensorRT format?

To convert your Ultralytics YOLOv8 models to TensorRT format for optimized NVIDIA GPU inference, follow these steps:

  1. Install the required package:

    pip install ultralytics
    
  2. Export your YOLOv8 model:

    from ultralytics import YOLO
    
    model = YOLO("yolov8n.pt")
    model.export(format="engine")  # creates 'yolov8n.engine'
    
    # Run inference
    model = YOLO("yolov8n.engine")
    results = model("https://ultralytics.com/images/bus.jpg")
    

For more details, visit the YOLOv8 Installation guide and the export documentation.

What are the benefits of using TensorRT for YOLOv8 models?

Using TensorRT to optimize YOLOv8 models offers several benefits:

  • Faster Inference Speed: TensorRT optimizes the model layers and uses precision calibration (INT8 and FP16) to speed up inference without significantly sacrificing accuracy.
  • Memory Efficiency: TensorRT manages tensor memory dynamically, reducing overhead and improving GPU memory utilization.
  • Layer Fusion: Combines multiple layers into single operations, reducing computational complexity.
  • Kernel Auto-Tuning: Automatically selects optimized GPU kernels for each model layer, ensuring maximum performance.

For more information, explore the detailed features of TensorRT here and read our TensorRT overview section.

Can I use INT8 quantization with TensorRT for YOLOv8 models?

Yes, you can export YOLOv8 models using TensorRT with INT8 quantization. This process involves post-training quantization (PTQ) and calibration:

  1. Export with INT8:

    from ultralytics import YOLO
    
    model = YOLO("yolov8n.pt")
    model.export(format="engine", batch=8, workspace=4, int8=True, data="coco.yaml")
    
  2. Run inference:

    from ultralytics import YOLO
    
    model = YOLO("yolov8n.engine", task="detect")
    result = model.predict("https://ultralytics.com/images/bus.jpg")
    

For more details, refer to the exporting TensorRT with INT8 quantization section.

How do I deploy YOLOv8 TensorRT models on an NVIDIA Triton Inference Server?

Deploying YOLOv8 TensorRT models on an NVIDIA Triton Inference Server can be done using the following resources:

These guides will help you integrate YOLOv8 models efficiently in various deployment environments.

What are the performance improvements observed with YOLOv8 models exported to TensorRT?

Performance improvements with TensorRT can vary based on the hardware used. Here are some typical benchmarks:

  • NVIDIA A100:

    • FP32 Inference: ~0.52 ms / image
    • FP16 Inference: ~0.34 ms / image
    • INT8 Inference: ~0.28 ms / image
    • Slight reduction in mAP with INT8 precision, but significant improvement in speed.
  • Consumer GPUs (e.g., RTX 3080):

    • FP32 Inference: ~1.06 ms / image
    • FP16 Inference: ~0.62 ms / image
    • INT8 Inference: ~0.52 ms / image

Detailed performance benchmarks for different hardware configurations can be found in the performance section.

For more comprehensive insights into TensorRT performance, refer to the Ultralytics documentation and our performance analysis reports.

📅 Created 9 months ago ✏️ Updated 1 month ago

Comments