paint-brush
Running a TensorFlow model on iOS and Androidby@mgazar
4,492 reads
4,492 reads

Running a TensorFlow model on iOS and Android

by Mostafa GazarSeptember 6th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

After that stripping the model was as simple as running the following command
featured image - Running a TensorFlow model on iOS and Android
Mostafa Gazar HackerNoon profile picture

Shrink the model size and reduce the computational resources needed to do the inference calculations

input layer

After that stripping the model was as simple as running the following command




bazel-bin/tensorflow/tools/graph_transforms/transform_graph \—-inputs=”input_1" —-in_graph=tmp/tensorflow_inception_graph.pb \—-outputs=”output_node0" —-out_graph=tmp/quantized_graph.pb \—-transforms=’add_default_attributes strip_unused_nodes(type=float, shape=”1,299,299,3") remove_nodes(op=Identity, op=CheckNumerics) fold_constants(ignore_errors=true) fold_batch_norms fold_old_batch_norms quantize_weights strip_unused_nodes sort_by_execution_order’

Now that we have the quantized graph, to run it on iOS we can just replace the one in https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/ios/camera/data. Do not forget to add the labels text file too.

Last change you need to make is to change the fields in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/ios/camera/CameraExampleViewController.mm#L38 to match what your model expects as an input.

In our case it was

const int wanted_input_width = 299;

const int wanted_input_height = 299;

const int wanted_input_channels = 3;

const float input_mean = 0.0f;

const float input_std = 255.0f;

const std::string input_layer_name = "input_1";

const std::string output_layer_name = "output_node0";

Running it on Android is very similar too, add your quantized graph and labels there https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android/assets. And update the lines in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/ClassifierActivity.java#L61 to match what your input expects.

In a coming post I will cover how you can embed the quantized graph from the last step into your existing iOS and Android apps.

Would you be interested in learning more about this https://leanpub.com/ml-mobile