TensorFlow Android Demo 项目地址html
既然提到了 TensorFlow,那是否是得神经网络、机器学习了解下?java
若是你能坚持把 机器学习速成课程 给啃完了,以为还挺有兴趣的,那能够考虑更深刻的展开。这份 机器学习速成课程 是 Google 特别为中国开发者提供的学习教程,此次就没理由再抱怨 GFW 阻碍了咱们学习的步伐了。android
可是,想要深刻学习这块得花大功夫,虽然说是速成教程,但对我而言,就算是前提条件其实也要挺多花时间才能达到吧。因此只能是简单地整理下相关基本概念,至少得对机器学习的一些概念和大体流程的有必定的了解。git
机器学习的基本概念github
简单概括来讲,机器学习分为两个阶段:算法
训练阶段:经过对有标签的样本数据进行训练获得合适的最佳模型api
推测阶段:根据模型对无标签的样本数据进行推段获得结果(标签)网络
在对模型的进行训练的过程当中,要考虑特征跟标签的关系推断所形成的偏差,经过计算损失来反复调整计算参数来获得最佳模型。这个过程有不少方式进行,好比下图的机器学习算法训练过程用的是迭代试错方法:框架
迭代试错方法过程机器学习
想要自行设计训练一个全新的模型,对开发人员掌握的基础知识仍是不少的,可是咱们能够利用现有的开源模型,经过新的样本数据集来 retrain 一个属于本身的模型。
在具备一个合适的模型之后,就能够进入推断阶段了。好比能够考虑在 Android 平台上经过 TensorFlow 框架来实现对模型的实际应用。咱们能够找到一个识别花朵类型的模型,而后就能够实现一个属于本身的拍照识花的应用了。想一想是否是以为还不错,赶忙干了这碗鸡汤。
步子迈太大很差,下面就从 Google 为咱们提供的 TensorFlow Demo 开始,先了解一些实际的应用场景。
git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git
使用
--recurse-submodules
clone 项目是 Google 的建议,能够防止 protobuf 编译出现一些问题。
Bazel 是 Google 开源的构建工具,Bazel 听说是数倍于 maven 的性能,快速的增量构建是它的特点,目前支持 java、cpp、Go 等语言。TensorFlow 项目的构建依赖于它来构建,更多关于 Bazel 的相关内容能够了解Bazel 官网
brew install bazel
bazel version
brew upgrade bazel
其余平台安装 Bazel 按照可参考:Installing Bazel
找到从 GitHub 下载的 TensorFlow 源码文件夹 tensorflow,修改 WORKSPACE 文件
找到 WORKSPACE 文件中 NDK 和 SDK 的配置部分,反注释掉原有代码后,按照版本要求填写本身本地环境配置。
注意:NDK 版本要设置为 14b ,若是选择 NDK 16 版本编译会出现未知问题!
# Uncomment and update the paths in these entries to build the Android demo. android_sdk_repository( name = "androidsdk", api_level = 26, # Ensure that you have the build_tools_version below installed in the # SDK manager as it updates periodically. build_tools_version = "26.0.2", # Replace with path to Android SDK on your system path = "/Users/baishixian/Library/Android/sdk", ) # android_ndk_repository( name="androidndk", path="/Users/baishixian/Library/Android/sdk/android-ndk-r14b", # This needs to be 14 or higher to compile TensorFlow. # Please specify API level to >= 21 to build for 64-bit # archtectures or the Android NDK will automatically select biggest # API level that it supports without notice. # Note that the NDK version is not the API level. api_level=25)
Google 对于此问题的相关说明:The Android NDK is required to build the native (C/C++) TensorFlow code. The current recommended version is 14b, which may be found here.
NDK 16, the revision released in November 2017, is incompatible with Bazel. See here.
找到下载的 tensorflow 目录,其中 tensorflow/tensorflow/examples/android
路径下,是 Google 提供的 example 项目 for Android,咱们能够经过编译运行该项目了解在 Android 平台上实现的几个典型的应用场景实力。
经过 Android Studio 进行编译
使用 Android Studio 找到对应的项目目录打开,修改 ndk 的环境配置后,点击编译运行便可。
经过命令行进行
经过命令行进入 tensorflow 目录,也就在 WORKSPACE 所在的项目根目录。
1. 编译项目
bazel build -c opt //tensorflow/examples/android:tensorflow_demo
2. 安装应用
adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk
具体可参照项目 GitHub 上的 doc for Android examples 文档。
这是我编译好的安装文件: TensorFlow Demo
Demo
在使用 demo 过程当中以为这个实时分析相机图像数据帧来识别物品类别的演示效果挺不错的。
TensorFlower Classify
后面将利用这个已有的数据模型,从一个应用开发者的角度使用 TensorFlow 来构建一个简单的应用。
接下来请看个人另一篇文章:基于现有 TensorFlow 模型构建 Android 应用
做者:石先 连接:https://www.jianshu.com/p/834bf90036c3 來源:简书 简书著做权归做者全部,任何形式的转载都请联系做者得到受权并注明出处。