ConvolutionNeuralNetwork(CNN) in Android — tictactoe.ai (Part 1) | by Harsh Patel | Towards Data Science

Google Tensorflow has now been used in Android for integrating Machine Learning Model.
So I want to create an Android App with Tensorflow, as other people just use the pre-trained model of the Tensorflow and integrate it inside the app. But I want to try something new, so have trained my own model and learned how Tensorflow interacts with Android. To know how to save the model for mobile usage.
I have implemented code for Convolutional Neural Network to classify X and O for TicTacToe Game.
Input image size is 28x28 and Output layer size is 2. As we have to classify the image in X and O.
While building the model you have to define which is your output node and which is your input node, give those nodes some specify name.
Then training of the model, I have trained my model on 6000 different images of X and O, run over 3 epochs
As the Trained model has trained weight which gives the perfect classification of the X and O. Now, we have to save the graph of the Tensorflow with the weights.
While freezing a graph we require a list of Input and Output node name, that why we have given a name to those to input and output nodes, which helpful while freezing the graph.
Frozen Graph consist of sequence of operation that has to perform to get the output. Operation consist of the matrix multiplication, addition and reshaping of the matrix with the frozen weight of trained model.
Below is the code of freezing a save model.
freeze_graph.freeze_graph('out/' + MODEL_NAME + '.pbtxt', None, False,'out/' + MODEL_NAME + '.chkp', output_node_name, "save/restore_all","save/Const:0", 'out/frozen_' + MODEL_NAME + '.pb', True, "")input_graph_def = tf.GraphDef()with tf.gfile.Open('out/frozen_' + MODEL_NAME + '.pb', "rb") as f:    input_graph_def.ParseFromString(f.read())input_node_names = ['input']    output_graph_def = optimize_for_inference_lib.optimize_for_inference(        input_graph_def, input_node_names, [output_node_name],        tf.float32.as_datatype_enum)with tf.gfile.FastGFile('out/opt_' + MODEL_NAME + '.pb', "wb") as f:    f.write(output_graph_def.SerializeToString())
Check out the frozen graph of my project here
Starting with Android, Firstly, we have integrated Tensorflow library just add it to dependencies.
dependencies {.....implementation ‘org.tensorflow:tensorflow-android:+’}
As Tensorflow is coded in C++, so you have to integrate NDK in our Android project. Then copy the model in the asset folder of the android directory.
How to run the graph in the Android using Java?
// added the path of modelprivate static final String MODEL_FILE_CNN = "file:///android_asset/opt_xo_differ_v2.pb";//declare the input and outputprivate static final String INPUT_NODE = "input";private static final String OUTPUT_NODE = "output"private TensorFlowInferenceInterface inferenceInterface;//load graphinferenceInterface = new TensorFlowInferenceInterface(getAssets(),MODEL_FILE_CNN);
Loading the model .pd file and create a TensorFlowInferenceInterface object
For detecting, I have created a method which reads Image pixels in a float. As DrawView is a canvas which gives us the pixels value of canvas drawn image then converting them into float and pass it to TensorflowInferenceInterface object.
So first you have to feed the data in Input Node (input_node and keep_prob), then run the InferenceInterface with the output_node
public float[] getDetectofDV(DrawView d){float pixels[] = d.getPixelData();inferenceInterface.feed(INPUT_NODE, pixels,1,28,28,1);inferenceInterface.feed("keep_prob", new float[] { 1 });final Operation operation = inferenceInterface.graphOperation(OUTPUT_NODE);final int numClasses = (int) operation.output(0).shape().size(1);// this run the operation of the graphinferenceInterface.run(new String[]{OUTPUT_NODE},false);float [] outputarray = new float[numClasses];//get the outputinferenceInterface.fetch(OUTPUT_NODE,outputarray);return outputarray;}
outputarray returns the answer of the detection which has 2 values if one index 0 value in greater than index 1. Then the image is detected to be ‘O’, else it is ‘X’.
This is the result after integrating the model with Android. As if work on touch listener when you pick up your finger from the canvas it pass the image for the classification and you will get the output in below matrix.
Check out the application on playstore. https://play.google.com/store/apps/details?id=com.harsh.xo
First I will try to reduce the size of the model. Model size of CNN is 17MB as it took a lot of space in the application. And the final size of the application 55 MB.
Working on integrating new model in the same application. A single user can play with the computer.