摘要
微軟的深度殘差網(wǎng)絡(luò)ResNet源于2016年CVPR最佳論文---圖像識(shí)別中的深度殘差學(xué)習(xí)(Deep Residual Learning for Image Recognition), 論文來源,翻譯地址
這個(gè)152層ResNet架構(gòu)深,除了在層數(shù)上面創(chuàng)紀(jì)錄,ResNet 的錯(cuò)誤率也低得驚人贷腕,達(dá)到了3.6%,人類都大約在5%~10%的水平殿较。這是目前為止最好的深度學(xué)習(xí)框架∽兀可以看作人工神經(jīng)網(wǎng)絡(luò)領(lǐng)域的又一里程碑淋纲。
2016年8月31日,Google團(tuán)隊(duì)宣布針對TensorFlow開源了最新發(fā)布的TF-slim資料庫院究,它是一個(gè)可以定義洽瞬、訓(xùn)練和評(píng)估模型的輕量級(jí)的軟件包本涕,也能對圖像分類領(lǐng)域中幾個(gè)主要有競爭力的網(wǎng)絡(luò)進(jìn)行檢驗(yàn)和定義模型。這其中伙窃,就包括了ResNet網(wǎng)絡(luò)結(jié)構(gòu)菩颖。本文將結(jié)合TF-slim庫中的ResNet模型的代碼,介紹一下ResNet網(wǎng)絡(luò)的結(jié)構(gòu)和原理为障。
ResNet的原理
論文中提到晦闰,近幾年的研究發(fā)現(xiàn)網(wǎng)絡(luò)的深度是使網(wǎng)絡(luò)性能更優(yōu)化的一個(gè)關(guān)鍵因素,但是隨著網(wǎng)絡(luò)深度的加深鳍怨,梯度消失&爆炸問題十分明顯呻右,網(wǎng)絡(luò)甚至出現(xiàn)了退化。在論文中通過一個(gè)20層和一個(gè)56層的普通網(wǎng)絡(luò)進(jìn)行了對比鞋喇,發(fā)現(xiàn)56層網(wǎng)絡(luò)的性能遠(yuǎn)低于20層網(wǎng)絡(luò)声滥,如圖1所示。
而在ResNet的這篇論文中确徙,通過引入一個(gè)深度殘差學(xué)習(xí)框架醒串,解決了這個(gè)退化問題。它不期望每一層能直接吻合一個(gè)映射鄙皇,而是明確的讓這些層去吻合殘差映射。形式上看仰挣,就是用H(X)來表示最優(yōu)解映射伴逸,但我們讓堆疊的非線性層去擬合另一個(gè)映射F(X):=H(X) - X, 此時(shí)原最優(yōu)解映射H(X)就可以改寫成F(X)+X,我們假設(shè)殘差映射跟原映射相比更容易被優(yōu)化膘壶。極端情況下错蝴,如果一個(gè)映射是可優(yōu)化的,那也會(huì)很容易將殘差推至0颓芭,把殘差推至0和把此映射逼近另一個(gè)非線性層相比要容易的多顷锰。F(X)+X的公式可以通過在前饋網(wǎng)絡(luò)中做一個(gè)“快捷連接”來實(shí)現(xiàn)(如圖2) ,快捷連接跳過一個(gè)或多個(gè)層亡问。在我們的用例中官紫,快捷連接簡單的執(zhí)行自身映射,它們的輸出被添加到疊加層的輸出中州藕。自身快捷連接既不會(huì)添加額外的參數(shù)也不會(huì)增加計(jì)算復(fù)雜度束世。整個(gè)網(wǎng)絡(luò)依然可以用SGD+反向傳播來做端到端的訓(xùn)練。
它有二層床玻,如下表達(dá)式毁涉,其中σ代表非線性函數(shù)ReLU
然后通過一個(gè)shortcut,和第2個(gè)ReLU锈死,獲得輸出y
而在論文的后續(xù)贫堰,又提出來深度瓶頸結(jié)構(gòu)穆壕,如圖3右側(cè).在文中是這樣描述這個(gè)結(jié)構(gòu)的:接下來我們描述我們?yōu)镮mageNet準(zhǔn)備的更深的網(wǎng)絡(luò)。因?yàn)樘^漫長的訓(xùn)練時(shí)間我們負(fù)擔(dān)不起其屏,所以修改了單元塊粱檀,改為一種瓶頸設(shè)計(jì)。對于每個(gè)殘差函數(shù)F漫玄,我們使用3層來描述茄蚯,而不是2層。這三層分別是1×1睦优、3×3渗常,和1×1的卷積層,其中1×1層負(fù)責(zé)先減少后增加(恢復(fù))尺寸的汗盘,使3×3層具有較小的輸入/輸出尺寸瓶頸皱碘。
這個(gè)深度瓶頸結(jié)構(gòu)在TF-Slim庫中的代碼實(shí)現(xiàn)如下所示:
def bottleneck(inputs, depth, depth_bottleneck, stride, rate=1,
outputs_collections=None, scope=None):
with tf.variable_scope(scope, 'bottleneck_v1', [inputs]) as sc:
depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = resnet_utils.subsample(inputs, stride, 'shortcut')
else:
shortcut = slim.conv2d(inputs, depth, [1, 1], stride=stride,
activation_fn=None, scope='shortcut')
residual = slim.conv2d(inputs, depth_bottleneck, [1, 1], stride=1,
scope='conv1')
residual = resnet_utils.conv2d_same(residual, depth_bottleneck, 3, stride,
rate=rate, scope='conv2')
residual = slim.conv2d(residual, depth, [1, 1], stride=1,
activation_fn=None, scope='conv3')
output = tf.nn.relu(shortcut + residual)
return slim.utils.collect_named_outputs(outputs_collections,
sc.original_name_scope,
output)
需要注意的是,在論文中提到的當(dāng)輸入輸出尺寸發(fā)生增加時(shí)(圖4中的虛線的快捷連接)隐孽,會(huì)考慮兩個(gè)策略:(a)快捷連接仍然使用自身映射癌椿,對于維度的增加用零來填補(bǔ)空缺。此策略不會(huì)引入額外的參數(shù)菱阵;(b)投影捷徑(公式2)被用來匹配尺寸(靠1×1的卷積完成)踢俄。對于這兩種選項(xiàng),當(dāng)快捷連接在兩個(gè)不同大小的特征圖譜上出現(xiàn)時(shí)晴及,用stride=2來處理都办。而在TF-Slim的代碼實(shí)現(xiàn)中我們可以看到采用了第二種解決方式,即通過通過1X1的卷積核卷積來達(dá)成尺寸匹配虑稼。(雖然論文中說這樣提高不多但需要更多參數(shù)所以最后沒有使用琳钉。)
同時(shí),在代碼中對于下采樣操作(subsample)是通過1x1的池化來完成的蛛倦。
ResNet的結(jié)構(gòu)
所以我們可以根據(jù)一個(gè)普通的神經(jīng)網(wǎng)絡(luò)來構(gòu)造一個(gè)ResNet歌懒,如圖4所示,論文中選擇的基礎(chǔ)網(wǎng)絡(luò)是VGG-Net溯壶。
而它的具體網(wǎng)絡(luò)結(jié)構(gòu)如圖5的表中所示及皂。
在TF-Slim中的代碼實(shí)現(xiàn)如下(以ResNet-50為例):
def resnet_v1_50(inputs,
num_classes=None,
is_training=True,
global_pool=True,
output_stride=None,
reuse=None,
scope='resnet_v1_50'):
"""ResNet-50 model of [1]. See resnet_v1() for arg and return description."""
blocks = [
resnet_utils.Block(
'block1', bottleneck, [(256, 64, 1)] * 2 + [(256, 64, 2)]),
resnet_utils.Block(
'block2', bottleneck, [(512, 128, 1)] * 3 + [(512, 128, 2)]),
resnet_utils.Block(
'block3', bottleneck, [(1024, 256, 1)] * 5 + [(1024, 256, 2)]),
resnet_utils.Block(
'block4', bottleneck, [(2048, 512, 1)] * 3)
]
return resnet_v1(inputs, blocks, num_classes, is_training,
global_pool=global_pool, output_stride=output_stride,
include_root_block=True, reuse=reuse, scope=scope)
在這段代碼中,其實(shí)只是聲明了一個(gè)通過Block組合成的List茸塞,Block的聲明如下躲庄,其中的關(guān)鍵是collections.namedtuple這個(gè)函數(shù),它把前面元組的值和后面的命名對應(yīng)了起來钾虐。
class Block(collections.namedtuple('Block', ['scope', 'unit_fn', 'args'])):
"""A named tuple describing a ResNet block.
Its parts are:
scope: The scope of the `Block`.
unit_fn: The ResNet unit function which takes as input a `Tensor` and
returns another `Tensor` with the output of the ResNet unit.
args: A list of length equal to the number of units in the `Block`. The list
contains one (depth, depth_bottleneck, stride) tuple for each unit in the
block to serve as argument to unit_fn.
"""
而將個(gè)元素為block的 LIst轉(zhuǎn)換為一個(gè)網(wǎng)絡(luò)的函數(shù)噪窘,則是resnet_v1,這個(gè)函數(shù)是ResNet的核心,而不同層數(shù)的ResNet只需要改變上述函數(shù)blocks中block的個(gè)數(shù)就可以了倔监。
def resnet_v1(inputs,
blocks,
num_classes=None,
is_training=True,
global_pool=True,
output_stride=None,
include_root_block=True,
reuse=None,
scope=None):
with tf.variable_scope(scope, 'resnet_v1', [inputs], reuse=reuse) as sc:
end_points_collection = sc.name + '_end_points'
with slim.arg_scope([slim.conv2d, bottleneck,
resnet_utils.stack_blocks_dense],
outputs_collections=end_points_collection):
with slim.arg_scope([slim.batch_norm], is_training=is_training):
net = inputs
if include_root_block:
if output_stride is not None:
if output_stride % 4 != 0:
raise ValueError('The output_stride needs to be a multiple of 4.')
output_stride /= 4
net = resnet_utils.conv2d_same(net, 64, 7, stride=2, scope='conv1')
net = slim.max_pool2d(net, [3, 3], stride=2, scope='pool1')
net = resnet_utils.stack_blocks_dense(net, blocks, output_stride)
if global_pool:
# Global average pooling.
net = tf.reduce_mean(net, [1, 2], name='pool5', keep_dims=True)
if num_classes is not None:
net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None,
normalizer_fn=None, scope='logits')
# Convert end_points_collection into a dictionary of end_points.
end_points = slim.utils.convert_collection_to_dict(end_points_collection)
if num_classes is not None:
end_points['predictions'] = slim.softmax(net, scope='predictions')
return net, end_points
在這個(gè)函數(shù)中直砂,將blocks轉(zhuǎn)換為net的語句是
net = resnet_utils.stack_blocks_dense(net, blocks, output_stride)
這個(gè)函數(shù)的具體實(shí)現(xiàn)如下,它通過一個(gè)循環(huán)將list中的每個(gè)block讀取出來浩习,然后將block中相應(yīng)的參數(shù)代入到前文提到的bottleneck這個(gè)函數(shù)中静暂,這樣就生成了相應(yīng)的ResNet網(wǎng)絡(luò)結(jié)構(gòu)。
def stack_blocks_dense(net,
blocks,
output_stride=None,
outputs_collections=None):
# The current_stride variable keeps track of the effective stride of the
# activations. This allows us to invoke atrous convolution whenever applying
# the next residual unit would result in the activations having stride larger
# than the target output_stride.
current_stride = 1
# The atrous convolution rate parameter.
rate = 1
for block in blocks:
with variable_scope.variable_scope(block.scope, 'block', [net]) as sc:
for i, unit in enumerate(block.args):
if output_stride is not None and current_stride > output_stride:
raise ValueError('The target output_stride cannot be reached.')
with variable_scope.variable_scope('unit_%d' % (i + 1), values=[net]):
unit_depth, unit_depth_bottleneck, unit_stride = unit
# If we have reached the target output_stride, then we need to employ
# atrous convolution with stride=1 and multiply the atrous rate by the
# current unit's stride for use in subsequent layers.
if output_stride is not None and current_stride == output_stride:
net = block.unit_fn(
net,
depth=unit_depth,
depth_bottleneck=unit_depth_bottleneck,
stride=1,
rate=rate)
rate *= unit_stride
else:
net = block.unit_fn(
net,
depth=unit_depth,
depth_bottleneck=unit_depth_bottleneck,
stride=unit_stride,
rate=1)
current_stride *= unit_stride
net = utils.collect_named_outputs(outputs_collections, sc.name, net)
if output_stride is not None and current_stride != output_stride:
raise ValueError('The target output_stride cannot be reached.')
return net
在這里谱秽,代碼中提到了 atrous convolution這個(gè)結(jié)構(gòu),簡單來說洽蛀,它是如圖6(b)所示的一個(gè)結(jié)構(gòu),可以起到在使用了步長為1的池化層后扔使得原結(jié)構(gòu)保持相同的感受野疟赊。
參考文獻(xiàn)
[1]Deep Residual Learning for Image Recognition
[2]http://blog.csdn.net/tiandijun/article/details/52526317
[3]http://blog.csdn.net/mao_feng/article/details/52734438
[4]http://blog.csdn.net/helei001/article/details/52692128
[5]http://blog.csdn.net/u012759136/article/details/52434826#t9