在网上找了很多现成的ResNet50代码,但发现一些嵌套让我迷糊了,所以我干脆把残差边做卷积的ConvBlock和不做卷积的IdentityBlock拆开,写成两个类,最后依次嵌入到ResNet50网络中,虽然繁琐,但可能会更容易理解。
1. 导入
# ---------- ResNet50 ----------
import torch
import torch.nn as nn
2. ConvBlock
class ConvBlock(nn.Module):
def __init__(self, input_channel, output_channel, stride):
super(ConvBlock, self).__init__()
self.conv1 = nn.Conv2d(input_channel, output_channel//4, kernel_size=1, stride=1, bias=False)
self.bn1 = nn.BatchNorm2d(output_channel//4)
self.conv2 = nn.Conv2d(output_channel//4, output_channel//4, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(output_channel//4)
self.conv3 = nn.Conv2d(output_channel//4, output_channel, kernel_size=1, stride=1, bias=False)
self.bn3 = nn.BatchNorm2d(output_channel)
self.relu = nn.ReLU(inplace=True)
self.conv4 = nn.Conv2d(input_channel, output_channel, kernel_size=1, stride=stride, bias=False)
self.bn4 = nn.BatchNorm2d(output_channel)
def forward(self, x):
residual = x
x = self.relu(self.bn1(self.conv1(x)))
x = self.relu(self.bn2(self.conv2(x)))
x = self.bn3(self.conv3(x))
residual = self.bn4(self.conv4(residual))
out = x + residual
out = self.relu(out)
return out
3. IdentityBlock
class IdentityBlock(nn.Module):
def __init__(self, input_channel, output_channel):
super(IdentityBlock, self).__init__()
self.conv1 = nn.Conv2d(input_channel, output_channel//4, kernel_size=1, stride=1, bias=False)
self.bn1 = nn.BatchNorm2d(output_channel//4)
self.conv2 = nn.Conv2d(output_channel//4, output_channel//4, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(output_channel//4)
self.conv3 = nn.Conv2d(output_channel//4, output_channel, kernel_size=1, stride=1, bias=False)
self.bn3 = nn.BatchNorm2d(output_channel)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
residual = x
x = self.relu(self.bn1(self.conv1(x)))
x = self.relu(self.bn2(self.conv2(x)))
x = self.bn3(self.conv3(x))
out = x + residual
out = self.relu(out)
return out
4. 整体网络
class ResNet50(nn.Module):
def __init__(self, ConvBlock, IdentityBlock, num_classes=10):
super(ResNet50, self).__init__()
self.inchannel = 64
self.Conv1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
self.Layer1 = nn.Sequential(
ConvBlock(64, 256, 1),
IdentityBlock(256, 256),
IdentityBlock(256, 256)
)
self.Layer2 = nn.Sequential(
ConvBlock(256, 512, 2),
IdentityBlock(512, 512),
IdentityBlock(512, 512),
IdentityBlock(512, 512)
)
self.Layer3 = nn.Sequential(
ConvBlock(512, 1024, 2),
IdentityBlock(1024, 1024),
IdentityBlock(1024, 1024),
IdentityBlock(1024, 1024),
IdentityBlock(1024, 1024),
IdentityBlock(1024, 1024)
)
self.Layer4 = nn.Sequential(
ConvBlock(1024, 2048, 2),
IdentityBlock(2048, 2048),
IdentityBlock(2048, 2048)
)
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(2048, num_classes, bias=True)
def forward(self, x): # 3*224*224
out = self.Conv1(x) # 64*56*56
out = self.Layer1(out) # 256*56*56
out = self.Layer2(out) # 512*28*28
out = self.Layer3(out) # 1024*14*14
out = self.Layer4(out) # 2048*7*7
out = self.avg_pool(out) # 2048*1*1
out = out.view(out.size(0), -1)
out = self.fc(out)
return out