Tensorflow 在 Android 平台的移植

原文連接:來自TalkingDataSDK技術博客

TensorFlow 簡介

2015 年 11 月 9 日,Google Research 發布了文章:TensorFlow - Google』s latest machine learning system, open sourced for everyone,正式宣布其新一代機器學習系統開源。

至於 Google 為什麼要開源 TensorFlow,官方的說法是:

If TensorFlow is so great, why open source it rather than keep it proprietary? The answer is simpler than you might think: We believe that machine learning is a key ingredient to the innovative products and technologies of the future. Research in this area is global and growing fast, but lacks standard tools. By sharing what we believe to be one of the best machine learning toolboxes in the world, we hope to create an open standard for exchanging research ideas and putting machine learning in products. Google engineers really do use TensorFlow in user-facing products and services, and our research group intends to share TensorFlow implementations along side many of our research publications.

Here』s Why Google Is Open-Sourcing Some Of Its Most Important Technology 文章中援引了 TensorFlow 開發者的說法:

The decision to open-source was the brainchild of Jeff Dean, who felt that the company』s innovation efforts were being hampered by the slow pace of normal science. Google researchers would write a paper, which would then be discussed at a conference some months later. Months after that somebody else would write another paper building on their work.

Dean saw that open-sourcing TensorFlow could significantly accelerate the process. Rather than having to wait for the next paper or conference, Google』s researchers could actively collaborate with the scientific community in real-time. Smart people outside of Google could also improve the source code and, by sharing machine learning techniques more broadly, it would help populate the field with more technical talent.

「Having this system open sourced we』re able to collaborate with many other researchers at universities and startups, which gives us new ideas about how we can advance our technology. Since we made the decision to open-source, the code runs faster, it can do more things and it』s more flexible and convenient,」 says Rajat Monga, who leads the TensorFlow team.

毫無意外地,TensorFlow 在 Github 上的 Repo 在很短的時間內就收穫了大量的 Star 和 Fork,學術界和工業界都對其表示了巨大的興趣,並投身於 TensorFlow 的社區和 Google 一起完善和改進 TensorFlow。

然而,當時在 Github 做基準測試、目前就職於 Facebook AI 部門的程序員 Soumith 發布了文章Benchmark TensorFlow(中文解讀),對 TensorFlow 和其他主流深度學習框架的性能進行了比較,結果差強人意。當然,Google 團隊表示會繼續優化,並在後面的版本中支持分散式。

2016 年 4 月 13 日,Google 通過文章 Announcing TensorFlow 0.8 – now with distributed computing support! 正式發布支持分散式的 TensorFlow 0.8 版本,結合之前對 CPU 和 GPU 的支持,TensorFlow 終於可以被用於實際的大數據生產環境中了。

2016 年 4 月 29 日,開發出目前最強圍棋 AI 的 Google 旗下 DeepMind 宣布:DeepMind moves to TensorFlow,這在業界被認為 TensorFlow 終於可以被當作 TensorFlow 在工業界發展的里程碑事件,極大提升了 TensorFlow 使用者的研究熱情。

The Good, Bad & Ugly of TensorFlow(中文翻譯)對目前 TensorFlow 的優缺點做了詳細的分析。

Windows 平台

Tensorflow 官方文檔 中,對 Android Demo 的編譯介紹時用到了 bazel,該工具對 Windows 平台的支持處於實驗階段,就不推薦了,Github 上有一個 使用 NDK 在 Anroid Studio 中進行編譯的示例工程,大家可以 clone 下來使用。

Ubuntu 14.04

這裡假定 Ubuntu 14.04 系統上還沒有 Android 開發環境。

安裝 Java 1.8

$ sudo apt-get install software-properties-common$ sudo add-apt-repository ppa:webupd8team/java$ sudo apt-get update$ sudo apt-get install oracle-java8-installer

配置 Java 環境變數,將下面的內容添加到 /etc/environment:

JAVA_HOME="/usr/lib/jvm/java-8-oracle"

安裝 bazel

$ echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list$ curl https://bazel.io/bazel-release.pub.gpg | sudo apt-key add -$ sudo apt-get update && sudo apt-get install bazel$ sudo apt-get upgrade bazel

詳細的說明可以參考 bazel 的官方文檔。

下載 tensorflow

$ cd ~/$ git clone https://github.com/tensorflow/tensorflow.git

之後的步驟基本來自 TensorFlow on Android 的翻譯:

下載解壓 Android SDK

$ wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz$ tar xvzf android-sdk_r24.4.1-linux.tgz -C ~/tensorflow

更新 SDK:

$ cd ~/tensorflow/android-sdk-linux# 如果希望在熟悉的 SDK Manager 中進行操作,可以去掉下面命令中的 --no-ui$ tools/android update sdk --no-ui

下載解壓 NDK

$ wget https://dl.google.com/android/repository/android-ndk-r12b-linux-x86_64.zip$ unzip android-ndk-r12b-linux-x86_64.zip -d ~/tensorflow

下載 tensorflow 的 model

$ cd ~/tensorflow$ wget https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip -O /tmp/inception5h.zip$ unzip /tmp/inception5h.zip -d tensorflow/examples/android/assets/

修改 WORKSPACE

$ gedit ~/tensorflow/WORKSPACE

反注釋 android_sdk_repository 和 android_ndk_repository 部分,用下面的內容替換:

android_sdk_repository( name = "androidsdk", api_level = 24, build_tools_version = "24.0.3", # Replace with path to Android SDK on your system path = "/home/ross/Downloads/android-sdk-linux",)android_ndk_repository( name="androidndk", path="/home/ross/Downloads/android-ndk-r12b", api_level=24)

編譯 tensorflow 的 Android Demo App:

$ cd ~/tensorflow$ bazel build //tensorflow/examples/android:tensorflow_demo

如果一切順利就會在最後看到下面的提示:

bazel-bin/tensorflow/examples/android/tensorflow_demo_deploy.jarbazel-bin/tensorflow/examples/android/tensorflow_demo_unsigned.apkbazel-bin/tensorflow/examples/android/tensorflow_demo.apkINFO: Elapsed time: 109.114s, Critical Path: 37.45s

Android Demo 分析

整個 Demo 的目錄結構和使用 Jni 的 Android 工程是相同的,在~/tensorflow/tensorflow/examples/android/jni 目錄下,放著 native 的代碼:

├── imageutils_jni.cc├── __init__.py├── rgb2yuv.cc├── rgb2yuv.h├── yuv2rgb.cc└── yuv2rgb.h

Java interface 相關的 Java 類在github.com/tensorflow/t目錄裡面,可以考慮將其直接集成到自己的項目中。

Demo 所需的 native 實現在github.com/tensorflow/t目錄裡面。

github.com/tensorflow/t裡面定義了用到的 tensorflow model,protobuf 格式,識別結果的 labels 等:

private static final Logger LOGGER = new Logger(); private static final boolean SAVE_PREVIEW_BITMAP = false; private static final String MODEL_FILE = "file:///android_asset/tensorflow_inception_graph.pb"; private static final String LABEL_FILE = "file:///android_asset/imagenet_comp_graph_label_strings.txt"; private static final int NUM_CLASSES = 1001; private static final int INPUT_SIZE = 224; private static final int IMAGE_MEAN = 117;

如果想使用自己的模型,使用 tensorflow 解決其他的問題,通過修改上面提到的代碼和模塊來完成。TensorFlow on Android 文章就提到了具體的步驟。

最後,Tensorflow 也支持移植到 iOS 應用中,可以參考 TalkingData SDK Team 的技術博客文章 iOS 開發迎來機器學習的春天— TensorFlow。


推薦閱讀:

AI Smart-V1.0.0
學習筆記TF042:TF.Learn、分散式Estimator、深度學習Estimator
一個基於 TensorFlow 的「顏值評分」開源項目:FaceRank
cs20si:tensorflow for research 學習筆記2
TensorFlow官方教程翻譯:導入數據

TAG:TensorFlow | 機器學習 | Android開發 |