Tensorflow 在 Android 平台的移植

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 在很短的时间内就收获了大量的 StarFork,学术界和工业界都对其表示了巨大的兴趣,并投身于 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_repositoryandroid_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.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 的目录结构和使用 Jni 的 Android 工程是相同的,在 ~/tensorflow/tensorflow/examples/android/jni 目录下,放着 native 的代码:

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

Java interface 相关的 Java 类在 https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android 目录里面,可以考虑将其直接集成到自己的项目中。

Demo 所需的 native 实现在 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 格式,识别结果的 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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,568评论 25 707
  • 人们常说情深自然共白首 而你住我心房多年 可我还在你的防线外 不论四季如何更迭 春花秋月,夏蝉冬雪 依旧无法进入你...
    青染i阅读 215评论 0 1
  • 你在这边难以入眠,你那么爱他,他都知道吗?
    蓝眼睛的瞎子阅读 348评论 0 0
  • 小女孩 总被 有点儿小坏的男孩 莫名吸引 憨少年 总为 鬼马精灵的女孩 神魂颠倒 他们说 这是 那万有引力 在捣鬼
    雪莉诗话阅读 231评论 5 11
  • 现在的我,依然心委屈,气难平。我明白生气对身体不好,死的早。我很爱惜自己的身体,深切地知道一个健康的身体对于我来说...
    我是张老师阅读 781评论 1 13