How to convert pytorch model to TensorRT? - opencv

I have trained the classification model on Nvidia GPU and saved the model weights(checkpoint.pth). If I want to deploy this model in jetson nano and test it.
Should I convert it to TenorRT? How to convert it to TensorRT?
I am new to this. It would be helpful if someone can even correct me.

The best way to achieve the way is to export the Onnx model from Pytorch.
Next, use the TensorRT tool, trtexec, which is provided by the official Tensorrt package, to convert the TensorRT model from onnx model.
You can refer to this page: https://github.com/NVIDIA/TensorRT/blob/master/samples/opensource/trtexec/README.md
The TRTEXEC is a more native tool that you can take it from NVIDIA NGC images or downloading from the official website directly.
If you use a tool such as torch2trt, it is easy to encounter the operator issue and complicated to resolve it indeed (if you are not familiar to deal with plugin issues).

You can use this tool:
https://github.com/NVIDIA-AI-IOT/torch2trt
Here are more details how to implent a converter to a engine file:
https://github.com/NVIDIA-AI-IOT/torch2trt/issues/254

Related

What is the format of pytorch models?

I have to create a workflow that uses pytorch models or torch script models in c++. However, I can't find the binary format. I tried looking through the serializer but to no avail. Does anybody have documentation or code that gives me a clue as to the model format?
PyTorch has a great tutorial of Tracing/Scripting models and Loading them via C++.

Torchscript vs TensorRT for real time inference

I have trained an object detection model to be used in production for real-time applications. I have the following two options. Can anyone suggest what is the best way to run inference on Jetson Xavier for best performance? Any other suggestions are also welcome.
Convert the model to ONXX format and use with TensorRT
Save the model as Torchscript and run inference in C++
On Jetson hardware, my experience is that using TensorRT is definitely faster. You can convert ONNX models to TensorRT using the ONNXParser from NVIDIA. For optimal performance you can choose to use mixed precision. How to convert ONNX to TensorRT is explained here: TensorRT. Section 3.2.5 for python bindings and Section 2.2.5 for the C++ bindings.
I don't have any experience in Jetson Xavier, but in Jetson Nano TensorRT is a little bit faster than ONNX or pytorch. TorchScript does no make any difference from pyTorch.

Is there method .predict in official python bindings for fastText

I know there are unofficial bindings with .predict method in python(fasttext, pyfasttext) but they do not work with recent models trained on on official FastText bash tool or do not have all the options. Official python bindings have only load_model(path)and tokenize(text) methods described , which sounds strange as with this you can not do any predictions. Am I missing something here?
I use the Python package built and installed according to this link https://github.com/facebookresearch/fastText/blob/master/README.md#building-fasttext-for-python. I consider it official.
The model object loaded via load_model has the requested predict method.

Convert .pb file (Tensorflow model file) to human readable format

I am new to tensorflow. I have downloaded and run the image classifier provided on tensorflow website. I can see link that downloads model from web.
I have to read the .pb file in human readable format.
Is this possible? if yes, How?
Thanks!
If you mean the model architecture then I recommend looking at the graph in Tensorboard, the graph visualisation tool provided with Tensorflow. I'm pretty sure that the demo code/tutorial already implements all the code required to import into tensorboard so it should just be a case or running tensorboard and pointing it to the log directory. (This should be defined in the code near the top).
Then run tensorboard --logdir=/path/to/logs/ and click the graphs tab. You will then see the various graphs for the different runs.
Alternatively, there are a couple of papers on inception that describe the model and the theory behind it. One is available here
Hope that helps you understand inception a bit more.

How do I create a custom haar classifier?

I am struggling to create a custom haar classifier. I have found a couple tutorials on the web, but they do not specify which version of opencv they are using. What I need is a very concise and simplified example of the steps that are required, along with a simple dataset of images. I also need to know the opencv version and the OS platform so I can get it running. I have tried a matrix of opencv versions on both windows and linux and I have run into memory error after memory error. I would like to start with a known good set of data and simple commands before expanding it to fit my problem.
Thanks for your help,
Chris
OpenCV provides two utility commands createsamples.exe and haartraining.exe, which can generate xml files used by Haar Classifiers. That is, with the xml file outputted from haartraining.exe, you can directly use the face detection sample with your xml file to detect any customized objects.
About the detailed procedures to use the commands, you may consult Page 513-516 in the book "Learning OpenCV", or this tutorial.
About the internal mechanism of how the classifier works, you may consult the paper "Rapid Object Detection using a Boosted Cascade of Simple
Features", which has been cited 5500+ times.

Resources