ResNet50网络(Pytorch)

在网上找了很多现成的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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值