All right reserved @ Jaewook Kang 2018
This repository provides a set of python scripts for
-
Easy tflite conversion from Tensorflow models.
-
Several use-case examples of tflite API -
Some others for fun!
- Tensorflow >= 1.7 (Git clone required)
- Bazel == 0.14.1
- Python2 <= 2.7.12
- Python3 <= 3.6.0
- Validated at OSX 10.11.6
- Validated at Ubuntu 16.04 LTS
- Windows 10 is not validated.
- Git clone from the Tensorflow Repository
$ git clone https://github.com/tensorflow/tensorflow
-
First note that you require to install JAVA JDK8 or later
# for installation
$ brew install bazel
# for upgrade
$ brew upgrade bazel
# for installation
$ echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
$ curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
$ sudo apt-get update && sudo apt-get install bazel
# for upgrade
$ sudo apt-get upgrade bazel
- Git clone this repository
$ git clone https://github.com/jwkanggist/tensorflowlite
tensorflowlite/ -- tflite-convertor/ (git submodule)
|- data/ (Storage for data set)
|- pb_and_ckpt/ (Storage for pb and ckpt files)
|- tf-cnn-model/ (git submoduled from https://github.com/jwkanggist/tf-cnn-model)
|- tf_logs/ (for Tensorboard use)
|
|- run_lenet5_save_pb_convertor.py
|- run_frozen_pb_convertor.py
|- run_tflite_convertor.py
We now support three-step model coversion from Tensorflow.
For the first conversion, you should check whether you built the toco converter
from
``
$ cd tensorflow/contrib/lite/toco
$ bazel build toco
1) graphdef and checkpoint exporting --> 2) frozen graph conversion --> 3) tflite conversion
where we wrap some details of shell commands for the tflite conversion, providing a set of python scripts.
All scripts are python, so you can run those with python
command:
$ python <script_name>
run_lenet5_save_pb_convertor.py
:- To export graphdef (.pb) and checkpoint (.ckpt) files from a Tensorflow model.
- where we generate a Lenet5 model in Tensorflow, provided by the author's repo.
run_frozen_pb_convertor.py
:- To generate frozen graph (.pb) by combining graphdef (.pb) and checkpoint (.ckpt)
- Where we use (.pb) and (.ckpt) generated by
run_lenet5_save_pb_convertor.py
run_tflite_convertor.py
:- To convert frozen graph to tflite format
- Where we use a frozen graph file generated by
run_frozen_pb_convertor
Note that you can apply these scripts to your own .tflite
conversion
once you have your own graphdef and checkpoint files.
In other ways, you can use tf.contrib.toco_convert
for direct .tflife
-conversion from Tensorflow scripts.
- For detail, see related Tensorflow API document.
Xcode version must be specified to use an Apple CROSSTOOL. Namely, when you update your Xcode, you must get an ERROR message like the below:
ERROR:/private/var/tmp/_bazel_Kakadu/3f0c35881c95d2c43f04614911c03a57/external/local_config_cc/BUILD:49:5: in apple_cc_toolchain rule @local_config_cc//:cc-compiler-darwin_x86_64: Xcode version must be specified to use an Apple CROSSTOOL.
In this case, follows the below command lines, and then rebuild the toco converter.
$ bazel clean --expunge
$ sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
$ sudo xcodebuild -license
$ bazel clean --expunge
$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
- Issues: report issues, bugs, and request new features
- Pull request
- Email: jwkang10@gmail.com
- Apach License 2.0
Jaewook Kang Ph.D.