码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-02-05 00:28:21      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:lex   accounts   func   lock   alt   ssis   ide   version   mis   

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】

 

 

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

  

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

标签:lex   accounts   func   lock   alt   ssis   ide   version   mis   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!