实验步骤
搭建resnet18网络
数据集加载
模型训练和改进
分析评估
Kaggle提交
网络构建
实验初期拟采用torchvision中实现的resnet18作为网络结构,为了方便修改网络结构,于是重新实现了resnet18网络
resnet18由一个7x7的降采样卷积,一个max pooling层,8个basicblock,一个全局池化层,最后接一个全连接层组成,如下图
tensorboard网络结构可视化,左图为torchvision中的resnet实现,右图为自定义实现
代码如下
定义残差块
class IdentityBlock(nn.Module):
def __init__(self, in_channels, out_channels, down_sampling=False):
super().__init__()
self.down_sampling = down_sampling
self.in_channels = in_channels
self.out_channels = out_channels
self.conv1 = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3,
stride=(1 if in_channels == out_channels else 2), padding=1,
bias=False)),
('bn1', nn.BatchNorm2d(out_channels)),
('relu1', nn.ReLU())
]))
self.shortcut = nn.Sequential(OrderedDict([
(
'conv',
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=2, bias=False)),
('bn', nn.BatchNorm2d(out_channels))
])) if in_channels != out_channels else nn.Sequential()
self.conv2 = nn.Sequential(OrderedDict([
('conv2', nn.Conv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1,
bias=False)),
('bn2', nn.BatchNorm2d(out_channels))
]))
self.relu2 = nn.ReLU()
def forward(self, x):
fx = self.conv1(x)
fx = self.conv2(fx)
x = self.shortcut(x)
hx = fx + x
hx = self.relu2(hx)
return hx
定义模型网络
class Resnet18(nn.Module):
def __init__(self, num_classes):
super(Resnet18, self).__init__()
self.conv1 = nn.Sequential(OrderedDict([
('conv', nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2, padding=3, bias=False)),
('bn', nn.BatchNorm2d(64)),
('relu', nn.ReLU()),
]))
self.max_pool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self.make_layer(