提交 3122283e 编写于 作者: W wizardforcel

2020-12-10 17:34:48

上级 929f8d5c
......@@ -109,7 +109,7 @@ pip install keras
![](img/99dc16a9-52c9-4adf-bd8f-1a244504de79.png)
图 10.1:The first layers of the deep residual network
图 10.1:深度残差网络的第一层
值得注意的是,神经网络称为残差网络(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 @@ pip install keras
![](img/e3383760-5f06-4d10-a903-8978f41281de.png)
图 10.2:The last layers of the deep resnet
图 10.2:深度 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)
图 10.3:Showing the AlphaZero iOS app in Xcode
图 10.3:在 Xcode 中显示 AlphaZero iOS 应用
我们只需要使用三个 UI 组件:
......@@ -826,13 +826,13 @@ int winners[69][4] = {
![](img/fa949c2a-3114-4a56-9041-9acb069b1ff4.png)
图 10.4:Playing with Connect4 on iOS
图 10.4:在 iOS 上玩 Connect4
使用 AI 玩一些游戏,图 10.5 显示了一些可能的最终游戏:
![](img/1e45df24-e772-462b-aef9-67c8f49bed67.png)
图 10.5:Some game results of Connect4 on iOS
图 10.5:iOS 上 Connect4 的一些游戏结果
在我们暂停之前,让我们快速看一下使用该模型并玩游戏的 Android 代码。
......@@ -1317,7 +1317,7 @@ void softmax(float vals[], int count) {
![](img/2d40ca7c-5d3d-4586-9034-2b2ae1c71ecb.png)
图 10.6:Showing the game board and some results on Android
图 10.6:在 Android 上显示游戏板和一些结果
当您使用前面的代码在 iOS 和 Android 上玩游戏时,很快就会发现该模型返回的策略并不强大-主要原因是 MCTS 没有出现在这里,由于范围限制,不会与深度神经网络模型一起使用。 强烈建议您自己研究和实现 MCTS,或者在源代码存储库中使用我们的实现作为参考。 您还应该将网络模型和 MCTS 应用于您感兴趣的其他游戏-毕竟,AlphaZero 使用了通用 MCTS 和无领域知识的自我强化学习,从而使超人学习轻松移植到其他问题领域。 通过将 MCTS 与深度神经网络模型结合,您可以实现 AlphaZero 所做的事情。
......
......@@ -118,7 +118,7 @@ target 'HelloTFLite'
![](img/b4b1750b-cc97-42f8-a032-226b595d7e46.png)
图 11.1:A new Xcode iOS project using the TensorFlow Lite pod
图 11.1:使用 TensorFlow Lite Pod 的新 Xcode iOS 项目
我们仅向您展示如何在 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)
图 11.2:The TensorFlow Mobile (left) and Lite code of loading and processing the image input
图 11.2:TensorFlow Mobile(左)和 Lite 代码,用于加载和处理图像输入
7. 在调用`GetTopN`辅助方法以获取前`N`个分类结果之前,调用`Interpreter`上的`Invoke`方法运行模型,并调用`typed_out_tensor`方法以获取模型的输出。 TensorFlow Mobile 和 Lite 之间的代码差异如图 11.3 所示:
![](img/c3e31c5c-775f-4af8-a000-d8d65f75f657.png)
图 11.3:The TensorFlow Mobile (left) and Lite code of running the model and getting the output
图 11.3:运行模型并获取输出的 TensorFlow Mobile(左)和 Lite 代码
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)
图 11.4:The TensorFlow Mobile (left) and Lite code of processing the model output to return the top results
图 11.4:处理模型输出以返回最高结果的 TensorFlow Mobile(左)和 Lite 模型代码
9. 如果值大于阈值(设置为`0.1f`),则使用简单的`UIAlertController`显示带有 TensorFlow Lite 模型返回的置信度值的最佳结果:
......@@ -200,7 +200,7 @@ NSString* RunInferenceOnImage() {
![](img/5568ba52-1f2e-4968-b6d5-5a1d33061c46.png)
图 11.5:The test image and model inference result
图 11.5:测试图像和模型推断结果
这就是您可以在新的 iOS 应用程序中使用预构建的 MobileNet TensorFlow Lite 模型的方式。 现在让我们看看如何使用经过重新训练的 TensorFlow 模型。
......@@ -331,7 +331,7 @@ bazel-bin/tensorflow/contrib/lite/toco/toco \
![](img/9df4ffa8-cd63-4b4c-a728-47cba1394711.png)
图 11.6:New Android app using TensorFlow Lite and the prebuilt MobileNet image classification model
图 11.6:使用 TensorFlow Lite 和预构建的 MobileNet 图像分类模型的新 Android 应用
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)
图 11.7:Showing the Swift iOS project and the Linear Regression Core ML model
图 11.7:显示 Swift iOS 项目和线性回归 Core ML 模型
其他`HouseSVM.mlmodel` 模型 看起来完全一样,只是机器学习模型名称和模型类从`HouseLR`更改为`HouseSVM`
......@@ -606,7 +606,7 @@ coreml_model.save("Stock.mlmodel")
![](img/fdf48153-937d-4066-9dd3-cc13ce841e6e.png)
图 11.8:Showing the stock prediction Core ML model converted from Keras and TensorFlow in an Objective-C app
图 11.8:在 Objective-C 应用中显示从 Keras 和 TensorFlow 转换而来的股票预测 Core ML 模型
使用 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)
图 11.9:Showing the stock prediction Core ML model converted from Keras and TensorFlow in a Swift app
图 11.9:在 Swift 应用中显示从 Keras 和 TensorFlow 转换而来的股票预测 Core ML 模型
要在 Objective-C 中使用该模型,请创建具有指定数据类型和形状的`Stock`对象和`MLMultiArray`对象,然后使用一些输入数据填充数组对象,并使用`StockInput`调用`predictionFromFeatures`方法 用 `MLMultiArray`数据初始化的实例:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册