Creating an activity

Click on the project and create an empty activity named MainActivity. In the layout of that activity, paste the following XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.vavinash.tensorflowsample.MainActivity">

<EditText
android:id="@+id/editNum1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="13dp"
android:layout_marginTop="129dp"
android:layout_toStartOf="@+id/button"
android:ems="10"
android:hint="a"
android:inputType="textPersonName"
android:textAlignment="center" />

<EditText
android:id="@+id/editNum2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editNum1"
android:layout_alignBottom="@+id/editNum1"
android:layout_toEndOf="@+id/button"
android:ems="10"
android:hint="b"
android:inputType="textPersonName"
android:textAlignment="center" />

<Button
android:text="Run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_below="@+id/editNum2"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Output"
android:id="@+id/txtViewResult"
android:layout_marginTop="85dp"
android:textAlignment="center"
android:layout_alignTop="@+id/button"
android:layout_centerHorizontal="true" />
</RelativeLayout>

In the mainactivity.java file, paste the following code:

package com.example.vavinash.tensorflowsample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;public class MainActivity extends AppCompatActivity {
//change with the file name of your own model generated in python tensorflow.
private static final String MODEL_FILE = "file:///android_asset/tfdroid.pb";

//here we are using this interface to perform the inference with our generated model. It internally uses c++ libraries and JNI.
private TensorFlowInferenceInterface inferenceInterface;
static {
System.loadLibrary("tensorflow_inference");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inferenceInterface = new TensorFlowInferenceInterface();
//instantiatind and setting our model file as input.
inferenceInterface.initializeTensorFlow(getAssets(), MODEL_FILE);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final EditText editNum1 = (EditText) findViewById(R.id.editNum1);
final EditText editNum2 = (EditText) findViewById(R.id.editNum2);
float num1 = Float.parseFloat(editNum1.getText().toString());
float num2 = Float.parseFloat(editNum2.getText().toString());
int[] i = {1};
int[] a = {((int) num1)};
int[] b = {((int) num2)};
//Setting input for variable a and b in our model.
inferenceInterface.fillNodeInt("a",i,a);
inferenceInterface.fillNodeInt("b",i,b);
//performing the inference and getting the output in variable c
inferenceInterface.runInference(new String[] {"c"});
//reading received output
int[] c = {0};
inferenceInterface.readNodeInt("c", c);
//projecting to user.
final TextView textViewR = (TextView) findViewById(R.id.txtViewResult);
textViewR.setText(Integer.toString(c[0]));
}
});
}
}

In the preceding program, we are loading the TensorFlow binaries using the following snippet:

System.loadLibrary("tensorflow_inference");

In the create Bundle method, we have the main logic. Here, we are creating the TensorFlow inference object by supplying the TensorFlow model's .pb file, which has been generated and we saw that in the section - create and save model

Then we registered a click event to the Run button. In this, we are feeding the values to the a and b nodes in TensorFlow and running the inference, then we fetch the value in the C node and show it to the user.

Now run the app to see the results of the (a+b)2 = c expression:

On the left side, it is showing the app's opening screen. In the provided text boxes, we need to give the a and b values. Once you click on the Run button, you will see the result in the output area.

You can get the preceding app code from the GitHub repository: https://github.com/PacktPublishing/Machine-Learning-for-Mobile/tree/master/tensorflow%20simple.
..................Content has been hidden....................

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