Giter Club home page Giter Club logo

sparseinst_tensorrt's Introduction

SparseInst_TensorRT

This repository implement the real-time Instance Segmentation Algorithm named SparseInst with TensoRT and ONNX.

Some remarks

  • The initial repository on which I build mine is from hustvl/SparseInst__ repository (https://github.com/hustvl/SparseInst.git), for additional information about the installation of SparseInst, refer to the original repository.
  • This project is built upon the excellent framework detectron2, and you should install detectron2 first, please check official installation guide for more details. (https://github.com/facebookresearch/detectron2.git)
  • For command other than TensoRT and ONNX inference, please refer to the initial repository (e.g test_net.py).
  • If you face any problem during the parsing time, don't hesitate to drop an issue or a โญ if there aren't any. if you have compatibility problem, check the model weights uploaded in the table below and go directly in the testing section.
  • Be aware that in order to parse the model to ONNX and TensorRT, the files sparseinst.py, encoder.py and decoder.py has been modified/slightly modified, don't forget to check the modifications if you come from the initial repository.

Prerequisites

Click me
  • Install Pytorch (1.10.0) and TorchVision (0.11.1)
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116

If other versions of torch are needed, select yours by putting torch==1.11.0+cu102 for example.
sudo wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo wget https://developer.download.nvidia.com/compute/cuda/11.7.1/local_insta
    llers/cuda-repo-wsl-ubuntu-11-7-local_11.7.1-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-7-local_11.7.1-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-11-7-local/cuda-96193861-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda
  • Install TensorRT (8.0.1.6), if you are using an nvidia edge device, TensorRT should already be installed
python3 -m pip install --upgrade setuptools pip
python3 -m pip install nvidia-pyindex
python3 -m pip install --upgrade nvidia-tensorrt

Verify installation by writing  : assert tensorrt.Builder(tensorrt.Logger())
  • Install ONNX and ONNXruntime
pip install onnxruntime-gpu
pip install onnxruntime
pip install numpy protobuf==4.21.5  
pip install onnx
  • Install all the other packages needed to run the original SparseInst algorithm (Should be done if you have installed Dectectron2)

Models and Results for TensorRT and ONNX inference script:

The inference speed for Pytorch, ONNX and TensorRT has been compared and shown in the table below. SparseInst running with TensoRT achieved more a less 3 times faster inference speed of SparseInst than running with Pytorch. Lowering the input size of the image can lead to a decent real-time speed. The models from TensorRT and ONNX are built upon the first Pytorch listed weights in the table below : SparseInst R-50 G-IAM.

Note: All the computations has been done on a Nvidia Jetson TX2 Jetpack 4.6. Further test will be done on a Nvidia 2070 RTI

Model Input Size Inference Speed Weights
Pytorch 640 1.71 FPS model
TensorRT 320 20.32 FPS model
TensorRT 640 6.00 FPS model
ONNX 320 0.22 FPS model
ONNX 640 0.03 FPS model

Alt text

Building the ONNX model :

To build the model from Pytorch to ONNX, you need to run the following command. You can set the arguments to default. Please check if the config path and the model weights path are correctly set up.

<sudo python3 convert_onnx.py --config-file config-gile --output output_directory_onnxmodel --image dummy_input --opts MODEL.WEIGHTS weights_directory>

Building the TensorRT model :

To build the model from ONNX to TensorRT, you need to run the following command. You can set the arguments to default. If you have any problem while parsing the model to TensorRT, don't hesitate to ask.

<sudo python3 convert_tensortt.py --onnx_model onnx-model-directory --output output_directory_TensoRTModel

Testing SparseInst with Pytorch, TensorRT and ONNX :

To test the inference speed (FPS) of the Pytorch, TensorRT and ONNX models, run the following command.

  1. Pytorch
sudo python3 eval_tensorrt_onnx.py  -c 0.2 --width_resized 320 --height_resized 320 --input datasets/coco/calib_images/*  --use_pytorch 
  1. TensorRT
sudo python3 eval_tensorrt_onnx.py  -c 0.2 --width_resized 320 --height_resized 320 --input datasets/coco/calib_images/*  --use_tensorrt --tensorrt_engine engine/sparseinst_trt_320_320.engine
  1. ONNX
sudo python3 eval_tensorrt_onnx.py  -c 0.2 --width_resized 320 --height_resized 320 --input datasets/coco/calib_images/* --use_onnx --onnx_engine onnx/sparseinst_onnx_320_320.onnx 

Notes :

  • Input argument can either be an image or a directory of images (directory/*)

  • You can of course infer all three together, just add the argument --use_model of the model you want to infer aswell as the engine (Not for Pytorch).

  • In the terminal :

    • TRT inference only time reprensents the inference speed of the model alone
    TRT inference only use time 4.970773220062256 for 100 images, FPS=20.117594501474272
    
    • TRT algorithm time represents the inference speed and the preprocessing time combined
    TRT algorithm use time 22.519110441207886 for 100 images, FPS=4.440672745980644
    

Visualizing SparseInst with Pytorch, TensorRT and ONNX :

To visualize segmentation results on your images, you can run the following commands :

  1. Pytorch
sudo python3 eval_tensorrt_onnx.py  -c 0.2 --width_resized 320 --height_resized 320 --input datasets/coco/calib_images/*  --use_pytorch --output_pytorch results/result_image_pytorch/result_pytorch --save_image
  1. TensorRT
sudo python3 eval_tensorrt_onnx.py  -c 0.2 --width_resized 320 --height_resized 320 --input datasets/coco/calib_images/*  --use_tensorrt --tensorrt_engine engine/sparseinst_trt_320_320.engine --output_tensorrt results/result_image_tensorrt/result_tensorrt --save_image
  1. ONNX
sudo python3 eval_tensorrt_onnx.py  -c 0.2 --width_resized 320 --height_resized 320 --input datasets/coco/calib_images/* --use_onnx --onnx_engine onnx/sparseinst_onnx_320_320.onnx --output_onnx results/result_image_onnx/result_onnx --save_image

Notes :

  • You can still infer and visualize all three together, just add all the argument together
  • If you don't specify --save_image, it will only infer the model and not save the outputs.

sparseinst_tensorrt's People

Contributors

leandro-svg avatar mohamadmansourx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

sparseinst_tensorrt's Issues

onnx model output dimension.

Hello! I am confused with the onnx model I exported. I wonder whether the 'masks' node's loss the first dimension or not. As far as I am concerned, it should be [1, 100, 160, 160], but not [100, 160, 160], which means it loss the batch dim.
ba3adc22d9c876268fa24d622e5386c

ONNX Inferencing Error

Hello,
Thank you for this great project to properly convert SparseInst to Onnx/TensorRT.

I started training through your updated implementation using a customized version of configs/sparse_inst_r50_giam_aug.yaml and 10 classes

I converted to onnx successfully

python3 convert_onnx.py --config-file configs/sparse_inst_r50_giam_aug.yaml --output on2/myonnx2.onnx --image assets/figures/t1.jpg --opts MODEL.WEIGHTS output/checkpoints/model_0001999.pth

Then when trying to inference using

python3 eval_tensorrt_onnx.py  -c 0.2 --width_resized 640 --height_resized 640 --input assets/figures/* --use_onnx --onnx_engine on2/myonnx2.onnx --output_onnx on2/ --save_image

I'm receiving the below error!

onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from on2/myonnx2.onnx failed:Type 
Error: Type parameter (T) of Optype (Add) bound to different types (tensor(float) and tensor(double) in node (Add_398).

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.