提交 923acf38 编写于 作者: W wizardforcel

2020-12-10 14:18:26

上级 c4120d74
......@@ -103,13 +103,13 @@ sudo pip install --upgrade /tmp/tensorflow_pkg/tensorflow-1.4.0-cp27-cp27m-macos
![](img/0e74beec-5ac6-4755-8268-dcf83c27a700.png)
Fig 1.1 Getting ready to download CUDA 8.0 on Ubuntu 16.04
图 1.1:准备在 Ubuntu 16.04 上下载 CUDA 8.0
2. 下载基本安装程序,如以下屏幕快照所示:
![](img/49296f66-83aa-48a5-973b-a4d9c8dd4348.png)
Fig 1.2 Choosing the CUDA 8.0 installer file for Ubuntu 16.04
图 1.2:为 Ubuntu 16.04 选择 CUDA 8.0 安装程序文件
3. 打开一个新的终端并运行以下命令(您还需要将最后两个命令添加到`.bashrc`文件中,以使两个环境变量在您下次启动新终端时生效):
......@@ -125,7 +125,7 @@ export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
![](img/f8e7636c-a193-4915-8c4f-8a234ffaf788.png)
Fig 1.3 Selecting cuDNN 6.0 for CUDA 8.0 on Linux
图 1.3:在 Linux 上为 CUDA 8.0 选择 cuDNN 6.0
5. 假设下载的文件位于默认的`~/Downloads`目录下,请解压缩该文件,然后您会看到一个名为`cuda`的文件夹,其中包含两个名为`include``lib64`的子文件夹
6. 将 cuDNN `include``lib64`文件复制到`CUDA_HOME``lib64``include`文件夹中:
......@@ -217,13 +217,13 @@ Android Studio 是开发 Android 应用程序的最佳工具,并且 TensorFlow
![](img/3b90efdb-6655-4e35-b74a-a44e69e17487.png)
Fig 1.4 Android SDK Manager to install SDK tools and NDK
图 1.4:Android SDK Manager,用于安装 SDK 工具和 NDK
最后,由于 TensorFlow Android 应用程序使用 C++ 中的本机 TensorFlow 库来加载和运行 TensorFlow 模型,因此您需要安装 Android **本机开发套件****NDK**),您可以执行以下任一操作 从上一个屏幕快照中显示的 Android SDK Manager 中获取,或者直接从[这里](https://developer.android.com/ndk/downloads/index.html)下载 NDK。 NDK 版本 r16b 和 r15c 均已通过测试可运行本书中的 Android 应用。 如果直接下载 NDK,则在打开项目并选择 Android Studio 的`File | Project Structure`后,您可能还需要设置 Android NDK 位置,如以下屏幕截图所示:
![](img/1f6a2d74-0be0-4f36-9796-2cf66eb1694f.png)
Fig 1.5 Setting project-level Android NDK location
图 1.5:设置项目级别的 Android NDK 位置
安装并设置了 Android SDK 和 NDK 之后,您就可以测试运行示例 TensorFlow Android 应用程序了。
......
......@@ -52,7 +52,9 @@
![](img/c8c4fa68-5db2-406d-9444-fbf1fdeb5418.png)
Figure 2.1 Dogset images separated by folders, or labels of dog breedYou may download the dataset and then run the `retrain.py` script on Mac, as it doesn't take too long (less than an hour) for the script to run on the relatively small dataset (about 20,000 images in total), but if you do that on a GPU-powered Ubuntu, as set up in the last chapter, the script can complete in just a few minutes. In addition, when retraining with a large image dataset, running on Mac may take hours or days so it makes sense to run it on a GPU-powered machine.
图 2.1:由文件夹或狗的品种分开的狗集图像
您可以下载数据集,然后在 Mac 上运行`retrain.py`脚本,因为该脚本在相对位置上运行不会花费太长时间(少于一小时) 小型数据集(总共约 20,000 张图像),但是,如上一章所述,如果您在 GPU 驱动的 Ubuntu 上执行此操作,则该脚本仅需几分钟即可完成。 此外,当使用大型图像数据集进行再培训时,在 Mac 上运行可能需要花费数小时或数天,因此在 GPU 驱动的计算机上运行它是有意义的。
假设您已经创建了`/tf_file`目录和`/tf_file/dogs_bottleneck`目录,那么重新训练模型的命令如下:
......@@ -161,19 +163,19 @@ bazel-bin/tensorboard/tensorboard --logdir /tmp/retrain_logs
![](img/3ae39776-c298-4b42-9125-f25ca84c9ed2.png)
Figure 2.2 Train and validation accuracy of the Inception v3 retrained model
图 2.2:Inception v3 重新训练模型的训练和验证准确性
在下面的屏幕截图中的交叉熵图,和我们之前对于运行`retrain.py`的输出所描述的一样:
![](img/cd4cea6a-8847-40bd-9ec4-3d9696972a5f.png)
Figure 2.3 Train and validation cross entropy of the Inception v3 retrained model
图 2.3:Inception v3 重新训练模型的训练和验证交叉熵
现在单击`GRAPHS`选项卡,您将看到一个名为`Mul`的操作,另一个名为`final_result`的操作 ,如下所示:
![](img/18aa75d8-28e3-497c-ba8f-145baa6cee37.png)
Figure 2.4 The Mul and final_result nodes in the retrained model
图 2.4:重新训练模型中的`Mul``final_result`节点
实际上,如果您希望与 TensorFlow 进行小的交互,则可以尝试几行 Python 代码来找出输出层和输入层的名称,如 iPython 交互所示:
......@@ -354,7 +356,7 @@ bazel-bin/tensorflow/tools/benchmark/benchmark_model
![](img/d2e25f9d-c88d-4fe5-918d-ba7e64420682.png)
Figure 2.5 Adding the retrained model file and the label file to app
图 2.5:将重新训练的模型文件和标签文件添加到应用程序
3. 单击 Xco​​de 中的`RunModelViewController.mm`文件,该文件使用 TensorFlow C++ API 处理输入图像,通过 Inception v1 模型运行它,并获得图像分类结果,并更改行:
......@@ -488,7 +490,7 @@ pod 'TensorFlow-experimental'
![](img/b89b3444-a6a9-45b4-bb02-ddf8388a6992.png)
Figure 2.6 Adding utility files, model files, label file and image files
图 2.6:添加实用程序文件,模型文件,标签文件和图像文件
6.`ViewController.m`重命名为`ViewController.mm`,因为我们将在该文件中混合使用 C++ 代码和 Objective-C 代码来调用 TensorFlow C++ API 并处理图像输入和推断结果。 然后,在`@interface ViewController`之前,添加以下`#include`和函数原型:
......@@ -637,13 +639,13 @@ GetTopN(output->flat<float>(), kNumResults, kThreshold, &top_results);
![](img/1823f6a6-f212-4ac9-9d78-a978a234c4aa.png)
Figure 2.7 Selecting different retrained model for inference
图 2.7:选择不同的再训练模型进行推理
然后,您将在选择模型后看到推断结果:
![](img/b9de1c02-8b3a-4482-b7f5-4d0d8d3e4692.png)
Figure 2.8 Inference results based on different retrained models
图 2.8:基于不同再训练模型的推理结果
请注意,MobileNet 训练后的模型在同一款 iPhone 上的运行速度要快得多,在 iPhone 6 上要比 Inception v3 训练后的模型快约一秒钟。
......@@ -671,13 +673,13 @@ pod 'TensorFlow-experimental'
![](img/afa22723-f59f-4544-8a61-e73ee7c43e85.png)
Figure 2.9 Creating Bridging Header when adding C++ file
图 2.9:添加 C++ 文件时创建桥接标题
5. 同样将`quantized_stripped_dogs_retrained .pb``dog_retrained_mobilenet10_224.pb`这两个模型,标签文件`dog_retrained_labels.txt`和几个测试图像文件拖放到项目文件夹中–之后,您应该会看到类似以下内容:
![](img/8236a55a-15fe-41a1-9ff7-c7280ae6c43f.png)
Figure 2.10 Adding utility files, model files, label file and image files
图 2.10:添加实用程序文件,模型文件,标签文件和图像文件
6. 使用以下代码创建一个名为`RunInference.h`的新文件(一个窍门是,我们必须在下一步中使用 Objective-C 类作为`RunInferenceOnImage`方法的包装,以便我们的 Swift 代码进行间接调用) 。否则,将发生构建错误):
......@@ -795,13 +797,13 @@ self.present(alert, animated: true, completion: nil)
![](img/098c0b7a-c32a-4ffc-bf3d-49c1ea80d36f.png)
Figure 2.11 Selecting a retrained model for inference
图 2.11:选择一个经过训练的模型进行推理
以及不同再训练模型的推结果:
以及不同再训练模型的推结果:
![](img/f3d05b5e-fda7-46d6-8e47-f234a1b4175e.png)
Figure 2.12 Inference results for different retrained models
图 2.12:不同再训练模型的推理结果
妳去 既然您知道了将强大的 TensorFlow 模型添加到 iOS 应用程序需要做什么,无论它是用 Objective-C 还是 Swift 编写的,都没有理由阻止您将 AI 添加到您的移动应用程序中,除非您是 Android。 但是您知道我们当然也会照顾 Android。
......@@ -822,19 +824,19 @@ Figure 2.12 Inference results for different retrained models
![](img/6cf6b889-8ecb-4837-9087-19b34d6e68ed.png)
Figure 2.13 Adding Assets Folder to a new project
图 2.13:将素材文件夹添加到新项目
5. 将两个重新训练的模型文件和标签文件以及几个测试图像拖放到资产文件夹中,如下所示:
![](img/fff27710-5222-410c-8d40-f451dea548f8.png)
Figure 2.14 Adding model files, the label file and test images to assets
图 2.14:将模型文件,标签文件和测试图像添加到素材
6. 按住选项按钮,将`tensorflow/examples/android/src/org/tensorflow/demo``Classifier.java``tensorflow/examples/android/src/org/tensorflow/demo`拖放到项目的 Java 文件夹中,如下所示:
![](img/7f41c4fe-6454-44e2-82d8-892290761eb5.png)
Figure 2.15 Adding TensorFlow classifier files to the project
图 2.15:将 TensorFlow 分类器文件添加到项目中
7. 打开`MainActivity`,首先创建与重新训练的 MobileNet 模型相关的常数-输入图像大小,节点名称,模型文件名和标签文件名:
......@@ -877,13 +879,13 @@ final List<Classifier.Recognition> results = classifier.recognizeImage(croppedBi
![](img/4c91b3a9-1a32-4288-aa75-205d0b71d584.png)
Figure 2.16 Recognition results using the MobileNet retrained model
图 2.16:使用 MobileNet 再训练模型的识别结果
如果您通过将`MODEL_FILE`更改为`quantized_stripped_dogs_retrained.pb`,将`INPUT_SIZE`更改为`299`,并且将`INPUT_NAME`更改为`Mul`来使用 Inception v3 训练后的模型,则调试该应用程序,您将获得如下所示的结果 :
![](img/ca49452a-b256-4c9e-8e96-6de4c75effa7.png)
Figure 2.17 Recognition results using the Inception v3 retrained model
图 2.17:使用 Inception v3 再训练模型的识别结果
既然您已经了解了如何将 TensorFlow 和经过重新训练的模型添加到自己的 iOS 和 Android 应用程序,那么想要添加非 TensorFlow 相关功能(例如使用手机的摄像头拍摄狗的照片和 认识它的品种。
......
......@@ -216,7 +216,7 @@ image_np_expanded = np.expand_dims(image_np, axis=0)
![](img/0df3a5c2-f983-4921-a901-b14c262d34a4.png)
Figure 3.1 Detected objects with bounding boxes and confidence scores
图 3.1:Detected objects with bounding boxes and confidence scores
在“在 iOS 中使用对象检测模型”部分,我们将了解如何在 iOS 设备上使用相同的模型并绘制相同的检测结果。
......@@ -387,13 +387,13 @@ python object_detection/eval.py \
![](img/539aa2c8-da68-42e8-ad74-ad3f2a25e07f.png)
Figure 3.2 Total loss trend when retraining an object detection model
图 3.2:Total loss trend when retraining an object detection model
您还将看到一些评估结果,如图 3.3 所示:
![](img/ee6c4a9d-473f-429c-a7e2-df3162500502.png)
Figure 3.3 Evaluation image detection results when retraining an object detection model
图 3.3:Evaluation image detection results when retraining an object detection model
10. 同样,您可以为`SSD_MobileNet`模型运行评估脚本,然后使用 TensorBoard 查看其损失趋势和评估图像结果:
......@@ -477,7 +477,7 @@ If you run the app, after completing the following steps, and encounter an error
![](img/a8474e70-b35d-4585-a9f9-620aa6cff313.png)
Figure 3.4 Adding a TENSORFLOW_ROOT user-defined setting
图 3.4:Adding a TENSORFLOW_ROOT user-defined setting
3. 单击目标,然后在“构建设置”下搜索“其他链接器标志”。 向其添加以下值:
......@@ -499,13 +499,13 @@ $(TENSORFLOW_ROOT) $(TENSORFLOW_ROOT)/tensorflow/contrib/makefile/downloads/prot
![](img/bae0f128-065f-4267-b1cc-b2450650e8d7.png)
Figure 3.5 Adding all TensorFlow-related build settings for the target
图 3.5:Adding all TensorFlow-related build settings for the target
5. 在目标的“构建阶段”中,在带库的链接二进制文件中添加 Accelerate 框架,如图 3.6 所示:
![](img/3c92f162-ad1d-4718-a72f-07f10ebc71fa.png)
Figure 3.6 Adding the Accelerate framework
图 3.6:Adding the Accelerate framework
6. 返回用于构建 TensorFlow iOS 库的终端,在`tensorflow/core/platform/default/mutex.h`中找到以下两行代码:
......@@ -549,7 +549,7 @@ cd <TENSORFLOW_ROOT>/models/research/object_detection/protos
![](img/7023289e-ae57-4518-920e-140985314d13.png)
Figure 3.7 The TFObjectDetection API project files
图 3.7:The TFObjectDetection API project files
7. 继续在`ViewController.mm`中添加其余代码。 在`viewDidLoad`中,添加以编程方式创建新`UIImageView`的代码,以首先显示测试图像,并在选择了特定模型以在测试图像上运行之后显示检测到的结果,然后添加以下功能实现:
......@@ -568,7 +568,7 @@ void RunInferenceOnImage(NSString *model)
![](img/411e68db-3026-4ba4-964a-3d9a174dce6f.png)
Figure 3.8 Running the app with different models and showing detection results
图 3.8:Running the app with different models and showing detection results
返回步骤 7 中的功能,`FilePathForResourceName`功能是用于返回资源文件路径的帮助程序功能:`mscoco_label_map.pbtxt`文件,该文件定义了要检测的 90 个对象类的 ID,内部名称和显示名称。 ,模型图文件和测试图像。 它的实现与我们在上一章的`HelloTensorFlow`应用中看到的实现相同。
......@@ -647,7 +647,7 @@ num_detections: 100, detection_scores size: 100, detection_classes size: 100, de
![](img/ced173a8-1b82-4868-a274-3cdf666556e1.png)
Figure 3.9 Comparing detected results of pre-trained and retrained models
图 3.9:Comparing detected results of pre-trained and retrained models
......@@ -695,7 +695,7 @@ python tensorflow/tools/quantization/quantize_graph.py --input=darkflow/built_gr
![](img/532a45ca-35ee-452b-b33a-79d557fee667.png)
Figure 3.10 Adding two YOLO models to the iOS app
图 3.10:Adding two YOLO models to the iOS app
3. 添加以下代码以将输入图像处理到张量中以馈送到输入节点,并在加载了 YOLO 模型图的情况下运行 TensorFlow 会话以生成检测输出:
......
......@@ -58,7 +58,7 @@ python style.py --style images/starry_night.jpg --test images/ww1.jpg --test-dir
![](img/ea1f4494-b7f4-4be2-a897-adfcbc4f0ef4.jpg)
Figure 4.1 Using Van Gogh's painting as the style image
图 4.1:Using Van Gogh's painting as the style image
在第 1 章, “移动 TensorFlow 入门”中设置的 NVIDIA GTX 1070 GPU 驱动的 Ubuntu 上,整个训练大约需要 5 个小时,并且在 CPU 上肯定要花更长的时间 。
......@@ -148,13 +148,13 @@ $(TENSORFLOW_ROOT) $(TENSORFLOW_ROOT)/tensorflow/contrib/makefile/downloads/prot
![](img/9bdc2a19-7e59-4f72-b41f-ea3eeba0c5c5.png)
Figure 4.2 The original dog picture before style applied
图 4.2:The original dog picture before style applied
5. 点击以选择快速样式转移,几秒钟后,您将在图 4.3 中看到一张新图片,其中已转移了繁星点点的夜色:
![](img/a78207bb-2407-4343-b84d-2f17aacfd4ff.png)
Figure 4.3 Like having Van Gogh draw your favorite dog
图 4.3:Like having Van Gogh draw your favorite dog
您只需选择喜欢的图片作为样式图像,然后按照上一节中的步骤操作,即可轻松构建具有不同样式的其他模型。 然后,您可以按照本节中的步骤在 iOS 应用中使用模型。 如果您想了解模型的训练方法,则应在上一节的 GitHub 存储库 中查看代码。 让我们详细看一下使用该模型完成魔术的 iOS 代码。
......@@ -420,7 +420,7 @@ mHandler.sendMessage(msg);
![](img/07ef305b-1e23-4a4c-98f5-faf14f398d65.png)
Figure 4.4 The original image and style transferred image on Android
图 4.4:The original image and style transferred image on Android
快速神经样式模型存在的一个问题是,即使量化后每个模型只有 1.7MB,我们仍然需要针对每种样式分别进行训练,并且每个训练的模型只能支持一种样式转换。 幸运的是,这个问题有很好的解决方案。
......@@ -474,7 +474,7 @@ auto image_tensor_mapped = image_tensor.tensor<float, 4>();
![](img/ea7a04fe-8b56-47b2-ac5a-7f25ced4acbd.png)
Figure 4.5 The 26 style images in the multi-style model
图 4.5:The 26 style images in the multi-style model
```py
tensorflow::Tensor style_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({ NUM_STYLES, 1}));
......@@ -515,25 +515,25 @@ tensorflow::Status run_status = session->Run({{input_layer, image_tensor}, {styl
![](img/f2841f1f-b0c3-412d-822b-5e9d0f34f235.png)
Figure 4.6 Showing the original content image
图 4.6:Showing the original content image
点击任意位置,您将看到两个样式选择,如图 4.7 所示:
![](img/c462927f-71be-46f1-995f-5cbd536ae175.png)
Figure 4.7 Showing the choice of two style models
图 4.7:Showing the choice of two style models
两张已传输图像的结果,`out_style[19] = 1.0`; 如图 4.8 所示:
![](img/cc28a550-1a66-4845-b330-1c01d3806e62.png)
Figure 4.8 The style-transferred results with two different models (fast style transfer on the left, multi-style on the right)
图 4.8:The style-transferred results with two different models (fast style transfer on the left, multi-style on the right)
图 4.9 显示了在图 4.5 中使用星夜图像和右上角图像的均等混合以及所有 26 种样式的均等混合的结果,如图 4.9 所示:
![](img/c5dd63f4-32a7-4177-afb8-af0e8a8770e8.png)
Figure 4.9 Results of different mixes of multiple styles (half starry night half the other on the left, mix of all 26 styles on the right)
图 4.9:Results of different mixes of multiple styles (half starry night half the other on the left, mix of all 26 styles on the right)
多样式模型在 iPhone 6 上运行大约需要 5 秒钟,比快速样式传输模型运行快大约 2-3 倍。
......@@ -623,7 +623,7 @@ for (int i=0; i < intValues.length; ++i) {
![](img/f582db90-6795-43d5-8d6d-28e2835e58de.png)
Figure 4.10 The original content image and the style transferred image with a mix of the 5th image and the starry night image
图 4.10:The original content image and the style transferred image with a mix of the 5th image and the starry night image
如果替换以下两行代码:
......@@ -654,7 +654,7 @@ for (int i = 0; i < NUM_STYLES; ++i) {
![](img/a6009bea-e770-4c76-b7ab-9f426ea66e0b.png)
Figure 4.11 Images stylized with the starry night style only and with all the 26 styles mixed equally
图 4.11:Images stylized with the starry night style only and with all the 26 styles mixed equally
在一些强大的 TensorFlow 模型的帮助下以及我们如何在移动应用程序中使用它们的知识,看起来我们的移动开发人员也可以成为伟大的艺术家。
......
......@@ -388,19 +388,19 @@ runOnUiThread(new Runnable() {
![](img/a321196b-4e44-43ec-94c5-42ee47ea29d9.png)
Figure 5.1 Showing the initial screen after app starts
图 5.1:Showing the initial screen after app starts
点击`START`按钮,然后开始说上面显示的 10 个命令之一。 您将看到按钮标题更改为“侦听...”,然后是“识别...”,如图 5.2 所示:
![](img/f4eb7e04-1d5f-4c70-a91c-055d8cdcb1c6.png)
Figure 5.2 Listening to record audio and recognizing the recorded audio
图 5.2:Listening to record audio and recognizing the recorded audio
识别结果几乎实时显示在屏幕中间,如图 5.3 所示:
![](img/bc4d09f3-f7d6-47fa-91f5-d0130635da7c.png)
Figure 5.3 Showing the recognized speech commands
图 5.3:Showing the recognized speech commands
整个识别过程几乎立即完成,用于识别的`speech_commands_graph.pb`模型仅为 3.7MB。 当然,它仅支持 10 条语音命令,但是即使使用`train.py`脚本的 `--wanted_words`参数或您自己的数据集支持数十个命令,大小也不会发生太大变化,正如我们在训练部分中讨论的那样。
......@@ -609,7 +609,7 @@ return commands[idx];
![](img/71ef1770-f7c0-4152-bb72-ea7993f5b728.png)
Figure 5.4 Showing the initial screen and recognition result
图 5.4:Showing the initial screen and recognition result
是的,应该会出现识别结果,但实际上不会出现,因为在 Xcode 输出窗格中会出现错误:
......@@ -665,7 +665,7 @@ The process on how to fix the "Not found: Op type not registered" error is a big
![](img/3febd651-7749-4a23-ba86-ae458ef8cfea.png)
Figure 5.5 The Swift-based iOS app project
图 5.5:The Swift-based iOS app project
4. 打开`ViewController.swift`。 在`import UIKit`之后的顶部添加以下代码:
......
......@@ -275,19 +275,19 @@ Running the `bazel build` command to build a TensorFlow Python script is optiona
![](img/d47ea13b-441c-4fb1-bccc-bbceaf2a8bdf.png)
Figure 6.1: Diagram showing the three output node names and one input node name
图 6.1:Diagram showing the three output node names and one input node name
图 6.2 显示了另一个输入节点名称`image_feed`
![](img/a7c39f91-68d3-4cd4-8e9b-0ef0b6dd11ff.png)
Figure 6.2: Diagram showing one additional input node name image_feed
图 6.2:Diagram showing one additional input node name image_feed
最后,图 6.3 显示了最后一个输入节点名称`input_feed`
![](img/887360ce-b609-4f8a-95d4-c1c3d2e8f97b.png)
Figure 6.3: Diagram showing the last input node name input_feed
图 6.3:Diagram showing the last input node name input_feed
当然,这里有很多我们不能也不会涵盖的细节。 但是,您将了解大局,同样重要的是,有足够的细节可以继续前进。 现在运行`freeze_graph.py`应该像轻风(双关语):
......@@ -356,7 +356,7 @@ x[:6]
![](img/eeb4e7ad-04ef-4ad5-ac89-a9a4a1e2f1e4.png)
Figure 6.4: Looking into the decode and conver_image nodes
图 6.4:Looking into the decode and conver_image nodes
实际上,在`im2txt/ops/image_processing.py`中有一行`image = tf.image.convert_image_dtype(image, dtype=tf.float32)`将解码的图像转换为浮点数。 让我们用`convert_image/Cast`代替 TensorBoard 中显示的名称`image_feed`,以及前面代码片段的输出,然后再次运行`strip_unused`
......@@ -467,7 +467,7 @@ target 'Image2Text'
![](img/62be062e-39dc-4f0c-8a59-7e0e675bafaa.png)
Figure 6.5: Setting up the Image2Text iOS app, also showing how LoadMemoryMappedModel is implemented
图 6.5:Setting up the Image2Text iOS app, also showing how LoadMemoryMappedModel is implemented
3. 打开`ViewController.mm`并添加一堆 Objective-C 和 C++ 常量,如下所示:
......@@ -687,7 +687,7 @@ return sentence;
![](img/dd9bd09b-1b39-47ff-8a8f-f7f180faff3c.png)
Figure 6.6: Running the Image2Text iOS app and selecting a model
图 6.6:Running the Image2Text iOS app and selecting a model
在 iOS 模拟器上,运行非映射模型需要 10 秒钟以上,运行映射模型则需要 5 秒钟以上。 在 iPhone 6 上,运行贴图模型还需要大约 5 秒钟,但由于模型文件和内存压力较大,运行非贴图模型时会崩溃。
......@@ -695,13 +695,13 @@ Figure 6.6: Running the Image2Text iOS app and selecting a model
![](img/59f462cb-59e6-4165-bd6f-80166f84bd61.png)
Figure 6.7: Showing the image captioning results
图 6.7:Showing the image captioning results
图 6.8 显示了 TensorFlow im2txt 网站上的结果,您可以看到我们更简单的贪婪搜索结果看起来也不错。 但是对于长颈鹿图片,看来我们的模型或推理代码不够好。 完成本章中的工作后,希望您会在改进训练或模型推断方面有所收获:
![](img/e08dec95-3b49-432a-bc4f-e3077151f2e9.png)
Figure 6.8: The example captioning results in the TensorFlow im2txt model website
图 6.8:The example captioning results in the TensorFlow im2txt model website
在我们进行下一个智能任务之前,是时候给 Android 开发人员一个不错的选择了。
......@@ -942,7 +942,7 @@ mHandler.sendMessage(msg);
![](img/249e6bc8-7f89-45d4-a217-2cf1c54bf5fe.png)
Figure 6.9: Showing image captioning results in Android
图 6.9:Showing image captioning results in Android
一些结果与 iOS 结果以及 TensorFlow im2txt 网站上的结果略有不同。 但是它们看起来都不错。 另外,在相对较旧的 Android 设备(例如 Nexus 5)上运行该模型的非映射版本也可以。 但是最好在 Android 中加载映射模型,以查看性能的显着提高,我们可能会在本书后面的章节中介绍。
......
......@@ -34,7 +34,7 @@
![](img/ef475044-6568-4e50-bc98-8ff0a0d6efbf.png)
Figure 7.1: The drawing classification mode
图 7.1:The drawing classification mode
与接受 2D 图像输入的 2D 卷积 API `tf.layers.conv2d` 不同,此处将 1D 卷积 API `tf.layers.conv1d`用于时间卷积(例如绘图)。 默认情况下,在图形分类模型中,使用三个 1D 卷积层,每个层具有 48、64 和 96 个过滤器,其长度分别为 5、5 和 3。 卷积层之后,将创建 3 个 LSTM 层,每层具有 128 个正向`BasicLSTMCell`节点和 128 个反向`BasicLSTMCell`节点,然后将其用于创建动态双向循环神经网络,该网络的输出将发送到最终的完全连接层以计算`logits`(非标准化的对数概率)。
......@@ -87,7 +87,7 @@ drwxr-xr-x 2 jeff jeff 4096 Feb 12 00:11 eval
![](img/479a1d37-1775-459d-a616-c45f2f10f0f1.png)
Figure 7.2: The accuracy and loss of the model after 300k training steps
图 7.2:The accuracy and loss of the model after 300k training steps
现在,我们可以像上一章一样运行`freeze_graph.py`工具,以生成用于移动设备的模型文件。 但是在执行此操作之前,我们首先来看一下如何在 Python 中使用该模型进行推断,例如上一章中的`run_inference.py`脚本。
......@@ -195,7 +195,7 @@ def predict_input_fn():
![](img/d911bab2-cfc2-41d3-a352-d00db55b1f71.png)
Figure 7.3: Showing possible output node names of the model
图 7.3:Showing possible output node names of the model
用您的`graph.pbtxt`文件的路径和最新的模型检查点前缀替换`--input_graph``--input_checkpoint`值后,在 TensorFlow 根目录中运行以下脚本以获取冻结的图:
......@@ -209,14 +209,14 @@ python tensorflow/python/tools/freeze_graph.py --input_graph=/tmp/graph.pbtxt -
![](img/a88ae370-8a39-4ee3-8ce0-ebaa5fd8a8ed.png)
Figure 7.4: Finding out possible input node namesYou may wonder why we can't fix the `Not found: Op type not registered 'OneShotIterator'` error with a technique we used before, which is to first find out which source file contains the op using the command `grep 'REGISTER.*"OneShotIterator"' tensorflow/core/ops/*.cc`
图 7.4:Finding out possible input node namesYou may wonder why we can't fix the `Not found: Op type not registered 'OneShotIterator'` error with a technique we used before, which is to first find out which source file contains the op using the command `grep 'REGISTER.*"OneShotIterator"' tensorflow/core/ops/*.cc`
(and you'll see the output as `tensorflow/core/ops/dataset_ops.cc:REGISTER_OP("OneShotIterator")`) then add `tensorflow/core/ops/dataset_ops.cc` to `tf_op_files.txt` and rebuild the TensorFlow library. Even if this were feasible, it would complicate the solution as now we need to feed the model with some data related to `OneShotIterator`, instead of the direct user drawing in points.
此外,在右侧上方一层(图 7.5),还有另一种操作 `Squeeze` ,它是 `rnn_classification` 子图的输入:
![](img/6230229e-1c21-4280-b60a-cac640c1cb1f.png)
Figure 7.5: Further research to find out input node names
图 7.5:Further research to find out input node names
我们不必担心`Reshape`右侧的`Shape`运算,因为它实际上是`rnn_classification`子图的输出。 因此,所有这些研究背后的直觉是,我们可以使用`Reshape``Squeeze`作为两个输入节点,然后使用在上一章中看到的`transform_graph` 工具,我们应该能够删除 `Reshape``Squeeze`以下的节点,包括`OneShotIterator`
......@@ -237,7 +237,7 @@ strip_unused_nodes(name=Squeeze,type_for_name=int64,shape_for_name="8",name=Resh
![](img/6cc9946c-dfb2-41de-af95-a35e47d405a7.png)
Figure 7.6: Finding out nodes causing model loading error but not related to model inference
图 7.6:Finding out nodes causing model loading error but not related to model inference
那么,如何摆脱`global_step`以及其他相关的`cond`节点,由于它们的隔离性,它们不会被变换图工具剥离掉? 幸运的是,`freeze_graph`脚本支持这一点 – [仅在其源代码中记录](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py)。 我们可以为脚本使用`variable_names_blacklist`参数来指定应在冻结模型中删除的节点:
......@@ -333,7 +333,7 @@ tensorflow/core/kernels/random_op.cc
![](img/ee464972-a065-4400-aea9-72ce9306b901.png)
Figure 7.7: Showing the QuickDraw Xcode project with initial content of ViewController.mm
图 7.7:Showing the QuickDraw Xcode project with initial content of ViewController.mm
我们现在准备单独处理`ViewController.mm`,以完成我们的任务。
......@@ -625,7 +625,7 @@ void normalizeScreenCoordinates(NSMutableArray *allPoints, float *normalized) {
![](img/48678dd2-0490-4b97-a0a6-fa4ba52bc27b.png)
Figure 7.8: Showing drawing and classification results on iOS
图 7.8:Showing drawing and classification results on iOS
......@@ -693,7 +693,7 @@ When using a manually built native library in an Android app, you need to let th
![](img/0865ad60-dece-4349-9825-24ca46220d07.png)
Figure 7.9: Showing the TensorFlow native library file
图 7.9:Showing the TensorFlow native library file
4. 通过运行以下命令构建 TensorFlow 本机库的 Java 接口:
......@@ -705,7 +705,7 @@ bazel build //tensorflow/contrib/android:android_tensorflow_inference_java
![](img/51e4c597-7447-4623-8e07-396b56faddfa.png)
Figure 7.10: Showing the Java interface file to the TensorFlow library
图 7.10:Showing the Java interface file to the TensorFlow library
现在,我们准备在 Android 中编码和测试模型。
......@@ -1031,7 +1031,7 @@ private double[] normalizeScreenCoordinates() {
![](img/560fa3a2-94a9-4f93-ae05-bd09921b8e0c.png)
Figure 7.11: Showing drawing and classification results on Android
图 7.11:Showing drawing and classification results on Android
既然您已经了解了训练 Quick Draw 模型的全过程,并在 iOS 和 Android 应用程序中使用了它,那么您当然可以微调训练方法,使其更加准确,并改善移动应用程序的乐趣。
......@@ -1039,7 +1039,7 @@ Figure 7.11: Showing drawing and classification results on Android
![](img/16494075-d42a-4b86-a48a-4cb0bc2ec865.png)
Figure 7.12: Checking out the TensorFlow native library file with Device File Explorer
图 7.12:Checking out the TensorFlow native library file with Device File Explorer
......
......@@ -271,7 +271,7 @@ You can replace BasicRNNCell in step 4 with BasicLSTMCell and run the training c
![](img/71f57975-9e80-4886-a13f-7a38b52dc84d.png)
Figure 8.1 Showing the stock price prediction correctness trained with TensorFlow RNN
图 8.1:Showing the stock price prediction correctness trained with TensorFlow RNN
注意,每次运行此训练和测试代码时,您获得的比率可能都会有所不同。 通过微调模型的超参数,您可能会获得超过 60% 的比率,这似乎比随机预测要好。 如果您乐观的话,您可能会认为至少有 50% (56.25%)的东西要显示出来,并且可能希望看到该模型在移动设备上运行。 但首先让我们看看是否可以使用酷的 Keras 库来构建更好的模型-在执行此操作之前,让我们通过简单地运行来冻结经过训练的 TensorFlow 模型:
......@@ -520,7 +520,7 @@ plt.show()
![](img/b8227667-1d0d-4ea7-bf0b-fa7dac192064.png)
Figure 8.2 Making stock price predictions with the Keras bidirectional and stacked LSTM layers
图 8.2:Making stock price predictions with the Keras bidirectional and stacked LSTM layers
很容易在堆栈中添加更多 LSTM 层,或者使用诸如学习率和辍学率以及许多恒定设置之类的超参数。 但是,对于使用`pred_len``shift_pred`的不同设置,正确率的差异还没有发现。 也许我们现在应该对接近 60% 的正确率感到满意,并看看如何在 iOS 和 Android 上使用 TensorFlow 和 Keras 训练的模型-我们可以在以后继续尝试改进模型,但是 了解使用 TensorFlow 和 Keras 训练的 RNN 模型是否会遇到任何问题将非常有价值。
......@@ -546,7 +546,7 @@ python tensorflow/python/tools/freeze_graph.py --input_meta_graph=/tmp/keras_amz
![](img/454020d6-1f32-480d-b159-b474b6878540.png)
Figure 8.3 iOS app using the TensorFlow- and Keras-trained models in Xcode
图 8.3:iOS app using the TensorFlow- and Keras-trained models in Xcode
`ViewController.mm`中,我们将首先声明一些变量和一个常量:
......@@ -689,7 +689,7 @@ const int SEQ_LEN = 20;
![](img/067fa448-a497-4f13-944f-e976240694c9.png)
Figure 8.4 Selecting the TensorFlow or Keras RNN model
图 8.4:Selecting the TensorFlow or Keras RNN model
如果选择 TensorFlow 模型,则可能会出现错误:
......@@ -713,13 +713,13 @@ Could not create TensorFlow Graph: Invalid argument: No OpKernel was registered
![](img/28715473-1f89-4f14-8e81-f98242a11c8d.png)
Figure 8.5 Predicting with the TensorFlow RNN model
图 8.5:Predicting with the TensorFlow RNN model
使用 Keras 模型输出不同的预测,如图 8.6 所示:
![](img/4114a6b9-15da-41f7-b535-bf04c7fb4dad.png)
Figure 8.6 Predicting with the Keras RNN model
图 8.6:Predicting with the Keras RNN model
我们无法确定哪个模型能在没有进一步研究的情况下更好地工作,但是我们可以确定的是,我们的两个 RNN 模型都使用 TensorFlow 和 Keras API 从头开始训练了,其准确性接近 60%, 在 iOS 上运行良好,这很值得我们付出努力,因为我们正在尝试建立一个许多专家认为将达到与随机选择相同的性能的模型,并且在此过程中,我们学到了一些新奇的东西-使用 TensorFlow 和 Keras 构建 RNN 模型并在 iOS 上运行它们。 在下一章中,我们只剩下一件事了:如何在 Android 上使用模型? 我们会遇到新的障碍吗?
......@@ -858,13 +858,13 @@ public void run() {
![](img/8ce6016b-658a-46c4-a051-04c772d503fc.png)
Figure 8.7 Using the TensorFlow model to make stock price predictions on Amazon
图 8.7:Using the TensorFlow model to make stock price predictions on Amazon
选择 KERAS 预测将为您提供如图 8.8 所示的结果:
![](img/ec5b48b2-0c2a-439a-af59-303b9c628019.png)
Figure 8.8 Using the Keras model to make stock price predictions on Amazon
图 8.8:Using the Keras model to make stock price predictions on Amazon
......
......@@ -253,7 +253,7 @@ if a.mode == "test":
![](img/b73deef3-1598-4019-ac72-5c0212d53c74.png)
Figure 9.1: The original, the blurry, and the generated
图 9.1:The original, the blurry, and the generated
10. 现在,将`newckpt`目录复制到`/tmp`,我们可以如下冻结模型:
......@@ -332,7 +332,7 @@ No OpKernel was registered to support Op 'FIFOQueueV2' with these attrs. Registe
![](img/9cb1fa66-5a4e-4a1b-aa51-a16fd9051f57.png)
Figure 9.2: Showing the GAN app in Xcode
图 9.2:Showing the GAN app in Xcode
我们将创建一个按钮,在点击该按钮时,提示用户选择一个模型以生成数字或增强图像:
......@@ -545,7 +545,7 @@ Figure 9.2: Showing the GAN app in Xcode
![](img/0e0f47b1-2efa-4ec0-9eaa-4b21356cad23.png)
Figure 9.3: Showing GAN model selection and results of generated handwritten digits
图 9.3:Showing GAN model selection and results of generated handwritten digits
这些数字看起来很像真实的人类手写数字,都是在训练了基本 GAN 模型之后完成的。 如果您返回并查看进行训练的代码,并且停下来思考一下 GAN 的工作原理,一般来说,则生成器和判别器如何相互竞争,以及 尝试达到稳定的纳什均衡状态,在这种状态下,生成器可以生成区分器无法分辨出真实还是伪造的真实假数据,您可能会更欣赏 GAN 的魅力。
......@@ -553,7 +553,7 @@ Figure 9.3: Showing GAN model selection and results of generated handwritten dig
![](img/63898b4a-4c1d-4605-9fa3-97c8aa00c968.png)
Figure 9.4: The original blurry and enhanced images on iOS
图 9.4:The original blurry and enhanced images on iOS
你知道该怎么做。 是时候将我们的爱献给 Android 了。
......@@ -571,7 +571,7 @@ Figure 9.4: The original blurry and enhanced images on iOS
![](img/f1c741dd-1fc6-41da-9243-3b0840aa9b20.png)
Figure 9.5: Android Studio GAN app overview, showing constant definitions
图 9.5:Android Studio GAN app overview, showing constant definitions
请注意,为简单起见,我们将`BATCH_SIZE`设置为 1。您可以轻松地将其设置为任何数字,并像在 iOS 中一样获得很多输出。 除了图 9.5 中定义的常量之外,我们还将创建一些实例变量:
......@@ -722,7 +722,7 @@ void runMNISTModel() {
![](img/1b2a1b02-e084-40bf-a33d-ce5561a34850.png)
Figure 9.6: Showing the generated digits
图 9.6:Showing the generated digits
......@@ -793,7 +793,7 @@ void runPix2PixBlurryModel() {
![](img/7e7c6110-09d5-4908-bfaa-e9b5b67596d3.png)
Figure 9.7: The blurry and enhanced images on Android
图 9.7:The blurry and enhanced images on Android
这使用两个 GAN 模型完成了我们的 Android 应用程序。
......
......@@ -109,7 +109,7 @@ pip install keras
![](img/99dc16a9-52c9-4adf-bd8f-1a244504de79.png)
Figure 10.1 The first layers of the deep residual network
图 10.1:The first layers of the deep residual network
值得注意的是,神经网络称为残差网络(ResNet),由 Microsoft 于 2015 年在 ImageNet 和 COCO 2015 竞赛的获奖作品中引入。 在 ResNet 中,使用身份映射(图 10.1 右侧的箭头)可避免在网络越深时出现更高的训练错误。 有关 ResNet 的更多信息,您可以查看原始论文[《用于图像识别的深度残差学习》](https://arxiv.org/pdf/1512.03385v1.pdf), 以及博客[《了解深度残差网络》](https://blog.waya.ai/deep-residual-learning-9610bb62c355) - 一个简单的模块化学习框架,它重新定义了构成最新技术的内容。
......@@ -117,7 +117,7 @@ Figure 10.1 The first layers of the deep residual network
![](img/e3383760-5f06-4d10-a903-8978f41281de.png)
Figure 10.2 The last layers of the deep resnet
图 10.2:The last layers of the deep resnet
在本节的最后部分,您将看到一些使用 Keras API 的 Python 代码片段,该片段对 ResNet 有着很好的支持,以构建这样的网络。 现在让我们让这些模型首先互相对抗,然后与我们一起对抗,看看这些模型有多好。
......@@ -380,7 +380,7 @@ Couldn't load model: Invalid argument: No OpKernel was registered to support Op
![](img/a0639a2a-55f3-485a-92cd-4ffdc99b398c.png)
Figure 10.3 Showing the AlphaZero iOS app in Xcode
图 10.3:Showing the AlphaZero iOS app in Xcode
我们只需要使用三个 UI 组件:
......@@ -826,13 +826,13 @@ int winners[69][4] = {
![](img/fa949c2a-3114-4a56-9041-9acb069b1ff4.png)
Figure 10.4 Playing with Connect4 on iOS
图 10.4:Playing with Connect4 on iOS
使用 AI 玩一些游戏,图 10.5 显示了一些可能的最终游戏:
![](img/1e45df24-e772-462b-aef9-67c8f49bed67.png)
Figure 10.5 Some game results of Connect4 on iOS
图 10.5:Some game results of Connect4 on iOS
在我们暂停之前,让我们快速看一下使用该模型并玩游戏的 Android 代码。
......@@ -1317,7 +1317,7 @@ void softmax(float vals[], int count) {
![](img/2d40ca7c-5d3d-4586-9034-2b2ae1c71ecb.png)
Figure 10.6 Showing the game board and some results on Android
图 10.6:Showing the game board and some results on Android
当您使用前面的代码在 iOS 和 Android 上玩游戏时,很快就会发现该模型返回的策略并不强大-主要原因是 MCTS 没有出现在这里,由于范围限制,不会与深度神经网络模型一起使用。 强烈建议您自己研究和实现 MCTS,或者在源代码存储库中使用我们的实现作为参考。 您还应该将网络模型和 MCTS 应用于您感兴趣的其他游戏-毕竟,AlphaZero 使用了通用 MCTS 和无领域知识的自我强化学习,从而使超人学习轻松移植到其他问题领域。 通过将 MCTS 与深度神经网络模型结合,您可以实现 AlphaZero 所做的事情。
......
......@@ -118,7 +118,7 @@ target 'HelloTFLite'
![](img/b4b1750b-cc97-42f8-a032-226b595d7e46.png)
Figure 11.1 A new Xcode iOS project using the TensorFlow Lite pod
图 11.1:A new Xcode iOS project using the TensorFlow Lite pod
我们仅向您展示如何在 iOS 应用中使用 TensorFlow Lite Pod。 还有另一种将 TensorFlow Lite 添加到 iOS 的方法,类似于构建自定义 TensorFlow Mobile iOS 库的过程,我们在前几章中已经做过很多次了。 有关如何构建自己的自定义 TensorFlow Lite iOS 库的更多信息,请参阅[以下位置的文档](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/ios.md)
......@@ -166,19 +166,19 @@ 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
图 11.2:The TensorFlow Mobile (left) and Lite code of loading and processing the image input
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
图 11.3:The TensorFlow Mobile (left) and Lite code of running the model and getting the output
8. 以类似于 HelloTensorFlow 中方法的方式实现`GetTopN`方法,对于 TensorFlow Lite 使用`const float* prediction`类型而不是对于 TensorFlow Mobile 使用`const Eigen::TensorMap<Eigen::Tensor<float, 1, Eigen::RowMajor>, Eigen::Aligned>& prediction`。 TensorFlow Mobile 和 Lite 中`GetTopN`方法的比较如图 11.4 所示:
![](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
图 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 模型返回的置信度值的最佳结果:
......@@ -200,7 +200,7 @@ Figure 11.4 The TensorFlow Mobile (left) and Lite code of processing the model o
![](img/5568ba52-1f2e-4968-b6d5-5a1d33061c46.png)
Figure 11.5 The test image and model inference result
图 11.5:The test image and model inference result
这就是您可以在新的 iOS 应用程序中使用预构建的 MobileNet TensorFlow Lite 模型的方式。 现在让我们看看如何使用经过重新训练的 TensorFlow 模型。
......@@ -331,7 +331,7 @@ bazel-bin/tensorflow/contrib/lite/toco/toco \
![](img/9df4ffa8-cd63-4b4c-a728-47cba1394711.png)
Figure 11.6 New Android app using TensorFlow Lite and the prebuilt MobileNet image classification model
图 11.6:New Android app using TensorFlow Lite and the prebuilt MobileNet image classification model
5. 像以前一样,在`activity_main.xml`中添加`ImageView``Button`,然后在`MainActivity.java``onCreate`方法中,将`ImageView`设置为测试图像的内容,然后单击 `Button`的侦听器以启动新线程,并实例化名为`classifier``ImageClassifier`实例:
......@@ -539,7 +539,7 @@ coreml_model.save("HouseSVM.mlmodel")
![](img/aaa2a1dd-d7f2-4fbc-8e70-3e6041ddda6e.png)
Figure 11.7 Showing the Swift iOS project and the Linear Regression Core ML model
图 11.7:Showing the Swift iOS project and the Linear Regression Core ML model
其他`HouseSVM.mlmodel` 模型 看起来完全一样,只是机器学习模型名称和模型类从`HouseLR`更改为`HouseSVM`
......@@ -606,7 +606,7 @@ coreml_model.save("Stock.mlmodel")
![](img/fdf48153-937d-4066-9dd3-cc13ce841e6e.png)
Figure 11.8 Showing the stock prediction Core ML model converted from Keras and TensorFlow in an Objective-C app
图 11.8:Showing the stock prediction Core ML model converted from Keras and TensorFlow in an Objective-C app
使用 coremltools 生成模型的 Core ML 格式的另一种方法是,首先将 Keras 构建的模型保存为 Keras HDF5 模型格式,这是我们在第 10 章,“构建类似 AlphaZero 的移动游戏应用程序”中,在转换为 AlphaZero TensorFlow 检查点文件之前使用的格式。 为此,只需运行`model.save('stock.h5')`
......@@ -624,7 +624,7 @@ coreml_model.save('Stock.mlmodel')
![](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
图 11.9:Showing the stock prediction Core ML model converted from Keras and TensorFlow in a Swift app
要在 Objective-C 中使用该模型,请创建具有指定数据类型和形状的`Stock`对象和`MLMultiArray`对象,然后使用一些输入数据填充数组对象,并使用`StockInput`调用`predictionFromFeatures`方法 用 `MLMultiArray`数据初始化的实例:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册