resnet50代码_机器学习-深度残差网络(ResNet)论文学习(附代码实现)

本文结合50层深度残差网络的实现学习何博士的大作-Deep Residual Learning for Image Recognition。理论上,深层网络结构包含了浅层网络结构所有可能的解空间,但是实际网络训练中,随着网络深度的增加,网络的准确度出现饱和,甚至下降的现象,这个现象可以在下图直观看出来:56层的网络比20层网络效果还要差。但是这种退化并不是因为过拟合导致的,因为56层的神经网络的训练误差同样高。

6a17869577b7e445156813b758ccda5b.png

这就是神经网络的退化现象。何博士提出的残差学习的方法解决了解决了神经网络的退化问题,在深度学习领域取得了巨大的成功。

1.Residual Networks

各个深度的神经网络的结构如下:

51dbdb17805aa72325b22f8feb0bd48b.png

50层网络的结构实际上是把34层网络的2个3x3的卷积层替换成3个卷积层:1x1、3x3、1x1,可以看到50层的网络相对于34层的网络,效果上有不小的提升。

19ce5a27765298c276f258aa9d358d32.png

代码实现

ResNet 50代码实现的网络结构与上图50层的网络架构完全一致。对于深度较深的神经网络,BN必不可少,关于BN的介绍和实现可以参考以前的文章。

class ResNet50(object):

def __init__(self, inputs, num_classes=1000, is_training=True,

scope="resnet50"):

self.inputs =inputs

self.is_training = is_training

self.num_classes = num_classes

with tf.variable_scope(scope):

# construct the model

net = conv2d(inputs, 64, 7, 2, scope="conv1") # -> [batch, 112, 112, 64]

net = tf.nn.relu(batch_norm(net, is_training=self.is_training, scope="bn1"))

net = max_pool(net, 3, 2, scope="maxpool1") # -> [batch, 56, 56, 64]

net = self._block(net, 256, 3, init_stride=1, is_training=self.is_training,

scope="block2") # -> [batch, 56, 56, 256]

net = self._block(net, 512, 4, is_training=self.is_training, scope="block3")

# -> [batch, 28, 28, 512]

net = self._block(net, 1024, 6, is_training=self.is_training, scope="block4")

# -> [batch, 14, 14, 1024]

net = self._block(net, 2048, 3, is_training=self.is_training, scope="block5")

# -> [batch, 7, 7, 2048]

net = avg_pool(net, 7, scope="avgpool5") # -> [batch, 1, 1, 2048]

net = tf.squeeze(net, [1, 2], name="SpatialSqueeze") # -> [batch, 2048]

self.logits = fc(net, self.num_classes, "fc6") # -> [batch, num_classes]

self.predictions = tf.nn.softmax(self.logits)

2.Building Block

每个Block中往往包含多个子Block,每个子Block又有多个卷积层组成。每个Block的第一个子Block的第一个卷积层的stride=2,完成Feature Map的下采样的工作。

ae11aed9c96038b28aab06dc4ad7790e.png

代码实现

def _block(self, x, n_out, n, init_stride=2, is_training=True, scope="block"):

with tf.variable_scope(scope):

h_out = n_out // 4

out = self._bottleneck(x, h_out, n_out, stride=init_stride,

is_training=is_training, scope="bottlencek1")

for i in range(1, n):

out = self._bottleneck(out, h_out, n_out, is_training=is_training,

scope=("bottlencek%s" % (i + 1)))

return out

3. Bottleneck Architectures

在更深层(esNet-50/101/152)的神经网络中为了节省计算耗时, 作者对神经网络的架构进行了改造,将原有的两层3x3卷积层改造为三层卷积层:1x1,3x3,1x1。

The three layers are 1×1, 3×3, and 1×1 convolutions, where the 1×1 layers are responsible for reducing and then increasing (restoring)dimensions, leaving the 3×3 layer a bottleneck with smaller input/output dimensions。

8564aaf1769985c1b2f89f1892693ae7.png

代码实现:

x: 是输入数据,格式为[BatchSize, ImageHeight,ImageWidth, ChannelNum];

h_out: 卷积核个数;

n_out: Block的输出的卷积核个数;

stride: 卷积步长;

is_training: 用于Batch Normalization;

def _bottleneck(self, x, h_out, n_out, stride=None, is_training=True, scope="bottleneck"):

""" A residual bottleneck unit"""

n_in = x.get_shape()[-1]

if stride is None:

stride = 1 if n_in == n_out else 2

with tf.variable_scope(scope):

h = conv2d(x, h_out, 1, stride=stride, scope="conv_1")

h = batch_norm(h, is_training=is_training, scope="bn_1")

h = tf.nn.relu(h)

h = conv2d(h, h_out, 3, stride=1, scope="conv_2")

h = batch_norm(h, is_training=is_training, scope="bn_2")

h = tf.nn.relu(h)

h = conv2d(h, n_out, 1, stride=1, scope="conv_3")

h = batch_norm(h, is_training=is_training, scope="bn_3")

if n_in != n_out:

shortcut = conv2d(x, n_out, 1, stride=stride, scope="conv_4")

shortcut = batch_norm(shortcut, is_training=is_training, scope="bn_4")

else:

shortcut = x

return tf.nn.relu(shortcut + h)

4. Shortcuts

Identity Mapping是深度残差网络的一个核心思想,深度残差网络中Building Block表达公式如下:

fd37c5b9047f643ac4980f8845e5323e.png

x是Layer Input, y是未经过Relu激活函数的Layer Output, 是待学习的残差映射。

上式仅仅能处理F(x, wi)和x维度相同的情况,当二者维度不同的情况下应该怎么处理呢?

作者提出了两种处理方式: zero padding shortcut和 projection shortcut。并在实验中构造三种shortcut的方式:

A) 当数据维度增加时,采用zero padding进行数据填充;

B) 当数据维度增加时,采用projection的方式;数据维度不变化时,直接使用恒等映射;

C) 数据维度增加与否都采用projection的方式;

三种方式的对比效果如下:

49f75b5927e96319595a13339c8c8cda.png

可以看到效果排序如下: A < B < C,但是三者的差别并不是很大,所以作者得出一个结论: projection shortcut并不是解决退化问题的关键,所以为了避免projection带来的计算负担,论文中很少采用C作为shortcut的方案。上述代码中使用projection shortcut结合Identity shortcut的映射方案。

5.其它辅助函数的实现

5.1 变量初始化

fc_initializer = tf.contrib.layers.xavier_initializer

conv2d_initializer = tf.contrib.layers.xavier_initializer_conv2d

5.2 创建变量的辅助函数

# create weight variable

def create_var(name, shape, initializer, trainable=True):

return tf.get_variable(name, shape=shape, dtype=tf.float32,

initializer=initializer, trainable=trainable)

5.3 卷积辅助函数

# conv2d layer

def conv2d(x, num_outputs, kernel_size, stride=1, scope="conv2d"):

num_inputs = x.get_shape()[-1]

with tf.variable_scope(scope):

kernel = create_var("kernel

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值