GithubHelp home page GithubHelp logo

tflite-tools's Introduction

tflite-tools

TFLite model analyser & memory optimizer.

The tool is able to produce a short analysis of a Tensorflow Lite (v3) models, which includes:

  • Information about intermediate tensors that need to be present in RAM (excludes weights, as they can be read directly from the model file.)
  • Operator evaluation schedule (as given by the operator order in the model file), along with tensors that need to present at every step of execution and the amount of memory occupied by them.
  • Plot memory usage during evaluation, detailing sizes of input and output tensors for each operator, as well as other tensors that are present in memory (see example image at the end of 'Example output' section).

The analysis can be printed to the standard output or to a set of CSV files using the --csv option.

Additionally, the tool can:

  • Modify the model to minimise peak memory usage by reordering operators in the model file (--optimize option).
  • Simulate code-book quantization by clustering the weights into n centroids, and replacing each weight with the closest centroid value. Note that this is done for each weight matrix separately and biases are left untouched.

The tool also offers an API through the TFLiteModel class --- see def main() in tflite_tools.py for example usage.

Setup

The tool requires Python 3.6+ and a few dependencies, as described in Pipfile. To create a new virtual environment with correct dependencies, run the following the root of the repository:

pipenv install

(requires pipenv, which you can install through your system's package manager or via pip: pip install pipenv)

Usage

% pipenv shell
% python tflite_tools.py --help
usage: tflite_tools.py [-h] [-i INPUT_PATH] [-o OUTPUT_PATH]
                       [--clusters CLUSTERS] [--optimize]

TFLite model analyser & memory optimizer

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT_PATH         input model file (.tflite)
  -o OUTPUT_PATH        output model file (.tflite)
  --clusters CLUSTERS   cluster weights into n-many values (simulate code-book
                        quantization)
  --optimize            optimize peak working set size
  --csv CSV_OUTPUT_FOLDER
                        output model analysis in CSV format into the specified
                        folder
  --plot PLOT_FILE      plot memory usage for each operator during the
                        execution

Example output

% python tflite_tools.py -i quantized_model.tflite -o quantized_model_optimized.tflite

Tensor information (weights excluded):
+----+-----------------------+-----------------+-----------------+
| Id |         Tensor        |      Shape      | Size in RAM (B) |
+----+-----------------------+-----------------+-----------------+
|  1 |       Conv1/Relu      | (1, 30, 30, 16) |          14,400 |
|  2 |      Conv1_input      |  (1, 32, 32, 3) |           3,072 |
|  4 |       Conv2/Relu      | (1, 28, 28, 16) |          12,544 |
|  5 |      FC2/BiasAdd      |     (1, 10)     |              10 |
|  7 |      FC2/Softmax      |     (1, 10)     |              10 |
|  8 |    activation/Relu    |     (1, 128)    |             128 |
|  9 | max_pooling2d/MaxPool | (1, 14, 14, 16) |           3,136 |
+----+-----------------------+-----------------+-----------------+

Operator execution schedule:
+------------------------+-------------------------+----------------+
| Operator (output name) | Tensors in memory (IDs) | Memory use (B) |
+------------------------+-------------------------+----------------+
|       Conv1/Relu       |          [1, 2]         |         17,472 |
|       Conv2/Relu       |          [1, 4]         |         26,944 |
| max_pooling2d/MaxPool  |          [4, 9]         |         15,680 |
|    activation/Relu     |          [8, 9]         |          3,264 |
|      FC2/BiasAdd       |          [8, 5]         |            138 |
|      FC2/Softmax       |          [5, 7]         |             20 |
+------------------------+-------------------------+----------------+
Current peak memory usage: 26,944 B

% python tflite_tools.py -i example_model.tflite --plot example_working_set.png

Example working set plot

tflite-tools's People

Contributors

eliberis 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar

tflite-tools's Issues

What falls into the category of "other tensors"?

Hi,

Thanks for this tool - looks to work great!

Just looking at the output graphs, I see a substantial amount of my data is falling into the category of "other tensors", what exactly does all this additional data consist of?

Best Regards,
Liam

Simulation or profiling?

Hi,

It is great work! I just wonder if the memory usage you calculated is based on simulation (calculate the tensor size and memory consumption) or profiling (poking the memory used by tflite interpreter code)?

Thank you!

MAC counts

Hi,
I found this code very useful, thanks for sharing it.
While run it for my model I wanted to calculate the number of macs, and eventhough I set the parameter to True it still didn't show up anything. I made a small modification of the script "tflite_model.py" in the function "_execution_schedule_info"
Before:
macs = self._macs_for_op(op) if macs else 0
Modified:
macs_val = self._macs_for_op(op) if macs else 0

So the variable macs does not get overwrite, and now it shows the macs count.

Running on official SPICE tflite model fails with assertion

I cloned repo, installed pipenv, ran pipenv shell in my exising env and then started tflite_tools.py with the official SPICE model (audio pitch tracking model by google, included with tf) as input. It then failed with assertion error. Please see full transcript below:

(bob) leo@katana:~/Desktop/Projects/Singularity/libs/tflite_build$ pip install pipenv

Collecting pipenv
  Downloading pipenv-2022.5.2-py2.py3-none-any.whl (3.9 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.9/3.9 MB 5.3 MB/s eta 0:00:00
Requirement already satisfied: pip>=22.0.4 in /home/leo/.venv/bob/lib/python3.7/site-packages (from pipenv) (22.1.2)
Collecting virtualenv
  Downloading virtualenv-20.14.1-py2.py3-none-any.whl (8.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 5.5 MB/s eta 0:00:00
Requirement already satisfied: setuptools>=36.2.1 in /home/leo/.venv/bob/lib/python3.7/site-packages (from pipenv) (40.8.0)
Collecting virtualenv-clone>=0.2.5
  Downloading virtualenv_clone-0.5.7-py3-none-any.whl (6.6 kB)
Requirement already satisfied: certifi in /home/leo/.venv/bob/lib/python3.7/site-packages (from pipenv) (2020.12.5)
Collecting distlib<1,>=0.3.1
  Downloading distlib-0.3.4-py2.py3-none-any.whl (461 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 461.2/461.2 kB 4.4 MB/s eta 0:00:00
Collecting filelock<4,>=3.2
  Downloading filelock-3.7.1-py3-none-any.whl (10 kB)
Requirement already satisfied: importlib-metadata>=0.12 in /home/leo/.venv/bob/lib/python3.7/site-packages (from virtualenv->pipenv) (3.4
.0)
Collecting platformdirs<3,>=2
  Downloading platformdirs-2.5.2-py3-none-any.whl (14 kB)
Requirement already satisfied: six<2,>=1.9.0 in /home/leo/.venv/bob/lib/python3.7/site-packages (from virtualenv->pipenv) (1.15.0)
Requirement already satisfied: typing-extensions>=3.6.4 in /home/leo/.venv/bob/lib/python3.7/site-packages (from importlib-metadata>=0.12
->virtualenv->pipenv) (3.7.4.3)
Requirement already satisfied: zipp>=0.5 in /home/leo/.venv/bob/lib/python3.7/site-packages (from importlib-metadata>=0.12->virtualenv->p
ipenv) (3.4.0)
Installing collected packages: distlib, virtualenv-clone, platformdirs, filelock, virtualenv, pipenv
  Attempting uninstall: filelock
    Found existing installation: filelock 3.0.12
    Uninstalling filelock-3.0.12:
      Successfully uninstalled filelock-3.0.12
Successfully installed distlib-0.3.4 filelock-3.7.1 pipenv-2022.5.2 platformdirs-2.5.2 virtualenv-20.14.1 virtualenv-clone-0.5.7

(bob) leo@katana:~/Desktop/Projects/Singularity/libs$ mkdir tflite_tools
(bob) leo@katana:~/Desktop/Projects/Singularity/libs$ cd tflite_tools/
(bob) leo@katana:~/Desktop/Projects/Singularity/libs/tflite_tools$ git clone https://github.com/eliberis/tflite-tools

Cloning into 'tflite-tools'...
remote: Enumerating objects: 314, done.
remote: Counting objects: 100% (22/22), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 314 (delta 15), reused 15 (delta 9), pack-reused 292
Receiving objects: 100% (314/314), 163.46 KiB | 2.27 MiB/s, done.
Resolving deltas: 100% (242/242), done.
(bob) leo@katana:~/Desktop/Projects/Singularity/libs/tflite_tools$ ll
total 12K
drwxr-xr-x 4 leo leo 4,0K juni   2 01:26 tflite-tools
drwxr-xr-x 3 leo leo 4,0K juni   2 01:26 .
drwxr-xr-x 7 leo leo 4,0K juni   2 01:26 ..

(bob) leo@katana:~/Desktop/Projects/Singularity/libs/tflite_tools$ cd tflite-tools/

(bob) leo@katana:~/Desktop/Projects/Singularity/libs/tflite_tools/tflite-tools$ ls

example_working_set.png  __init__.py  LICENSE  Pipfile  Pipfile.lock  README.md  tflite_tools  tflite_tools.py

(bob) leo@katana:~/Desktop/Projects/Singularity/libs/tflite_tools/tflite-tools$ pipenv install

Courtesy Notice: Pipenv found itself running within a virtual environment, so it will automatically use that environment, instead of creating 
its own for any project. You can set PIPENV_IGNORE_VIRTUALENVS=1 to force pipenv to ignore that environment and create its own instead. You ca
n set PIPENV_VERBOSITY=-1 to suppress this warning.
Installing dependencies from Pipfile.lock (f23fad)...
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 17/17 — 00:00:21
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

(bob) leo@katana:~/Desktop/Projects/Singularity/libs/tflite_tools/tflite-tools$ pipenv shell

Courtesy Notice: Pipenv found itself running within a virtual environment, so it will automatically use that environment, instead of creating its own for any project. You can set PIPENV_IGNORE_VIRTUALENVS=1 to force pipenv to ignore that environment and create its own instead. You can set PIPENV_VERBOSITY=-1 to suppress this warning.
Launching subshell in virtual environment...

(bob) leo@katana:~/Desktop/Projects/Singularity/libs/tflite_tools/tflite-tools$ python tflite_tools.py --help

usage: tflite_tools.py [-h] [-i INPUT_PATH] [-o OUTPUT_PATH]
                       [--clusters CLUSTERS] [--optimize]
                       [--csv CSV_OUTPUT_FOLDER] [--plot PLOT_FILE]
                       [--calc-macs] [--calc-size]

TFLite model analyser & memory optimizer

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT_PATH         input model file (.tflite)
  -o OUTPUT_PATH        output model file (.tflite)
  --clusters CLUSTERS   cluster weights into n-many values (simulate code-book
                        quantization)
  --optimize            optimize peak working set size
  --csv CSV_OUTPUT_FOLDER
                        output model analysis in CSV format into the specified
                        folder
  --plot PLOT_FILE      plot memory usage for each operator during the
                        execution
  --calc-macs           Calculate approximate MAC usage
  --calc-size           Calculate parameter size

(bob) leo@katana:~/Desktop/Projects/Singularity/libs/tflite_tools/tflite-tools$ python tflite_tools.py -i ../../../data/ml/lite-model_spice_1.tflite

Traceback (most recent call last):
  File "tflite_tools.py", line 50, in <module>
    main()
  File "tflite_tools.py", line 24, in main
    model = TFLiteModel.load_from_file(args.input_path)
  File "/home/leo/Desktop/Projects/Singularity/libs/tflite_tools/tflite-tools/tflite_tools/tflite_model.py", line 119, in load_from_file
    return cls(bytearray(f.read()))
  File "/home/leo/Desktop/Projects/Singularity/libs/tflite_tools/tflite-tools/tflite_tools/tflite_model.py", line 114, in __init__
    self._build_graph()
  File "/home/leo/Desktop/Projects/Singularity/libs/tflite_tools/tflite-tools/tflite_tools/tflite_model.py", line 174, in _build_graph
    assert op.OutputsLength() <= 1
AssertionError

KeyError: 9

Traceback` (most recent call last):
File "tflite_tools.py", line 55, in
main()
File "tflite_tools.py", line 35, in main
model.print_model_analysis()
File "/home/pi/tflite-tools/tflite_tools/tflite_model.py", line 402, in print_model_analysis
self._print_tensor_details()
File "/home/pi/tflite-tools/tflite_tools/tflite_model.py", line 329, in _print_tensor_details
if t.size != 0:
File "/home/pi/tflite-tools/tflite_tools/tflite_model.py", line 64, in size
return 0 if self.is_constant else np.prod(self.shape) * get_buffer_element_size(self.type)
File "/home/pi/tflite-tools/tflite_tools/tflite_model.py", line 47, in get_buffer_element_size
return sizes[t]
KeyError: 9

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.