Tensorflow 在 Android 平臺的移植

TensorFlow 簡介

2015 年 11 月 9 日陆错,Google Research 發(fā)布了文章:TensorFlow - Google’s latest machine learning system, open sourced for everyone,正式宣布其新一代機器學(xué)習(xí)系統(tǒng)開源腊瑟。

至于 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 開發(fā)者的說法:

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 在很短的時間內(nèi)就收獲了大量的 StarFork,學(xué)術(shù)界和工業(yè)界都對其表示了巨大的興趣蒲祈,并投身于 TensorFlow 的社區(qū)和 Google 一起完善和改進 TensorFlow甘萧。

然而,當(dāng)時在 Github 做基準(zhǔn)測試梆掸、目前就職于 Facebook AI 部門的程序員 Soumith 發(fā)布了文章 Benchmark TensorFlow中文解讀)扬卷,對 TensorFlow 和其他主流深度學(xué)習(xí)框架的性能進行了比較,結(jié)果差強人意酸钦。當(dāng)然怪得,Google 團隊表示會繼續(xù)優(yōu)化,并在后面的版本中支持分布式卑硫。

2016 年 4 月 13 日徒恋,Google 通過文章 Announcing TensorFlow 0.8 – now with distributed computing support! 正式發(fā)布支持分布式的 TensorFlow 0.8 版本,結(jié)合之前對 CPU 和 GPU 的支持欢伏,TensorFlow 終于可以被用于實際的大數(shù)據(jù)生產(chǎn)環(huán)境中了入挣。

2016 年 4 月 29 日,開發(fā)出目前最強圍棋 AI 的 Google 旗下 DeepMind 宣布:DeepMind moves to TensorFlow颜懊,這在業(yè)界被認(rèn)為 TensorFlow 終于可以被當(dāng)作 TensorFlow 在工業(yè)界發(fā)展的里程碑事件财岔,極大提升了 TensorFlow 使用者的研究熱情。

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

Windows 平臺

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

Ubuntu 14.04

這里假定 Ubuntu 14.04 系統(tǒng)上還沒有 Android 開發(fā)環(huán)境酿雪。

安裝 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 環(huán)境變量,將下面的內(nèi)容添加到 /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

詳細(xì)的說明可以參考 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_repositoryandroid_ndk_repository 部分,用下面的內(nèi)容替換:

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.jar
bazel-bin/tensorflow/examples/android/tensorflow_demo_unsigned.apk
bazel-bin/tensorflow/examples/android/tensorflow_demo.apk
INFO: Elapsed time: 109.114s, Critical Path: 37.45s

Android Demo 分析

整個 Demo 的目錄結(jié)構(gòu)和使用 Jni 的 Android 工程是相同的州丹,在 ~/tensorflow/tensorflow/examples/android/jni 目錄下醋安,放著 native 的代碼:

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

Java interface 相關(guān)的 Java 類在 https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android 目錄里面杂彭,可以考慮將其直接集成到自己的項目中。

Demo 所需的 native 實現(xiàn)在 https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android/jni 目錄里面吓揪。

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowImageListener.java 里面定義了用到的 tensorflow model亲怠,protobuf 格式,識別結(jié)果的 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 應(yīng)用中,可以參考 TalkingData SDK Team 的技術(shù)博客文章 iOS 開發(fā)迎來機器學(xué)習(xí)的春天--- TensorFlow放棒。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末姻报,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子间螟,更是在濱河造成了極大的恐慌吴旋,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件厢破,死亡現(xiàn)場離奇詭異荣瑟,居然都是意外死亡,警方通過查閱死者的電腦和手機摩泪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門笆焰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人见坑,你說我怎么就攤上這事嚷掠。” “怎么了荞驴?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵不皆,是天一觀的道長。 經(jīng)常有香客問我熊楼,道長霹娄,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任鲫骗,我火速辦了婚禮犬耻,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘执泰。我一直安慰自己枕磁,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布术吝。 她就那樣靜靜地躺著计济,像睡著了一般晴楔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上峭咒,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天,我揣著相機與錄音纪岁,去河邊找鬼凑队。 笑死,一個胖子當(dāng)著我的面吹牛幔翰,可吹牛的內(nèi)容都是我干的漩氨。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼遗增,長吁一口氣:“原來是場噩夢啊……” “哼叫惊!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起做修,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤霍狰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后饰及,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蔗坯,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年燎含,在試婚紗的時候發(fā)現(xiàn)自己被綠了宾濒。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡屏箍,死狀恐怖绘梦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情赴魁,我是刑警寧澤卸奉,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站尚粘,受9級特大地震影響择卦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜郎嫁,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一秉继、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧泽铛,春花似錦尚辑、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽月褥。三九已至,卻和暖如春瓢喉,著一層夾襖步出監(jiān)牢的瞬間宁赤,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工栓票, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留决左,地道東北人。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓走贪,卻偏偏與公主長得像佛猛,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子坠狡,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,074評論 25 707
  • 人們常說情深自然共白首 而你住我心房多年 可我還在你的防線外 不論四季如何更迭 春花秋月继找,夏蟬冬雪 依舊無法進入你...
    青染i閱讀 235評論 0 1
  • 你在這邊難以入眠凯亮,你那么愛他缩搅,他都知道嗎?
    藍(lán)眼睛的瞎子閱讀 390評論 0 0
  • 小女孩 總被 有點兒小壞的男孩 莫名吸引 憨少年 總為 鬼馬精靈的女孩 神魂顛倒 他們說 這是 那萬有引力 在搗鬼
    雪莉詩話閱讀 240評論 5 11
  • 現(xiàn)在的我触幼,依然心委屈硼瓣,氣難平。我明白生氣對身體不好置谦,死的早堂鲤。我很愛惜自己的身體,深切地知道一個健康的身體對于我來說...
    我是張老師閱讀 870評論 1 13