YOLOv11 Tutorial

This tutorial will guide you through using trtutils with YOLOv11 models. We will cover:

  1. Downloading ONNX weights from YOLOv11

  2. Building a TensorRT engine

  3. Running inference with the engine

Downloading ONNX Weights

YOLOv11 models can be automatically downloaded and converted to ONNX format using the trtutils CLI:

# Download and convert YOLOv11 models to ONNX
# Available models: yolov11n, yolov11s, yolov11m, yolov11l, yolov11x
$ python3 -m trtutils download --model yolov11n --output yolov11n.onnx --imgsz 640 --opset 17

# For other YOLOv11 variants
$ python3 -m trtutils download --model yolov11s --output yolov11s.onnx --imgsz 640 --opset 17

Building TensorRT Engine

Once you have the ONNX weights, build a TensorRT engine:

# build_yolo is an alias for the 'build' command with '--yolo' passed to it
python3 -m trtutils build_yolo \
    --onnx PATH_TO_WEIGHTS \
    --output PATH_TO_OUTPUT \
    --fp16 \
    --num_classes 80 \
    --iou_threshold 0.5 \
    --conf_threshold 0.25 \
    --top_k 100

Alternatively, if you want to export the engine using the Python API:

from trtutils.builder import build_engine, hooks

build_engine(
    onnx="yolov11.onnx",
    output="yolov11.engine",
    fp16=True,
    hooks=[hooks.yolo_efficient_nms_hook(
        num_classes=80,
        iou_threshold=0.5,
        conf_threshold=0.25,
        top_k=100,
    )]
)

Running Inference

The YOLO class provides a high-level interface for running YOLOv11 inference:

import cv2
from trtutils.models import YOLO, YOLOv11

# Load the YOLOv11 model
yolo = YOLO("yolov11.engine")

# OR, use the YOLOv11 class
yolo = YOLOv11("yolov11.engine")

# Read and process an image
img = cv2.imread("example.jpg")
detections = yolo.end2end(img)

# Print results
for bbox, confidence, class_id in detections:
    print(f"Class: {class_id}, Confidence: {confidence}")
    print(f"Bounding Box: {bbox}")