Close

Failed body_25 with tensorrt

A project log for Auto tracking camera

A camera that tracks a person & counts reps using *AI*.

lion-mclionheadlion mclionhead 02/25/2023 at 21:001 Comment

Many internet commenters have dreamed of running body_25 using tensorrt but failed.

The journey began with this program for converting a caffe model to pytorch.

https://github.com/vadimkantorov/caffemodel2pytorch/blob/master/caffemodel2pytorch.py

python3 caffemodel2pytorch.py openpose/models/pose/body_25/pose_iter_584000.caffemodel -o body_25.pt

 Then came the previously used program for converting pytorch to onnyx.

python3 convert2onnx.py -i body_25.pt -o body_25.onnx

Which quickly died with an exception.   Convert2onnx.py requires a lot of python code specific to the model & is hard coded for resnet18.

The next hit was direct conversion from caffe to onnx.

https://github.com/asiryan/caffe2onnx

After python3 setup.py install do

python3 -m caffe2onnx.convert --prototxt openpose/models/pose/body_25/pose_deploy.prototxt  --caffemodel openpose/models/pose/body_25/pose_iter_584000.caffemodel --onnx body25.onnx

This reported a successful conversion, several minutes later.

Then convert the onnx to a tensorrt engine.

./trt_engine body25.onnx body25_fp16.engine

This failed with a parser error.  "broadcast dimensions must be conformable" in the logger.  

The explanation of this error is buried in:

https://www.photoroom.com/tech/stable-diffusion-25-percent-faster-and-save-seconds

They recommend replacing the broadcast operator with the expand operator.  2 hits recommend using onnx-simplify for doing this which sadly has many compile errors.

You have to run 

git clone https://github.com/daquexian/onnx-simplifier.git
git submodule update --init --recursive

This has to be a done from the confuser with your github private key since they don't provide https repositories.  This too fails with many "reference is not a tree" errors.  The submodules have to be manually checked out with git clone then recursively calling git submodule update.  

python3 setup.py install

 

A few more std::filesystem instances had to be converted to std::experimental::filesystem & it built.  Helas, it still died.

ModuleNotFoundError: No module named 'onnxruntime'

The setup script didn't install onnxruntime.  Compiling onnxruntime requires creating a cmake/build directory & running cmake .. -DPYTHON_EXECUTABLE=$(which python3) in there.  Unfortunately, it had an unknown error during the linking process & further challenges accessing std::experimental::filesystem.  It seems to require adding libstdc++fs to a bunch of CMake files.  It was time to move on from building.


They provide a command for installing it from a python repository.

pip3 install onnxsim

Helas, this bricks python3 with an illegal instruction.  GDB showed it auto updated numpy to broken version 1.19.5.  It's not compatible with the jetson nano derivative of aarch64.  It had to be downgraded with 

pip3 install --force-reinstall numpy==1.19.4 

The canned distribution still failed with missing std::filesystem calls.


Finally, there's a web app which advertises ONNX simplification.

https://convertmodel.com/#input=onnx&output=onnx

& the simplified model still reported "broadcast dimensions must be conformable".  The simplifier didn't do anything but add 2 bytes.  It has a few more conversion options.

onnx -> tengine failed "ReferenceError: SharedArrayBuffer is not defined"

Tengine is not the same as a tensorrt engine.

caffe -> onnx failed "TypeError: func is not a function"

That's a bug in their javascript.


Porting body_25 to tensorrt might require training a new model which doesn't use the broadcast operator.  The lion kingdom doesn't have enough GPU memory to train even the 1st model.  There might be other possible changes in caffe2onnx which could avoid the broadcast operator.

There were other openpose models based on body_25, but they all stuck to caffe.  Work with the original body_25 model stopped many years ago, as GAN's & then transformers became the big thing.

At this point, body_25 may either have been too complex to port or it may not have gained enough speed for anyone to bother.  Lions have noticed the industry wants new models but it doesn't want existing models to be perfected.  Efficientdet might be worth another try for the tracking applications.  

Discussions

Eugene wrote 05/05/2023 at 07:08 point

Hi, can i ask if you managed to convert the openpose body_25 caffe model to ONNX?

  Are you sure? yes | no