import torch
import torch.nn as nn
import torch.nn.functional as F
class Residual(nn.Module):
def __init__(self, input_channels, num_channels, num_layers, use_1x1conv=False, strides=1):
super().__init__()
self.num_layers = num_layers
if num_layers in [18, 34]:
self.conv1 = nn.Conv2d(input_channels, num_channels, kernel_size=3, padding=1, stride=strides)
self.conv2 = nn.Conv2d(num_channels, num_channels, kernel_size=3, padding=1, stride=1)
if use_1x1conv:
self.conv3 = nn.Conv2d(input_channels, num_channels, kernel_size=3,
padding=1, stride=strides)
else:
self.conv3 = None
self.bn1 = nn.BatchNorm2d(num_channels)
self.bn2 = nn.BatchNorm2d(num_channels)
self.bn3 = nn.BatchNorm2d(num_channels)
else:
self.conv1 = nn.Conv2d(input_channels, num_channels//4, kernel_size=1, stride=1)
self.conv2 = nn.Conv2d(num_channels//4, num_channels//4, kernel_size=3, padding=1, stride=strides)
self.conv3 = nn.Conv2d(num_channels//4, num_channels, kernel_size=1, stride=1)
if use_1x1conv:
self.conv4 = nn.Conv2d(input_channels, num_channels, kernel_size=3, padding=1, stride=strides)
else:
self.conv4 = None
self.bn1 = nn.BatchNorm2d(num_channels//4)
self.bn2 = nn.BatchNorm2d(num_channels//4)
self.bn3 = nn.BatchNorm2d(num_channels)
self.bn4 = nn.BatchNorm2d(num_channels)
def forward(self, X):
if self.num_layers in [18, 34]:
Y = F.relu(self.bn1(self.conv1(X)))
Y = self.bn2(self.conv2(Y))
if self.conv3 is not None:
Y += self.conv3(X)
Y = self.bn3(Y)
else:
Y = F.relu(self.bn1(self.conv1(X)))
Y = F.relu(self.bn2(self.conv2(Y)))
Y = self.bn3(self.conv3(Y))
if self.conv4 is not None:
Y += self.conv4(X)
Y = self.bn4(Y)
return F.relu(Y)
def resnet_block(input_channels, num_channels, num_residuals, num_layers, first_block=False):
blk = []
for i in range(num_residuals):
if num_layers in [18, 34]:
if i == 0 and not first_block:
blk.append(Residual(input_channels, num_channels, num_layers, use_1x1conv=True, strides=2))
else:
blk.append(Residual(num_channels, num_channels, num_layers))
else:
if i == 0:
blk.append(Residual(input_channels, num_channels, num_layers, use_1x1conv=True, strides=2))
else:
blk.append(Residual(num_channels, num_channels, num_layers, use_1x1conv=True))
return blk
b1 = nn.Sequential(nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3),
nn.BatchNorm2d(64), nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
def resnet18():
b2 = nn.Sequential(*resnet_block(64, 64, 2, 18, first_block=True))
b3 = nn.Sequential(*resnet_block(64, 128, 2, 18))
b4 = nn.Sequential(*resnet_block(128, 256, 2, 18))
b5 = nn.Sequential(*resnet_block(256, 512, 2, 18))
return nn.Sequential(b1, b2, b3, b4, b5, nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(), nn.Linear(512, 10))
def resnet34():
b2 = nn.Sequential(*resnet_block(64, 64, 3, 34, first_block=True))
b3 = nn.Sequential(*resnet_block(64, 128, 4, 34))
b4 = nn.Sequential(*resnet_block(128, 256, 6, 34))
b5 = nn.Sequential(*resnet_block(256, 512, 3, 34))
return nn.Sequential(b1, b2, b3, b4, b5, nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(), nn.Linear(512, 10))
def resnet50():
b2 = nn.Sequential(*resnet_block(64, 256, 3, 50, first_block=True))
b3 = nn.Sequential(*resnet_block(256, 512, 4, 50))
b4 = nn.Sequential(*resnet_block(512, 1024, 6, 50))
b5 = nn.Sequential(*resnet_block(1024, 2048, 3, 50))
return nn.Sequential(b1, b2, b3, b4, b5, nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(), nn.Linear(2048, 10))
def resnet101():
b2 = nn.Sequential(*resnet_block(64, 256, 3, 101, first_block=True))
b3 = nn.Sequential(*resnet_block(256, 512, 4, 101))
b4 = nn.Sequential(*resnet_block(512, 1024, 23, 101))
b5 = nn.Sequential(*resnet_block(1024, 2048, 3, 101))
return nn.Sequential(b1, b2, b3, b4, b5, nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(), nn.Linear(2048, 10))
def resnet152():
b2 = nn.Sequential(*resnet_block(64, 256, 3, 152, first_block=True))
b3 = nn.Sequential(*resnet_block(256, 512, 8, 152))
b4 = nn.Sequential(*resnet_block(512, 1024, 36, 152))
b5 = nn.Sequential(*resnet_block(1024, 2048, 3, 152))
return nn.Sequential(b1, b2, b3, b4, b5, nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(), nn.Linear(2048, 10))