TensorFlow Lite( [https://www.tensorflow.org/mobile/tflite](https://www.tensorflow.org/mobile/tflite) )是一种轻量级解决方案,可在移动和嵌入式设备上运行深度学习模型。 如果可以将 TensorFlow 或 Keras 内置的模型成功转换为 TensorFlow Lite 格式,请基于 FlatBuffers( [https://google.github.io/flatbuffers](https://google.github.io/flatbuffers) ),与 ProtoBuffers ProtoBuffers 类似,但 相似,但速度更快,并且大小要小得多。 *检测对象及其位置* 和,然后可以期望模型以低延迟和较小的二进制大小运行。 在您的移动应用程序中使用 TensorFlow Lite 的基本工作流程如下:
[TensorFlow Lite](https://www.tensorflow.org/mobile/tflite) 是一种轻量级解决方案,可在移动和嵌入式设备上运行深度学习模型。 如果可以将 TensorFlow 或 Keras 内置的模型成功转换为 TensorFlow Lite 格式,请基于 [FlatBuffers](https://google.github.io/flatbuffers),与 ProtoBuffers ProtoBuffers 类似,但速度更快,并且大小要小得多。 *检测对象及其位置* 和,然后可以期望模型以低延迟和较小的二进制大小运行。 在您的移动应用程序中使用 TensorFlow Lite 的基本工作流程如下:
1. 使用 TensorFlow 或 Keras 以 TensorFlow 作为后端来构建和训练(或重新训练)TensorFlow 模型,例如我们在前几章中训练的模型。
You can also pick a prebuilt TensorFlow Lite model, such as the MobileNet models available at [https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet_v1.md](https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet_v1.md), which we used for retraining in [Chapter 2](../Text/02.html), *Classifying Images with Transfer Learning*. Each of the MobileNet model `tgz` files that you can download there contains a converted TensorFlow Lite model. For example, the `MobileNet_v1_1.0_224.tgz` file contains a `mobilenet_v1_1.0_224.tflite` file that you can use directly on mobile. If you use such a prebuilt TensorFlow Lite model, you can skip steps 2 and 3.
3. 使用 TensorFlow Lite 转换器工具将 TensorFlow 模型转换为 TensorFlow Lite 模型。 在下一节中,您将看到一个详细的示例。
4. 在 iOS 或 Android 上部署 TensorFlow Lite 模型-对于 iOS,使用 C ++ API 加载和运行模型; 对于 Android,请使用 Java API(围绕 C ++ API 的包装器)加载和运行模型。 与我们之前在 TensorFlow Mobile 项目中使用的`Session`类不同,C ++和 Java API 均使用 TensorFlow-lite 特定的`Interpreter`类来推断模型。 在接下来的两个部分中,我们将向您展示 iOS C ++代码和 Android Java 代码以使用`Interpreter`。
...
...
@@ -58,7 +58,7 @@ If you run a TensorFlow Lite model on Android, and if the Android device is Andr
有两个用于 iOS 的 TensorFlow Lite 示例应用程序,名为[simple and camera]的,类似于 TensorFlow Mobile iOS 应用程序 simple 和 camera,但在 TensorFlow 1.5-1.8 的官方版本中的 TensorFlow Lite API 中实现 [https://github.com/tensorflow/tensorflow/releases](https://github.com/tensorflow/tensorflow/releases) ,并且可能也在最新的 TensorFlow 仓库中。 您可以运行以下命令来准备和运行这两个应用,类似地在[的“ iOS 演示应用”下进行了记录:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite):
有两个用于 iOS 的 TensorFlow Lite 示例应用程序,名为[simple and camera]的,类似于 TensorFlow Mobile iOS 应用程序 simple 和 camera,[但在 TensorFlow 1.5-1.8 的官方版本中的 TensorFlow Lite API 中实现](https://github.com/tensorflow/tensorflow/releases),并且可能也在最新的 TensorFlow 仓库中。 您可以运行以下命令来准备和运行这两个应用,类似地在[“iOS 演示应用”](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite)下进行了记录:
如果要使用 Apple 预先训练的 MobileNet Core ML 模型,请在[https:// developer 上查看 Apple 不错的示例代码项目*使用 Vision 和 Core ML* 对图像进行分类。 apple.com/documentation/vision/classifying_images_with_vision_and_core_ml](https://developer.apple.com/documentation/vision/classifying_images_with_vision_and_core_ml) ,还观看 [https://developer.apple.com/machine-learning](https://developer.apple.com/machine-learning)上列出的有关核心 ML 的 WWDC 2017 视频。
如果要使用 Apple 预先训练的 MobileNet Core ML 模型,请在[这个页面](https://developer.apple.com/documentation/vision/classifying_images_with_vision_and_core_ml)上查看 Apple 不错的示例代码项目,它使用 Vision 和 Core ML 对图像进行分类,还观看[这个页面](https://developer.apple.com/machine-learning)上列出的有关核心 ML 的 WWDC 2017 视频。
有关转换器工具的更多详细信息,请参见其在线文档,网址为 [https://apple.github.io/coremltools/coremltools.converters.html](https://apple.github.io/coremltools/coremltools.converters.html)。 现在,我们可以将这两个模型添加到 Objective-C 或 Swift iOS 应用程序中,但是我们仅在此处显示 Swift 示例。 您将在下一节中看到使用从 Keras 和 TensorFlow 模型转换而来的股票预测 Core ML 模型得到的 Objective-C 和 Swift 示例。
有关转换器工具的更多详细信息,请参见其[在线文档](https://apple.github.io/coremltools/coremltools.converters.html)。 现在,我们可以将这两个模型添加到 Objective-C 或 Swift iOS 应用程序中,但是我们仅在此处显示 Swift 示例。 您将在下一节中看到使用从 Keras 和 TensorFlow 模型转换而来的股票预测 Core ML 模型得到的 Objective-C 和 Swift 示例。
...
...
@@ -567,7 +567,7 @@ override func viewDidLoad() {
coremltools 工具还正式支持转换使用 Keras 构建的模型(请参见[https://apple.github.io/coremltools/coremltools.converters.html](https://apple.github.io/coremltools/coremltools.converters.html) 上的 keras.convert 链接)。 截至 2018 年 3 月,最新版本的 coremltools 0.8 的版本可与 TensorFlow 1.4 和 Keras 2.1.5 配合使用,我们在[第 8 章](../Text/08.html)和*预测中使用了 Keras 股票预测模型 RNN* 的股票价格。 您可以使用两种方法使用 coremltools 生成模型的 Core ML 格式。 首先是在训练模型后,直接在 Python Keras 代码中调用 coremltools 的`convert`和`save`方法。 例如,将下面的最后三行代码添加到`model.fit`之后的`ch8/python/keras/train.py`文件中:
coremltools 工具还正式支持转换使用 Keras 构建的模型(请参见[`keras.convert`链接](https://apple.github.io/coremltools/coremltools.converters.html))。 截至 2018 年 3 月,最新版本的 coremltools 0.8 的版本可与 TensorFlow 1.4 和 Keras 2.1.5 配合使用,我们在[第 8 章](../Text/08.html)和*预测中使用了 Keras 股票预测模型 RNN* 的股票价格。 您可以使用两种方法使用 coremltools 生成模型的 Core ML 格式。 首先是在训练模型后,直接在 Python Keras 代码中调用 coremltools 的`convert`和`save`方法。 例如,将下面的最后三行代码添加到`model.fit`之后的`ch8/python/keras/train.py`文件中:
```py
model.fit(
...
...
@@ -694,7 +694,7 @@ class ViewController: UIViewController {
请注意,就像使用 TensorFlow Mobile iOS 应用程序一样,我们使用`bidirectional_1_input`和`activation_1_Identity`来设置输入并获取输出。
如果尝试转换在[第 10 章](../Text/10.html),*构建类似 AlphaZero 的移动游戏应用程序*中在 Keras 中构建和训练的 AlphaZero 模型,则会收到错误消息`ValueError: Unknown loss function:softmax_cross_entropy_with_logits`。 如果您尝试转换我们在本书中构建的其他 TensorFlow 模型,则可以使用的最佳非官方工具是`https://github.com/tf-coreml/tf-coreml`上的 TensorFlow 到核心 ML 转换器。 不幸的是,类似于 TensorFlow Lite,它仅支持有限的 TensorFlow 操作集,其中一些原因是 Core ML 的限制,另一些原因是 tf-coreml 转换器的限制。 我们不会详细介绍将 TensorFlow 模型转换为 Core ML 模型的细节。 但是至少您已经了解了如何转换和使用 Scikit Learn 构建的传统机器学习模型以及基于 Keras 的 RNN 模型,该模型有望为您提供构建和使用 Core ML 模型的良好基础。 当然,如果您喜欢 Core ML,则应留意其将来的改进版本,以及 coremltools 和 tf-coreml 转换器的将来版本。 关于 Core ML,我们还没有涉及很多内容-要了解其确切功能,请参阅[https://developer.apple.com/documentation/coreml/core_ml_api](https://developer.apple.com/documentation/coreml/core_ml_api) 上完整的 API 文档。
如果尝试转换在[第 10 章](../Text/10.html),*构建类似 AlphaZero 的移动游戏应用程序*中在 Keras 中构建和训练的 AlphaZero 模型,则会收到错误消息`ValueError: Unknown loss function:softmax_cross_entropy_with_logits`。 如果您尝试转换我们在本书中构建的其他 TensorFlow 模型,则可以使用的最佳非官方工具是`https://github.com/tf-coreml/tf-coreml`上的 TensorFlow 到核心 ML 转换器。 不幸的是,类似于 TensorFlow Lite,它仅支持有限的 TensorFlow 操作集,其中一些原因是 Core ML 的限制,另一些原因是 tf-coreml 转换器的限制。 我们不会详细介绍将 TensorFlow 模型转换为 Core ML 模型的细节。 但是至少您已经了解了如何转换和使用 Scikit Learn 构建的传统机器学习模型以及基于 Keras 的 RNN 模型,该模型有望为您提供构建和使用 Core ML 模型的良好基础。 当然,如果您喜欢 Core ML,则应留意其将来的改进版本,以及 coremltools 和 tf-coreml 转换器的将来版本。 关于 Core ML,我们还没有涉及很多内容-要了解其确切功能,请参阅[完整的 API 文档](https://developer.apple.com/documentation/coreml/core_ml_api)。
小型单板 Raspberry Pi 计算机系列包括 Raspberry Pi 3 B +,3 B,2B,1 B +,1 A +,零和零 W(有关详细信息,请参见 [https:// /www.raspberrypi.org/products/#buy-now-modal](https://www.raspberrypi.org/products/#buy-now-modal) )。 我们将在此处使用 Pi 3 B 主板,您可以从前面的链接或在 Amazon( [https://www.amazon.com/gp/product/B01CD5VC92](https://www.amazon.com/gp/product/B01CD5VC92) )上以$ 35 的价格购买。 我们在主板上使用并测试过的配件及其价格如下:
小型单板 Raspberry Pi 计算机系列包括 Raspberry Pi 3B+,3B,2B,1B+,1A+,0 和 0W(有关详细信息,请参见[这里](https://www.raspberrypi.org/products/#buy-now-modal))。 我们将在此处使用 Pi 3B 主板,您可以从前面的链接或在 [Amazon](https://www.amazon.com/gp/product/B01CD5VC92) 上以 35 美元的价格购买。 我们在主板上使用并测试过的配件及其价格如下:
*CanaKit 5V 2.5A Raspberry Pi 电源约 10 美元( [https://www.amazon.com/gp/product/B00MARDJZ4](https://www.amazon.com/gp/product/B00MARDJZ4) ),可在开发期间使用。
*Kinobo-大约 4 美元的( [https://www.amazon.com/gp/product/B00IR8R7WQ](https://www.amazon.com/gp/product/B00IR8R7WQ) )可以记录您的语音命令的 USB 2.0 微型麦克风。
*USHONK USB 微型扬声器约合 12 美元( [https://www.amazon.com/gp/product/B075M7FHM1](https://www.amazon.com/gp/product/B075M7FHM1) ),可以播放合成声音。
1. 为 Windows 或 Mac 下载并安装 SD 格式化程序( [https://www.sdcard.org/downloads/formatter_4/index.html](https://www.sdcard.org/downloads/formatter_4/index.html) )。
1. 为 Windows 或 Mac 下载并安装 [SD 格式化程序](https://www.sdcard.org/downloads/formatter_4/index.html)。
2. 使用 SD 格式化程序格式化 MicroSD 卡。
3. 在[https://www.raspberrypi.org/downloads/noobs](https://www.raspberrypi.org/downloads/noobs)上下载 Raspbian 的官方简易安装程序 New Out Of Box Software(NOOBS)的离线 ZIP 版本,将其解压缩,然后拖动并 将提取的`NOOBS`文件夹中的所有文件拖放到格式化的 MicroSD 卡中。
3. 在[这个页面](https://www.raspberrypi.org/downloads/noobs)上下载 Raspbian 的官方简易安装程序 New Out Of Box Software(NOOBS)的离线 ZIP 版本,将其解压缩,然后拖动并 将提取的`NOOBS`文件夹中的所有文件拖放到格式化的 MicroSD 卡中。
现在弹出 MicroSD 卡并将其插入 Raspberry Pi 板上。 将显示器的 HDMI 电缆以及 USB 键盘和鼠标连接到开发板上。 用电源为开发板供电,然后按照屏幕上的步骤完成 Raspbian 的安装,包括设置 Wifi 网络。 整个安装过程不到一个小时即可完成。 完成后,您可以打开一个终端并输入`ifconfig`来查找电路板的 IP 地址,然后从您的计算中使用`ssh pi@<board_ip_address>`来访问它,正如我们稍后将要看到的,这确实很方便并且需要 在移动中测试控制 Raspberry Pi 机器人-当移动时,您不想或不能将键盘,鼠标和显示器与板子一起使用。
...
...
@@ -148,13 +148,13 @@ Occasionally, after the Pi board reboots, the card number for the USB speaker ge
GoPiGo 是一个流行的工具包,可将您的 Raspberry Pi 板变成移动的机器人。 购买并收到我们之前提到的 GoPiGo 机器人基础套件后,请按照[https://www.dexterindustries.com/GoPiGo/get-started-with-the-gopigo3-raspberry- pi-robot / 1-assemble-gopigo3](https://www.dexterindustries.com/GoPiGo/get-started-with-the-gopigo3-raspberry-pi-robot/1-assemble-gopigo3)将其与您的 Pi 板组装在一起。 根据您是同时观看 March March Madness 还是 NBA 季后赛比赛,这大约需要一两个小时。
GoPiGo 是一个流行的工具包,可将您的 Raspberry Pi 板变成移动的机器人。 购买并收到我们之前提到的 GoPiGo 机器人基础套件后,请按照[这里](https://www.dexterindustries.com/GoPiGo/get-started-with-the-gopigo3-raspberry-pi-robot/1-assemble-gopigo3)将其与您的 Pi 板组装在一起。 根据您是同时观看 March March Madness 还是 NBA 季后赛比赛,这大约需要一两个小时。
完成后,您的 Raspberry Pi 机器人以及我们之前列出的所有附件应如下所示:
![](img/a745c220-8c27-4b70-bef4-bc8dcbc518d8.png)Figure 12\. 1 Raspberry Pi robot with GoPiGo Kit and camera, USB speaker, and USB microphone
现在,使用 Raspberry Pi 电源打开 Pi 机器人,并在启动后使用`ssh pi@<your_pi_board_ip>`连接到它。 要安装 GoPiGo Python 库,以便我们可以使用 GoPiGo 的 Python API( [http://gopigo3.readthedocs.io/en/master/api-basic.html](http://gopigo3.readthedocs.io/en/master/api-basic.html) )控制机器人,请运行以下命令 ,它将执行一个 shell 脚本,该脚本创建一个新的`/home/pi/Dexter`目录并在其中安装所有库和固件文件:
现在,使用 Raspberry Pi 电源打开 Pi 机器人,并在启动后使用`ssh pi@<your_pi_board_ip>`连接到它。 要安装 GoPiGo Python 库,以便我们可以使用 [GoPiGo 的 Python API](http://gopigo3.readthedocs.io/en/master/api-basic.html)控制机器人,请运行以下命令 ,它将执行一个 shell 脚本,该脚本创建一个新的`/home/pi/Dexter`目录并在其中安装所有库和固件文件: