├── .gitignore ├── LICENSE ├── README.md ├── platforms ├── jetson-nano │ ├── Dockerfile │ ├── README.md │ ├── build.sh │ ├── files │ │ └── requirements.txt │ └── run-latest.sh └── x86_64 │ ├── Dockerfile │ ├── README.md │ ├── build.sh │ ├── files │ └── requirements.txt │ └── run-latest.sh └── tools ├── inference ├── classify-trt.py └── trtops │ └── tensorrt │ ├── __init__.py │ ├── trt_classify.py │ └── trt_engine.py └── onnx2trt └── onnx2trt.sh /.gitignore: -------------------------------------------------------------------------------- 1 | local/ 2 | models/ 3 | .pytest_cache/ 4 | 5 | *.egg-info/ 6 | .pkl_memoize_py3/ 7 | 8 | .DS_Store 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Paul Ryan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Inference Using Nvidia TensorRT 2 | 3 | This repository has tools and guidelines for converting ONNX models to [TensortRT](https://developer.nvidia.com/tensorrt) 4 | engines and running classification inference using the exported model. 5 | 6 | The tools include: 7 | 8 | * bash script wrapper for trtexe 9 | * running inference using the exported engine 10 | 11 | The `tools` directory contains the source code in python for the onnx2trt conversion and the inference. It builds 12 | on the tools in [inference-onnx](https://github.com/parlaynu/inference-onnx). Models converted to ONNX using the 13 | `inference-onnx` project can be used as input to the tools here. 14 | 15 | The `platforms` directory contains the tooling to build docker images with the tools and packages to 16 | run the conversion and inference. 17 | 18 | Each platform needs to do its own conversion as the TensorRT engine is a binary format matched to the GPU 19 | on the system. 20 | 21 | ## The Tools 22 | 23 | ### Convert ONNX to TensorRT 24 | 25 | This tools converts an ONNX model to a TensorRT engine. It is a wrapper script around the Nvidia tool `trtexec`. 26 | 27 | The full usage is: 28 | 29 | $ ./onnx2trt.sh 30 | Usage: onnx2trt.sh model.onnx 31 | 32 | The containers built by the platform tools mount a directory called `models` from the host file system which 33 | can be used as the source for ONNX model files. 34 | 35 | ### Running Inference 36 | 37 | The tool `classify-trt.py` runs inference on the exported TensorRT engine. The full usage is: 38 | 39 | $ ./classify-trt.py -h 40 | usage: classify-trt.py [-h] [-l LIMIT] [-r RATE] engine dataspec 41 | 42 | positional arguments: 43 | engine path to the tensorrt engine file 44 | dataspec the data source specification 45 | 46 | options: 47 | -h, --help show this help message and exit 48 | -l LIMIT, --limit LIMIT 49 | maximum number of images to process 50 | -r RATE, --rate RATE requests per second 51 | 52 | A simple run using a camera server from the `inference-onnx` project looks like this: 53 | 54 | $ ./classify-trt.py -l 10 ../models/resnet18-1x3x224x224.trt tcp://192.168.24.31:8089 55 | loading engine... 56 | - input shape: [1, 3, 224, 224] 57 | - output shape: [1, 1000] 58 | 00 image_0000 640x480x3 59 | 315 @ 51.36 60 | 01 image_0001 640x480x3 61 | 315 @ 25.14 62 | 02 image_0002 640x480x3 63 | 315 @ 50.38 64 | 03 image_0003 640x480x3 65 | 315 @ 37.04 66 | 04 image_0004 640x480x3 67 | 315 @ 27.28 68 | 05 image_0005 640x480x3 69 | 315 @ 46.95 70 | 06 image_0006 640x480x3 71 | 315 @ 41.22 72 | 07 image_0007 640x480x3 73 | 315 @ 52.79 74 | 08 image_0008 640x480x3 75 | 315 @ 53.13 76 | 09 image_0009 640x480x3 77 | 315 @ 47.75 78 | runtime: 0 seconds 79 | fps: 13.13 80 | 81 | See the [inference-onnx](https://github.com/parlaynu/inference-onnx) project for details on the camera server. 82 | 83 | ## The Platforms 84 | 85 | Under the `platforms` directory, there is a directory for each platform supported. This project builds a single 86 | container that can be used by all the tools. 87 | 88 | In the platform directory are the tools to build the conversion container and launch it. 89 | 90 | Use the `build.sh` script to build the container. This does everything automatically including downloading the 91 | TVM source code and compiling it and building and installing the python package. This takes some time on the 92 | JetsonNano and RaspberryPi4 platforms. 93 | 94 | ./build.sh 95 | 96 | Use the `run-latest.sh` script to launch the container with the correct parameters: 97 | 98 | $ ./run-latest.sh 99 | 100 | root@eximius:/workspace# ls 101 | inference models onnx2trt 102 | 103 | The `models` directory is mounted from the host system from ${HOME}/Workspace/models. Place any models you want to convert 104 | into this directory so they are accessible from this container. 105 | 106 | ## References 107 | 108 | * https://docs.nvidia.com/deeplearning/tensorrt/api/python_api/index.html 109 | * https://documen.tician.de/pycuda/ 110 | 111 | 112 | -------------------------------------------------------------------------------- /platforms/jetson-nano/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nvcr.io/nvidia/l4t-cuda:10.2.460-runtime 2 | 3 | RUN mkdir -p /workspace 4 | 5 | WORKDIR /workspace 6 | 7 | COPY local/nvidia-l4t-apt-source.list /etc/apt/sources.list.d/ 8 | COPY local/jetson-ota-public.asc /etc/apt/trusted.gpg.d/ 9 | RUN rm -f /etc/apt/sources.list.d/cuda.list 10 | 11 | RUN apt-get update && \ 12 | DEBIAN_FRONTEND=noninteractive apt-get upgrade -y 13 | 14 | RUN DEBIAN_FRONTEND=noninteractive apt-get install -y python3.6 python3.6-dev python3.6-venv \ 15 | build-essential \ 16 | python3-libnvinfer python3-numpy \ 17 | git wget && \ 18 | rm -rf /var/lib/apt/lists/* 19 | 20 | RUN wget https://bootstrap.pypa.io/pip/3.6/get-pip.py && \ 21 | python3.6 get-pip.py && \ 22 | rm get-pip.py 23 | 24 | COPY files/requirements.txt requirements.txt 25 | RUN pip3.6 install --no-cache-dir -r requirements.txt && \ 26 | rm requirements.txt 27 | 28 | RUN git clone https://github.com/parlaynu/inference-onnx.git && \ 29 | mv inference-onnx/tools/inference . && \ 30 | rm -rf inference-onnx && \ 31 | rm -f inference/classify-onnx.py 32 | 33 | COPY local/onnx2trt /workspace/onnx2trt 34 | COPY local/inference /workspace/inference/ 35 | 36 | RUN sed -i 's/env python3/env python3.6/g' /workspace/inference/classify-trt.py 37 | 38 | ENTRYPOINT ["/bin/sh", "-c", "exec \"$0\" \"$@\""] 39 | CMD ["/bin/bash"] 40 | 41 | -------------------------------------------------------------------------------- /platforms/jetson-nano/README.md: -------------------------------------------------------------------------------- 1 | # Platform: Jetson Nano 2 | 3 | | Item | Version | 4 | | ------- | ------------------- | 5 | | Hardare | Jetson Nano | 6 | | Memory | 4 GBytes | 7 | | L4T | 32.7.4 | 8 | | OS | Ubuntu 18.04.6 LTS | 9 | | Jetpack | 4.6.4-b39 | 10 | 11 | ## Setup 12 | 13 | ### Operating System 14 | 15 | Install your Operating system as described in detail [here](https://developer.nvidia.com/embedded/learn/get-started-jetson-nano-devkit) 16 | 17 | ### Additional OS Setup 18 | 19 | A few other configuration items you might want to do are listed in this section. 20 | 21 | Update the operating system: 22 | 23 | $ sudo apt update 24 | $ sudo apt dist-upgrade 25 | $ sudo reboot 26 | 27 | Turn off the GUI so you have more RAM available: 28 | 29 | $ sudo systemctl set-default multi-user 30 | $ sudo reboot 31 | 32 | If you wish to turn it back on at any time, run this command: 33 | 34 | $ sudo systemctl set-default graphical 35 | 36 | Disable In-Memory Swap 37 | 38 | $ sudo systemctl disable nvzramconfig.service 39 | 40 | Setup Swap File 41 | 42 | $ SWAPLOCATION=/swapfile 43 | $ SWAPSIZE=4G 44 | $ sudo fallocate -l ${SWAPSIZE} ${SWAPLOCATION} 45 | $ sudo chmod 600 ${SWAPLOCATION} 46 | $ sudo mkswap ${SWAPLOCATION} 47 | $ sudo swapon ${SWAPLOCATION} 48 | 49 | Add to /etc/fstab: 50 | 51 | /swapfile none swap sw 0 0 52 | 53 | And reboot: 54 | 55 | $ sudo reboot 56 | 57 | Install the latest pip: 58 | 59 | $ wget https://bootstrap.pypa.io/pip/3.6/get-pip.py 60 | $ sudo -H python3 get-pip.py 61 | $ rm get-pip.py 62 | 63 | Install jtop: 64 | 65 | $ sudo -H python3 -m pip install -U jetson-stats 66 | $ sudo reboot 67 | 68 | ### Docker 69 | 70 | This repository uses docker to build the environments and run the tools. I've taken this approach as it can 71 | be fully automated and isolated from the host environment - it should work reliably no matter what you have 72 | installed on your host. 73 | 74 | Docker is installed and ready to use as part of the standard install. 75 | 76 | Add your user to the docker group so you can interact with the system without being root or 77 | running sudo all the time: 78 | 79 | $ sudo usermod -aG docker 80 | 81 | Log out and back in and you'll be ready to go. Test by running the hello-world example: 82 | 83 | $ docker run hello-world 84 | 85 | -------------------------------------------------------------------------------- /platforms/jetson-nano/build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # make sure we're in the right location 4 | RUN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 5 | cd ${RUN_DIR} 6 | 7 | # a stamp to tag the image with 8 | STAMP=$(date +%s) 9 | 10 | # collect the files needed 11 | rm -rf local 12 | mkdir -p local 13 | cp -r ../../tools/inference local 14 | cp -r ../../tools/onnx2trt local 15 | 16 | # copy in the files to setup the repo 17 | cp /etc/apt/sources.list.d/nvidia-l4t-apt-source.list local 18 | cp /etc/apt/trusted.gpg.d/jetson-ota-public.asc local 19 | 20 | # build the image 21 | docker build \ 22 | --tag local/inference-tensorrt:${STAMP} \ 23 | --tag local/inference-tensorrt:latest \ 24 | . 25 | 26 | -------------------------------------------------------------------------------- /platforms/jetson-nano/files/requirements.txt: -------------------------------------------------------------------------------- 1 | # imaging 2 | pillow 3 | pycuda==2017.1.1 4 | six 5 | 6 | # for camera server 7 | pyzmq==25.1.1 8 | 9 | -------------------------------------------------------------------------------- /platforms/jetson-nano/run-latest.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | mkdir -p "${HOME}/Workspace/models" 4 | 5 | docker run -it --rm --network=host --runtime=nvidia --gpus all \ 6 | -v "${HOME}/Workspace/models":/workspace/models \ 7 | -v "${HOME}/Projects/datasets":/workspace/datasets \ 8 | local/inference-tensorrt:latest /bin/bash 9 | 10 | -------------------------------------------------------------------------------- /platforms/x86_64/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nvidia/cuda:12.2.2-cudnn8-devel-ubuntu22.04 2 | 3 | RUN mkdir -p /workspace 4 | 5 | WORKDIR /workspace 6 | 7 | RUN apt-get update && \ 8 | DEBIAN_FRONTEND=noninteractive apt-get install -y python3 python3-pip \ 9 | libnvinfer-bin python3-libnvinfer \ 10 | git wget && \ 11 | rm -rf /var/lib/apt/lists/* 12 | 13 | COPY files/requirements.txt requirements.txt 14 | RUN pip3 install --no-cache-dir -r requirements.txt && \ 15 | rm requirements.txt 16 | 17 | RUN git clone https://github.com/parlaynu/inference-onnx.git && \ 18 | cd inference-onnx && \ 19 | git checkout 74a032633c283134c3677ef241b17e31fe3179b8 && \ 20 | cd .. && \ 21 | mv inference-onnx/tools/inference . && \ 22 | rm -rf inference-onnx 23 | 24 | COPY local/onnx2trt /workspace/onnx2trt 25 | COPY local/inference /workspace/inference/ 26 | 27 | ENTRYPOINT ["/bin/sh", "-c", "exec \"$0\" \"$@\""] 28 | CMD ["/bin/bash"] 29 | 30 | 31 | -------------------------------------------------------------------------------- /platforms/x86_64/README.md: -------------------------------------------------------------------------------- 1 | # Platform: AMD64 with Nvidia RTX2060 2 | 3 | | Item | Version | 4 | | ------- | ----------------------------- | 5 | | CPU | AMD Ryzen 7 3700X | 6 | | GPU | Nvidia GeForce RTX 2060 SUPER | 7 | | Memory | 32 GBytes | 8 | | OS | Ubuntu 22.04 | 9 | 10 | ## Setup 11 | 12 | ### Docker 13 | 14 | This repository uses docker to build the environments and run the tools. I've taken this approach as it can 15 | be fully automated and isolated from the host environment - it should work reliably no matter what you have 16 | installed on your host. 17 | 18 | Install docker following these [instructions](https://docs.docker.com/engine/install/ubuntu/) 19 | 20 | Then install the nvidia container toolkit following these [instructions](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html) 21 | 22 | Once installed, add your user to the docker group so you can interact with the system without being root or 23 | running sudo all the time: 24 | 25 | $ sudo usermod -aG docker 26 | 27 | Log out and back in and you'll be ready to go. Test by running the nvidia-smi example: 28 | 29 | $ docker run --rm --runtime=nvidia --gpus all ubuntu nvidia-smi 30 | 31 | -------------------------------------------------------------------------------- /platforms/x86_64/build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # make sure we're in the right location 4 | RUN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 5 | cd ${RUN_DIR} 6 | 7 | # a stamp to tag the image with 8 | STAMP=$(date +%s) 9 | 10 | # collect the files needed 11 | rm -rf local 12 | mkdir -p local 13 | cp -r ../../tools/inference local 14 | cp -r ../../tools/onnx2trt local 15 | 16 | # build the image 17 | docker build \ 18 | --tag local/inference-tensorrt:${STAMP} \ 19 | --tag local/inference-tensorrt:latest \ 20 | . 21 | 22 | -------------------------------------------------------------------------------- /platforms/x86_64/files/requirements.txt: -------------------------------------------------------------------------------- 1 | # imaging 2 | pillow 3 | opencv-python-headless 4 | pycuda 5 | 6 | # for onnx 7 | onnx 8 | onnxruntime-gpu 9 | 10 | # for camera server 11 | pyzmq==25.1.1 12 | 13 | -------------------------------------------------------------------------------- /platforms/x86_64/run-latest.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | mkdir -p "${HOME}/Workspace/models" 4 | 5 | docker run -it --rm --network=host --runtime=nvidia --gpus all \ 6 | -v "${HOME}/Workspace/models":/workspace/models \ 7 | local/inference-tensorrt:latest /bin/bash 8 | 9 | -------------------------------------------------------------------------------- /tools/inference/classify-trt.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import argparse 3 | import time 4 | 5 | import inferlib.ops as ops 6 | import inferlib.ops.classify as classify 7 | # import inferlib.ops.imaging as imaging 8 | import inferlib.ops.utils as utils 9 | 10 | import trtops.tensorrt as trt_ops 11 | 12 | 13 | def build_pipeline(engine, dataspec, rate, limit): 14 | 15 | # get the shape of the input 16 | image_shape = trt_ops.get_binding_shape(engine, "image") 17 | preds_shape = trt_ops.get_binding_shape(engine, "preds") 18 | 19 | print(f"- input shape: {image_shape}") 20 | print(f"- output shape: {preds_shape}") 21 | 22 | batch_size, nchans, height, width = image_shape 23 | 24 | pipe = ops.datasource(dataspec, resize=(width, height), silent=True) 25 | if rate > 0: 26 | pipe = utils.rate_limiter(pipe, rate=rate) 27 | if limit > 0: 28 | pipe = utils.limiter(pipe, limit=limit) 29 | pipe = utils.worker(pipe) 30 | 31 | # pipe = imaging.resize(pipe, width=width, height=height) 32 | pipe = classify.preprocess(pipe) 33 | pipe = trt_ops.classify(pipe, engine=engine) 34 | pipe = classify.postprocess(pipe) 35 | 36 | return pipe 37 | 38 | 39 | def run(pipe): 40 | start = time.time() 41 | 42 | for idx, item in enumerate(pipe): 43 | image_id = item['image_id'] 44 | image_size = item['image_size'] 45 | tops = item['top'] 46 | 47 | print(f"{idx:02d} {image_id} {image_size}") 48 | for top, prob in tops: 49 | print(f" {top} @ {prob*100.0:0.2f}") 50 | 51 | duration = time.time() - start 52 | 53 | if item.get('jpeg', None): 54 | with open("image.jpg", "wb") as f: 55 | f.write(item['jpeg']) 56 | 57 | return duration, idx+1 58 | 59 | 60 | def main(): 61 | parser = argparse.ArgumentParser() 62 | parser.add_argument('-l', '--limit', help='maximum number of images to process', type=int, default=0) 63 | parser.add_argument('-r', '--rate', help='requests per second', type=int, default=0) 64 | parser.add_argument('engine', help='path to the tensorrt engine file', type=str) 65 | parser.add_argument('dataspec', help='the data source specification', type=str) 66 | args = parser.parse_args() 67 | 68 | engine = trt_ops.load_engine(args.engine) 69 | 70 | pipe = build_pipeline(engine, args.dataspec, args.rate, args.limit) 71 | duration, count = run(pipe) 72 | 73 | print(f"runtime: {int(duration)} seconds") 74 | print(f" fps: {count/duration:0.2f}") 75 | 76 | 77 | if __name__ == "__main__": 78 | main() 79 | -------------------------------------------------------------------------------- /tools/inference/trtops/tensorrt/__init__.py: -------------------------------------------------------------------------------- 1 | from .trt_engine import load_engine 2 | from .trt_engine import get_binding_shape 3 | from .trt_engine import get_binding_dtype 4 | 5 | from .trt_classify import classify 6 | 7 | -------------------------------------------------------------------------------- /tools/inference/trtops/tensorrt/trt_classify.py: -------------------------------------------------------------------------------- 1 | import time 2 | import numpy as np 3 | import tensorrt as trt 4 | 5 | import pycuda.autoinit 6 | import pycuda.driver as cuda 7 | 8 | 9 | def classify(pipe, *, engine, batch_size=1): 10 | 11 | # prepare the bindings 12 | # h_input, h_output - host input and output 13 | # d_input, d_output - device input and output 14 | ishape = engine.get_binding_shape("image") 15 | itype = trt.nptype(engine.get_binding_dtype("image")) 16 | h_input = np.empty(shape=ishape, dtype=itype) 17 | d_input = cuda.mem_alloc(h_input.nbytes) 18 | 19 | oshape = engine.get_binding_shape("preds") 20 | otype = trt.nptype(engine.get_binding_dtype("preds")) 21 | h_output = np.empty(shape=oshape, dtype=otype) 22 | d_output = cuda.mem_alloc(h_output.nbytes) 23 | 24 | bindings = [int(d_input), int(d_output)] 25 | 26 | # create the execution context for the engine 27 | context = engine.create_execution_context() 28 | stream = cuda.Stream() 29 | 30 | # run the loop 31 | total_time = 0 32 | 33 | for item in pipe: 34 | start = time.time() 35 | 36 | image = item['image'] 37 | 38 | cuda.memcpy_htod_async(d_input, image, stream) 39 | context.execute_async_v2(bindings, stream.handle, None) 40 | cuda.memcpy_dtoh_async(h_output, d_output, stream) 41 | stream.synchronize() 42 | 43 | item['preds'] = np.copy(h_output) 44 | 45 | total_time += (time.time() - start) 46 | item['inference_time'] = total_time 47 | 48 | yield item 49 | 50 | -------------------------------------------------------------------------------- /tools/inference/trtops/tensorrt/trt_engine.py: -------------------------------------------------------------------------------- 1 | import tensorrt as trt 2 | 3 | 4 | def load_engine(engine_path): 5 | print("loading engine...", flush=True) 6 | 7 | # load the engine archive 8 | with open(engine_path, "rb") as f: 9 | runtime = trt.Runtime(trt.Logger(trt.Logger.WARNING)) 10 | engine = runtime.deserialize_cuda_engine(f.read()) 11 | 12 | return engine 13 | 14 | 15 | def get_binding_shape(engine, name): 16 | return list(engine.get_binding_shape(name)) 17 | 18 | def get_binding_dtype(engine, name): 19 | return trt.nptype(engine.get_binding_dtype("image")) 20 | 21 | -------------------------------------------------------------------------------- /tools/onnx2trt/onnx2trt.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ $# -ne 1 ]; then 4 | echo "Usage: $(basename $0) model.onnx" 5 | exit 6 | fi 7 | 8 | ONNX_MODEL=$1 9 | TRT_MODEL="${ONNX_MODEL%.*}".trt 10 | 11 | if [ ! -f "${ONNX_MODEL}" ]; then 12 | echo Error: onnx model not found 13 | exit 1 14 | fi 15 | 16 | 17 | /usr/src/tensorrt/bin/trtexec --onnx="${ONNX_MODEL}" --saveEngine="${TRT_MODEL}" 18 | 19 | --------------------------------------------------------------------------------