提交 725f4847 编写于 作者: W wizardforcel

2020-12-09 20:24:19

上级 42b79dba
......@@ -4,7 +4,7 @@
在前九章中,我们使用 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
cd tensorflow/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.
2. 将使用`tensorflow/contrib/lite/examples/ios/simple/data`文件夹中的前面的 download_models.sh 脚本生成的模型文件和标签文件,以及第二章源代码文件夹中的测试图像(例如`lab1.jpg`)添加到项目中。
2. 将使用`tensorflow/contrib/lite/examples/ios/simple/data`文件夹中的前面的`download_models.sh`脚本生成的模型文件和标签文件,以及第二章源代码文件夹中的测试图像(例如`lab1.jpg`)添加到项目中。
3. 关闭项目并创建一个名为`Podfile`的新文件,其内容如下:
......@@ -162,7 +162,7 @@ NSString* RunInferenceOnImage() {
![](img/8df9836e-fbf9-4caf-97af-7d415b6dc5bb.png)Figure 11.2 The TensorFlow Mobile (left) and Lite code of loading and processing the image input
7. 在调用`GetTopN` helper 方法以获取前 N 个分类结果之前,调用`Interpreter`上的`Invoke`方法运行模型,并调用`typed_out_tensor`方法以获取模型的输出。 TensorFlow Mobile 和 Lite 之间的代码差异如图 11.3 所示:
7. 在调用`GetTopN`辅助方法以获取前`N`个分类结果之前,调用`Interpreter`上的`Invoke`方法运行模型,并调用`typed_out_tensor`方法以获取模型的输出。 TensorFlow Mobile 和 Lite 之间的代码差异如图 11.3 所示:
![](img/c3e31c5c-775f-4af8-a000-d8d65f75f657.png)Figure 11.3 The TensorFlow Mobile (left) and Lite code of running the model and getting the output
......@@ -170,7 +170,7 @@ NSString* RunInferenceOnImage() {
![](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 模型返回的置信度值的最佳结果:
```py
-(void) showResult:(NSString *)result {
......@@ -227,7 +227,7 @@ bazel-bin/tensorflow/contrib/lite/toco/toco \
在前面的章节中,我们已经训练了许多定制的 TensorFlow 模型,并将其冻结以供移动使用。 不幸的是,如果您尝试使用上一节中内置的`bazel-bin/tensorflow/contrib/lite/toco/toco` TensorFlow Lite 转换工具 将模型从 TensorFlow 格式转换为 TensorFlow Lite 格式,则它们都会失败,除了 第 2 章, *通过迁移学习* 对图像进行分类的再训练模型; 大多数错误属于“转换不受支持的操作”类型。 例如,以下命令尝试将第 3 章, “检测对象及其位置” 中的 TensorFlow 对象检测模型转换为 TensorFlow Lite 格式:
在前面的章节中,我们已经训练了许多定制的 TensorFlow 模型,并将其冻结以供移动使用。 不幸的是,如果您尝试使用上一节中内置的`bazel-bin/tensorflow/contrib/lite/toco/toco` TensorFlow Lite 转换工具 将模型从 TensorFlow 格式转换为 TensorFlow Lite 格式,则它们都会失败,除了第 2 章, “通过迁移学习对图像进行分类”的再训练模型; 大多数错误属于“转换不受支持的操作”类型。 例如,以下命令尝试将第 3 章, “检测对象及其位置” 中的 TensorFlow 对象检测模型转换为 TensorFlow Lite 格式:
```py
bazel-bin/tensorflow/contrib/lite/toco/toco \
......@@ -292,7 +292,7 @@ bazel-bin/tensorflow/contrib/lite/toco/toco \
为简单起见,我们将仅演示如何在新的 Android 应用程序中将 TensorFlow Lite 与预构建的 TensorFlow Lite MobileNet 模型一起添加,并在此过程中发现一些有用的技巧。 有一个使用 TensorFlow Lite 的示例 Android 应用程序,[您可能希望首先在具有 API 级别的 Android 设备上与 Android Studio 一起运行](https://www.tensorflow.org/mobile/tflite/demo_android)。在执行以下步骤在新的 Android 应用中使用 TensorFlow Lite 之前,至少需要 15 个(版本至少为 4.0.3)。 如果您成功构建并运行了演示应用程序,则在 Android 设备上移动时,应该能够通过设备摄像头和 TensorFlow Lite MobileNet 模型看到识别出的对象。
现在执行以下步骤来创建一个新的 Android 应用程序,并添加 TensorFlow Lite 支持以对图像进行分类,就像我们在第 2 章,“通过迁移学习对图像进行分类”中的 HelloTensorFlow Android 应用程序一样。 ]
现在执行以下步骤来创建一个新的 Android 应用程序,并添加 TensorFlow Lite 支持以对图像进行分类,就像我们在第 2 章,“通过迁移学习对图像进行分类”中的 HelloTensorFlow Android 应用程序一样:
1. 创建一个新的 Android Studio 项目,并将应用命名为`HelloTFLite`。 将最低 SDK 设置为 API 15:Android 4.0.3,并接受所有其他默认设置。
2. 创建一个新的`assets`文件夹,从演示应用程序`tensorflow/contrib/lite/java/demo/app/src/main/assets`文件夹中拖放`mobilenet_quant_v1_224.tflite` TensorFlow Lite 文件和`labels.txt`文件,以及测试图像 到 HelloTFLite 应用程序的`assets`文件夹中。
......@@ -355,7 +355,7 @@ runOnUiThread(
});
```
如果立即运行该应用程序,您将看到测试图像和一个标题为“ TF Lite 分类”的按钮。 轻按它,您将看到分类结果,例如“拉布拉多犬:0.86 哈巴狗:0.05 达尔马提亚狗:0.04”。
如果立即运行该应用程序,您将看到测试图像和一个标题为“TF Lite 分类”的按钮。 轻按它,您将看到分类结果,例如“拉布拉多犬:0.86 哈巴狗:0.05 达尔马提亚狗:0.04”。
`ImageClassifier`中与 TensorFlow Lite 相关的代码使用核心`org.tensorflow.lite.Interpreter`类及其`run`方法来运行模型,如下所示:
......@@ -397,7 +397,7 @@ private MappedByteBuffer loadModelFile(Activity activity) throws IOException {
}
```
回想一下,在步骤 4 中,我们必须在 build.gradle 文件中添加`noCompress "tflite"`,否则`openFd`方法将导致错误。 该方法返回模型的映射版本,我们在第 6 章,“使用自然语言描述图像”时使用`convert_graphdef_memmapped_format`工具将 TensorFlow Mobile 模型转换为映射格式。 和第 9 章,“使用 GAN 生成和增强图像”。
回想一下,在步骤 4 中,我们必须在`build.gradle`文件中添加`noCompress "tflite"`,否则`openFd`方法将导致错误。 该方法返回模型的映射版本,我们在第 6 章,“使用自然语言描述图像”时使用`convert_graphdef_memmapped_format`工具将 TensorFlow Mobile 模型转换为映射格式。 和第 9 章,“使用 GAN 生成和增强图像”。
这就是在新的 Android 应用程序中加载并运行预构建的 TensorFlow Lite 模型所需的一切。 如果您有兴趣使用经过重新训练和转换的 TensorFlow Lite 模型(如我们在 iOS 应用程序,Android 应用程序中所做的那样),或者自定义 TensorFlow Lite 模型(如果您成功获得了转换后的模型),则可以在 HelloTFLite 应用程序。 我们将暂时保留最先进的 TensorFlow Lite,并继续为 iOS 开发人员介绍另一个非常酷的 WWDC 重量级主题。
......@@ -417,7 +417,7 @@ Core ML 格式的预训练模型包括流行的 MobileNet 和 Inception V3 模
如果要使用 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 视频。
在接下来的两个部分中,我们将向您展示两个教程,该教程以 TensorFlow 为后端,在 Keras 中如何转换和使用 Scikit Learn 模型和股票预测 RNN 模型,它们是在第 8 章, *使用 RNN* 预测股票价格。 您将在 Objective-C 和 Swift 中看到使用源代码从头开始构建的完整 iOS 应用程序,以使用转换后的 Core ML 模型。 如果短语“从头开始”使您兴奋并使您想起 AlphaZero,则您可能喜欢上一章第 10 章,“构建类似 AlphaZero 的移动游戏应用程序”。
在接下来的两个部分中,我们将向您展示两个教程,该教程以 TensorFlow 为后端,在 Keras 中如何转换和使用 Scikit Learn 模型和股票预测 RNN 模型,它们是在第 8 章, “使用 RNN 预测股票价格”。 您将在 Objective-C 和 Swift 中看到使用源代码从头开始构建的完整 iOS 应用程序,以使用转换后的 Core ML 模型。 如果短语“从头开始”使您兴奋并使您想起 AlphaZero,则您可能喜欢上一章第 10 章,“构建类似 AlphaZero 的移动游戏应用程序”。
......@@ -602,7 +602,7 @@ output_names = ['activation_1/Identity'])
coreml_model.save('Stock.mlmodel')
```
请注意,此处使用与冻结 TensorFlow 检查点文件相同的输入和输出名称。 如果将`Stock.mlmodel`拖放到 Objective-C 项目,则自动生成的 Stock.h 中将出现错误,因为 Xcode 9.2 中的错误无法正确处理代码中的“ /”字符 `activation_1/Identity`输出名称。 如果它是 Swift iOS 对象,则自动生成的`Stock.swift`文件会正确地将“ /”字符更改为“ _”,从而避免了编译器错误,如图 11.9 所示。
请注意,此处使用与冻结 TensorFlow 检查点文件相同的输入和输出名称。 如果将`Stock.mlmodel`拖放到 Objective-C 项目,则自动生成的`Stock.h`中将出现错误,因为 Xcode 9.2 中的错误无法正确处理代码中的`/`字符`activation_1/Identity`输出名称。 如果它是 Swift iOS 对象,则自动生成的`Stock.swift`文件会正确地将`/`字符更改为`_`,从而避免了编译器错误,如图 11.9 所示。
![](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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册