resnet网络

简单堆叠网络容易造成梯度消失或梯度爆炸,解决的方法主要有数据标准化,权重初始化和BN(Batch Normalization)层
resnet主要创新点:
①. 能够拥有超深的网络结构
②. 提出残差(residual)模块
③. 使用BN(Batch Normalization)层加速训练
1、残差结构
主分支与shotcut的输出特征矩阵shape必须一致。


残差结构图

左图应用于resnet-34, 右图残差结构应用于resnet-50/101/152
右图使用1×1的卷积用于降维和升维


不同网络的参数比较

虚线残差结构

注意 原论文中右图残差结构的主分支上,第一个1×1的卷积层步长为2,第二个3×3的卷积层是1
pytorch官方实现的代码中第一个1×1的卷积层步长为1,第二个3×3的卷积层是2,在imagenet数据集上准确率能够提升0.5%。
该结构用于改变特征图的宽和高
2、resnet-18、resnet-34使用的残差结构
BasicBlock

代码部分
class BasicBlock(nn.Module):
    """resnet-18、resnet-34所用的残差块"""
    # 残差结构主分支中所采用的卷积核个数是否改变
    expansion = 1

    def __init__(self, in_channel, out_channel, stride = 1, downsample = None):
        """
        :param in_channel: 输入特征矩阵深度
        :param out_channel: 输出特征矩阵深度
        :param stride:
        :param downsample: 下采样
        """
        super(BasicBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,
                               kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(out_channel)
        self.relu = nn.ReLU()
        self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,
                               kernel_size=3, stride=1, padding=1, bias=1)
        self.bn2 = nn.BatchNorm2d(out_channel)
        self.downsample = downsample

    def forward(self, x):
        identity = x   # 捷径分支输出值
        if self.downsample is not None:
            identity = self.downsample(x)

        # 主分支输出
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        out += identity
        out = self.relu(out)
        return out

3、resnet-50、resnet-101、resnet-152使用的残差结构


Bottleneck

代码部分

class Bottleneck(nn.Module):
    """resnet-50、resnet-101、resnet-152所用残差结构"""
    expansion = 4

    def __init__(self, in_channel, out_channel, stride = 1, downsample = None):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=in_channel,out_channels=out_channel,
                               kernel_size=1, stride=1, bias=False)
        self.bn1 = nn.BatchNorm2d(out_channel)

        self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,
                               kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(out_channel)

        self.conv3 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel * self.expansion,
                               kernel_size=1, stride=1,bias=False)
        self.bn3 = nn.BatchNorm2d(out_channel * self.expansion)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample

    def forward(self, x):
        identity = x
        if self.downsample is not None:
            identity = self.downsample(x)

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)
        out += identity

        out = self.relu(out)
        return out

4、resnet系列网络
代码部分

class Resnet(nn.Module):
    def __init__(self, block, blocks_num):
        """
        :param block:
        :param blocks_num: 残差结构数目
        """
        super(Resnet, self).__init__()
        self.in_channel = 64
        self.conv1 = nn.Conv2d(3, self.in_channel, kernel_size=(7, 7), padding=(3, 3), bias=False)
        self.bn1 = nn.BatchNorm2d(self.in_channel)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

        self.layer1 = self._make_layer(block, 64, blocks_num[0])
        self.layer2 = self._make_layer(block, 128, blocks_num[1], stride=2)
        self.layer3 = self._make_layer(block, 256, blocks_num[2], stride=2)
        self.layer4 = self._make_layer(block, 512, blocks_num[3], stride=2)

        # 网络参数初始化
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode="fan_out",nonlinearity='relu')

    def _make_layer(self, block, channel, block_num, stride=1):
        downsample = None
        if stride != 1 or self.in_channel != channel * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.in_channel, channel * block.expansion, kernel_size=1, stride=stride,bias=False),
                nn.BatchNorm2d(channel*block.expansion)
            )

        layers = []
        layers.append(block(self.in_channel, channel, downsample=downsample, stride=stride))
        self.in_channel = channel * block.expansion

        for _ in range(1, block_num):
            layers.append(block(self.in_channel, channel))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        return x

def resnet18(pretrained=True):
    """Constructs a ResNet-18 model.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = Resnet(BasicBlock, [2, 2, 2, 2])
    if pretrained:
        model.load_state_dict(model_zoo.load_url(
            model_urls['resnet18']), strict=False)
    return model

if __name__ == '__main__':
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    resnet18 = resnet18(True).to(device)
    print(summary(resnet18, input_size = (3, 224, 224)))

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容