在前九章中,我们使用 TensorFlow Mobile 在移动设备上运行各种由 TensorFlow 和 Keras 构建的强大的深度学习模型。 正如我们在第 1 章,“移动 TensorFlow 入门”中提到的那样,Google 还提供了 TensorFlow Lite(可替代 TensorFlow Mobile 的版本)在移动设备上运行模型。 尽管自 Google I/O 2018 起它仍在开发人员预览中,但 Google 打算“大大简化开发人员针对小型设备的模型定位的体验。” 因此,值得详细研究 TensorFlow Lite 并为未来做好准备。
在前九章中,我们使用 TensorFlow Mobile 在移动设备上运行各种由 TensorFlow 和 Keras 构建的强大的深度学习模型。 正如我们在第 1 章,“移动 TensorFlow 入门”中提到的那样,Google 还提供了 TensorFlow Lite(可替代 TensorFlow Mobile 的版本)在移动设备上运行模型。 尽管自 Google I/O 2018 起它仍在开发人员预览中,但 Google 打算“大大简化开发人员针对小型设备的模型定位的体验。” 因此,值得详细研究 TensorFlow Lite 并为未来做好准备。
如果您是 iOS 开发人员,或者同时使用 iOS 和 Android,则 Apple 一年一度的**全球开发人员大会**(**WWDC**)是您不容错过的活动。 在 WWDC 2017 中,Apple 宣布了新的 Core ML 框架,以支持 iOS(以及所有其他 Apple OS 平台:macOS,tvOS 和 watchOS)上的深度学习模型和标准机器学习模型的运行。 自 iOS 11 起,Core ML 就可用了,截至 2018 年 5 月,Core ML 已占到 80% 的标记份额。至少了解您可以在 iOS 应用中使用 Core ML 的基本知识绝对有意义。
...
...
@@ -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),并且可能也在最新的 TensorFlow 仓库中。 您可以运行以下命令来准备和运行这两个应用,类似地在[“iOS 演示应用”](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)下进行了记录:
```py
cdtensorflow/contrib/lite/examples/ios
...
...
@@ -78,7 +78,7 @@ open simple.xcworkspace
您可以查看 Xcode `tflite_camera_example`项目的`CameraExampleViewController.mm`文件和`tflite_simple_example``RunModelViewController.mm`文件中的源代码,以了解如何使用 TensorFlow Lite API 加载和运行 TensorFlow Lite 模型。 在逐步指导您如何创建新的 iOS 应用并向其添加 TensorFlow Lite 支持以运行预先构建的 TensorFlow Lite 模型的逐步教程之前,我们将快速以具体数字向您展示的好处之一 如前所述,使用 TensorFlow Lite-应用程序二进制大小:
位于`tensorflow/examples/ios/camera`文件夹中的`tf_camera_example` TensorFlow Mobile 示例应用程序中使用的`tensorflow_inception.graph.pb`模型文件为 95.7MB,而`mobilenet_quant_v1_224.tflite` TensorFlow Lite 模型文件 位于`tensorflow/contrib/lite/examples/ios/camera` 文件夹中的 `tflite_camera_example` TensorFlow Lite 示例应用程序中使用的仅 4.3MB。 TensorFlow Mobile 重新训练的 Inception 3 模型文件的量化版本,如我们在第 2 章,“通过迁移学习对图像进行分类”的 HelloTensorFlow 应用中所见,约为 22.4MB,并且重新训练的 MobileNet TensorFlow Mobile 模型文件为 17.6MB。 总之,以下列出了四种不同类型的模型的大小:
位于`tensorflow/examples/ios/camera`文件夹中的`tf_camera_example` TensorFlow Mobile 示例应用程序中使用的`tensorflow_inception.graph.pb`模型文件为 95.7MB,而`mobilenet_quant_v1_224.tflite` TensorFlow Lite 模型文件 位于`tensorflow/contrib/lite/examples/ios/camera` 文件夹中的 `tflite_camera_example` TensorFlow Lite 示例应用程序中使用的仅 4.3MB。 TensorFlow Mobile 重新训练的 Inception 3 模型文件的量化版本,如我们在第 2 章,“通过迁移学习对图像进行分类”的 HelloTensorFlow 应用中所见,约为 22.4MB,并且重新训练的 MobileNet TensorFlow Mobile 模型文件为 17.6MB。 总之,以下列出了四种不同类型的模型的大小:
* TensorFlow Mobile Inception 3 模型:95.7MB
* 量化和重新训练的 TensorFlow Mobile Inception 3 模型:22.4MB
...
...
@@ -103,7 +103,7 @@ open simple.xcworkspace
If you prefer Swift as the programming language, you can refer to [Chapter 2](../Text/02.html), *Classifying Images with Transfer Learning*, or [Chapter 5](../Text/05.html), *Understanding Simple Speech Commands*, after following the steps here, to see how to convert the Objective-C app to a Swift app. But be aware that the TensorFlow Lite inference code still needs to be in C++ so you'll end up with a mix of Swift, Objective-C, and C++ code, with your Swift code mainly responsible for the UI and pre- and post-processing of the TensorFlow Lite inference.
![](img/0f0c6dc7-939f-4b15-9542-c56a9f3f427f.png)Figure 11.4 The TensorFlow Mobile (left) and Lite code of processing the model output to return the top results
9. 如果值大于阈值(设置为 0.1f),则使用简单的`UIAlertController`显示带有 TensorFlow Lite 模型返回的置信度值的最佳结果:
9. 如果值大于阈值(设置为`0.1f`),则使用简单的`UIAlertController`显示带有 TensorFlow Lite 模型返回的置信度值的最佳结果:
如果要使用 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 视频。
![](img/b77107fa-7de7-4602-84d1-d4c5b01c6efc.png)Figure 11.9 Showing the stock prediction Core ML model converted from Keras and TensorFlow in a Swift app