This repository provides a pipeline for running inference with a ResNet model using TensorRT. The pipeline includes steps to export the model to ONNX, build a TensorRT engine, and perform inference with visualization. It supports both .pt (PyTorch) and .engine (TensorRT) models. This README guides you through the setup, directory structure, and execution process.
- Overview
- Directory Structure
- Configuration File
- Execution Order
- Running with Docker
- Optimizing the Dockerfile
- Notes
This pipeline processes images using a ResNet model, either directly with a PyTorch .pt file or with a TensorRT .engine file for optimized inference. The pipeline includes visualization of predictions and saves results to a YAML file (predictions.yaml).
- Export a PyTorch
.ptmodel to ONNX. - Build a TensorRT engine from the ONNX model.
- Perform inference with visualization using either the
.ptor.enginemodel. - Configurable via a YAML file, with support for bind mounting in Docker.
The expected directory structure on the host machine is as follows:
/home/user/
├── new_config.yaml # Configuration file to override defaults
├── output/ # Directory to store predictions.yaml
├── engine.trt # TensorRT engine file
├── images/ # Directory containing input images
└── train/ # Directory containing training data
The container maps these directories using Docker bind mounts:
/home/user/new_config.yaml→/app/config/config.yaml/home/user/engine.trt→/data/engine.trt/home/user/images→/data/images/home/user/train→/data/train/home/user/output→/app/output
The pipeline uses a YAML configuration file (new_config.yaml) to specify paths and settings. Below is an example of new_config.yaml:
tensorrt:
engine_file_path: "/data/engine.trt"
inference:
image_dir: "/data/images"
image_size: [224, 224]
output_yaml: "/app/output/predictions.yaml"
preprocess_data:
train_dir: "/data/train"tensorrt.engine_file_path: Path to the TensorRT engine file inside the container.inference.image_dir: Directory containing input images for inference.inference.image_size: Size of images after preprocessing (e.g.,[224, 224]for ResNet).inference.output_yaml: Path where the inference results (predictions.yaml) will be saved.preprocess_data.train_dir: Directory containing training data (used for preprocessing if needed).
The pipeline consists of several scripts that must be run in the following order:
main.py: The entry point to execute the entire pipeline. It orchestrates the other scripts.export_resnet_to_onnx.py: Converts the PyTorch.ptmodel to ONNX format.build_trt_engine.py: Builds a TensorRT engine from the ONNX model.- Inference:
- If using a TensorRT engine (
.engine): Runinference_trt_engine.py. - If using the PyTorch model (
.pt): Runinference_resnet.py.
- If using a TensorRT engine (
inference_trt_engine.pyandinference_resnet.pyinclude visualization of predictions (e.g., drawing bounding boxes or class labels on images).- Results are saved to
/app/output/predictions.yaml(mapped to/home/user/output/predictions.yamlon the host).
The pipeline is containerized using Docker, leveraging NVIDIA TensorRT for GPU acceleration. Use the following command to run the pipeline with bind mounts:
docker run --gpus all \
-v /home/user/new_config.yaml:/app/config/config.yaml \
-v /home/user/engine.trt:/data/engine.trt \
-v /home/user/images:/data/images \
-v /home/user/train:/data/train \
-v /home/user/output:/app/output \
tensorrt-pipeline-v /home/user/new_config.yaml:/app/config/config.yaml: Overrides the default configuration file in the container with the host'snew_config.yaml.-v /home/user/engine.trt:/data/engine.trt: Mounts the TensorRT engine file from the host to the container.-v /home/user/images:/data/images: Mounts the directory containing input images.-v /home/user/train:/data/train: Mounts the directory containing training data.-v /home/user/output:/app/output: Mounts the output directory to savepredictions.yaml.
- The container reads the configuration from
/app/config/config.yaml. - Inference results are saved to
/app/output/predictions.yaml, which appears on the host at/home/user/output/predictions.yaml.
The default Dockerfile copies a config/ directory into the container. However, since the pipeline always mounts new_config.yaml from the host, you can optimize the Dockerfile by removing the COPY config/ step. Below is the minimal Dockerfile:
FROM nvcr.io/nvidia/tensorrt:25.03-py3
WORKDIR /app
RUN apt-get update && apt-get install -y \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip3 install --no-cache-dir -r requirements.txt
COPY src/ ./src/
CMD ["python3", "src/inference.py"]- Avoid Redundancy: If you always mount
new_config.yamlfrom the host, there's no need to copy a defaultconfig/directory into the image. - Smaller Image Size: Removing the
COPY config/step reduces the image size slightly.
- If you remove
COPY config/, you must mount a configuration file when running the container (e.g.,-v /home/user/new_config.yaml:/app/config/config.yaml). Otherwise, the script will fail because/app/config/config.yamlwill not exist.
- GPU Support: Ensure your host machine has an NVIDIA GPU and the NVIDIA Container Toolkit installed to use the
--gpus allflag. - File Paths: Double-check the paths in
new_config.yamlto match the container's filesystem (e.g.,/data/engine.trtinstead of/home/user/engine.trt). - Visualization: The inference scripts (
inference_trt_engine.pyandinference_resnet.py) include visualization. Ensure you have a display server (e.g., X11) if running visualization inside the container, or modify the scripts to save visualized images instead. - Fallback Option: If you don't have a TensorRT engine, you can use
inference_resnet.pyto run inference directly with the.ptmodel, though this will be slower than using the.enginefile.
-
Error: "Cannot find /app/config/config.yaml":
- Ensure you mounted the configuration file using
-v /home/user/new_config.yaml:/app/config/config.yaml. - If you modified the
Dockerfileto removeCOPY config/, this file must be mounted.
- Ensure you mounted the configuration file using
-
Error: "No images found in /data/images":
- Verify that the
/home/user/imagesdirectory on the host contains images and is correctly mounted.
- Verify that the
-
Error: "Failed to load engine.trt":
- Check that
/home/user/engine.trtexists on the host and is mounted correctly.
- Check that
For further assistance, please open an issue in the repository.