提交 7be73b7b 编写于 作者: W wizardforcel

2020-12-10 12:11:57

上级 d65234ac
......@@ -34,7 +34,7 @@
迁移学习的第二个主要好处是,我们只需要少量的训练数据就可以重新训练 CNN 的最后一层。 如果必须从头开始训练深层 CNN 的数百万个参数,则需要大量的训练数据。 例如,对于我们的狗品种再训练,我们只需要为每个狗品种提供 100 幅以上的图像,即可建立一个比原始图像分类模型更好的狗品种分类模型。
If you're unfamiliar with CNN, check out the videos and notes of one of the best resources on it, the Stanford CS231n course *CNN for Visual Recognition* ([http://cs231n.stanford.edu](http://cs231n.stanford.edu)). Another good resource on CNN is Chapter 6 of *Michael Nielsen's* online book, *Neural Networks and Deep Learning*: [http://neuralnetworksanddeeplearning.com/chap6.html#introducing_convolutional_networks](http://neuralnetworksanddeeplearning.com/chap6.html#introducing_convolutional_networks).
如果您不熟悉 CNN,请查看其中的最佳资源之一的视频和注释,这是 Stanford CS231n 课程[“用于视觉识别的 CNN”](http://cs231n.stanford.edu)。 CNN 的另一个很好的资源是 [Michael Nielsen 的在线书籍《神经网络和深度学习》的第 6 章](http://neuralnetworksanddeeplearning.com/chap6.html#introducing_convolutional_networks)
在接下来的两个部分中,我们将使用针对 TensorFlow 的两个最佳的经过预训练的 CNN 模型和一个犬种数据集来重新训练模型并生成更好的犬种识别模型。 第一个模型是 Inception v3,它是比 Inception v1 更准确的模型,已针对准确性进行了优化,但尺寸较大。 另一个模型是 MobileNet,它针对移动设备的大小和效率进行了优化。 TensorFlow 支持的预训练模型的详细列表位于[这里](https://github.com/tensorflow/models/tree/master/research/slim#pre-trained-models)
......@@ -98,7 +98,7 @@ INFO:tensorflow:Final test accuracy = 91.0% (N=2109)
如前面的输出所示,在再训练结束时,我们看到验证精度与火车精度相似(90% 和 93%,相比之初为 3% 和 21%),最终测试精度为 91% 。 交叉熵也从开始时的 4.767 下降到最后的 0.379。 因此,我们现在有了一个很好的再训练狗品种识别模型。
To further improve the accuracy, you can play with the `retrain.py`'s other parameters such as training steps (`--how_many_training_steps`), learning rate (`--learning_rate`), and data augmentation (`--flip_left_right`, `--random_crop`, `--random_scale`, `--random_brightness`). Generally, this is a tedious process that involves a lot of "dirty work" as called by Andrew Ng, one of the best-known deep learning experts, in his *Nuts and Bolts of Applying Deep Learning* speech (video is available at: [https://www.youtube.com/watch?v=F1ka6a13S9I](https://www.youtube.com/watch?v=F1ka6a13S9I)).
为了进一步提高准确性,您可以使用`retrain.py`的其他参数(例如训练步骤) (`--how_many_training_steps`),学习率(`--learning_rate`),和数据扩充(`--flip_left_right`, `--random_crop`, `--random_scale`, `--random_brightness`). 通常,这是一个乏味的过程,涉及到许多“肮脏的工作”,这是最著名的深度学习专家之一 Andrew Ng 在他的“应用深度学习的基本原理”演讲中提到的([视频可在以下位置找到](https://www.youtube.com/watch?v=F1ka6a13S9I))。
您可以使用另一个 Python 脚本`label_image`来对经过重新训练的模型进行快速测试,以测试自己的图像(例如`/tmp/lab1.jpg`中的 Labrador Retriever 图像),您可以在首先对其进行如下构建后运行该脚本:
......@@ -185,7 +185,7 @@ Out[5]: [u'final_result']
1. **去除未使用的节点**:删除模型中仅在训练期间使用但在推理期间不需要的节点。
2. **量化模型**:将模型参数的所有 32 位浮点数转换为 8 位值。 这样可以将模型大小减小到其原始大小的 25%,同时保持推理精度大致相同。
TensorFlow's documentation ([https://www.tensorflow.org/performance/quantization](https://www.tensorflow.org/performance/quantization)) offers more details on quantization and why it works.
[TensorFlow 文档](https://www.tensorflow.org/performance/quantization)提供有关量化及其工作原理的更多详细信息。
有两种方法可以执行前面的两个任务:使用`strip_unused`工具的旧方法和使用`transform_graph`工具的新方法。
......
......@@ -29,7 +29,7 @@
自从提出 RCNN 以来,性能更好的对象检测算法纷至沓来:快速 RCNN,更快的 RCNN,YOLO(您只看一次),SSD(单发多框检测器)和 YOLO v2。
Andrej Karpathy wrote a good introduction to RCNN, "Playing around with RCNN, State of the Art Object Detector” in 2014 ([https://cs.stanford.edu/people/karpathy/rcnn](https://cs.stanford.edu/people/karpathy/rcnn)). There's a nice video lecture, “Spatial Localization and Detection", as part of Stanford’s CS231n course by Justin Johnson, on object detection, with details on RCNN, Fast RCNN, Faster RCNN, and YOLO. SSD is described in detail at [https://github.com/weiliu89/caffe/tree/ssd](https://github.com/weiliu89/caffe/tree/ssd). And the cool YOLO2 website is [https://pjreddie.com/darknet/yolo](https://pjreddie.com/darknet/yolo).
2014 年,Andrej Karpathy 对 RCNN 作了很好的介绍,[“玩转 RCNN,先进的物体检测器”](https://cs.stanford.edu/people/karpathy/rcnn)。 贾斯汀·约翰逊(Justin Johnson)在斯坦福大学CS231n课程中提供了一个很好的视频讲座[“空间定位和检测”](https://github.com/weiliu89/caffe/tree/ssd),内容涉及物体检测,其中包括 RCNN,Fast RCNN,Faster RCNN 和 YOLO 的详细信息。 YOLO2 网站是[这里](https://pjreddie.com/darknet/yolo)
快速 RCNN 通过首先在整个输入图像上而不是数千个建议的区域上应用 CNN,然后处理区域建议,从而显着改善了训练过程和推理时间(10 小时的训练和 2.x 秒的推理)。 更快的 RCNN 通过使用区域建议网络进一步将推理速度提高到了实时(0.2 秒),因此在训练后,不再需要耗时的区域建议过程。
......
......@@ -28,7 +28,7 @@
尽管原始神经样式转换算法的结果令人惊叹,但其性能却很差-训练是样式转换图像生成过程的一部分,通常在 GPU 上花费几分钟,在 CPU 上花费约一个小时才能生成良好的图像。 结果。
If you're interested in the details of the original algorithm, you can read the paper along with a well-documented Python implementation, at [https://github.com/log0/neural-style-painting/blob/master/art.py](https://github.com/log0/neural-style-painting/blob/master/art.py). We won't discuss this original algorithm as it's not feasible to run on mobile phone, but it's fun and instrumental to try it to get a better understanding of how to use a pre-trained deep CNN model for different computer vision tasks.
如果您对原始算法的细节感兴趣,可以在[以下位置](https://github.com/log0/neural-style-painting/blob/master/art.py)阅读该论文以及文档齐全的 Python 实现。我们不会讨论这种原始算法,因为在手机上运行该算法是不可行的,但是尝试该算法很有趣且有用,可以更好地了解如何针对不同的计算机视觉任务使用预先训练的深度 CNN 模型。
自然地,在 2016 年,论文中发布了一种“快三个数量级”的新算法,即[《实时样式传递和超分辨率的感知损失》](https://cs.stanford.edu/people/jcjohns/eccv16/),作者是 Justin Johnson 等。 它使用单独的训练过程,并定义了更好的损失函数,这些函数本身就是深度神经网络。 训练后(在下一节中我们将看到,在 GPU 上可能要花费几个小时),使用训练后的模型来生成样式转换的图像在计算机上几乎是实时的,而在智能手机上只需几秒钟。
......
......@@ -30,7 +30,7 @@
现代语音识别引擎几乎可以理解嘈杂环境下人们的任何说话,并且基于端到端深度学习,尤其是另一种更适合自然语言处理的深度神经网络,称为**循环神经网络****RNN**)。 与传统的基于 HMM 的语音识别不同,传统的基于 HMM 的语音识别需要人的专业知识来构建和微调手工设计的功能以及声学和语言模型,而基于 RNN 的端到端语音识别系统则将音频输入直接转换为文本,而无需 将音频输入转换为语音表示以进行进一步处理。
RNN allows us to handle sequences of input and/or output, because the network, by design, has memory of previous items in an input sequence or can generate a sequence of output. This makes RNN more appropriate for speech recognition, (where the input is a sequence of words uttered by users), image captioning, (where the output is a natural language sentence consisting of a series of words), text generation, and time series prediction. If you're unfamiliar with RNN, you should definitely check out *Andrey Karpathy's* blog, *The Unreasonable Effectiveness of Recurrent Neural Networks* ([http://karpathy.github.io/2015/05/21/rnn-effectiveness](http://karpathy.github.io/2015/05/21/rnn-effectiveness)). We'll also cover some detailed RNN models later in the book.
RNN 允许我们处理输入和/或输出的序列,因为根据设计,网络可以存储输入序列中的先前项目或可以生成输出序列。 这使RNN更适用于语音识别(输入是用户说出的单词序列),图像标题(输出是由一系列单词组成的自然语言句子),文本生成和时间序列预测 。 如果您不熟悉 RNN,则一定要查看 Andrey Karpathy 的博客,[循环神经网络的不合理有效性](http://karpathy.github.io/2015/05/21/rnn-effectiveness)。 在本书的后面,我们还将介绍一些详细的 RNN 模型。
[关于 RNN 端到端语音识别的第一篇研究论文发表于 2014 年](http://proceedings.mlr.press/v32/graves14.pdf),使用的是**连接主义的时间分类****CTC**)层。 2014 年下半年,百度发布了 [Deep Speech](https://arxiv.org/abs/1412.5567),这是第一个使用基于 CTC 的端到端 RNN 构建但拥有庞大数据集的商业系统之一 ,并在嘈杂的环境中实现了比传统 ASR 系统更低的错误率。 如果您有兴趣,可以查看[深度语音的 TensorFlow 实现](https://github.com/mozilla/DeepSpeech),但是生成的模型会 由于此类基于 CTC 的系统存在问题,因此需要太多的资源才能在手机上运行。 在部署期间,它需要一个大型语言模型来纠正部分由 RNN 的性质引起的生成的文本错误(如果您想知道为什么,请阅读前面链接的 RNN 博客以获取一些见识)。
......@@ -55,11 +55,11 @@ RNN allows us to handle sequences of input and/or output, because the network, b
我们将建立的简单语音命令识别模型将能够识别 10 个单词:`"yes", "no", "up", "down", "left", "right", "on", "off", "stop", "go"`; 它也可以检测沉默。 如果没有发现沉默,并且没有发现 10 个单词,它将生成“未知”。 稍后运行`tensorflow/example/speech_commands/train.py`脚本时,我们将下载语音命令数据集并用于训练模型,实际上除了这 10 个单词外,还包含 20 个单词:`"zero", "two", "three", ..., "ten"`(到目前为止,您已经看到的 20 个词称为核心词)和 10 个辅助词:`"bed", "bird", "cat", "dog", "happy", "house", "marvin", "sheila", "tree", "wow"`。 核心词比辅助词(约 1750)具有更多的`.wav`文件记录(约 2350)。
The speech commands dataset is collected from an Open Speech Recording site ([https://aiyprojects.withgoogle.com/open_speech_recording](https://aiyprojects.withgoogle.com/open_speech_recording)). You should give it a try and maybe contribute a few minutes of your own recordings to help it improve and also get a sense of how you can collect your own speech commands dataset if needed. There's also a Kaggle competition ([https://www.kaggle.com/c/tensorflow-speech-recognition-challenge](https://www.kaggle.com/c/tensorflow-speech-recognition-challenge)) on using the dataset to build a model and you can learn more about speech models and tips there.
语音命令数据集是从[开放语音记录站点](https://aiyprojects.withgoogle.com/open_speech_recording)收集的。您应该尝试一下,也许自己花些时间来录制自己的录音,以帮助改善录音效果,并在需要时了解如何收集自己的语音命令数据集。 关于使用数据集构建模型,[还有一个 Kaggle 竞赛](https://www.kaggle.com/c/tensorflow-speech-recognition-challenge),您可以在此处了解有关语音模型和提示的更多信息。
在移动应用中要训练和使用的模型基于纸质卷积神经网络,[用于小尺寸关键词发现](http://www.isca-speech.org/archive/interspeech_2015/papers/i15_1478.pdf),这与大多数其他基于 RNN 的大规模语音识别模型不同。 基于 CNN 的语音识别模型是可能的,但很有趣,因为对于简单的语音命令识别,我们可以在短时间内将音频信号转换为图像,或更准确地说,将频谱图转换为频率 窗口期间音频信号的分布(有关使用`wav_to_spectrogram`脚本生成的示例频谱图图像,请参见本节开头的 TensorFlow 教程链接)。 换句话说,我们可以将音频信号从其原始时域表示转换为频域表示。 进行此转换的最佳算法是**离散傅立叶变换****DFT**),**快速傅立叶变换****FFT**)只是一种有效的选择 DFT 实现的算法。
As mobile developers, you probably don't need to understand DFT and FFT. But you'd better appreciate how all this model training works when used in mobile apps by knowing that behind the scenes of the TensorFlow simple speech commands model training that we're about to cover, it's the use of FFT, one of the top 10 algorithms in the 20th century, among other things of course, that makes the CNN-based speech command recognition model training possible. For a fun and intuitive tutorial on DFT, you can read this article: [http://practicalcryptography.com/miscellaneous/machine-learning/intuitive-guide-discrete-fourier-transform](http://practicalcryptography.com/miscellaneous/machine-learning/intuitive-guide-discrete-fourier-transform) .
作为移动开发人员,您可能不需要了解 DFT 和 FFT。 但是,您最好了解所有这些模型训练在移动应用中使用时是如何工作的,因为我们知道我们将要介绍的 TensorFlow 简单语音命令模型训练的幕后花絮,这是 FFT 的使用,前十大模型之一 当然,除其他事项外,20 世纪的算法使基于 CNN 的语音命令识别模型训练成为可能。 有关DFT的有趣且直观的教程,您可以阅读[以下文章](http://practicalcryptography.com/miscellaneous/machine-learning/intuitive-guide-discrete-fourier-transform)
现在,让我们执行以下步骤来训练简单语音命令识别模型:
......@@ -289,7 +289,7 @@ if (record.getState() != AudioRecord.STATE_INITIALIZED) return;
record.startRecording();
```
There are two classes in Android for recording audio: `MediaRecorder` and `AudioRecord`. `MediaRecorder` is easier to use than `AudioRecord`, but it saves compressed audio files until Android API Level 24 (Android 7.0), which supports recording raw, unprocessed audio. According to [https://developer.android.com/about/dashboards/index.html](https://developer.android.com/about/dashboards/index.html), as of January 2018, there are more than 70% of Android devices in the market that still run Android versions older than 7.0\. You probably would prefer not to target your app to Android 7.0 or above. In addition, to decode the compressed audio recorded by `MediaRecorder`, you have to use `MediaCodec`, which is pretty complicated to use. `AudioRecord`, albeit a low-level API, is actually perfect for recording raw unprocessed data which is then sent to the speech commands recognition model for processing.
Android 中有两个用于记录音频的类:`MediaRecorder``AudioRecord``MediaRecorder``AudioRecord`更易于使用,但是它会保存压缩的音频文件,直到 Android API Level 24(Android 7.0)为止,该 API 支持录制未经处理的原始音频。 根据[这里](https://developer.android.com/about/dashboards/index.html),截至 2018 年 1 月,市场上有 70% 以上的 Android 设备仍在运行 7.0 或更早的 Android 版本。 您可能不希望将应用程序定位到 Android 7.0 或更高版本。 另外,要解码由`MediaRecorder`录制的压缩音频,您必须使用`MediaCodec`,使用起来非常复杂。 `AudioRecord`尽管是一个低级的 API,但实际上非常适合记录未处理的原始数据,然后将其发送到语音命令识别模型进行处理。
9. 创建两个由 16 位短整数组成的数组`audioBuffer``recordingBuffer`,对于 1 秒记录,每次`AudioRecord`对象读取并填充`audioBuffer`数组后,实际读取的数据都会附加到 `recordingBuffer`
......@@ -412,7 +412,7 @@ runOnUiThread(new Runnable() {
如果您已经阅读了前三章中的 iOS 应用程序,那么您可能更喜欢使用手动构建的 TensorFlow iOS 库而不是 TensorFlow 实验窗格,就像使用手动库方法一样,您可以更好地控制可以使用哪些 TensorFlow 操作 添加更好的模型使您的模型满意,这也是我们决定专注于 TensorFlow Mobile 而不是第 1 章,“移动 TensorFlow”的 TensorFlow Lite 的原因之一。
因此,尽管您可以在阅读本书时尝试使用 TensorFlow 吊舱,以查看吊舱是否已更新以支持模型中使用的所有操作,但从现在开始,我们将始终使用手动构建的 TensorFlow 库( 请参见 iOS 应用中第 3 章,“检测对象及其位置”的“在 iOS 中使用对象检测模型的”部分的步骤 1 和 2)。
因此,尽管您可以在阅读本书时尝试使用 TensorFlow Pod,以查看Pod是否已更新以支持模型中使用的所有操作,但从现在开始,我们将始终使用手动构建的 TensorFlow 库( 请参见 iOS 应用中第 3 章,“检测对象及其位置”的“在 iOS 中使用对象检测模型的”部分的步骤 1 和 2)。
......
......@@ -32,7 +32,7 @@ In the next section, you'll see a detailed code snippet that matches the given d
在生成者和区分者两个参与者的竞争目标下,GAN 是一个寻求两个对手之间保持平衡的系统。 如果两个玩家都具有无限的能力并且可以进行最佳训练,那么纳什均衡(继 1994 年诺贝尔经济学奖得主约翰·纳什和电影主题《美丽心灵》之后) 一种状态,在这种状态下,任何玩家都无法通过仅更改其自己的策略来获利,这对应于生成器生成数据的状态,该数据看起来像真实数据,而判别器无法从假数据中分辨真实数据。
If you're interested in knowing more about the Nash Equilibrium, Google *"khan academy nash equilibrium"* and watch the two fun videos on it by Sal Khan. The Wikipedia page on Nash Equilibrium and the article, *"**What is the Nash equilibrium and why does it matter?"* in The Economist explaining economics ([https://www.economist.com/blogs/economist-explains/2016/09/economist-explains-economics](https://www.economist.com/blogs/economist-explains/2016/09/economist-explains-economics)) are also a good read. Understanding the basic intuition and idea behind GANs will help you appreciate more why it has great potential.
如果您有兴趣了解有关纳什均衡的更多信息,请访问 Google “可汗学院纳什均衡”,并观看 Sal Khan 撰写的两个有趣的视频。 《经济学家》解释经济学的“纳什均衡”维基百科页面和文章[“纳什均衡是什么,为什么重要?”](https://www.economist.com/blogs/economist-explains/2016/09/economist-explains-economics)也是不错的读物。 了解 GAN 的基本直觉和想法将有助于您进一步了解 GAN 具有巨大潜力的原因。
生成器能够生成看起来像真实数据的数据的潜力意味着可以使用 GAN 开发各种出色的应用程序,例如:
......
......@@ -39,7 +39,7 @@ AlphaZero 算法包含三个主要组件:
如果深层神经网络使用自我玩法强化学习所学习的策略是理想的,则我们可能无需让程序在游戏过程中执行任何 MCTS,而程序总是可以最大可能地选择移动。 但是在诸如象棋或围棋的复杂游戏中,无法生成完美的策略,因此 MCTS 必须与训练有素的深度网络一起工作,以指导针对每种游戏状态的最佳可能动作的搜索。
If you're not familiar with reinforcement learning or MCTS, there's lots of information about them on the internet. Consider checking out *Richard Sutton* and *Andrew Barto's* classic book, *Reinforcement Learning: An Introduction*, which is publicly available at [http://incompleteideas.net/book/the-book-2nd.html](http://incompleteideas.net/book/the-book-2nd.html). You can also watch the reinforcement learning course videos by *David Silver*, the technical lead for *AlphaGo* at *DeepMind*, on YouTube (search "reinforcement learning David Silver"). A fun and useful toolkit for reinforcement learning is OpenAI Gym ([https://gym.openai.com](https://gym.openai.com)). In the last chapter of the book, we'll go deeper into reinforcement learning and OpenAI Gym. For MCTS, check out its Wiki page, [https://en.wikipedia.org/wiki/Monte_Carlo_tree_search](https://en.wikipedia.org/wiki/Monte_Carlo_tree_search), as well as this blog: [http://tim.hibal.org/blog/alpha-zero-how-and-why-it-works](http://tim.hibal.org/blog/alpha-zero-how-and-why-it-works).
如果您不熟悉强化学习或 MCTS,则在互联网上有很多关于强化学习或 MCTS 的信息。 考虑查看 Richard Sutton 和 Andrew Barto 的经典著作《强化学习:简介》,该书可在[以下网站](http://incompleteideas.net/book/the-book-2nd.html)上公开获得。 您还可以在YouTube上观看 DeepMind 的 AlphaGo 的技术负责人 David Silver 的强化学习课程视频(搜索“强化学习 David Silver”)。 一个有趣且有用的强化学习工具包是 [OpenAI Gym](https://gym.openai.com)。 在本书的最后一章中,我们将更深入地学习强化学习和 OpenAI Gym。 对于 MCTS,请查看其[维基页面](https://en.wikipedia.org/wiki/Monte_Carlo_tree_search),以及[此博客](http://tim.hibal.org/blog/alpha-zero-how-and-why-it-works)
在下一节中,我们将研究以 TensorFlow 为后端的 Keras 实现 AlphaZero 算法,其目标是使用该算法构建和训练模型以玩 Connect4。您将看到 模型架构看起来像,并且是关键的 Keras 代码来构建模型。
......@@ -356,7 +356,7 @@ python tensorflow/python/tools/freeze_graph.py \
对于新冻结的,可选的经过转换和映射的模型,您始终可以将其与 TensorFlow 吊舱一起尝试,以查看是否有幸能够以简单的方式使用它。 在我们的案例中,当使用 TensorFlow Pod 加载它时,我们生成的`alphazero19.pb` 模型 会导致以下错误:
对于新冻结的,可选的经过转换和映射的模型,您始终可以将其与 TensorFlow Pod一起尝试,以查看是否有幸能够以简单的方式使用它。 在我们的案例中,当使用 TensorFlow Pod 加载它时,我们生成的`alphazero19.pb` 模型 会导致以下错误:
```py
Couldn't load model: Invalid argument: No OpKernel was registered to support Op 'Switch' with these attrs. Registered devices: [CPU], Registered kernels:
......
......@@ -29,14 +29,14 @@
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.
您还可以选择一个预先构建的 TensorFlow Lite 模型,例如可从以下位置获得的 [MobileNet 模型](https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet_v1.md),我们在第 2 章,《使用转移学习对图像进行分类》中将其用于再训练。 您可以在此处下载的每个MobileNet 模型`tgz`文件都包含转换后的 TensorFlow Lite 模型。 例如,`MobileNet_v1_1.0_224.tgz`文件包含一个`mobilenet_v1_1.0_224.tflite`文件,您可以直接在移动设备上使用它。 如果使用这样的预构建 TensorFlow Lite 模型,则可以跳过步骤 2 和 3。
2. 构建 TensorFlow Lite 转换器工具。 如果您从[这里](https://github.com/tensorflow/tensorflow/releases)下载 TensorFlow 1.5 或 1.6 版本,则可以从 TensorFlow 源根目录在终端上运行`bazel build tensorflow/contrib/lite/toco:toco`。 如果您使用更高版本或获取最新的 TensorFlow 仓库,您应该可以使用此`build`命令来执行此操作,但如果没有,请查看该新版本的文档。
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`
If you run a TensorFlow Lite model on Android, and if the Android device is Android 8.1 (API level 27) or above and supports hardware acceleration with a dedicated neural network hardware, a GPU, or some other digital signal processors, then the `Interpreter` will use the Android Neural Networks API ([https://developer.android.com/ndk/guides/neuralnetworks/index.html](https://developer.android.com/ndk/guides/neuralnetworks/index.html)) to speed up the model running. For example, Google's Pixel 2 phone has a custom chip optimized for image processing, which can be turned on with Android 8.1, and support hardware acceleration.
如果您在 Android 上运行 TensorFlow Lite 模型,并且 Android 设备为 Android 8.1(API 级别 27)或更高版本,并且通过专用的神经网络硬件,GPU 或某些其他数字信号处理器支持硬件加速,则`Interpreter`将使用 [Android 神经网络 API](https://developer.android.com/ndk/guides/neuralnetworks/index.html)来加快模型运行。 例如,谷歌的 Pixel 2 手机具有针对图像处理进行了优化的自定义芯片,可以通过 Android 8.1 开启该芯片,并支持硬件加速。
现在让我们看看如何在 iOS 中使用 TensorFlow Lite。
......@@ -116,7 +116,9 @@ target 'HelloTFLite'
运行`pod install`。 然后在 Xcode 中打开 `HelloTFLite.xcworkspace`,将`ViewController.m`重命名为`ViewController.mm`,并添加必要的 C++ 头文件和 TensorFlow Lite 头文件。 您的 Xcode 项目应类似于以下屏幕截图:
![](img/b4b1750b-cc97-42f8-a032-226b595d7e46.png)Figure 11.1 A new Xcode iOS project using the TensorFlow Lite podWe're only showing you how to use the TensorFlow Lite pod in your iOS apps. There's another way to add TensorFlow Lite to iOS, similar to building the custom TensorFlow Mobile iOS library that we've done many times in the previous chapters. For more information on how to build your own custom TensorFlow Lite iOS library, see the documentation at [https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/ios.md](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/ios.md).
![](img/b4b1750b-cc97-42f8-a032-226b595d7e46.png)Figure 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)
4. 将第 2 章,“通过迁移学习对图像进行分类”的 iOS 应用中的类似 UI 代码复制到`ViewController.mm`,后者使用`UITapGestureRecognizer`捕获用户的手势 屏幕上,然后调用`RunInferenceOnImage`方法,该方法将加载 TensorFlow Lite 模型文件:
......
......@@ -41,7 +41,7 @@
您还需要 HDMI 电缆将 Raspberry Pi 板连接到计算机显示器,USB 键盘和 USB 鼠标。 总共要花 200 美元,包括 110 美元的 GoPiGo,来构建一个可以移动,看,听,说的 Raspberry Pi 机器人。 尽管与功能强大的 Raspberry Pi 计算机相比,GoPiGo 套件似乎有点昂贵,但是如果没有它,那么一动不动的 Raspberry Pi 可能会失去很多吸引力。
There's an older blog, *How to build a robot that “sees” with $100 and TensorFlow* ([https://www.oreilly.com/learning/how-to-build-a-robot-that-sees-with-100-and-tensorflow](https://www.oreilly.com/learning/how-to-build-a-robot-that-sees-with-100-and-tensorflow)), written by *Lukas Biewald* in September 2016, that covers how to use TensorFlow and Raspberry Pi 3 with some alternative parts to build a robot that sees and speaks. It's a fun read. What we cover here offers more detailed steps to set up Raspberry Pi 3 with GoPiGo, the user-friendly and Google-recommended toolkit to turn Pi into a robot, and the newer version of TensorFlow 1.6, in addition to adding voice command recognition and reinforcement learning.
有一个较旧的博客,[“如何用100美元和TensorFlow构建“可视”的机器人”](https://www.oreilly.com/learning/how-to-build-a-robot-that-sees-with-100-and-tensorflow),由 Lukas Biewald 于 2016 年 9 月撰写,内容涵盖了如何使用 TensorFlow 和 Raspberry Pi 3 以及一些其他部件来构建能够说话和说话的机器人。 这很有趣。 我们这里介绍的内容除了提供语音命令识别和强化学习外,还提供了更详细的步骤来设置带有 GoPiGo 的 Raspberry Pi 3,GoPiGo(易于使用且受 Google 推荐的工具包,可将 Pi 变成机器人)以及更新版本的 TensorFlow 1.6。
现在,让我们首先看看如何设置 Raspbian,Raspberry Pi 开发板的操作系统。
......@@ -735,7 +735,7 @@ print(np.mean(total_rewards))
现在让我们看看我们是否可以制定出更好,更复杂的政策。 回想一下,策略只是从状态到操作的映射或功能。 在过去的几年中,我们在神经网络的兴起中了解到的一件事是,如果不清楚如何定义复杂的功能(例如强化学习中的策略),请考虑一下神经网络,毕竟这是通用函数近似器 (有关详细信息,请参见[神经网络可以计算任何函数的可视化证明](http://neuralnetworksanddeeplearning.com/chap4.html),Michael Nelson)。
We covered AlphaGo and AlphaZero in the last chapter, and Jim Fleming wrote an interesting blog entry titled Before AlphaGo there was TD-Gammon ([https://medium.com/jim-fleming/before-alphago-there-was-td-gammon-13deff866197](https://medium.com/jim-fleming/before-alphago-there-was-td-gammon-13deff866197)), which was the first reinforcement learning application that trains itself using a neural network as an evaluation function to beat human Backgammon champions. Both the blog entry and the book, *Reinforcement Learning: An Introduction* by *Sutton* and *Barto*, have in-depth descriptions of TD-Gammon; you can also Google "Temporal Difference Learning and TD-Gammon" for the original paper if you want to know more about using neural networks as a powerful universal function.
在上一章中我们介绍了 AlphaGo 和 AlphaZero,Jim Fleming 撰写了一篇有趣的博客文章,标题为[“在 AlphaGo 之前的 TD-Gammon”](https://medium.com/jim-fleming/before-alphago-there-was-td-gammon-13deff866197),这是第一个强化学习应用程序,它使用神经网络作为评估功能来训练自己,以击败五子棋冠军。 博客条目和 Sutton 和 Barto 的《强化学习:入门》一书都对 TD-Gammon 进行了深入的描述。 如果您想了解有关使用神经网络作为强大的通用函数的更多信息,还可以使用 Google 搜索“时差学习和 TD-Gammon”的原始论文。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册