Search code examples
tensorflowcoremltensorflow-litemlmodel

Freeze TensorFlow graph to use in iOS app


I have the below files: 1. retrained_graph.pb 2. retrained_labels.txt 3. _retrain_checkpoint.meta 4. _retrain_checkpoint.index 5. _retrain_checkpoint.data-00000-of-00001 6. checkpoint

Command Executed:

python freeze_graph.py 
--input_graph=/Users/saurav/Desktop/example/tmp/retrained_graph.pb 
--input_checkpoint=./_retrain_checkpoint 
--output_graph=/Users/saurav/Desktop/example/tmp/frozen_graph.pb --output_node_names=softmax

Getting error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 44: invalid start byte

Here are screenshots: enter image description here


Solution

  • Finally I found the answer. To freeze a graph you need to build with "bazel". 1. Install bazel by using homebrew. brew install bazel 2. If you don't have homebrew get it installed.

    /usr/bin/ruby -e "$(curl -fsSL \
    https://raw.githubusercontent.com/Homebrew/install/master/install)" 
    
    1. Clone tensorflow by command git clone https://github.com/tensorflow/tensorflow
    2. Change directory to tensorflow in terminal
    3. run command ./Configure. It asks few questions answer according your need. Most of them you can type "NO". It asks default path to Python you need to specify the path or just hit "enter".
    4. Now build bazel for freeze_graph using command: bazel build tensorflow/python/tools:freeze_graph
    5. Keep the retrained graph and checkpoints in folder.
    6. Run bazel command to freeze the graph.

    bazel-bin/tensorflow/python/tools/freeze_graph \ --input_graph=YouDirectory/retrained_graph.pb \ --input_checkpoint=YouDirectory/_retrain_checkpoint \ --output_graph=YouDirectory/frozen_graph.pb