TensorRT Export

Environment Setup

Prerequisites

  • OS: Linux, Windows (WSL2 is required!)
  • 3.8 <= Python <= 3.12
  • A virtual environment is recommended, such as conda or virtualenvwrapper
  • CUDA >= 11.8

Install Packages

There are two main apt packages to be installed

Install and Configure Docker

sudo apt-get install docker.io
sudo groupadd docker ## you may need to restart your shell session after this step
sudo usermod -aG docker $USER

## test with this command, if no permission errors, Docker has been set up correctly
docker image list

Install and Configure nvidia-container-toolkit

curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | \
    sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.list | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit

sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker

Install Python Packages

colored==1.4.4
docker==7.0.0
numpy>=1.23.2,<2.0.0
nvidia-ml-py==12.560.30
opencv-python-headless==4.7.0.72
Pillow>=9.5.0
pywin32; platform_system=="Windows"
requests==2.31.0
tritonclient[all]==2.43.0
pip3 install -r requirements.txt

TensorRT

TensorRTConverter

Main TensorRT module for conversion and inference.

Arguments

AttributeTypeDescription
docker_paramsDockerParamsOptional Docker parameters for conversion and inference. If not provided, default settings will be used.

Examples

Initialize TensorRT converter with specified Docker image tags:

from datature.utils.experimental.convert import TensorRTConverter

docker_params = {
    "conversion_docker_image": "nvcr.io/nvidia/tensorflow:24.07-tf2-py3",
    "inference_docker_image": "nvcr.io/nvidia/tritonserver:24.07-py3"
}

trt = TensorRTConverter(docker_params=DockerParams(**docker_params))

Conversion

🚧

This is an experimental tool that is not guaranteed to support conversion of all model architectures. For maximum compatibility, use the conversion tool to export the model and convert instead of providing a local exported model path.

convert

Converts an ONNX model into a TensorRT plan file using the experimental conversion tool.

Arguments

AttributeTypeDescription
projectProjectDatature Project instance. This should be provided if you want to export and convert a specific model from your Nexus project.
artifact_idstrArtifact ID of the model to export. This should be provided if you want to export and conver a specific model from your Nexus project.
model_pathstrThe path to the saved model directory. This should be provided if you already have an exported model from Nexus that is stored on your local filesystem.
output_pathstrThe path to save the TensorRT plan file. This should have the structure of <MODEL_FOLDER>/<MODEL_NAME>/<VERSION_TAG>/model.plan
conversion_paramsConversionParamsExperimental conversion settings. This is for advanced users and will likely break the conversion. If not provided, default settings will be used.

Examples

Export and convert a model from Nexus project:

from datature.nexus import Client
from datature.utils.experimental.convert import TensorRTConverter

secret_key = "<YOUR_SECRET_KEY>"
project_key = "proj_<YOUR_PROJECT_KEY>"
run_id = "run_<YOUR_RUN_ID>"

client = Client(secret_key)
project = client.get_project(project_key)

artifact_id = project.artifacts.list(
    filters={"run_ids": [run_id]}
)[0]["id"]

trt = TensorRTConverter()

trt.convert(
    project,
    artifact_id,
    output_path="./trt_models/football/1/model.plan"
)

Convert a downloaded model on your local filesystem:

from datature.utils.experimental.convert import TensorRTConverter

trt = TensorRTConverter()

trt.convert(
    model_path="./models/model.onnx"
    output_path="./trt_models/football/1/model.plan"
)

Supported Model Architectures

Object DetectionInstance SegmentationSemantic SegmentationKeypoint DetectionClassification
YOLO11 [new!]YOLO11-SEG [new!]SegformerYOLO11-POSE [new!]YOLO11-CLS [new!]
YOLOv9YOLOv8-SEGDeepLabV3YOLOv8-POSEMoViNet
YOLOv8MaskRCNNFCNYOLOv8-CLS
FasterRCNNUNet
RetinaNet
EfficientDet
MobileNet