Code Monkey home page Code Monkey logo

tf-trt_tutorial's Introduction

TF-TRT tutorial

This tutorial describes the steps to convert your tensorflow saved model to TensorRT engine and how to run it on video. The instructions are applicable for running the TRT engine on any Nvidia Jetson device with Jetpack installed. Also you can folow this tutorial to inference the TRT engine right on your PC with Nvidia Gpu, but you need to set up cuda,cudnn,tensorrt libraries for this.

There steps are:

  • Convert saved model to ONNX using tf2onnx python library
  • Simplify ONNX model using onnxsimplifier python library
  • Copy siplified ONNX model from your PC to Jetson
  • Create TensorRT engine from ONNX model on the Jetson using trtexec
  • Adjust given script for inferencing(change the model shape, input names, adjust input preprocesing and output processing), and run the script

Convert saved model to ONNX using tf2onnx python library:

Install tf2onnx:

pavlo@pc:~$ pip3 install tf2onnx

Convert tf saved model to onnx:

pavlo@pc:~$ python3 -m tf2onnx.convert --saved-model "my_tf_saved_model" --output "my_model.onnx" --opset 11

Simplify ONNX model using onnxsimplifier python library:

Install onnxsimplifyer:

pavlo@pc:~$ pip3 install onnxsim

Simplify onnx model:

pavlo@pc:~$ python3 -m onnxsim my_model.onnx my_model_simpl.onnx --input-shape 1,224,224,3

Now copy my_model_simpl.onnx to Jetson device

Create TensorRT engine from ONNX model:

jetson@myjetsonnano:~$ /usr/src/tensorrt/bin/trtexec --onnx=/home/myjetsonnano/Desktop/Projects/trt_tutorial/my_model_simpl.onnx --shapes=\'input_1:0\':1x224x224x3 --saveEngine=/home/myjetsonnano/Desktop/Projects/trt_tutorial/engine.trt --verbose --explicitBatch

Install dependencies on Jetson to run the script for inferencing:

export PATH=/usr/local/cuda-10.2/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64:$LD_LIBRARY_PATH
sudo apt install python3-pip
pip3 install cyton
pip3 install pycuda
sudo apt-get libprotobuf-dev
sudo apt-get protobuf-compiler

Open run_trt_engine_on_video.py and edit input name, input shape and path to the video:

screenshot1

add data preprocessing if needed:

screenshot2

Now run the script!

P.S if your model has 2 outputs, you should uncoment commented lines

tf-trt_tutorial's People

Contributors

pavloshargan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.