現(xiàn)在重新稍微系統(tǒng)的介紹一下ResNet網(wǎng)絡(luò)結(jié)構(gòu)。 ResNet結(jié)構(gòu)首先通過一個卷積層然后有一個池化層,然后通過一系列的殘差結(jié)構(gòu),最后再通過一個平均池化下采樣操作,以及一個全連接層的得到了一個輸出。ResNet網(wǎng)絡(luò)可以達(dá)到很深的層數(shù)的原因就是不斷的堆疊殘差結(jié)構(gòu)而來的。
但是,一般來說,并不是一直的加深神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)就會得到一個更好的結(jié)果,一般太深的網(wǎng)絡(luò)會出現(xiàn)過擬合的現(xiàn)象嚴(yán)重,可能還沒有一些淺層網(wǎng)絡(luò)要好。
當(dāng)層數(shù)過多的時候,假設(shè)每一層的誤差梯度都是一個小于1的數(shù)值,當(dāng)進(jìn)行方向傳播的過程中,每向前傳播一層,都要乘以一個小于1的誤差梯度,當(dāng)網(wǎng)絡(luò)越來越深時,所成的小于1的系數(shù)也就越來越多,此時梯度便越趨近于0,這樣梯度便會越來越小。這便會造成梯度消失的現(xiàn)象。
而當(dāng)所成的誤差梯度是一個大于1的系數(shù),而隨著網(wǎng)絡(luò)層數(shù)的加深,梯度便會越來越大,這便會造成梯度爆炸的現(xiàn)象。
當(dāng)解決了梯度消失或者梯度爆炸的問題之后,其實網(wǎng)絡(luò)的效果可能還是不盡如意,還可能有退化問題。為此,ResNet提出了殘差結(jié)構(gòu)來解決這個退化問題。 也正是因為有這個殘差的結(jié)構(gòu),所以才可以搭建這么深的網(wǎng)絡(luò)。
作圖是針對ResNet-18/34層淺層網(wǎng)絡(luò)的結(jié)構(gòu),右圖是ResNet-50/101/152層深層網(wǎng)絡(luò)的結(jié)構(gòu),其中注意:主分支與shortcut 的輸出特征矩陣shape。
需要注意,有些殘差結(jié)構(gòu)的ShortCut是實線,而有的是虛線,這兩者是不同的。對于左圖來說,ShortCut是實線,這表明輸入與輸出的shape是一樣的,所以可以直接的進(jìn)行相加。而對于右圖來說,其輸入的shape與輸出的shape是不一樣的,這時候需要調(diào)整步長stribe與kernel size來使得兩條路(主分支與捷徑分支)所處理好的shape是一模一樣的。
右側(cè)虛線殘差結(jié)構(gòu)的主分支上、第一個1x1卷積層的步距是2,第二個3x3卷積層的步距是1.
而在pytorch官方實現(xiàn)的過程中是第一個1x1卷積層的步距是1,第二個3x3卷積層步距是2,這樣能夠在ImageNet的top1上提升大概0.5%的準(zhǔn)確率。
所以在conv3_x,conv4_x,conv5_x中所對應(yīng)的殘差結(jié)構(gòu)的第一層,都是指虛線的殘差結(jié)構(gòu),其他的殘差結(jié)構(gòu)是實線的殘差結(jié)構(gòu)。
對于每個大模塊中的第一個殘差結(jié)構(gòu),需要通過虛線分支來調(diào)整殘差結(jié)構(gòu)的輸入與輸出是同一個shape。此時使用了下采樣的操作函數(shù)。
對于每個大模塊中的其他剩余的殘差結(jié)構(gòu),只需要通過實線分支來調(diào)整殘差網(wǎng)絡(luò)結(jié)構(gòu),因為其輸出和輸入本身就是同一個shape的。
對于第一個大模塊的第一個殘差結(jié)構(gòu),其第二個3x3的卷積中,步長是1的,而其他的三個大模塊的步長均為2.
在每一個大模塊的維度變換中,主要是第一個殘差結(jié)構(gòu)使得shape減半,而模塊中其他的殘差結(jié)構(gòu)都是沒有改變shape的。也真因為沒有改變shape,所以這些殘差結(jié)構(gòu)才可以直接的通過實線進(jìn)行相加。
Batch Normalization是google團隊在2015年論文《Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift》提出的。通過該方法能夠加速網(wǎng)絡(luò)的收斂并提升準(zhǔn)確率。
import torch
import torch.nn as nn
# 分類數(shù)目
num_class = 5
# 各層數(shù)目
resnet18_params = [2, 2, 2, 2]
resnet34_params = [3, 4, 6, 3]
resnet50_params = [3, 4, 6, 3]
resnet101_params = [3, 4, 23, 3]
resnet152_params = [3, 8, 36, 3]
# 定義Conv1層
def Conv1(in_planes, places, stride=2):
return nn.Sequential(
nn.Conv2d(in_channels=in_planes,out_channels=places,kernel_size=7,stride=stride,padding=3, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
# 淺層的殘差結(jié)構(gòu)
class BasicBlock(nn.Module):
def __init__(self,in_places,places, stride=1,downsampling=False, expansion = 1):
super(BasicBlock,self).__init__()
self.expansion = expansion
self.downsampling = downsampling
# torch.Size([1, 64, 56, 56]), stride = 1
# torch.Size([1, 128, 28, 28]), stride = 2
# torch.Size([1, 256, 14, 14]), stride = 2
# torch.Size([1, 512, 7, 7]), stride = 2
self.basicblock = nn.Sequential(
nn.Conv2d(in_channels=in_places, out_channels=places, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=places, out_channels=places, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(places * self.expansion),
)
# torch.Size([1, 64, 56, 56])
# torch.Size([1, 128, 28, 28])
# torch.Size([1, 256, 14, 14])
# torch.Size([1, 512, 7, 7])
# 每個大模塊的第一個殘差結(jié)構(gòu)需要改變步長
if self.downsampling:
self.downsample = nn.Sequential(
nn.Conv2d(in_channels=in_places, out_channels=places*self.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(places*self.expansion)
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
# 實線分支
residual = x
out = self.basicblock(x)
# 虛線分支
if self.downsampling:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
# 深層的殘差結(jié)構(gòu)
class Bottleneck(nn.Module):
# 注意:默認(rèn) downsampling=False
def __init__(self,in_places,places, stride=1,downsampling=False, expansion = 4):
super(Bottleneck,self).__init__()
self.expansion = expansion
self.downsampling = downsampling
self.bottleneck = nn.Sequential(
# torch.Size([1, 64, 56, 56]),stride=1
# torch.Size([1, 128, 56, 56]),stride=1
# torch.Size([1, 256, 28, 28]), stride=1
# torch.Size([1, 512, 14, 14]), stride=1
nn.Conv2d(in_channels=in_places,out_channels=places,kernel_size=1,stride=1, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
# torch.Size([1, 64, 56, 56]),stride=1
# torch.Size([1, 128, 28, 28]), stride=2
# torch.Size([1, 256, 14, 14]), stride=2
# torch.Size([1, 512, 7, 7]), stride=2
nn.Conv2d(in_channels=places, out_channels=places, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
# torch.Size([1, 256, 56, 56]),stride=1
# torch.Size([1, 512, 28, 28]), stride=1
# torch.Size([1, 1024, 14, 14]), stride=1
# torch.Size([1, 2048, 7, 7]), stride=1
nn.Conv2d(in_channels=places, out_channels=places * self.expansion, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(places * self.expansion),
)
# torch.Size([1, 256, 56, 56])
# torch.Size([1, 512, 28, 28])
# torch.Size([1, 1024, 14, 14])
# torch.Size([1, 2048, 7, 7])
if self.downsampling:
self.downsample = nn.Sequential(
nn.Conv2d(in_channels=in_places, out_channels=places*self.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(places*self.expansion)
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
# 實線分支
residual = x
out = self.bottleneck(x)
# 虛線分支
if self.downsampling:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self,blocks, blockkinds, num_classes=num_class):
super(ResNet,self).__init__()
self.blockkinds = blockkinds
self.conv1 = Conv1(in_planes = 3, places= 64)
# 對應(yīng)淺層網(wǎng)絡(luò)結(jié)構(gòu)
if self.blockkinds == BasicBlock:
self.expansion = 1
# 64 -> 64
self.layer1 = self.make_layer(in_places=64, places=64, block=blocks[0], stride=1)
# 64 -> 128
self.layer2 = self.make_layer(in_places=64, places=128, block=blocks[1], stride=2)
# 128 -> 256
self.layer3 = self.make_layer(in_places=128, places=256, block=blocks[2], stride=2)
# 256 -> 512
self.layer4 = self.make_layer(in_places=256, places=512, block=blocks[3], stride=2)
self.fc = nn.Linear(512, num_classes)
# 對應(yīng)深層網(wǎng)絡(luò)結(jié)構(gòu)
if self.blockkinds == Bottleneck:
self.expansion = 4
# 64 -> 64
self.layer1 = self.make_layer(in_places = 64, places= 64, block=blocks[0], stride=1)
# 256 -> 128
self.layer2 = self.make_layer(in_places = 256,places=128, block=blocks[1], stride=2)
# 512 -> 256
self.layer3 = self.make_layer(in_places=512,places=256, block=blocks[2], stride=2)
# 1024 -> 512
self.layer4 = self.make_layer(in_places=1024,places=512, block=blocks[3], stride=2)
self.fc = nn.Linear(2048, num_classes)
self.avgpool = nn.AvgPool2d(7, stride=1)
# 初始化網(wǎng)絡(luò)結(jié)構(gòu)
for m in self.modules():
if isinstance(m, nn.Conv2d):
# 采用了何凱明的初始化方法
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def make_layer(self, in_places, places, block, stride):
layers = []
# torch.Size([1, 64, 56, 56]) -> torch.Size([1, 256, 56, 56]), stride=1 故w,h不變
# torch.Size([1, 256, 56, 56]) -> torch.Size([1, 512, 28, 28]), stride=2 故w,h變
# torch.Size([1, 512, 28, 28]) -> torch.Size([1, 1024, 14, 14]),stride=2 故w,h變
# torch.Size([1, 1024, 14, 14]) -> torch.Size([1, 2048, 7, 7]), stride=2 故w,h變
# 此步需要通過虛線分支,downsampling=True
layers.append(self.blockkinds(in_places, places, stride, downsampling =True))
# torch.Size([1, 256, 56, 56]) -> torch.Size([1, 256, 56, 56])
# torch.Size([1, 512, 28, 28]) -> torch.Size([1, 512, 28, 28])
# torch.Size([1, 1024, 14, 14]) -> torch.Size([1, 1024, 14, 14])
# torch.Size([1, 2048, 7, 7]) -> torch.Size([1, 2048, 7, 7])
# print("places*self.expansion:", places*self.expansion)
# print("block:", block)
# 此步需要通過實線分支,downsampling=False, 每個大模塊的第一個殘差結(jié)構(gòu)需要改變步長
for i in range(1, block):
layers.append(self.blockkinds(places*self.expansion, places))
return nn.Sequential(*layers)
def forward(self, x):
# conv1層
x = self.conv1(x) # torch.Size([1, 64, 56, 56])
# conv2_x層
x = self.layer1(x) # torch.Size([1, 256, 56, 56])
# conv3_x層
x = self.layer2(x) # torch.Size([1, 512, 28, 28])
# conv4_x層
x = self.layer3(x) # torch.Size([1, 1024, 14, 14])
# conv5_x層
x = self.layer4(x) # torch.Size([1, 2048, 7, 7])
x = self.avgpool(x) # torch.Size([1, 2048, 1, 1]) / torch.Size([1, 512])
x = x.view(x.size(0), -1) # torch.Size([1, 2048]) / torch.Size([1, 512])
x = self.fc(x) # torch.Size([1, 5])
return x
def ResNet18():
return ResNet(resnet18_params, BasicBlock)
def ResNet34():
return ResNet(resnet34_params, BasicBlock)
def ResNet50():
return ResNet(resnet50_params, Bottleneck)
def ResNet101():
return ResNet(resnet101_params, Bottleneck)
def ResNet152():
return ResNet(resnet152_params, Bottleneck)
if __name__=='__main__':
# model = torchvision.models.resnet50()
# 模型測試
# model = ResNet18()
# model = ResNet34()
# model = ResNet50()
# model = ResNet101()
model = ResNet152()
# print(model)
input = torch.randn(1, 3, 224, 224)
out = model(input)
print(out.shape)
以上就是pytorch實現(xiàn)ResNet結(jié)構(gòu)的實例代碼的詳細(xì)內(nèi)容,更多關(guān)于pytorch ResNet結(jié)構(gòu)的資料請關(guān)注腳本之家其它相關(guān)文章!