Coder Social home page Coder Social logo

pinto0309 / tflite2json2tflite Goto Github PK

View Code? Open in Web Editor NEW
23.0 5.0 3.0 5.55 MB

Convert tflite to JSON and make it editable in the IDE. It also converts the edited JSON back to tflite binary.

License: Apache License 2.0

Dockerfile 100.00%
json model-converter models tensorflow tflite docker

tflite2json2tflite's Introduction

tflite2json2tflite

Convert tflite to JSON and make it editable in the IDE. It also converts the edited JSON back to tflite binary.

GitHub

Usage sample

1. Docker run

docker run --rm -it -v `pwd`:/home/user/workdir ghcr.io/pinto0309/tflite2json2tflite:latest

2. tflite to JSON

image

./flatc -t \
--strict-json \
--defaults-json \
-o workdir \
./schema.fbs -- workdir/model_float32.tflite

3. JSON edit

sed -i -e 's/Placeholder/input/g' workdir/model_float32.json
sed -i -e 's/fusion\/fusion_3\/BiasAdd/output/g' workdir/model_float32.json

4. JSON to tflite

./flatc \
-o workdir \
-b ./schema.fbs workdir/model_float32.json

rm workdir/model_float32.json

image

5. flatbuffers (flatc)

I have made my own modifications to the official flatbuffers(flatc) to preserve the accuracy of the quantization parameters output to JSON. For more information, please see this issue. tflite to JSON to tflite quantization error #1

https://github.com/google/flatbuffers

  • flatbuffers/include/flatbuffers/util.h

    • From:
      template<> inline std::string NumToString<double>(double t) {
        return FloatToString(t, 12);
      }
      template<> inline std::string NumToString<float>(float t) {
        return FloatToString(t, 6);
      }
    • To:
      template<> inline std::string NumToString<double>(double t) {
        return FloatToString(t, 12);
      }
      template<> inline std::string NumToString<float>(float t) {
        return FloatToString(t, 17);
      }
  • build

    cd flatbuffers && mkdir build && cd build
    cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ..
    make -j$(nproc)

tflite2json2tflite's People

Contributors

pinto0309 avatar

Stargazers

 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

Forkers

faustpy

tflite2json2tflite's Issues

tflite to JSON to tflite quantization error

Hi!

When I convert my int8 quantized tflite model to JSON and back the quantization scale parameters are different (they are correct up to ~4 decimals).

This makes the edgetpu compiler return an error when I try to convert it

edgetpu_compiler -m 13 -sa model_unroll.tflite
Edge TPU Compiler version 16.0.384591198
Started a compilation timeout timer of 180 seconds.
ERROR: :514 output->params.scale == 1. / 256 was not true.
ERROR: Node number 6 (LOGISTIC) failed to prepare.

Compilation failed: Model failed in Tflite interpreter. Please ensure model can be loaded/run in Tflite interpreter.
Compilation child process completed within timeout period.
Compilation failed! 

Any idea of how to fix this?

Inputs and outputs of the model before conversion
Skjermbilde 2023-02-20 kl  12 40 11

Inputs and outputs of the model after conversion
Skjermbilde 2023-02-20 kl  12 40 42

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.