Creating and Saving the TF model

First, we first create a simple model and save its computation graph as a serialized GraphDef file. After training the model, we then save the values of its variables into a checkpoint file. We have to turn these two files into an optimized standalone file, which is all we need to use inside the Android app.

For this tutorial, we create a very simple TensorFlow graph that implements a small use case that will calculate (a+b)2=c. Here, we are saving the input as a and b, and the output as c.

To implement this sample program, we are going to use Python. So, as a prerequisite, you need to install python in your machine and install the TensorFlow libraries on your machine using pip.

Please check the software installations/appendix section of this book for instructions on how to install Python. pip is a python package manager that comes with Python.

Once you install python and set the path correctly, you can run the pip command from the command prompt. To install TensorFlow, run the following command:

pip install tensorflow

This sample might seem too simple and might not contain anything related to machine learning, but this example should be a good starting point to understand the concepts of TensorFlow and its working:

import tensorflow as tf 
a = tf.placeholder(tf.int32, name='a') # input
b = tf.placeholder(tf.int32, name='b') # input
times = tf.Variable(name="times", dtype=tf.int32, initial_value=2)
c = tf.pow(tf.add(a, b), times, name="c")
saver = tf.train.Saver()

init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) tf.train.write_graph(sess.graph_def, '.', 'tfdroid.pbtxt')

sess.run(tf.assign(name="times", value=2, ref=times)) # save the graph
# save a checkpoint file, which will store the above assignment saver.save(sess, './tfdroid.ckpt')

In the preceding program, we are creating two placeholders, named a and b, that can hold integer values. For now, just you can imagine placeholders as nodes in a tree for a decision tree. In the next line, we are creating a variable named times. We are creating this to store how many times we need to multiply the input. In this case, we are giving two as agenda is to do for (a+b)2.

In the next line, we are applying addition operation on both the a and b nodes. And for that sum, we are applying power operation and saving the result in a new node called c. To run the code, first save it in a file with the .py extension. Then execute the program using the python command, as follows:

python (filename)

Running the previous piece of code will produce two files. First, it saves the TF computation graph in a GraphDef text file called tfdroid.pbtxt. Next, it will perform a simple assignment (which normally would be done through actual learning) and save a checkpoint of the model variables in tfdroid.ckpt.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset