如何在 Android Things 上运行 TensorFlow Inference,例如对图像进行分类? [英] How to run TensorFlow Inference on Android Things for example classifying images?

查看:29
本文介绍了如何在 Android Things 上运行 TensorFlow Inference,例如对图像进行分类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Things 是否支持 TensorFlow?TensorFlow Android 示例可以移植到 Android Things 上运行吗?如果是这样,最简单的方法是什么?

Is TensorFlow supported on Android Things? Can TensorFlow Android samples be ported to run on Android Things? If so, what’s the simplest way to do it?

推荐答案

简短的回答:是的,您确实可以在运行 Android Things(例如 Raspberry Pi 3)的嵌入式设备上运行 TensorFlow.我们已将 TensorFlow 图像分类应用程序移植到 Android事物.它位于:https://github.com/androidthings/sample-tensorflow-imageclassifier.

The short answer: Yes, you can indeed run TensorFlow on embedded devices running Android Things such as Raspberry Pi 3. We have ported the TensorFlow image classification app to Android Things. It is available at: https://github.com/androidthings/sample-tensorflow-imageclassifier.

长答案:为了运行 TensorFlow,您首先需要构建模型图并针对大型输入数据集运行训练.完成后,您可以根据经过训练的模型运行推理以做出明智的决策.训练的第一部分是资源匮乏和密集型的,通常是事先完成的,而推理部分相对较轻.这是已移植到运行 Android Things 的设备上运行的推理部分.

The long answer: In order to run TensorFlow you first need to build model graph and run training against large input dataset. Once that’s done, you run inference based on the trained model to make intelligent decisions. The first part in training is resources hungry and intensive, typically done beforehand while the inference part is relatively lightweight. It is the inference part that has been ported to run on devices running Android Things.

以下总结了将 TensorFlow 集成到您的 Android Things 项目时需要执行的操作:

The following summarizes what you need to do when integrating TensorFlow into your Android Things project:

  • 首先,将 TensorFlow Android Inference 库文件添加到您的项目中:Tensorflow-Android-Inference-alpha-debug.aar

  • First, add the TensorFlow Android Inference library file into your project: Tensorflow-Android-Inference-alpha-debug.aar

接下来,在您的应用的 build.gradle 文件中添加一个依赖项,如下所示:编译(名称:'Tensorflow-Android-Inference-alpha-debug',ext:'aar')

Next, add a dependency in your app’s build.gradle file like this: compile(name: 'Tensorflow-Android-Inference-alpha-debug', ext: 'aar')

要使用 TensorFlow Android Inference 库,您需要实例化 org.tensorflow.contrib.android.TensorFlowInferenceInterface 实例以公开以下方法:

To utilize the TensorFlow Android Inference library, you instantiate an org.tensorflow.contrib.android.TensorFlowInferenceInterface instance in order to expose the following methods:

  • initializeTensorFlow:使用模型图作为输入初始化 TensorFlow 对象
  • fillNodeFloat:将输入数据复制到 TensorFlow 输入数组中
  • runInference:运行推理并将结果保存在 TensorFlow 输出数组中
  • readNodeFloat:从 TensorFlow 输出数组中读取并保存到您自己的数组中

查看图像分类器示例的 TensorFlowImageClassifier.java 中的实现,了解如何将输入传递给 TensorFlow、运行推理,然后从 TensorFlow 中提取输出标签.例如,在我们的图像分类示例中,我们的应用可以在显示狗的图像时检测出哪些品种的狗.

Check out the implementation in TensorFlowImageClassifier.java of the image classifier sample to see how you can pass inputs to TensorFlow, run inference, and then extract output labels from TensorFlow. For example in our image classification sample, our app can detect what breeds of dogs when shown an image of a dog.

示例中的模型图使用 Google Inception V3 TensorFlow 模型构建,训练集包含来自 ImageNet 的 120 万张图像.如果您想构建自己的模型图,请务必在分类器实现中相应地更新模型文件、标签文件和输入/输出名称.

The model graph in the sample is built using Google Inception V3 TensorFlow model with a training set of 1.2 million images from ImageNet. If you like to build your own model graph, be sure to update the model file, the label file, and input/output names accordingly in your classifier implementation.

简而言之,上述使用 TensorFlow Android Inference 库作为 gradle 依赖项的方法提供了一种快速简便的方法,可以将 TensorFlow 核心功能添加到任何 Android 项目中,甚至是 具有本机代码并需要 NDK 集成的 TensorFlow Android 示例.这种方法还将主机平台扩展到那些目前没有像 Windows 那样完全支持 Bazel 的平台.

In short the approach outlined above using TensorFlow Android Inference library as a gradle dependency offers a quick and easy way to add TensorFlow core functionalities into any Android projects, even complex projects such as the TensorFlow Android samples that have native code and require NDK integration. This approach also extends host platforms to those that currently do not have full Bazel support like Windows.

这篇关于如何在 Android Things 上运行 TensorFlow Inference,例如对图像进行分类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆