【圖像分類】手撕ResNet——復現ResNet(Pytorch)
目錄
RseNet50、 RseNet101、 RseNet152、
摘要
ResNet(Residual Neural Network)由微軟研究院的Kaiming He等四名華人提出,通過使用ResNet Unit成功訓練出了152層的神經網絡,並在ILSVRC2015比賽中取得冠軍,在top5上的錯誤率為3.57%,同時參數量比VGGNet低,效果非常明顯。
模型的創新點在於提出殘差學習的思想,在網絡中增加了直連通道,將原始輸入信息直接傳到後面的層中,如下圖所示:
\
傳統的卷積網絡或者全連接網絡在信息傳遞的時候或多或少會存在信息丟失,損耗等問題,同時還有導致梯度消失或者梯度爆炸,導致很深的網絡無法訓練。ResNet在一定程度上解決了這個問題,通過直接將輸入信息繞道傳到輸出,保護信息的完整性,整個網絡只需要學習輸入、輸出差別的那一部分,簡化學習目標和難度。VGGNet和ResNet的對比如下圖所示。ResNet最大的區別在於有很多的旁路將輸入直接連接到後面的層,這種結構也被稱為shortcut或者skip connections。
在ResNet網絡結構中會用到兩種殘差模塊,一種是以兩個3*3的卷積網絡串接在一起作為一個殘差模塊,另外一種是1*1、3*3、1*1的3個卷積網絡串接在一起作為一個殘差模塊。如下圖所示:
ResNet有不同的網絡層數,比較常用的是18-layer,34-layer,50-layer,101-layer,152-layer。他們都是由上述的殘差模塊堆疊在一起實現的。 下圖展示了不同的ResNet模型。
實現殘差模塊
第一個殘差模塊
```python class ResidualBlock(nn.Module): """ 實現子module: Residual Block """
def __init__(self, inchannel, outchannel, stride=1, shortcut=None):
super(ResidualBlock, self).__init__()
self.left = nn.Sequential(
nn.Conv2d(inchannel, outchannel, 3, stride, 1, bias=False),
nn.BatchNorm2d(outchannel),
nn.ReLU(inplace=True),
nn.Conv2d(outchannel, outchannel, 3, 1, 1, bias=False),
nn.BatchNorm2d(outchannel))
self.right = shortcut
def forward(self, x):
out = self.left(x)
residual = x if self.right is None else self.right(x)
out += residual
return F.relu(out)
```
第二個殘差模塊
```python class Bottleneck(nn.Module): 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(
nn.Conv2d(in_channels=in_places,out_channels=places,kernel_size=1,stride=1, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=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*self.expansion, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(places*self.expansion),
)
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
```
ResNet18, ResNet34
```python import torch import torchvision from torch import nn from torch.nn import functional as F from torchsummary import summary
class ResidualBlock(nn.Module): """ 實現子module: Residual Block """
def __init__(self, inchannel, outchannel, stride=1, shortcut=None):
super(ResidualBlock, self).__init__()
self.left = nn.Sequential(
nn.Conv2d(inchannel, outchannel, 3, stride, 1, bias=False),
nn.BatchNorm2d(outchannel),
nn.ReLU(inplace=True),
nn.Conv2d(outchannel, outchannel, 3, 1, 1, bias=False),
nn.BatchNorm2d(outchannel)
)
self.right = shortcut
def forward(self, x):
out = self.left(x)
residual = x if self.right is None else self.right(x)
out += residual
return F.relu(out)
class ResNet(nn.Module): """ 實現主module:ResNet34 ResNet34包含多個layer,每個layer又包含多個Residual block 用子module來實現Residual block,用_make_layer函數來實現layer """
def __init__(self, blocks, num_classes=1000):
super(ResNet, self).__init__()
self.model_name = 'resnet34'
# 前幾層: 圖像轉換
self.pre = nn.Sequential(
nn.Conv2d(3, 64, 7, 2, 3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2, 1))
# 重複的layer,分別有3,4,6,3個residual block
self.layer1 = self._make_layer(64, 64, blocks[0])
self.layer2 = self._make_layer(64, 128, blocks[1], stride=2)
self.layer3 = self._make_layer(128, 256, blocks[2], stride=2)
self.layer4 = self._make_layer(256, 512, blocks[3], stride=2)
# 分類用的全連接
self.fc = nn.Linear(512, num_classes)
def _make_layer(self, inchannel, outchannel, block_num, stride=1):
"""
構建layer,包含多個residual block
"""
shortcut = nn.Sequential(
nn.Conv2d(inchannel, outchannel, 1, stride, bias=False),
nn.BatchNorm2d(outchannel),
nn.ReLU()
)
layers = []
layers.append(ResidualBlock(inchannel, outchannel, stride, shortcut))
for i in range(1, block_num):
layers.append(ResidualBlock(outchannel, outchannel))
return nn.Sequential(*layers)
def forward(self, x):
x = self.pre(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = F.avg_pool2d(x, 7)
x = x.view(x.size(0), -1)
return self.fc(x)
def ResNet18(): return ResNet([2, 2, 2, 2])
def ResNet34(): return ResNet([3, 4, 6, 3])
if name == 'main': device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model = ResNet34() model.to(device) summary(model, (3, 224, 224)) ```
RseNet50、 RseNet101、 RseNet152、
```python import torch import torch.nn as nn import torchvision import numpy as np
print("PyTorch Version: ",torch.version) print("Torchvision Version: ",torchvision.version)
all = ['ResNet50', 'ResNet101','ResNet152']
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) )
class Bottleneck(nn.Module): 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(
nn.Conv2d(in_channels=in_places,out_channels=places,kernel_size=1,stride=1, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=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*self.expansion, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(places*self.expansion),
)
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, num_classes=1000, expansion = 4): super(ResNet,self).init() self.expansion = expansion
self.conv1 = Conv1(in_planes = 3, places= 64)
self.layer1 = self.make_layer(in_places = 64, places= 64, block=blocks[0], stride=1)
self.layer2 = self.make_layer(in_places = 256,places=128, block=blocks[1], stride=2)
self.layer3 = self.make_layer(in_places=512,places=256, block=blocks[2], stride=2)
self.layer4 = self.make_layer(in_places=1024,places=512, block=blocks[3], stride=2)
self.avgpool = nn.AvgPool2d(7, stride=1)
self.fc = nn.Linear(2048,num_classes)
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 = []
layers.append(Bottleneck(in_places, places,stride, downsampling =True))
for i in range(1, block):
layers.append(Bottleneck(places*self.expansion, places))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
def ResNet50(): return ResNet([3, 4, 6, 3])
def ResNet101(): return ResNet([3, 4, 23, 3])
def ResNet152(): return ResNet([3, 8, 36, 3])
if name=='main': #model = torchvision.models.resnet50() model = ResNet50() print(model)
input = torch.randn(1, 3, 224, 224)
out = model(input)
print(out.shape)
```
- YoloV5實戰:手把手教物體檢測——YoloV5
- 基於阿里Semantatic Human Matting算法,實現精細化人物摳圖
- PPv3-OCR自定義數據從訓練到部署
- 如何下載pytorch的歷史版本?
- WinForm——Button總結
- WinForm——MDI窗體
- 升級 pip
- 將8位的tif圖片改為png圖片
- RepLKNet實戰:使用RepLKNet實現對植物幼苗的分類(非官方)(二)
- 關於OpenCV imread和imdecode讀取圖片是BGR的證明
- opencv讀取圖片通道以及顯示
- 萬字整理聯邦學習系統架構設計參考
- 編譯器堆空間不足
- 【圖像分類】實戰——使用EfficientNetV2實現圖像分類(Pytorch)
- MMDetection實戰:MMDetection訓練與測試
- UNet語義分割實戰:使用UNet實現對人物的摳圖
- MobileVIT實戰:使用MobileVIT實現圖像分類
- SwinIR實戰:如何使用SwinIR和預訓練模型實現圖片的超分
- 【圖像分類】手撕ResNet——復現ResNet(Pytorch)
- Deeplab實戰:使用deeplabv3實現對人物的摳圖