TensorRT Export

Environment Setup

Prerequisites

  • OS: Linux, Windows (WSL2 is required!)
  • Python >=3.8
  • 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
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 = "844725944fc918e143e456d55a4c2c767e6767e3964dfae3398e3421fb9c52b9"
project_key = "proj_ae577bfac77e7c02912bb356fda76d9a"
run_id = "run_fe73e93f-7dc3-4432-895d-16c2227d26cd"

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
YOLOv9YOLOv8-SEGSegformerYOLOv8-POSEMoViNet
YOLOv8MaskRCNNDeepLabV3YOLOv8-CLS
FasterRCNNFCN
RetinaNetUNet
EfficientDet
MobileNet