Tensorflow Lite (.tflite) のモデル構造の可視化 - Tensorflow v2.2.0版


# 1. Environment
- Ubuntu 18.04 x86_64
- Tensorflow v2.2.0
- Tensorflow Lite
- Hand Landmark 3D model - MediaPipe

# 2. Procedure

```console:
### Clone Tensorflow v2.2.0
$ cd ~
$ git clone -b v2.2.0 https://github.com/tensorflow/tensorflow.git
$ cd tensorflow

### Download hand_landmark_3d.tflite
$ wget https://github.com/google/mediapipe/raw/master/mediapipe/models/hand_landmark_3d.tflite

### Download Bazel Installer 2.0.0
$ curl -sc /tmp/cookie "https://drive.google.com/uc?export=download&id=1SsI3G-WcKdt5C3ZPNZ9dF5WvpXtxD_-q" > /dev/null && \
  CODE="$(awk '/_warning_/ {print $NF}' /tmp/cookie)" && \
  curl -Lb /tmp/cookie "https://drive.google.com/uc?export=download&confirm=${CODE}&id=1SsI3G-WcKdt5C3ZPNZ9dF5WvpXtxD_-q" -o bazel-2.0.0-installer-linux-x86_64.sh
$ sudo chmod +x bazel-2.0.0-installer-linux-x86_64.sh && ./bazel-2.0.0-installer-linux-x86_64.sh

### Run visualize
$ bazel run \
  --config=noaws \
  --config=nohdfs \
  --config=nonccl \
  //tensorflow/lite/tools:visualize \
  ${HOME}/tensorflow/hand_landmark_3d.tflite \
  ${HOME}/tensorflow/visualized_model.html
```

![FireShot Capture 034 -  - .png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/194769/6ab41939-592c-22b0-9a17-29d812f33baa.png)

# 3. Reference articles
- **[TensorFlow Lite グラフの可視化ツール(visualize.py)がうまく動作しない場合の解決方法 - Qiita - wakame1367さん](https://qiita.com/wakame1367/items/533a9f15e8e7de7c2e62)**
- **https://www.tensorflow.org/lite/guide/faq#how_do_i_inspect_a_tflite_file**
- **[Structure visualization of Tensorflow Lite model files (.tflite) - Qiita - PINTO](https://qiita.com/PINTO/items/d74d92ece31c5f3fd040)**

# 4. Finally
**`MediaPipe`** の Tensorflow Liteモデル **`BlazeFace`** をFreeze_Graphに逆コンバートするために **`flatc`** を手に入れるついでに実施。 前回記事から１年経過したので何か見た目が変わったかどうかを確認するために興味本位で実施。 が、相変わらず質素でした。

```console:flatcのバイナリ生成先
${HOME}/tensorflow/bazel-out/host/bin/external/flatbuffers/flatc

${HOME}/tensorflow/bazel-out/host/bin/external/flatbuffers/flatc --version
flatc version 1.11.0

Usage: ./host/bin/external/flatbuffers/flatc [OPTION]... FILE... [-- FILE...]
  --binary         -b    Generate wire format binaries for any data definitions.
  --json           -t    Generate text output for any data definitions.
  --cpp            -c    Generate C++ headers for tables/structs.
  --go             -g    Generate Go files for tables/structs.
  --java           -j    Generate Java classes for tables/structs.
  --js             -s    Generate JavaScript code for tables/structs.
  --dart           -d    Generate Dart classes for tables/structs.
  --ts             -T    Generate TypeScript code for tables/structs.
  --csharp         -n    Generate C# classes for tables/structs.
  --python         -p    Generate Python files for tables/structs.
  --lobster              Generate Lobster files for tables/structs.
  --lua            -l    Generate Lua files for tables/structs.
  --rust           -r    Generate Rust files for tables/structs.
  --php                  Generate PHP files for tables/structs.
  --kotlin               Generate Kotlin classes for tables/structs.
  --jsonschema           Generate Json schema.
  --swift                Generate Swift files for tables/structs.
  -o PATH                Prefix PATH to all generated files.
  -I PATH                Search for includes in the specified path.
  -M                     Print make rules for generated files.
  --version              Print the version number of flatc and exit.
  --strict-json          Strict JSON: field names must be / will be quoted,
                         no trailing commas in tables/vectors.
  --allow-non-utf8       Pass non-UTF-8 input through parser and emit nonstandard
                         \x escapes in JSON. (Default is to raise parse error on
                         non-UTF-8 input.)
  --natural-utf8         Output strings with UTF-8 as human-readable strings.
                         By default, UTF-8 characters are printed as \uXXXX escapes.
  --defaults-json        Output fields whose value is the default when
                         writing JSON
  --unknown-json         Allow fields in JSON that are not defined in the
                         schema. These fields will be discared when generating
                         binaries.
  --no-prefix            Don't prefix enum values with the enum type in C++.
  --scoped-enums         Use C++11 style scoped and strongly typed enums.
                         also implies --no-prefix.
  --gen-includes         (deprecated), this is the default behavior.
                         If the original behavior is required (no include
                         statements) use --no-includes.
  --no-includes          Don't generate include statements for included
                         schemas the generated file depends on (C++ / Python).
  --gen-mutable          Generate accessors that can mutate buffers in-place.
  --gen-onefile          Generate single output file for C# and Go.
  --gen-name-strings     Generate type name functions for C++.
  --gen-object-api       Generate an additional object-based API.
  --gen-compare          Generate operator== for object-based API types.
  --gen-nullable         Add Clang _Nullable for C++ pointer. or @Nullable for Java
  --java-checkerframe    work Add @Pure for Java.
  --gen-generated        Add @Generated annotation for Java
  --gen-all              Generate not just code for the current schema files,
                         but for all files it includes as well.
                         If the language uses a single file for output (by default
                         the case for C++ and JS), all code will end up in this one
                         file.
  --cpp-include          Adds an #include in generated file.
  --cpp-ptr-type T       Set object API pointer type (default std::unique_ptr).
  --cpp-str-type T       Set object API string type (default std::string).
                         T::c_str(), T::length() and T::empty() must be supported.
                         The custom type also needs to be constructible from std::string
                         (see the --cpp-str-flex-ctor option to change this behavior).
  --cpp-str-flex-ctor     Don't construct custom string types by passing std::string
                         from Flatbuffers, but (char* + length).
  --cpp-std CPP_STD      Generate a C++ code using features of selected C++ standard.
                         Supported CPP_STD values:
                          * 'c++0x' - generate code compatible with old compilers;
                          * 'c++11' - use C++11 code generator (default);
                          * 'c++17' - use C++17 features in generated code (experimental).
  --object-prefix        Customise class prefix for C++ object-based API.
  --object-suffix        Customise class suffix for C++ object-based API.
                         Default value is "T".
  --no-js-exports        Removes Node.js style export lines in JS.
  --goog-js-export       Uses goog.exports* for closure compiler exporting in JS.
  --es6-js-export        Uses ECMAScript 6 export style lines in JS.
  --go-namespace         Generate the overrided namespace in Golang.
  --go-import            Generate the overrided import for flatbuffers in Golang
                         (default is "github.com/google/flatbuffers/go").
  --raw-binary           Allow binaries without file_indentifier to be read.
                         This may crash flatc given a mismatched schema.
  --size-prefixed        Input binaries are size prefixed buffers.
  --proto                Input is a .proto, translate to .fbs.
  --proto-namespace-suffix Add this namespace to any flatbuffers generated
    SUFFIX                 from protobufs.
  --oneof-union          Translate .proto oneofs to flatbuffer unions.
  --grpc                 Generate GRPC interfaces for the specified languages.
  --schema               Serialize schemas instead of JSON (use with -b).
  --bfbs-comments        Add doc comments to the binary schema files.
  --bfbs-builtins        Add builtin attributes to the binary schema files.
  --conform FILE         Specify a schema the following schemas should be
                         an evolution of. Gives errors if not.
  --conform-includes     Include path for the schema given with --conform PATH
  --include-prefix       Prefix this path to any generated include statements.
    PATH
  --keep-prefix          Keep original prefix of schema include statement.
  --no-fb-import         Don't include flatbuffers import statement for TypeScript.
  --no-ts-reexport       Don't re-export imported dependencies for TypeScript.
  --short-names          Use short function names for JS and TypeScript.
  --reflect-types        Add minimal type reflection to code generation.
  --reflect-names        Add minimal type/name reflection.
  --root-type T          Select or override the default root_type
  --force-defaults       Emit default values in binary output from JSON
  --force-empty          When serializing from object API representation,
                         force strings and vectors to empty rather than null.
  --force-empty-vectors  When serializing from object API representation,
                         force vectors to empty rather than null.
  --flexbuffers          Used with "binary" and "json" options, it generates
                         data using schema-less FlexBuffers.
FILEs may be schemas (must end in .fbs), binary schemas (must end in .bfbs),
or JSON files (conforming to preceding schema). FILEs after the -- must be
binary flatbuffer format files.
Output files are named using the base file name of the input,
and written to the current directory or the path given by -o.
example: ./host/bin/external/flatbuffers/flatc -c -b schema1.fbs schema2.fbs data.json
```

```console:schema.fbsのパス
${HOME}/tensorflow/lite/schema/schema.fbs
```