All Projects → NVIDIA-AI-IOT → Torch2trt

NVIDIA-AI-IOT / Torch2trt

Licence: mit
An easy to use PyTorch to TensorRT converter

Programming Languages

python
139335 projects - #7 most used programming language
C++
36643 projects - #6 most used programming language
shell
77523 projects
Dockerfile
14818 projects

Projects that are alternatives of or similar to Torch2trt

Jetson Inference
Hello AI World guide to deploying deep-learning inference networks and deep vision primitives with TensorRT and NVIDIA Jetson.
Stars: ✭ 5,191 (+74.55%)
Mutual labels:  inference, tensorrt, jetson-tx2, jetson-xavier, jetson-nano
ros jetson stats
🐢 The ROS jetson-stats wrapper. The status of your NVIDIA jetson in diagnostic messages
Stars: ✭ 55 (-98.15%)
Mutual labels:  jetson-tx2, jetson-xavier, jetson-nano
yolov5-deepsort-tensorrt
A c++ implementation of yolov5 and deepsort
Stars: ✭ 207 (-93.04%)
Mutual labels:  tensorrt, jetson-xavier, jetson-nano
homesecurity
Security camera with Raspberry pi and NVIDIA Jetson platforms
Stars: ✭ 141 (-95.26%)
Mutual labels:  jetson-tx2, jetson-xavier, jetson-nano
trt pose hand
Real-time hand pose estimation and gesture classification using TensorRT
Stars: ✭ 137 (-95.39%)
Mutual labels:  tensorrt, jetson-xavier, jetson-nano
jetsonUtilities
Get information about the NVIDIA Jetson OS environment. Lists L4T and JetPack versions, along with major libraries.
Stars: ✭ 171 (-94.25%)
Mutual labels:  jetson-tx2, jetson-xavier
smart-social-distancing
Social Distancing Detector using deep learning and capable to run on edge AI devices such as NVIDIA Jetson, Google Coral, and more.
Stars: ✭ 129 (-95.66%)
Mutual labels:  jetson-tx2, jetson-nano
installROS
Install ROS Melodic on NVIDIA Jetson Development Kits
Stars: ✭ 75 (-97.48%)
Mutual labels:  jetson-tx2, jetson-nano
flexinfer
A flexible Python front-end inference SDK based on TensorRT
Stars: ✭ 83 (-97.21%)
Mutual labels:  classification, tensorrt
keras-tensorrt-jetson
Example of loading a Keras model into TensorRT C++ API
Stars: ✭ 51 (-98.29%)
Mutual labels:  tensorrt, jetson-tx2
InferenceHelper
C++ Helper Class for Deep Learning Inference Frameworks: TensorFlow Lite, TensorRT, OpenCV, OpenVINO, ncnn, MNN, SNPE, Arm NN, NNabla, ONNX Runtime, LibTorch, TensorFlow
Stars: ✭ 142 (-95.23%)
Mutual labels:  inference, tensorrt
monocular person following
Monocular camera-based person tracking and identification ROS framework for person following robots
Stars: ✭ 124 (-95.83%)
Mutual labels:  jetson-tx2, jetson-xavier
Tnn
TNN: developed by Tencent Youtu Lab and Guangying Lab, a uniform deep learning inference framework for mobile、desktop and server. TNN is distinguished by several outstanding features, including its cross-platform capability, high performance, model compression and code pruning. Based on ncnn and Rapidnet, TNN further strengthens the support and …
Stars: ✭ 3,257 (+9.52%)
Mutual labels:  inference, tensorrt
mediapipe plus
The purpose of this project is to apply mediapipe to more AI chips.
Stars: ✭ 38 (-98.72%)
Mutual labels:  inference, tensorrt
FAST-Pathology
⚡ Open-source software for deep learning-based digital pathology
Stars: ✭ 54 (-98.18%)
Mutual labels:  inference, tensorrt
Gfocal
Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for Dense Object Detection, NeurIPS2020
Stars: ✭ 376 (-87.36%)
Mutual labels:  classification, inference
Bmw Classification Inference Gpu Cpu
This is a repository for an image classification inference API using the Gluoncv framework. The inference REST API works on CPU/GPU. It's supported on Windows and Linux Operating systems. Models trained using our Gluoncv Classification training repository can be deployed in this API. Several models can be loaded and used at the same time.
Stars: ✭ 27 (-99.09%)
Mutual labels:  classification, inference
Ml privacy meter
Machine Learning Privacy Meter: A tool to quantify the privacy risks of machine learning models with respect to inference attacks, notably membership inference attacks
Stars: ✭ 167 (-94.38%)
Mutual labels:  inference
Imgclsmob
Sandbox for training deep learning networks
Stars: ✭ 2,405 (-19.13%)
Mutual labels:  classification
Server
The Triton Inference Server provides an optimized cloud and edge inferencing solution.
Stars: ✭ 2,994 (+0.67%)
Mutual labels:  inference

torch2trt

What models are you using, or hoping to use, with TensorRT? Feel free to join the discussion here.

torch2trt is a PyTorch to TensorRT converter which utilizes the TensorRT Python API. The converter is

  • Easy to use - Convert modules with a single function call torch2trt

  • Easy to extend - Write your own layer converter in Python and register it with @tensorrt_converter

If you find an issue, please let us know!

Please note, this converter has limited coverage of TensorRT / PyTorch. We created it primarily to easily optimize the models used in the JetBot project. If you find the converter helpful with other models, please let us know.

Usage

Below are some usage examples, for more check out the notebooks.

Convert

import torch
from torch2trt import torch2trt
from torchvision.models.alexnet import alexnet

# create some regular pytorch model...
model = alexnet(pretrained=True).eval().cuda()

# create example data
x = torch.ones((1, 3, 224, 224)).cuda()

# convert to TensorRT feeding sample data as input
model_trt = torch2trt(model, [x])

Execute

We can execute the returned TRTModule just like the original PyTorch model

y = model(x)
y_trt = model_trt(x)

# check the output against PyTorch
print(torch.max(torch.abs(y - y_trt)))

Save and load

We can save the model as a state_dict.

torch.save(model_trt.state_dict(), 'alexnet_trt.pth')

We can load the saved model into a TRTModule

from torch2trt import TRTModule

model_trt = TRTModule()

model_trt.load_state_dict(torch.load('alexnet_trt.pth'))

Models

We tested the converter against these models using the test.sh script. You can generate the results by calling

./test.sh TEST_OUTPUT.md

The results below show the throughput in FPS. You can find the raw output, which includes latency, in the benchmarks folder.

Model Nano (PyTorch) Nano (TensorRT) Xavier (PyTorch) Xavier (TensorRT)
alexnet 46.4 69.9 250 580
squeezenet1_0 44 137 130 890
squeezenet1_1 76.6 248 132 1390
resnet18 29.4 90.2 140 712
resnet34 15.5 50.7 79.2 393
resnet50 12.4 34.2 55.5 312
resnet101 7.18 19.9 28.5 170
resnet152 4.96 14.1 18.9 121
densenet121 11.5 41.9 23.0 168
densenet169 8.25 33.2 16.3 118
densenet201 6.84 25.4 13.3 90.9
densenet161 4.71 15.6 17.2 82.4
vgg11 8.9 18.3 85.2 201
vgg13 6.53 14.7 71.9 166
vgg16 5.09 11.9 61.7 139
vgg19 54.1 121
vgg11_bn 8.74 18.4 81.8 201
vgg13_bn 6.31 14.8 68.0 166
vgg16_bn 4.96 12.0 58.5 140
vgg19_bn 51.4 121

Setup

torch2trt depends on the TensorRT Python API. On Jetson, this is included with the latest JetPack. For desktop, please follow the TensorRT Installation Guide. You may also try installing torch2trt inside one of the NGC PyTorch docker containers for Desktop or Jetson.

Option 1 - Without plugins

To install without compiling plugins, call the following

git clone https://github.com/NVIDIA-AI-IOT/torch2trt
cd torch2trt
python setup.py install

Option 2 - With plugins

To install with plugins to support some operations in PyTorch that are not natviely supported with TensorRT, call the following

Please note, this currently only includes the interpolate plugin. This plugin requires PyTorch 1.3+ for serialization.

git clone https://github.com/NVIDIA-AI-IOT/torch2trt
cd torch2trt
sudo python setup.py install --plugins

Option 3 - With support for experimental community contributed features

To install torch2trt with experimental community contributed features under torch2trt.contrib, like Quantization Aware Training (QAT)(requires TensorRT>=7.0), call the following,

git clone https://github.com/NVIDIA-AI-IOT/torch2trt
cd torch2trt/scripts    
bash build_contrib.sh   

This enables you to run the QAT example located here.

How does it work?

This converter works by attaching conversion functions (like convert_ReLU) to the original PyTorch functional calls (like torch.nn.ReLU.forward). The sample input data is passed through the network, just as before, except now whenever a registered function (torch.nn.ReLU.forward) is encountered, the corresponding converter (convert_ReLU) is also called afterwards. The converter is passed the arguments and return statement of the original PyTorch function, as well as the TensorRT network that is being constructed. The input tensors to the original PyTorch function are modified to have an attribute _trt, which is the TensorRT counterpart to the PyTorch tensor. The conversion function uses this _trt to add layers to the TensorRT network, and then sets the _trt attribute for relevant output tensors. Once the model is fully executed, the final tensors returns are marked as outputs of the TensorRT network, and the optimized TensorRT engine is built.

How to add (or override) a converter

Here we show how to add a converter for the ReLU module using the TensorRT python API.

import tensorrt as trt
from torch2trt import tensorrt_converter

@tensorrt_converter('torch.nn.ReLU.forward')
def convert_ReLU(ctx):
    input = ctx.method_args[1]
    output = ctx.method_return
    layer = ctx.network.add_activation(input=input._trt, type=trt.ActivationType.RELU)  
    output._trt = layer.get_output(0)

The converter takes one argument, a ConversionContext, which will contain the following

  • ctx.network - The TensorRT network that is being constructed.

  • ctx.method_args - Positional arguments that were passed to the specified PyTorch function. The _trt attribute is set for relevant input tensors.

  • ctx.method_kwargs - Keyword arguments that were passed to the specified PyTorch function.

  • ctx.method_return - The value returned by the specified PyTorch function. The converter must set the _trt attribute where relevant.

Please see this folder for more examples.

See also

  • JetBot - An educational AI robot based on NVIDIA Jetson Nano

  • JetRacer - An educational AI racecar using NVIDIA Jetson Nano

  • JetCam - An easy to use Python camera interface for NVIDIA Jetson

  • JetCard - An SD card image for web programming AI projects with NVIDIA Jetson Nano

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].