0%

Android TensorFlow Machine Learning Example

Article Source

Machine Learning: Integrating Tensorflow in Android

As we all know Google has open-sourced a library called TensorFlow that can be used in Android for implementing Machine Learning.

TensorFlow is an open-source software library for Machine Intelligence provided by Google.

I searched the internet a lot but did not find a simple way or a simple example to build TensorFlow for Android. After going through many resources, I was able to build it. Then, I decided to write on it so that it would not take time for others.

Credit: The classifier example has been taken from Google TensorFlow example.

This article is for those who are already familiar with machine learning and know how to the build model for machine learning(for this example I will be using a pre-trained model). Sooner, I will be writing a series of articles on machine learning so that everybody can learn how to build the model for machine learning.

Let’s start with the building process for Android
Few important pointers that we should know:
The core of the TensorFlow is written in c++.
In order to build for android, we have to use JNI(Java Native Interface) to call the c++ functions like loadModel, getPredictions, etc.

We will have a .so(shared object) file which is a c++ compiled file and a jar file which will consist of JAVA API that will be calling the native c++. And then, we will be calling the JAVA API to get things done easily.

So, we need the jar(Java API) and a .so(c++ compiled) file.

We must have the pre-trained model file and a label file for the classification.

We will be building the below object detection.

So let’s build the jar and .so file.

git clone –recurse-submodules https://github.com/tensorflow/tensorflow.git
Note: –recurse-submodules is important to pull submodules.

Download NDK from here.
Download Android SDK or we can provide the path from Android Studio SDK.
Install Bazel from here. Bazel is the primary build system for TensorFlow.
Now, edit the WORKSPACE, we can find the WORKSPACE file in the root directory of the TensorFlow that we have cloned earlier.

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 = “<PATH_TO_SDK>”,

#)
#

#android_ndk_repository(

name=”androidndk”,

path=”<PATH_TO_NDK>”,

api_level=14)

Like below with our sdk and ndk path:
android_sdk_repository(
name = “androidsdk”,
api_level = 23,
build_tools_version = “25.0.1”,

# Replace with path to Android SDK on your system
path = "/Users/amitshekhar/Library/Android/sdk/",

)
android_ndk_repository(
name=”androidndk”,
path=”/Users/amitshekhar/Downloads/android-ndk-r13/“,
api_level=14)
Then build the .so file.
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
Replacing armeabi-v7a with our desired target architecture.
The library will be located at:
bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so
To build the Java counterpart:
bazel build //tensorflow/contrib/android:android_tensorflow_inference_java

We can find the JAR file at:
bazel-bin/tensorflow/contrib/android/libandroid_tensorflow_inference_java.jar

Now we have both jar and .so file. I have already built both .so file and jar, you can directly use from the below project.
I have created a complete running sample application here.
MindorksOpenSource/AndroidTensorFlowMachineLearningExample

AndroidTensorFlowMachineLearningExample - Android TensorFlow MachineLearning Example (Building TensorFlow for Android)
github.com

But, we need the pre-trained model and label file.
In this example, we will use the Google pre-trained model which does the object detection on a given image.
We can download the model from here.

Unzip this zip file, we will get imagenet_comp_graph_label_strings.txt(label for objects) and tensorflow_inception_graph.pb (pre-trained model).

Now, create an android sample project in Android Studio.
Put imagenet_comp_graph_label_strings.txt and tensorflow_inception_graph.pb into assets folder.
Put libandroid_tensorflow_inference_java.jar in libs folder and right click and add as library.

compile files(‘libs/libandroid_tensorflow_inference_java.jar’)
Create jniLibs folder in main directory and put libtensorflow_inference.so in jniLibs/armeabi-v7a/ folder.

Now, we will be able to call TensorFlow Java API.
The TensorFlow Java API has exposed all the required methods through a class TensorFlowInferenceInterface.
Now, we have to call the TensorFlow Java API with the model path and load it.

And, then we can provide the input image to get the prediction.

To see it completely working, clone the project, build and run.

MindorksOpenSource/AndroidTensorFlowMachineLearningExample

AndroidTensorFlowMachineLearningExample - Android TensorFlow MachineLearning Example (Building TensorFlow for Android)
github.com

If you are getting any problem in building the project, connect with me, I will be happy to help.
Update: Check Creating Custom Model For Android Using TensorFlow Here.
Creating Custom Model For Android Using TensorFlow

As I had promised in my previous article on building TensorFlow for Android that I will be writing an article on How to…

blog.mindorks.com
Happy Coding :)

Thanks for reading this article. Be sure to click ❤ below to recommend this article if you found it helpful. It means a lot to me.

For more about programming, follow me and Mindorks , so you’ll get notified when we write new posts.
Check out all the Mindorks best articles here.
Also, Let’s become friends on Twitter, Linkedin, Github and Facebook.

机器学习:如何在安卓上集成 TensorFlow


授权转自 THU 数据派
作者:Amit Shekhar

翻译:梁傅淇 王军福

校对:李君

原文链接:https://blog.mindorks.com/android-tensorflow-machine-learning-example-ff0e9b2654cc#.aoq0izsg6

我们都知道,谷歌有一个开源库叫做 TensorFlow,可被用在安卓系统中实现机器学习。换言之,TensorFlow 是谷歌为机器智能提供的一个开源软件库。

我在网络上搜寻了很久,都没有找到在安卓上搭建 TensorFlow 的简单的方法或实例。仔细查阅许多资料以后,我终于可以搭建它了。于是,我决定把我搭建的过程写出来,这样其他人就不必再浪费时间了。

这篇文章是写给那些熟悉机器学习并且知道怎样为机器学习搭建模型的人的(在这个示例中我会使用一个预训练模型)。近期,我会写一系列关于机器学习的文章,这样每个人都能够学到如何为机器学习搭建模型。

从搭建安卓上的机器学习模型过程讲起

我们需要知道的几个要点:

TensorFlow 的核心是用 C++ 编写的;

为了在安卓上搭建 TensorFlow,我们需要用 JNI(Java 本地接口)来调用 C++ 函数,比如说 loadModel,getPredictions,等等;

我们会用到 .so(shared object,即共享对象)文件,它是 C++ 编译文件;还会用到 jar 文件,它由能够调用本地 C++ 的 Java API 组成。之后,我们就可以调用 Java API 轻松地把事情做好;

所以我们需要 jar(Java API)和一个 .so(C++ 编译)文件;

我们必须要有一个预训练模型文件和一个用于分类的标签文件。

我们会做以下的目标检测:

编译 jar 和 .so 文件
注意:–recurse-submodules 对于提取子模块(pull submodules)很重要。

在这里(https://developer.android.com/ndk/downloads/older_releases.html#ndk-12b-downloads)下载 NDK。

下载安卓 SDK,或者,我们也可以从 Android Studio SDK 提供路径。

安装 Bazel(https://bazel.build/versions/master/docs/install.html)。Bazel 是 TensorFlow 的主要编译系统。

现在,编辑工作空间(WORKSPACE),我们可以在早先克隆的 TesnsorFlow 根路径中找到工作空间(WORKSPACE)文件。

我们的 SDK 和 NDK 路径就跟下面一样:

然后编译生成 .so 文件:
将 armeabi-v7a 换成我们所需要的目标架构。

库会被放置在:

编译 Java 副本:
我们可以在这里找到 JAR 文件:

现在我们有了 jar 和 .so 文件。你也可以从下面的工程中直接提取使用我已经建立好的 .so 文件和 jar。

我已经在这里(https://github.com/MindorksOpenSource/AndroidTensorFlowMachineLearningExample)创建了一个完整可运行的示例应用。

但是,我们需要预训练模型和标签文件。

在这个例子中,我们会使用 Google 预训练模型,它实现了在一张给定的照片上做目标检测。

解压缩 zip 文件,我们就会得到 imagenet_comp_graph_label.strings.txt(目标标签)以及 tensorflow_inception_graph.pb(预训练模型)。

现在,在 Android Studio 上创建安卓示例工程吧。

将 imagenet_comp_graph_label.strings.txt(目标标签)以及 tensorflow_inception_graph.pb 放进 assets 文件夹。

将 libandroid_tensorflow_inference_java.jar 放进 lib 文件夹,单击右键,添加库。

在主目录新建一个 jniLibs 文件夹并且将 libtensorflow_inference.so 放到 jniLibs/armeabi-v7a 文件夹中。
现在,我们就可以调用 TensorFlow Java API 了。

TensorFlow Java API 通过 TensorFlowInferenceInterface 类开放了所有需要的方法。

现在,我们可以用模型路径调用 TensorFlow Java API 并且加载它了。

然后,我们可以输入一张图片来获取预测结果。

如果想要体会完整的流程,克隆这个项目(https://github.com/MindorksOpenSource/AndroidTensorFlowMachineLearningExample),搭建并运行它吧。

如果你在搭建这个项目的过程中有任何问题的话,联系我,我会非常乐意帮助你。

Happy Coding: )

读者问答

Q:我很疑惑要怎么连接到‘ so ’库?也没有任何一行像’ System.loadLibray ’的代码?

A:System.loadLibrary 已经写在 TensorFlow jar 中了。

Q:所以这是一个 C++ 应用还是 Java 应用啊?你用的是什么语言?

A:这是一个用 Java 语言编写的安卓应用,它通过 Java 本地接口(JNI: Java Native Interface)调用 C++ 做预测(机器学习)。

欢迎关注我的其它发布渠道