2015年,微軟亞洲研究院的何凱明團(tuán)隊(duì)發(fā)布了一種特殊的卷積神經(jīng)網(wǎng)絡(luò)——?dú)埐钌窠?jīng)網(wǎng)絡(luò)(ResNet)桦他。在殘差神經(jīng)網(wǎng)絡(luò)出現(xiàn)之前凰荚,最深的深度神經(jīng)網(wǎng)絡(luò)只有二三十層左右,這該神經(jīng)網(wǎng)絡(luò)卻可以在實(shí)驗(yàn)中輕松達(dá)到上百層甚至上千層织阅,另外不會(huì)占用過多訓(xùn)練時(shí)間,也正因如此震捣,圖像識(shí)別準(zhǔn)確率有了顯著增強(qiáng)荔棉。此模型更是在同年的ImageNet大賽中,獲得圖像分類蒿赢、定位润樱、檢測(cè)三個(gè)項(xiàng)目的冠軍。在國際大賽上取得如此優(yōu)異的成績(jī)羡棵,證明了殘差神經(jīng)網(wǎng)絡(luò)是個(gè)實(shí)用性強(qiáng)且優(yōu)異的模型壹若。在本研究中的貓狗二分類的實(shí)驗(yàn)中,也是基于殘差神經(jīng)網(wǎng)絡(luò)來構(gòu)建分類模型的。
在本文中我們將把kaggle貓狗數(shù)據(jù)集應(yīng)用于ResNet-18和ResNet-50網(wǎng)絡(luò)模型店展。使用Resnet來探究當(dāng)前使用卷積神經(jīng)網(wǎng)絡(luò)的準(zhǔn)確率养篓。如圖4-1為ResNet的經(jīng)典網(wǎng)絡(luò)結(jié)構(gòu)圖——ResNet-18。
ResNet-18都是由BasicBlock組成赂蕴,從圖4-2也可得知50層及以上的ResNet網(wǎng)絡(luò)模型由BottleBlock組成柳弄。在我們就需要將我們預(yù)處理過的數(shù)據(jù)集放入現(xiàn)有的Resnet-18和ResNet-50模型中去訓(xùn)練,首先我們通過前面提到的圖像預(yù)處理把訓(xùn)練圖像裁剪成一個(gè)96x96的正方形尺寸概说,然后輸入到我們的模型中碧注,這里就介紹一下ResNet-18的網(wǎng)絡(luò)模型的結(jié)構(gòu),因?yàn)镽esNet50與第五章的ResNet-34模型結(jié)構(gòu)相仿糖赔。
ResNet-18的模型結(jié)構(gòu)為:首先第一層是一個(gè)7×7的卷積核,輸入特征矩陣為[112,112,64],經(jīng)過卷積核64萍丐,stride為2得到出入特征矩陣[56,56,64]。第二層一開始是由一個(gè)3×3的池化層組成的放典,接著是2個(gè)殘差結(jié)構(gòu)逝变,一開始的輸入的特征矩陣為[56,56,64],需要輸出的特征矩陣shape為[28,28,128], 然而主分支與shortcut的輸出特征矩陣shape必須相同刻撒,所以[56,56,64]這個(gè)特征矩陣的高和寬從56通過主分支的stride為2來縮減為原來的一半即為28骨田,再通過128個(gè)卷積核來改變特征矩陣的深度耿导。然而這里的shortcut加上了一個(gè)1x1的卷積核声怔,stride也為2,通過這個(gè)stride舱呻,輸入的特征矩陣的寬和高也縮減為原有的一半醋火,同時(shí)通過128個(gè)卷積核將輸入的特征矩陣的深度也變?yōu)榱?28。第三層箱吕,有2個(gè)殘差結(jié)構(gòu)芥驳,輸入的特征矩陣shape是[28,28,128],輸出特征矩陣shape是[14,14,256], 然而主分支與shortcut的輸出特征矩陣shape必須相同茬高,所以[14,14,256]這個(gè)特征矩陣的高和寬從14通過主分支的stride為2來縮減為原來的一半即為7兆旬,再通過128個(gè)卷積核來改變特征矩陣的深度。然而這里的shortcut加上了一個(gè)1×1的卷積核怎栽,stride也為2丽猬,通過這個(gè)stride,輸入的特征矩陣的寬和高也縮減為原有的一半熏瞄,同時(shí)通過256個(gè)卷積核將輸入的特征矩陣的深度也變?yōu)榱?56脚祟。第四層,有2個(gè)殘差結(jié)構(gòu),經(jīng)過上述的相同的變化過程得到輸出的特征矩陣為[7,7,512]强饮。第五層由桌,有2個(gè)殘差結(jié)構(gòu), 經(jīng)過上述的相同的變化過程得到輸出的特征矩陣為[1,1,512]。接著是平均池化和全連接層。
import torch.nn as nn
class BasicBlock(nn.Module):
"""Basic Block for resnet 18 and resnet 34
"""
#BasicBlock and BottleNeck block
#have different output size
expansion = 1
def __init__(self, in_channels, out_channels, stride=1):
super().__init__()
#residual function
self.residual_function = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels * BasicBlock.expansion, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels * BasicBlock.expansion)
)
#shortcut
self.shortcut = nn.Sequential()
#the shortcut output dimension is not the same with residual function
#use 1*1 convolution to match the dimension
if stride != 1 or in_channels != BasicBlock.expansion * out_channels:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channels, out_channels * BasicBlock.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_channels * BasicBlock.expansion)
)
def forward(self, x):
return nn.ReLU(inplace=True)(self.residual_function(x) + self.shortcut(x))
class BottleNeck(nn.Module):
"""Residual block for resnet over 50 layers
"""
expansion = 4
def __init__(self, in_channels, out_channels, stride=1):
super().__init__()
self.residual_function = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, stride=stride, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels * BottleNeck.expansion, kernel_size=1, bias=False),
nn.BatchNorm2d(out_channels * BottleNeck.expansion),
)
self.shortcut = nn.Sequential()
if stride != 1 or in_channels != out_channels * BottleNeck.expansion:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channels, out_channels * BottleNeck.expansion, stride=stride, kernel_size=1, bias=False),
nn.BatchNorm2d(out_channels * BottleNeck.expansion)
)
def forward(self, x):
return nn.ReLU(inplace=True)(self.residual_function(x) + self.shortcut(x)) #激活
class ResNet(nn.Module):
def __init__(self, block, num_block, num_classes=2):
super().__init__()
self.in_channels = 64
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1, bias=False), # 第一個(gè)卷積層行您,輸入3通道铭乾,輸出64通道,卷積核大小3 x 3邑雅,padding1
nn.BatchNorm2d(64),
nn.ReLU(inplace=True))
#we use a different inputsize than the original paper
#so conv2_x's stride is 1
# 以下構(gòu)建殘差塊片橡, 具體參數(shù)可以查看resnet參數(shù)表
self.conv2_x = self._make_layer(block, 64, num_block[0], 1)
self.conv3_x = self._make_layer(block, 128, num_block[1], 2)
self.conv4_x = self._make_layer(block, 256, num_block[2], 2)
self.conv5_x = self._make_layer(block, 512, num_block[3], 2)
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes) #fully connected layer
def _make_layer(self, block, out_channels, num_blocks, stride):
"""make resnet layers(by layer i didnt mean this 'layer' was the
same as a neuron netowork layer, ex. conv layer), one layer may
contain more than one residual block
Args:
block: block type, basic block or bottle neck block
out_channels: output depth channel number of this layer
num_blocks: how many blocks per layer
stride: the stride of the first block of this layer
Return:
return a resnet layer
"""
# 擴(kuò)維
# we have num_block blocks per layer, the first block
# could be 1 or 2, other blocks would always be 1
strides = [stride] + [1] * (num_blocks - 1)#減少特征圖尺寸
layers = []
# 特判第一殘差塊
for stride in strides:
layers.append(block(self.in_channels, out_channels, stride))#不減少特征圖尺寸
self.in_channels = out_channels * block.expansion
return nn.Sequential(*layers)
def forward(self, x): #forward方法,即向前計(jì)算淮野,通過該方法獲取網(wǎng)絡(luò)輸入數(shù)據(jù)后的輸出值
output = self.conv1(x) #第一次卷積
output = self.conv2_x(output)
output = self.conv3_x(output)
output = self.conv4_x(output)
output = self.conv5_x(output)
output = self.avg_pool(output)
output = output.view(output.size(0), -1)# resize batch-size output H
output = self.fc(output)
return output
def resnet18():
""" return a ResNet 18 object
"""
return ResNet(BasicBlock, [2, 2, 2, 2])
def resnet34():
""" return a ResNet 34 object
"""
return ResNet(BasicBlock, [3, 4, 6, 3])
def resnet50():
""" return a ResNet 50 object
"""
return ResNet(BottleNeck, [3, 4, 6, 3])
def resnet101():
""" return a ResNet 101 object
"""
return ResNet(BottleNeck, [3, 4, 23, 3])
def resnet152():
""" return a ResNet 152 object
"""
return ResNet(BottleNeck, [3, 8, 36, 3])