提交 1d75f63e 编写于 作者: W wizardforcel

2020-09-12 13:36:44

上级 eb6875fb
# 前言
本书旨在为您提供实用的机器学习动手入门,其目的是使任何人都可以在该领域开始工作。 我们将主要关注深度学习方法以及如何将其用于解决重要的计算机视觉问题,但是此处获得的知识可以转移到许多不同的领域。 在此过程中,读者还将掌握如何使用流行的深度学习库 TensorFlow。
# 这本书是给谁的
任何对实用的机器学习指南(特别是深度学习和计算机视觉)感兴趣的人都将从阅读本书中受益。 此外,以下人员也将受益:
* 机器学习工程师
* 数据科学家
* 对学习深度学习和计算机视觉领域感兴趣的开发人员
* 学生学习机器学习
# 本书涵盖的内容
[第 1 章](../Text/1.xhtml)*Tensorflow 的设置和简介,*涵盖了 TensorFlow 的设置和安装,以及编写用于机器学习的简单 Tensorflow 模型。
[第 2 章](../Text/2.xhtml)*深度学习和卷积神经网络*向您介绍了机器学习,人工智能以及人工神经网络以及如何对其进行训练。 它还涵盖了 CNN 以及如何使用 TensorFlow 训练自己的 CNN。
[第 3 章](../Text/3.xhtml)*Tensorflow* 中的图像分类,讨论了如何构建 CNN 模型以及如何训练它们以对 CIFAR10 数据集进行分类。 它还探讨了通过谈论初始化和正则化的不同方法来帮助提高我们训练后的模型的质量的方法。
[第 4 章](../Text/4.xhtml)*对象检测和分割*教授对象定位,检测和分割的基础知识以及与这些主题相关的最著名的算法。
[第 5 章](../Text/5.xhtml)*VGG,接收模块,残差和 MobileNets* 向您介绍了不同的卷积神经网络设计,例如 VGGNet,GoggLeNet 和 MobileNet。
[第 6 章](../Text/6.xhtml)*自动编码器,变体自动编码器和生成对抗网络,*向您介绍生成模型,生成对抗网络和不同类型的编码器。
[第 7 章](../Text/7.xhtml)*转移学习,*涵盖了转移学习的用法并在我们自己的任务中实现。
[第 8 章](../Text/8.xhtml)*机器学习最佳实践和* *故障排除,*向我们介绍了如何准备并将数据集拆分为子集并执行有意义的测试。 本章还讨论了过拟合和过拟合以及解决这些问题的最佳实践。
[第 9 章](../Text/9.xhtml)*大规模培训,*教您如何在多个 GPU 和机器上训练 TensorFlow 模型。 它还涵盖了存储数据并将其输入模型的最佳实践。
# 充分利用这本书
为了充分利用本书,读者应该对 Python 编程语言以及如何安装一些必需的软件包有所了解。 本书将以简单的语言介绍所有其他内容。 安装说明将在本书和存储库中给出。
# 下载示例代码文件
您可以从 [www.packtpub.com](http://www.packtpub.com) 的帐户中下载本书的示例代码文件。 如果您在其他地方购买了此书,则可以访问 [www.packtpub.com/support](http://www.packtpub.com/support) 并注册以将文件直接通过电子邮件发送给您。
您可以按照以下步骤下载代码文件:
1. 登录或注册 [www.packtpub.com](http://www.packtpub.com/support)
2. 选择支持选项卡。
3. 单击代码下载和勘误。
4. 在搜索框中输入书籍的名称,然后按照屏幕上的说明进行操作。
下载文件后,请确保使用以下最新版本解压缩或解压缩文件夹:
* Windows 的 WinRAR / 7-Zip
* Mac 版 Zipeg / iZip / UnRarX
* 适用于 Linux 的 7-Zip / PeaZip
本书的代码包也托管在 GitHub 的 [https://github.com/PacktPublishing/Hands-on-Convolutional-Neural-Networks-with-Tensorflow](https://github.com/PacktPublishing/Hands-on-Convolutional-Neural-Networks-with-Tensorflow) 上。 如果代码有更新,它将在现有 GitHub 存储库上进行更新。
我们还从 **[https://github.com/PacktPublishing/](https://github.com/PacktPublishing/)** 提供了丰富的书籍和视频目录中的其他代码包。 去看一下!
# 使用约定
本书中使用了许多文本约定。
`CodeInText`:指示文本,数据库表名称,文件夹名称,文件名,文件扩展名,路径名,虚拟 URL,用户输入和 Twitter 句柄中的代码字。 这是一个示例:“将下载的`WebStorm-10*.dmg`磁盘映像文件安装为系统中的另一个磁盘。”
代码块设置如下:
```py
import tensorflow as tf
# XOR dataset
XOR_X = [[0, 0], [0, 1], [1, 0], [1, 1]]
XOR_Y = [[0], [1], [1], [0]]
```
当我们希望引起您对代码块特定部分的注意时,相关的行或项目以粗体显示:
```py
import tensorflow as tf
# XOR dataset
XOR_X = [[0, 0], [0, 1], [1, 0], [1, 1]]
XOR_Y = [[0], [1], [1], [0]]
```
任何命令行输入或输出的编写方式如下:
```py
$ pip install numpy
$ pip install scipy
```
**粗体**:表示新术语,重要单词或您在屏幕上看到的单词。 例如,菜单或对话框中的单词会出现在这样的文本中。 这是一个示例:“从管理面板中选择系统信息。”
警告或重要提示如下所示。
提示和技巧如下所示。
# 保持联系
始终欢迎读者的反馈。
**一般反馈**:给`feedback@packtpub.com`发送电子邮件,并在邮件主题中提及书名。 如果您对本书的任何方面有疑问,请通过`questions@packtpub.com`向我们发送电子邮件。
**勘误表**:尽管我们已尽一切努力确保内容的准确性,但还是会发生错误。 如果您在这本书中发现错误,请向我们报告,我们将不胜感激。 请访问 [www.packtpub.com/submit-errata](http://www.packtpub.com/submit-errata) ,选择您的图书,点击勘误提交表格链接,然后输入详细信息。
**盗版**:如果您在互联网上以任何形式看到我们的作品的任何非法复制品,请向我们提供位置地址或网站名称,我们将不胜感激。 请通过`copyright@packtpub.com`与我们联系,并提供材料链接。
**如果您有兴趣成为作者**:如果您有某个专业知识并且有兴趣撰写或撰写书籍,请访问 [authors.packtpub.com](http://authors.packtpub.com/) ]。
# 评论
请留下评论。 阅读和使用本书后,为什么不在您购买本书的网站上留下评论? 然后,潜在的读者可以查看并使用您的公正意见做出购买决定,Packt 的我们可以理解您对我们产品的看法,我们的作者可以在书中看到您的反馈。 谢谢!
有关 Packt 的更多信息,请访问 [packtpub.com](https://www.packtpub.com/)
\ No newline at end of file
此差异已折叠。
# References
# Chapter 1
Here we're going to learn how to install and the basics of Tensorflow, as references we have:
* [https://www.tensorflow.org/tutorials/](https://www.tensorflow.org/tutorials/)
* [https://www.tensorflow.org/install/](https://www.tensorflow.org/install/)
# Chapter 2
This chapter will tackle the principals of Machine Learning and Neural Networks with emphasis on Computer Vision with Convolutional Neural Networks. The references for the chapter are:
* [https://en.wikipedia.org/wiki/Artificial_neural_network](https://en.wikipedia.org/wiki/Artificial_neural_network)
* [https://en.wikipedia.org/wiki/Timeline_of_machine_learning](https://en.wikipedia.org/wiki/Timeline_of_machine_learning)
* [http://ais.informatik.uni-freiburg.de/teaching/ss11/ki/slides/ai12_acting_under_uncertainty_handout_4up.pdf](http://ais.informatik.uni-freiburg.de/teaching/ss11/ki/slides/ai12_acting_under_uncertainty_handout_4up.pdf)
* [http://yann.lecun.com/exdb/publis/pdf/lecun-98.pdf](http://yann.lecun.com/exdb/publis/pdf/lecun-98.pdf)
* [https://www.facebook.com/yann.lecun/posts/10152820758292143](https://www.facebook.com/yann.lecun/posts/10152820758292143)
* [https://towardsdatascience.com/types-of-convolutions-in-deep-learning-717013397f4d](https://towardsdatascience.com/types-of-convolutions-in-deep-learning-717013397f4d)
* [http://cs231n.stanford.edu/](http://cs231n.stanford.edu/)
* [http://cs224d.stanford.edu/](http://cs224d.stanford.edu/)
# Chapter 3
This chapter will be cover image classification using Deep learning, and why CNNs disrupted the way we do computer vision now. The references for this chapter are:
* [Learning Multiple Layers of Features from Tiny Images](https://www.cs.toronto.edu/~kriz/learning-features-2009-TR.pdf), Alex Krizhevsky, 2009
* An excellent flashback on image representation techniques can be found in the *Computer Vision: Algorithms and Applications*, Richard Szeliski, 2010
* [http://www.vision.caltech.edu/Image_Datasets/Caltech101/](http://www.vision.caltech.edu/Image_Datasets/Caltech101/)
* Griffin, Gregory and Holub, Alex and Perona, Pietro (2007) Caltech–256 *Object Category Dataset*
* Everingham, M., Van Gool, L., Williams, C. K. I., Winn, J. and Zisserman, *A International Journal of Computer Vision*, 88(2), 303-338, 2010
* *ImageNet Large Scale Visual Recognition Challenge*, IJCV, 2015
* [https://wordnet.princeton.edu/](https://wordnet.princeton.edu/)
* *What Does Classifying More Than 10,000 Image Categories Tell Us?* Jia Deng, Alexander C. Berg, Kai Li, and Li Fei-Fei
* Olga Russakovsky, Jia Deng et al. (2015) *ImageNet Large Scale Visual Recognition Challenge*, [https://arxiv.org/pdf/1409.0575.pdf](https://arxiv.org/pdf/1409.0575.pdf)
* Alex Krizhevsky, Ilya Sutskever and Geoffrey Hinton, *ImageNet Classification with Deep Convolutional Neural Networks*, 2012
* [https://arxiv.org/pdf/1311.2901.pdf](https://arxiv.org/pdf/1311.2901.pdf)
* *Going deeper with convolutions* by Christian Szegedy Google Inc. et al
* *Deep Residual Learning for Image Recognition*, Kaiming He et al.
* [https://arxiv.org/pdf/1709.01507.pdf](https://arxiv.org/pdf/1709.01507.pdf)
* The batch norm paper is a really well written paper that is easy to understand and explains the concept in much more detail, [https://arxiv.org/pdf/1502.03167.pdf](https://arxiv.org/pdf/1502.03167.pdf)
# Chapter 4
In this chapter we will learn about object detection and segmentation. The references for this chapter are: 
* [https://arxiv.org/pdf/1311.2524.pdf](https://arxiv.org/pdf/1311.2524.pdf) (*Rich feature hierarchies for accurate object detection and semantic segmentation*)
* [https://arxiv.org/pdf/1504.08083.pdf](https://arxiv.org/pdf/1504.08083.pdf) (*Fast RCNN*)
* [https://arxiv.org/pdf/1506.01497.pdf](https://arxiv.org/pdf/1506.01497.pdf) (*Faster RCNN Towards Real-Time Object Detection with Region Proposals*)
* [https://www.youtube.com/watch?v=v5bFVbQvFRk](https://www.youtube.com/watch?v=v5bFVbQvFRk)
* [https://arxiv.org/pdf/1506.02640.pdf](https://arxiv.org/pdf/1506.02640.pdf) (*You Only Look Once: Unified, Real-Time Object Detection* )
* [https://coursera.org/specializations/deep-learning](https://coursera.org/specializations/deep-learning) (*Deep Learning* course by Andrew Ng)
* [https://people.eecs.berkeley.edu/~jonlong/long_shelhamer_fcn.pdf](https://people.eecs.berkeley.edu/~jonlong/long_shelhamer_fcn.pdf) (*Fully Convolutional Neural Network for Semantic Segmentation*)
* [https://arxiv.org/pdf/1606.00915.pdf](https://arxiv.org/pdf/1606.00915.pdf) ( Semantic Image Segmentation with Deep Convolutional Nets, Atrous Convolution, and Fully Connected CRFs)
# Chapter 5
In this chapter we will learn about some common CNN architectures (that is, VGG, ResNet, GoogleNet). The references for this chapter are: 
* Simonyan, K. and Zisserman, A., 2014, *Very Deep Convolutional Networks for Large-Scale Image Recognition*[arXiv preprint arXiv:1409.1556](https://arxiv.org/abs/1409.1556)
* *Going Deeper With Convolutions,* [https://arxiv.org/abs/1409.4842](https://arxiv.org/abs/1409.4842)
* *Deep Residual Learning for Image Recognition*, Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun, Microsoft Research
* *Mobilenets: Efficient Convolutional Neural Networks for Mobile Vision Applications*[https://arxiv.org/abs/1704.04861](https://arxiv.org/abs/1704.04861)
* [https://arxiv.org/pdf/1801.04381.pdf](https://arxiv.org/pdf/1801.04381.pdf), MobileNets V2
# Chapter 7
This chapter will discuss transfer learning and how we can take advantage of other people's model training to help us train our own networks. The references for this chapter are:
* [https://www.cse.ust.hk/~qyang/Docs/2009/tkde_transfer_learning.pdf](https://www.cse.ust.hk/~qyang/Docs/2009/tkde_transfer_learning.pdf)
* [ftp://ftp.cs.wisc.edu/machine-learning/shavlik-group/torrey.handbook09.pdf](ftp://ftp.cs.wisc.edu/machine-learning/shavlik-group/torrey.handbook09.pdf)
* [https://arxiv.org/pdf/1403.6382.pdf](https://arxiv.org/pdf/1403.6382.pdf) (*CNN Features off-the-shelf: an Astounding Baseline for Recognition*)
* [https://arxiv.org/pdf/1310.1531.pdf](https://arxiv.org/pdf/1310.1531.pdf) (*DeCAF: A Deep Convolutional Activation Feature for Generic Visual Recognition*)
# Chapter 9
In the last chapter of this book we will learn how to take advantage of parallel cluster of computers in the cloud to accelerate model training. The references for this chapter are:
* [https://www.oreilly.com/ideas/distributed-tensorflow](https://www.oreilly.com/ideas/distributed-tensorflow)
* [https://research.fb.com/wp-content/uploads/2017/06/imagenet1kin1h5.pdf](https://research.fb.com/wp-content/uploads/2017/06/imagenet1kin1h5.pdf)
* [https://learningtensorflow.com/lesson11/](https://learningtensorflow.com/lesson11/)
* [https://www.tensorflow.org/deploy/distributed](https://www.tensorflow.org/deploy/distributed)
* [https://www.tensorflow.org/programmers_guide/low_level_intro](https://www.tensorflow.org/programmers_guide/low_level_intro)
* [https://github.com/tmulc18/Distributed-TensorFlow-Guide](https://github.com/tmulc18/Distributed-TensorFlow-Guide)
* [https://clusterone.com/blog/2017/09/13/distributed-tensorflow-clusterone/](https://clusterone.com/blog/2017/09/13/distributed-tensorflow-clusterone/)
* [https://www.youtube.com/watch?v=-h0cWBiQ8s8&](https://www.youtube.com/watch?v=-h0cWBiQ8s8&)
* [https://www.youtube.com/watch?v=uIcqeP7MFH0](https://www.youtube.com/watch?v=uIcqeP7MFH0)
* [https://www.youtube.com/watch?v=bRMGoPqsn20](https://www.youtube.com/watch?v=bRMGoPqsn20)
* [https://www.youtube.com/watch?v=1cHx1baKqq0](https://www.youtube.com/watch?v=1cHx1baKqq0)
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
# 转移学习
转移学习的作用恰如其名。 这个想法是将从一项任务中学到的东西转移到另一项任务上。 为什么? 实际上,每次都从头开始训练整个模型的效率很低,其成功取决于许多因素。 另一个重要原因是,对于某些应用程序,公开可用的数据集不够大,无法训练出像 AlexNet 或 ResNet 这样的深层架构而又不会过度拟合,这意味着无法推广。 示例应用程序可以从用户给出的一些示例中进行在线学习,也可以是细粒度的分类,其中类别之间的差异很小。
一个非常有趣的观察结果是,由于您冻结了所有其余部分(无论是检测还是分类),最终的层可以用于完成不同的任务,最终权重看起来非常相似。
这导致了转移学习的想法。 这意味着在大量数据上训练的深度架构(例如 ImageNet)可以很好地概括化,以至于其卷积权重可以充当特征提取器,类似于常规的视觉表示,并且可以用于训练线性分类器以用于 各种任务。
本章旨在教读者如何在 TensorFlow 中采用现成的训练有素的模型,更改其结构以及为特定任务重新训练某些层。 我们将看到转移学习将如何帮助改善结果并加快培训时间。
本章涵盖的主要主题如下:
* 使用来自另一个训练过的模型的权重预先初始化一个模型
* 在需要时使用 TensorFlow 加载模型并冻结/解冻图层
# 什么时候?
研究表明,在 ImageNet 上训练的卷积网络权重中的特征提取优于常规特征提取方法,例如 SURF,[H​​TG0]可变形部分描述符( **DPD** s),**定向梯度直方图****HOG** )和**一袋单词****BoW** )。 这意味着无论常规视觉表示如何工作,卷积特征都可以同样好地使用,唯一的缺点是更深的架构可能需要更长的时间来提取特征。
当在 ImageNet 上训练深层卷积神经网络时,第一层中的卷积滤波器的可视化(请参见下图)显示,他们学习了*低层*特征,类似于边缘检测滤波器,而卷积滤波器 在最后一层学习*高级*功能,这些功能捕获特定于类的信息。 因此,如果我们在第一个池化层之后提取 ImageNet 的特征并将其嵌入 2D 空间(例如,使用 t-SNE),则可视化将显示数据中存在一些无政府状态,而如果执行 在完全连接的层上相同,我们将注意到具有相同语义信息的数据被组织成簇。 这意味着网络可以在更高层次上很好地概括,并且有可能将这种知识转移到看不见的类别中。
![](img/b7b186d6-7803-4517-add9-f559586b2576.png)
根据对与 ImageNet 相似度较小的数据集进行的实验,在以下任务上,基于 ImageNet 训练的基于卷积神经网络权重的特征比常规特征提取方法的性能更好:
* **对象识别**:此 CNN 特征提取器可以成功地对其他类别不可见的数据集执行分类任务。
* **域适应**:这是当训练和测试数据来自不同的分布,而标签和类别数相同时。 不同的域可以考虑使用不同的设备或在不同的设置和环境条件下捕获的图像。 具有 CNN 功能的线性分类器可以在不同域中成功地将具有相同语义信息的图像聚类,而 SURF 功能则可以针对特定领域的特征进行过拟合。
* **精细**-**精细分类**:这是我们要在同一高级类中的子类之间进行分类的时候。 例如,我们可以对鸟类进行分类。 尽管没有对细粒度数据进行训练,但 CNN 功能以及逻辑回归功能的性能优于基线方法。
* **场景识别**:在这里,我们需要对整个图像的场景进行分类。 在对象分类数据库上经过训练的 CNN 特征提取器,顶部带有一个简单的线性分类器,其性能优于应用于识别数据的传统特征提取器的复杂学习算法。
这里提到的某些任务与图像分类没有直接关系,图像分类是 ImageNet 培训的主要目标,因此有人希望 CNN 功能无法推广到看不见的场景。 但是,这些功能与简单的线性分类器相结合,性能优于手工制作的功能。 这意味着 CNN 的学习权重是可重用的。
那么什么时候应该使用迁移学习呢? 当我们有一个任务时,由于问题的性质,可用数据集很小(例如对蚂蚁/蜜蜂进行分类)。 在这种情况下,我们可以在包含相似语义信息的较大数据集上训练我们的模型,然后用较小的数据集仅训练最后一层(线性分类器)。 如果我们只有足够的可用数据,并且有一个更大的相似数据集,则对该相似数据集进行预训练可能会导致模型更健壮。 通常情况下,我们使用随机初始化的权重来训练模型,在这种情况下,将使用在其他数据集上训练过的权重来初始化模型。 这将有助于网络更快地融合并更好地推广。 在这种情况下,仅微调模型顶端的几层是有意义的。
经验法则是,从网络顶部开始,可用数据越多,可以训练的层就越多。 通过预训练(例如在 ImageNet 上)模型初始化模型权重。
# 怎么样? 概述
我们应该如何使用转学? 有两种典型的解决方法。 第一种不太及时的方法是使用所谓的预训练模型,即预先在大型数据集(例如 ImageNet 数据集)上训练过的模型。 这些经过预先训练的模型可以在不同的深度学习框架中轻松获得,并且通常被称为“模型动物园”。 预训练模型的选择在很大程度上取决于当前要解决的任务是什么,以及数据集的大小。 选择模型后,我们可以使用全部或部分模型作为要解决的实际任务的初始化模型。
深度学习的另一种不太常见的方式是自己预先训练模型。 当可用的预训练网络不适合解决特定问题时,通常会发生这种情况,我们必须自己设计网络体系结构。 显然,这需要更多的时间和精力来设计模型和准备数据集。 在某些情况下,用于进行网络预训练的数据集甚至可以是合成的,可以从计算机图形引擎(例如 3D Studio Max 或 Unity)或其他卷积神经网络(例如 GAN)生成。 可以对虚拟数据进行预训练的模型在真实数据上进行微调,并且可以与仅对真实数据进行训练的模型一起很好地工作。
例如,如果我们想区分猫和狗,而我们没有足够的数据,则可以从“模型动物园”下载在 ImageNet 上训练的网络,并使用除最后一层以外的所有层的权重。 最后一层必须调整为具有与班级数量相同的大小,在本例中为两个,并且权重需要重新初始化和训练。 这样,通过将这些层的学习率设置为零或非常小的值(请参见下图),我们将冻结那些不需训练的层。 如果有更大的数据集,我们可以训练最后三个完全连接的层。 有时,预训练网络只能用于初始化权重,然后再进行正常训练。
迁移学习之所以有效,是因为在初始层计算出的特征更通用并且看起来很相似。 在顶层提取的特征对于我们要解决的问题变得更加具体。
为了进一步研究如何使用迁移学习,以及对该主题的更深刻理解,让我们看一下代码。
![](img/a6a25c2f-05f4-4396-8db5-9a2fe9c555dd.png)
# 怎么样? 代码示例
在本节中,我们将学习在 TensorFlow 中进行迁移学习所需的实用技能。 更具体地说,我们将学习如何从检查点选择要加载的层,以及如何指示我们的求解器仅优化特定的层而冻结其他层。
# TensorFlow 有用的元素
由于转移学习是关于训练一个网络的权重,而该网络已从另一个训练后的模型中获取了权重,因此我们将需要找到一个。 在我们的示例中,我们将使用预训练卷积自动编码器的编码部分,该部分在[第 6 章](../Text/6.xhtml)中进行了说明。 使用自动编码器的优点是我们不需要标记的数据,也就是说,可以完全不受监督地对其进行训练。
# 没有解码器的自动编码器
包含两个卷积层和一个完全连接层的编码器(不带解码器部分的自动编码器)如下所示。 父自动编码器在 MNIST 数据集上进行了训练。 因此,网络将大小为 28x28x1 的图像作为输入,并在潜在空间将其编码为 10 维矢量,每个类别一维:
```py
# Only half of the autoencoder changed for classification
class CAE_CNN_Encoder(object):
......
def build_graph(self, img_size=28):
self.__x = tf.placeholder(tf.float32, shape=[None, img_size * img_size], name='IMAGE_IN')
self.__x_image = tf.reshape(self.__x, [-1, img_size, img_size, 1])
self.__y_ = tf.placeholder("float", shape=[None, 10], name='Y')
with tf.name_scope('ENCODER'):
##### ENCODER
# CONV1: Input 28x28x1 after CONV 5x5 P:2 S:2 H_out: 1 + (28+4-5)/2 = 14,
# W_out= 1 + (28+4-5)/2 = 14
self.__conv1_act = tf.layers.conv2d(inputs=self.__x_image, strides=(2, 2), name='conv1',
filters=16, kernel_size=[5, 5], padding="same", activation=tf.nn.relu)
# CONV2: Input 14x14x16 after CONV 5x5 P:0 S:2 H_out: 1 + (14+4-5)/2 = 7,
# W_out= 1 + (14+4-5)/2 = 7
self.__conv2_act = tf.layers.conv2d(inputs=self.__conv1_act, strides=(2, 2),
name='conv2', filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.relu)
with tf.name_scope('LATENT'):
# Reshape: Input 7x7x32 after [7x7x32]
self.__enc_out = tf.layers.flatten(self.__conv2_act, name='flatten_conv2')
self.__dense = tf.layers.dense(inputs=self.__enc_out, units=200, activation=tf.nn.relu, name='fc1')
self.__logits = tf.layers.dense(inputs=self.__dense, units=10, name='logits')
def __init__(self, img_size=28):
if CAE_CNN_Encoder.__instance is None:
self.build_graph(img_size)
@property
def output(self):
return self.__logits
@property
def labels(self):
return self.__y_
@property
def input(self):
return self.__x
@property
def image_in(self):
return self.__x_image
```
# 选择图层
一旦定义了模型`model = CAE_CNN_Encoder` `()`,选择将要使用预训练权重初始化的图层就很重要。 请注意,两个网络的结构(要初始化的网络和提供训练后的权重的网络)必须相同。 因此,例如,以下代码片段将选择名称为`convs``fc`的所有层:
```py
from models import CAE_CNN_Encoder
model = CAE_CNN_Encoder()
list_convs = [v for v in tf.global_variables() if "conv" in v.name]
list_fc_linear = [v for v in tf.global_variables() if "fc" in v.name or "output" in v.name]
```
请注意,这些列表是从`tf.global_variables()`填充的; 如果选择打印其内容,则可能会发现它包含所有模型变量,如下所示:
```py
[<tf.Variable 'conv1/kernel:0' shape=(5, 5, 1, 16) dtype=float32_ref>,
<tf.Variable 'conv1/bias:0' shape=(16,) dtype=float32_ref>,
<tf.Variable 'conv2/kernel:0' shape=(5, 5, 16, 32) dtype=float32_ref>,
<tf.Variable 'conv2/bias:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'fc1/kernel:0' shape=(1568, 200) dtype=float32_ref>,
<tf.Variable 'fc1/bias:0' shape=(200,) dtype=float32_ref>,
<tf.Variable 'logits/kernel:0' shape=(200, 10) dtype=float32_ref>,
<tf.Variable 'logits/bias:0' shape=(10,) dtype=float32_ref>]
```
将定义图的图层分为卷积和完全连接两个列表后,您将使用`tf.Train.Saver`加载所需的权重。 首先,我们需要创建一个 saver 对象,将要从检查点加载的变量列表作为输入,如下所示:
```py
# Define the saver object to load only the conv variables
saver_load_autoencoder = tf.train.Saver(var_list=list_convs)
```
除了`saver_load_autoencoder`,我们还需要创建另一个`saver`对象,该对象将允许我们将要训练的网络的所有变量存储到检查点中。
```py
# Define saver object to save all the variables during training
saver = tf.train.Saver()
```
然后,在使用`init = tf.global_variables_initializer()`初始化图形并创建会话之后,我们可以使用`saver_load_autoencoder`从检查点恢复卷积层,如下所示:
```py
# Restore only the weights (From AutoEncoder)
saver_load_autoencoder.restore(sess, "../tmp/cae_cnn/model.ckpt-34")
```
请注意,调用`restore`会覆盖`global_variables_initializer`,所有选定的权重都将替换为检查点的权重。
# 仅训练一些层次
迁移学习的另一个重要部分是冻结不需要训练的图层的权重,同时允许对某些图层(通常是最后一层)进行训练。 在 TensorFlow 中,我们可以仅将要优化的层传递给求解器(在此示例中,仅将 FC 层传递给):
```py
train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss, var_list=list_fc_linear)
```
# 完整资料
在此示例中,我们将从 MNIST 卷积自动编码器示例中加载权重。 我们将仅恢复编码器部分的权重,冻结 CONV 层,并训练 FC 层以执行数字分类:
```py
import tensorflow as tf
import numpy as np
import os
from models import CAE_CNN_Encoder
SAVE_FOLDER='/tmp/cae_cnn_transfer'
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
model = CAE_CNN_Encoder(latent_size = 20)
model_in = model.input
model_out = model.output
labels_in = model.labels
# Get all convs weights
list_convs = [v for v in tf.global_variables() if "conv" in v.name]
# Get fc1 and logits
list_fc_layers = [v for v in tf.global_variables() if "fc" in v.name or "logits" in v.name]
# Define the saver object to load only the conv variables
saver_load_autoencoder = tf.train.Saver(var_list=list_convs)
# Define saver object to save all the variables during training
saver = tf.train.Saver()
# Define loss for classification
with tf.name_scope("LOSS"):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=model_out, labels=labels_in))
correct_prediction = tf.equal(tf.argmax(model_out,1), tf.argmax(labels_in,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# Solver configuration
with tf.name_scope("Solver"):
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss, var_list=list_fc_layers)
# Initialize variables
init = tf.global_variables_initializer()
# Avoid allocating the whole memory
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.200)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
sess.run(init)
# Restore only the CONV weights (From AutoEncoder)
saver_load_autoencoder.restore(sess, "/tmp/cae_cnn/model.ckpt-34")
# Add some tensors to observe on tensorboad
tf.summary.image("input_image", model.image_in, 4)
tf.summary.scalar("loss", loss)
merged_summary = tf.summary.merge_all()
writer = tf.summary.FileWriter(SAVE_FOLDER)
writer.add_graph(sess.graph)
#####Train######
num_epoch = 200
batch_size = 10
for epoch in range(num_epoch):
for i in range(int(mnist.train.num_examples / batch_size)):
# Get batch of 50 images
batch = mnist.train.next_batch(batch_size)
# Dump summary
if i % 5000 == 0:
# Other summaries
s = sess.run(merged_summary, feed_dict={model_in:batch[0], labels_in:batch[1]})
writer.add_summary(s,i)
# Train actually here (Also get loss value)
_, val_loss, t_acc = sess.run((train_step, loss, accuracy), feed_dict={model_in:batch[0], labels_in:batch[1]})
print('Epoch: %d/%d loss:%d' % (epoch, num_epoch, val_loss))
print('Save model:', epoch)
saver.save(sess, os.path.join(SAVE_FOLDER, "model.ckpt"), epoch)
```
# 摘要
在本章中,我们学习了如何,何时以及为什么使用迁移学习。 这被认为是一个非常强大的工具,因为它使我们能够使用从其他领域学到的功能来以较少的数据很好地概括。 我们看了一些示例,现在应该清楚如何在自己的任务中实施转移学习。
在下一章中,我们将看到如何组织我们的数据以及如何扩展 CNN 架构,以构建准确而实用的机器学习系统。
\ No newline at end of file
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册