[JAVA] How to make an app using Tensorflow with Android Studio

Addendum 3: I received a correction request regarding the notation from @ 84d010m08 and merged it. Thank you very much. Currently, there seem to be two methods, TensorFlow mobile and lite. Please refer to this article.

Addendum 2: Please refer to the official explanation for a proper explanation. https://www.tensorflow.org/mobile/android_build

Postscript: The content of this article is out of date. To explain the current method very simply, write the following contents in build.gradle.

build.gradle


allprojects {
    repositories {
        jcenter()
    }
}

dependencies {
    compile 'org.tensorflow:tensorflow-android:+'
}

By doing so, you can use the TensorFlowInferenceInterface. (See the old article below for TensorFlowInferenceInterface) The names of the methods have changed a lot, so it's a good idea to look for them in the official repositories. At the time of addition, ・ Input with feed () ・ Learn with run () -Fetch output with fetch () is. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/android/java/org/tensorflow/contrib/android/TensorFlowInferenceInterface.java

Article before addition below


Although there were some articles that ran Tensorflow on Android, http://qiita.com/tomoima525/items/99a2df5cb0559c41647a http://qiita.com/ornew/items/cf7e4478936fbf28b271 http://qiita.com/ornew/items/cf7e4478936fbf28b271 http://qiita.com/miyosuda/items/e53ad2efeed0ff040606 Since there was no Japanese document on how to simply execute using the TensorFlowInferenceInterface class, it was just a memo. The method in this article allows you to create an app with just a trained model and Android Studio. There are many good articles on how to use Deep Learning and how to use Tensorflow, so please look for them.

I'm writing a lot, so I'll fix it one by one when I have time.

Official documentation

The following three. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/java/ https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android

Preparation

From the preparation of .jar and .so files. It is the same as the content of the official document below. https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android

If you don't want to put bazel https://ci.tensorflow.org/view/Nightly/job/nightly-android/ It seems that you can get the pre-built file from (unconfirmed)

First of all

git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git

Download the source of Tensorflow at

next

tensorflow/WORKSPACE

Change inside Uncomment the following part and specify the path appropriately

\# Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
    name = "androidsdk",
    api_level = 23,
    build_tools_version = "25.0.1",
    \# Replace with path to Android SDK on your system
    path = "/home/user/Android/Sdk",
)

\#Android NDK r12b is recommended (higher may cause issues with Bazel)
android_ndk_repository(
    name="androidndk",
    path="/home/user/Android/Sdk/ndk-bundle",
    api_level=14) \# This needs to be 14 or higher to compile TensorFlow.

Next, build.

bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so \
   --crosstool_top=//external:android/crosstool \
   --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
   --cpu=armeabi-v7a

Build with bazel.

--cpu=armeabi-v7a

The part of

arm64-v8a x86 x86_64

Please change to an appropriate one.

bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so

Has been generated.

next

bazel build //tensorflow/contrib/android:android_tensorflow_inference_java

so

bazel-bin/tensorflow/contrib/android/libandroid_tensorflow_inference_java.jar

A jar file will be created in.

Android Studio settings

▽libs   libandroid_tensorflow_inference_java.jar   ▽armeabi-v7a     libtensorflow_inference.so Arrange the files like this in app / build.gradle

app/build.gradle


sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
    }
}

Added

How to write code

Import-> Create Instance-> Input-> Run-> Get Output It is a procedure of.

import

import org.tensorflow.contrib.android.TensorFlowInferenceInterface;

When

static {
    System.loadLibrary("tensorflow_inference");
}

You can use it by writing.

Instance generation

public TensorFlowInferenceInterface inferenceInterface;
inferenceInterface = new TensorFlowInferenceInterface();

Create an instance with, etc.

inferenceInterface.initializeTensorFlow(getAssets(), "PATH_TO_MODEL");

Load the model from app / src / main / assets / model.pb with. Please change model.pb and "PATH_TO_MODEL" according to your environment.

Enter data

inferenceInterface.fillNodeFloat("NAME_OF_INPUT", int[] shape = {1,784},float[] input);

Enter data into the trained model with "NAME_OF_INPUT" is the name of the placeholder in the input part Tensorflow's MNIST tutorial

x = tf.placeholder(tf.float32, [None, 784],name="input")

It is "input". Similarly, int [] shape = {1,784} is part of [None, 784]. Also, float [] input is the vector string you want to input, or in MNIST, the vector string of handwritten characters. Please change it according to your own environment.

In addition to Float for the input array inferenceInterface.fillNodeInt() inferenceInterface.fillNodeByte() And so on. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/android/java/org/tensorflow/contrib/android/TensorFlowInferenceInterface.java Please find it from here.

Run

inferenceInterface.runInference(String[] {"NAME_OF_OUTPUT"});

Calculate the output from the input with. "NAME_OF_OUTPUT" is Tensorflow

y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2,name="output")

It is a name given by such as. Since there may be multiple output layers, it is String [].

Extract output

inferenceInterface.readNodeFloat("NAME_OF_OUTPUT", float[] output);

Receives the output at the location specified by "NAME_OF_OUTPUT" in float [] output. This is the same as input, and Int and Double can also be output.

in conclusion

The explanation is quite rough & there is no sample code, so I think I do not understand the reason, so I will correct it sequentially when I have time. I have prepared sample code, but since there are many parts such as the input part that I made by copying and pasting, I will add it after correcting it so that there is no copyright problem.

I think that the official explanation at the time of implementation on Android is too few and I think that there is a part that I misunderstood, so if there is a mistake, please comment.

Recommended Posts

How to make an app using Tensorflow with Android Studio
[2020 version] How to send an email using Android Studio Javamail
Make an executable jar using Android Studio
How to make an crazy Android music player
How to make an app with a plugin mechanism [C # and Java]
Make an android app. (Day 5)
How to push an app developed with Rails to Github
How to make an oleore generator using swagger codegen
How to make an almost static page with rails
App development beginners tried to make an Android calculator app
I'm making an Android app and I'm stuck with errors and how to solve it
How to take a screenshot with the Android Studio emulator
Make an android app. (First day)
How to specify db when creating an app with rails
[Android] How to make Dialog Fragment
How to make an image posted using css look like an icon
I tried to make an Android application with MVC now (Java)
How to crop an image with libGDX
[Android] How to deal with dark themes
Face recognition app with OpenCV + Android Studio
How to make asynchronous pagenations using Kaminari
How to use ExpandableListView in Android Studio
Downgrade an existing app created with rails 5.2.4 to 5.1.6
[Swift] How to link the app with Firebase
How to make Unity Native Plugin (Android version)
[Rails] How to build an environment with Docker
How to create an Excel form using a template file with Spring MVC
How to make shaded-jar
How to make an image partially transparent in Processing
How to make batch processing with Rails + Heroku configuration
How to make a factory with a model with polymorphic association
[Android Studio] How to change TextView to any font [Java]
How to unit test with JVM with source using RxAndroid
How to make LINE messaging function made with Ruby
[Android Studio] [Java] How to fix the screen vertically
How to output standard from an array with forEach
How to make Laravel faster with Docker for Mac
How to get started with creating a Rails app
I tried to make an introduction to PHP + MySQL with Docker
Let's write how to make API with SpringBoot + Docker from 0
How to easily implement in-app purchase using itemstore <Implementation: Android>
How to use trained model of tensorflow2.0 with Kotlin / Java
Make an instagram clone app ④
Java --How to make JTable
I want to push an app made with Rails 6 to GitHub
How to create a Wear OS by Google app project on Android Studio 3.0 or higher
[iOS] [Objective-C] How to update a widget from an Objective-C app
Use Java included with Android Studio to build React Native
Make an instagram clone app ②
How to get values in real time with TextWatcher (Android)
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to deploy jQuery in your Rails app using Webpacker
[swift5] Try to make an API client with various methods
Is it an Android app?
Make an instagram clone app ③
How to handle an instance
How to fix Android apps crashing with RenderThread mystery error
How to develop an app with Jersey Java RESTful API on Alibaba Cloud ECS instance
Make an instagram clone app ①
[Rails] How to make seed
What is an immutable object? [Explanation of how to make]