3、PyTorch从零构建AlexNet训练MNIST数据集
5、PyTorch从零构建GoogLeNet训练MNIST数据集
1、ResNet
2、PyTorch构建残差块Residual
python
class Residual(nn.Module):
def __init__(self, in_channel, out_channel, stride, upsamlpe):
super(Residual, self).__init__()
self.conv1 = nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=stride, padding=1)
self.conv2 = nn.Conv2d(out_channel, out_channel, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=stride)
self.bn1 = nn.BatchNorm2d(out_channel, affine=False)
self.bn2 = nn.BatchNorm2d(out_channel, affine=False)
self.relu1 = nn.ReLU()
self.relu2 = nn.ReLU()
def forward(self, x):
out = self.relu1(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
x = self.conv3(x)
out = self.relu2(out + x)
# print(out.shape)
return out
3、PyTorch构建ResNet
python
class ResNet(nn.Module):
def __init__(self, num_classes):
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(1, 64, kernel_size=1)
self.maxpool1 = nn.MaxPool2d(3, stride=2, padding=1)
self.resblock1 = Residual(64, 64, 1, True)
self.resblock2 = Residual(64, 64, 1, True)
self.resblock3 = Residual(64, 128, 2, True)
self.resblock4 = Residual(128, 128, 1, True)
self.resblock5 = Residual(128, 256, 1, True)
self.resblock6 = Residual(256, 256, 1, True)
self.resblock7 = Residual(256, 512, 1, True)
self.resblock8 = Residual(512, 512, 1, True)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512, num_classes)
def forward(self, x):
x = self.maxpool1(self.conv1(x))
x = self.resblock1(x)
x = self.resblock2(x)
x = self.resblock3(x)
x = self.resblock4(x)
x = self.resblock5(x)
x = self.resblock6(x)
x = self.resblock7(x)
x = self.resblock8(x)
x = self.avgpool(x)
x = x.reshape(x.shape[0], -1)
x = self.fc(x)
return x