课程四(Convolutional Neural Networks),第二 周(Deep convolutional models: case studies) ——3.Programming assignments : Residual Networks

Residual Networks

Welcome to the second assignment of this week! You will learn how to build very deep convolutional networks, using Residual Networks (ResNets). In theory, very deep networks can represent very complex functions; but in practice, they are hard to train. Residual Networks, introduced by He et al., allow you to train much deeper networks than were previously practically feasible.

In this assignment, you will:

  • Implement the basic building blocks of ResNets.
  • Put together these building blocks to implement and train a state-of-the-art neural network for image classification.

This assignment will be done in Keras.

【中文翻译】

欢迎来到这周的第二次任务!您将学习如何使用Residual 网络 (ResNets) 构建非常深的卷积网络。理论上, 非常深的网络可以代表非常复杂的函数;但在实践中, 它们很难训练。 由He 等提出的Residual网络, 允许你训练更深的网络。

在此任务中, 您将:

  • 实现 ResNets 的基本构件。
  • 把这些构件放在一起, 实现并训练一种state-of-the-art 神经网络进行图像分类。

这项任务将在 Keras 中完成。

Before jumping into the problem, let‘s run the cell below to load the required packages.

【code】

import numpy as np
from keras import layers
from keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
from keras.models import Model, load_model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
import pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
from resnets_utils import *
from keras.initializers import glorot_uniform
import scipy.misc
from matplotlib.pyplot import imshow
%matplotlib inline

import keras.backend as K
K.set_image_data_format(‘channels_last‘)
K.set_learning_phase(1)

1 - The problem of very deep neural networks

Last week, you built your first convolutional neural network. In recent years, neural networks have become deeper, with state-of-the-art networks going from just a few layers (e.g., AlexNet) to over a hundred layers.

The main benefit of a very deep network is that it can represent very complex functions. It can also learn features at many different levels of abstraction, from edges (at the lower layers) to very complex features (at the deeper layers). However, using a deeper network doesn‘t always help. A huge barrier to training them is vanishing gradients: very deep networks often have a gradient signal that goes to zero quickly, thus making gradient descent unbearably slow. More specifically, during gradient descent, as you backprop from the final layer back to the first layer, you are multiplying by the weight matrix on each step, and thus the gradient can decrease exponentially quickly to zero (or, in rare cases, grow exponentially quickly and "explode" to take very large values).

During training, you might therefore see the magnitude (or norm) of the gradient for the earlier layers descrease to zero very rapidly as training proceeds:

You are now going to solve this problem by building a Residual Network!

【中文翻译】

上周, 你建立了你的第一个卷积神经网络。近年来, 神经网络已经变得更深了, 如state-of-the-art 网络, 从短短的几层到(如, AlexNet) 超过100层。

一个非常深的网络的主要好处是它可以代表非常复杂的函数。它还可以在许多不同的抽象层次上学习特征, 从边缘 (在底层) 到非常复杂的特征 (在更深的层)。但是, 使用更深的网络并不总是有帮助。训练它们的一个巨大障碍是消失的梯度( vanishing gradients): 非常深的网络通常有一个梯度信号, 它很快地变成0, 从而使梯度下降变得很缓慢。更具体地说, 在梯度下降期间, 当您从最后一层返回到第一层时, 您将在每个步骤乘以权重矩阵, 因此梯度可以以指数速度快速地减少到零 (或者, 在极少数情况下, 增长迅速,增长到很大的值)。

在训练期间, 您可能因此看见,在前面的层中,随着训练的继续,梯度的大小 (或范数) 非常快速地减少到零:

图片见英文部分

你现在要通过建立一个 Residual网络来解决这个问题!

2 - Building a Residual Network

In ResNets, a "shortcut" or a "skip connection" allows the gradient to be directly backpropagated to earlier layers:

The image on the left shows the "main path" through the network. The image on the right adds a shortcut to the main path. By stacking these ResNet blocks on top of each other, you can form a very deep network.

We also saw in lecture that having ResNet blocks with the shortcut also makes it very easy for one of the blocks to learn an identity function. This means that you can stack on additional ResNet blocks with little risk of harming training set performance. (There is also some evidence that the ease of learning an identity function--even more than skip connections helping with vanishing gradients--accounts for ResNets‘ remarkable performance.)

Two main types of blocks are used in a ResNet, depending mainly on whether the input/output dimensions are same or different. You are going to implement both of them.

【中文翻译】

在 ResNets 中, "捷径" 或 "跳跃连接" 允许将梯度直接 反向传播到更早的层:

左侧的图像通过网络显示 "主路径"。右侧的图像为主路径添加了一个捷径。通过堆叠这些 ResNet 块在彼此之上, 您可以形成一个非常深的网络。

图片见英文部分

我们还在讲座中看到, 使用捷径的ResNet 块也使其中一个块学习恒等函数变得非常容易。这意味着您可以在额外的 ResNet 块上叠加, 而不会危害训练集的性能。(还有一些证据表明, 学习一个恒等函数的简单性,甚至比跳跃连接对梯度消失的缓解更有帮助,这些证明了ResNets 的卓越性能。

ResNet 中使用了两种主要类型的块, 主要取决于输入/输出维度是否相同或不同。你要实现这两个。

2.1 - The identity block

The identity block is the standard block used in ResNets, and corresponds to the case where the input activation (say a[l]) has the same dimension as the output activation (say a[l+2]). To flesh out the different steps of what happens in a ResNet‘s identity block, here is an alternative diagram showing the individual steps:

The upper path is the "shortcut path." The lower path is the "main path." In this diagram, we have also made explicit the CONV2D and ReLU steps in each layer. To speed up training we have also added a BatchNorm step. Don‘t worry about this being complicated to implement--you‘ll see that BatchNorm is just one line of code in Keras!

In this exercise, you‘ll actually implement a slightly more powerful version of this identity block, in which the skip connection "skips over" 3 hidden layers rather than 2 layers. It looks like this:

Here‘re the individual steps.

First component of main path:

  • The first CONV2D has F1 filters of shape (1,1) and a stride of (1,1). Its padding is "valid" and its name should be conv_name_base + ‘2a‘. Use 0 as the seed for the random initialization.
  • The first BatchNorm is normalizing the channels axis. Its name should be bn_name_base + ‘2a‘.
  • Then apply the ReLU activation function. This has no name and no hyperparameters.

Second component of main path:

  • The second CONV2D has F2F2 filters of shape (f,f)(f,f) and a stride of (1,1). Its padding is "same" and its name should be conv_name_base + ‘2b‘. Use 0 as the seed for the random initialization.
  • The second BatchNorm is normalizing the channels axis. Its name should be bn_name_base + ‘2b‘.
  • Then apply the ReLU activation function. This has no name and no hyperparameters.

Third component of main path:

  • The third CONV2D has F3F3 filters of shape (1,1) and a stride of (1,1). Its padding is "valid" and its name should be conv_name_base + ‘2c‘. Use 0 as the seed for the random initialization.
  • The third BatchNorm is normalizing the channels axis. Its name should be bn_name_base + ‘2c‘. Note that there is no ReLU activation function in this component.

Final step:

  • The shortcut and the input are added together.
  • Then apply the ReLU activation function. This has no name and no hyperparameters.

【中文翻译】

2.1 - 恒等模块

恒等模块是 ResNets 中使用的标准块, 对应于输入激活 (例如, a [l]) 与输出激活具有相同维度的情况 (例如, a [l + 2])。为了使 ResNet 的恒等模块中的不同步骤更加明显, 这里是一个可选的图表, 显示各个步骤:

上面的路径是 "捷径"。下面的路径是 "主路径"。在这个图中, 我们还明确了每个层中的 CONV2D 和 ReLU 步骤。为了加快训练, 我们也增加了一个 BatchNorm 的步骤。不要担心这是复杂的实现-你会看到, 在 Keras中,BatchNorm 只是一行代码!

在本练习中, 您将实际实现这个恒等模块的一个稍微更强大的版本, 其中跳跃连接 "跳过" 3 隐藏层, 而不是2层。它看起来像这样:

下面是各个步骤。

主路径的第一个组件:

  • 第一 CONV2D 有 F1 个滤波器,形状为 (1,1) 和步幅为 (1,1)。其填充为 "valid", 其名称应为 conv_name_base + "2a"。使用0作为随机初始化的种子。
  • 第一个 BatchNorm 是对通道轴进行规范化。它的名字应该是 bn_name_base + "2a"。
  • 然后应用 ReLU 激活函数。没有名字也没有参数

主路径的第二个组成部分:

  • 第二 CONV2D 有 F2个滤波器, 形状为(f,f) 和步幅 (1,1)。它的填充方式是 "same", 其名称应该是 conv_name_base + "2b"。使用0作为随机初始化的种子。
  • 第二个 BatchNorm 是对通道轴进行规范化。它的名字应该是 bn_name_base + "2b"。
  • 然后应用 ReLU 激活函数。没有名字也没有参数

主路径的第三个组成部分:

  • 第三 CONV2D 有 F3个滤波器 ,形状为(1,1) 和步幅 (1,1)。其填充为 "same", 其名称应为 conv_name_base + "2c"。使用0作为随机初始化的种子。
  • 第三个 BatchNorm 对通道轴进行规范化。它的名字应该是 bn_name_base + "2c"。请注意, 此组件中没有 ReLU 激活函数。

最后一步:

  • 捷径和输入一起添加。
  • 然后应用 ReLU 激活函数。没有名字也没有参数

Exercise: Implement the ResNet identity block. We have implemented the first component of the main path. Please read over this carefully to make sure you understand what it is doing. You should implement the rest.

  • To implement the Conv2D step: See reference
  • To implement BatchNorm: See reference (axis: Integer, the axis that should be normalized (typically the channels axis))
  • For the activation, use: Activation(‘relu‘)(X)
  • To add the value passed forward by the shortcut: See reference

【中文翻译】

练习: 实现 ResNet 恒等块。我们已经实现了主路径的第一个组成部分。请仔细阅读这一点, 以确保您了解它在做什么。你应该实现剩下的。

  • 实现 Conv2D 步骤: 请参阅参考
  • 要实现 BatchNorm: 请参见参考 (轴: 整数, 应规范化的轴 (通常为通道轴))
  • 对于激活, 使用:  Activation(‘relu‘)(X)
  • 要添加由捷径向前传递的值: 请参阅参考

【code】

-------------------------------------------------------

  

原文地址:https://www.cnblogs.com/hezhiyao/p/8414540.html

时间: 2024-08-01 03:19:01

课程四(Convolutional Neural Networks),第二 周(Deep convolutional models: case studies) ——3.Programming assignments : Residual Networks的相关文章

课程四(Convolutional Neural Networks),第二 周(Deep convolutional models: case studies) —— 0.Learning Goals

Learning Goals Understand multiple foundational papers of convolutional neural networks Analyze the dimensionality reduction of a volume in a very deep network Understand and Implement a Residual network Build a deep neural network using Keras Implem

[C6] Andrew Ng - Convolutional Neural Networks

About this Course This course will teach you how to build convolutional neural networks and apply it to image data. Thanks to deep learning, computer vision is working far better than just two years ago, and this is enabling numerous exciting applica

课程四(Convolutional Neural Networks),第一周(Foundations of Convolutional Neural Networks) —— 0.Learning Goals

Learning Goals Understand the convolution operation Understand the pooling operation Remember the vocabulary used in convolutional neural network (padding, stride, filter, ...) Build a convolutional neural network for image multi-class classification

2016.4.5 ImageNet Classification with Deep Convolutional Neural Networks

ImageNet Classification with Deep Convolutional Neural Networks http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf 这个网络也叫做alexnet,因为第一作者的名字是alex,这是个经典的网络,因为这个网络在12年的时候在imagenet上面提升了十个点的准确率.第三作者是hinton

ImageNet Classification with Deep Convolutional Neural Networks

ImageNet Classification with Deep Convolutional Neural Networks Alex Krizhevsky, Ilya Sutskever, Geoffrey E. Hinton 摘要 我们训练了一个大型的深度卷积神经网络,来将在ImageNet LSVRC-2010大赛中的120万张高清图像分为1000个不同的类别.对测试数据,我们得到了top-1误差率37.5%,以及top-5误差率17.0%,这个效果比之前最顶尖的都要好得多.该神经网络有

中文版 ImageNet Classification with Deep Convolutional Neural Networks

ImageNet Classification with Deep Convolutional Neural Networks 摘要 我们训练了一个大型深度卷积神经网络来将ImageNet LSVRC-2010竞赛的120万高分辨率的图像分到1000不同的类别中.在测试数据上,我们得到了top-1 37.5%, top-5 17.0%的错误率,这个结果比目前的最好结果好很多.这个神经网络有6000万参数和650000个神经元,包含5个卷积层(某些卷积层后面带有池化层)和3个全连接层,最后是一个1

ImageNet?Classification?with?Deep?Convolutional?Neural?Networks?阅读笔记 转载

ImageNet Classification with Deep Convolutional Neural Networks 阅读笔记 (2013-07-06 22:16:36) 转载▼ 标签: deep_learning imagenet hinton 分类: 机器学习 (决定以后每读一篇论文,都将笔记记录于博客上.) 这篇发表于NIPS2012的文章,是Hinton与其学生为了回应别人对于deep learning的质疑而将deep learning用于ImageNet(图像识别目前最大的

【Papers】《ImageNet Classification with Deep Convolutional Neural Networks》阅读笔记

参考资料: ImageNet Classification with Deep Convolutional Neural Networks,Alex Krizhevsky,Ilya Sutskever,Geoffrey E. Hinton http://www.cnblogs.com/tornadomeet/p/3258122.html http://blog.sina.com.cn/s/blog_890c6aa30100z7su.html

ImageNet Classification with Deep Convolutional Neural Networks(转载)

ImageNet Classification with Deep Convolutional Neural Networks 阅读笔记 (决定以后每读一篇论文,都将笔记记录于博客上.) 这篇发表于NIPS2012的文章,是Hinton与其学生为了回应别人对于deep learning的质疑而将deep learning用于ImageNet(图像识别目前最大的数据库)上,最终取得了非常惊人的结果,其结果相对原来的state of the art好了非常多(前5选错误率由25%降低为17%). I