1

I'm trying to figure out how to use a tensorflow model from training the image_ocr example in Keras on Android. I've followed this tutorial in creating a tensorflow model (i.e. freezing the graph to creata a .pb file) to be used by the application.

The examples in TFDroid are pretty good but none of them seem to be applicable with the model I have. I have a few questions for now:

  1. What are the things to consider when you're going to use a model of your own on Android?
  2. What's the workflow from training a tensorflow model to using it on Android?
Rocket Pingu
  • 621
  • 9
  • 26

1 Answers1

1

I'm working on better documentation around this, but for now here's an extract from my current draft that may help:

In most situations, training a model with TensorFlow will give you a folder containing a GraphDef file (usually ending with the .pb or .pbtxt extension) and a set of checkpoint files. What you need for mobile or embedded deployment is a single GraphDef file that’s been ‘frozen’, or had its variables converted into inline constants so everything’s in one file. To handle the conversion, you’ll need the freeze_graph.py script, that’s held in tensorflow/pythons/tools/freeze_graph.py. You’ll run it like this:

bazel build tensorflow/tools:freeze_graph
bazel-bin/tensorflow/tools/freeze_graph \
--input_graph=/tmp/model/my_graph.pb \ --input_checkpoint=/tmp/model/model.ckpt-1000 \ --output_graph=/tmp/frozen_graph.pb \
--input_node_names=input_node \
--output_node_names=output_node \

The input_graph argument should point to the GraphDef file that holds your model architecture. It’s possible that your GraphDef has been stored in a text format on disk, in which case it’s likely to end in ‘.pbtxt’ instead of ‘.pb’, and you should add an extra --input_binary=false flag to the command.

The input_checkpoint should be the most recent saved checkpoint. As mentioned in the checkpoint section, you need to give the common prefix to the set of checkpoints here, rather than a full filename.

output_graph defines where the resulting frozen GraphDef will be saved. Because it’s likely to contain a lot of weight values that take up a large amount of space in text format, it’s always saved as a binary protobuf. output_node_names is a list of the names of the nodes that you want to extract the results of your graph from. This is needed because the freezing process needs to understand which parts of the graph are actually needed, and which are artifacts of the training process, like summarization ops. Only ops that contribute to calculating the given output nodes will be kept. If you know how your graph is going to be used, these should just be the names of the nodes you pass into Session::Run() as your fetch targets. If you don’t have this information handy, you can get some suggestions on likely outputs by running the summarize_graph tool.

Because the output format for TensorFlow has changed over time, there are a variety of other less commonly used flags available too, like input_saver, but hopefully you shouldn’t need these on graphs trained with modern versions of the framework.

Pete Warden
  • 2,866
  • 1
  • 13
  • 12
  • I thank you for your response. After trying out so many things, I already have my frozen graph. Now I want to use it for my android application. I currently have this [problem](https://stackoverflow.com/questions/46577833/using-bi-lstm-ctc-tensorflow-model-in-android). – Rocket Pingu Oct 10 '17 at 08:00