Skip to content

Creating Model Summary (TFLite)

Alex Hoffman edited this page May 2, 2023 · 11 revisions

Flatc summaries - Not really useful

The flatbuffer compiler (flatc) can generate JSON files from TFLite models simply my invoking the following command

flatc -t --strict-json --defaults-json schema/tflite.fbs -- $MODEL.tflite

Note that this output files are usually too large to practically use.

Summary script

There is a script CreateModelSummary.py that will step through a provided model using the MLTK python library to create a JSON summary.

usage: CreateModelSummary.py [-h] --model MODEL [--outputdir OUTPUTDIR] --output OUTPUT

Pass in the model file to be summarized

options:
  -h, --help            show this help message and exit
  --model MODEL         Path to the TFLite model that is to be loaded and summarized
  --outputdir OUTPUTDIR
                        Output directory for JSON file, defults to modelsummaries folder in java
                        resources
  --output OUTPUT       Filename of output JSON file

Dependencies

Update all python packages, important is setup tools

python -m pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 python -m pip install -U 

Python

python -m pip install silabs-mltk[full] --upgrade
python -m pip install metrics visualization tensorrt

Error with MLTK

If you're getting errors during intalling MLTK that are due to the abseil library then it could be due to the problem discussed here. For me the solution was to download this version of the abseil library then make and install it by running

mkdir build && cd build
cmake ..
sudo make install

TensorRT version 7

It stupidly seems that there is no 7.X versions of tensorrt available on pip, but luckily 8.X is compatible with 7.X so we can just symlink 8.X to 7.X.

Installing (Ubuntu)

apt-get install libnvinfer8 libnvinfer-plugin8

Installing (Arch)

To install TenorRT you must download the package from NVidia's website here (login required) and place it in the package folder from this AUR repo.

git clone https://aur.archlinux.org/tensorrt.git

and place the TensorRT file inside that folder. Then run makepkg -si from the folder. You might need to change the cuda version within the PKGBUILD file to be that of the one you want, for me this was changing 12.0 -> 11.8.

Version 7 required

Check where you libraries are installed

sudo find / -name "libnvinfer*"

On the docker image, for example, the libraries, libnvinfer.so.7 and libnvinfer_plugin.so.7, are both located in /usr/lib/x86_64-linux-gnu so I can symlink using

ln -s /usr/lib/x86_64-linux-gnu/libnvinfer.so.8 /usr/lib/x86_64-linux-gnu/libnvinfer.so.7
ln -s /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.8 /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.7

Finally we need to add this path to the LD_LIBRARY_PATH if it is not already, check with echo $LD_LIBRARY_PATH. Adding

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INSTALL_PATH

For me this evaluates to

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/alxhoff/.local/lib/python3.10/site-packages/tensorrt/

on my personal machine.

Other

I would recommend installing the following dependencies in the order: cudnn, cuda-tools and finally cuda.

Cudnn

sudo pacman -Sy cudnn

Also this wiki entry if you need a specific cudnn version.

Cuda tools

sudo pacman -Sy cuda-tools

Cuda 11

See this wiki entry.

Clone this wiki locally