Resnet50学习

1.残差块
在这里插入图片描述
2.模块图
在这里插入图片描述
代码详解

import torch
from torch import nn

class Bottleneck(nn.Module):
    def __init__(self, in_channels,filters,stride=1):
        super(Bottleneck, self).__init__()
        self.stride = stride
        self.in_channels = in_channels
        F1, F2, F3 = filters
        self.out_channels = F3

        self.block = nn.Sequential(
            nn.Conv2d(in_channels, F1, 1, stride=stride, padding=0, bias=False),
            nn.BatchNorm2d(F1),
            nn.ReLU(inplace=True),
            # padding *2+1 = kernel_size大小
            nn.Conv2d(F1, F2, 3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(F2),
            nn.ReLU(inplace=True),
            nn.Conv2d(F2, F3, 1, stride=1, padding=0,bias =False),
            nn.BatchNorm2d(F3)
        )
        self.downsample = nn.Sequential(
            nn.Conv2d(in_channnels=in_channnels, out_channels=F3,
                      kernel_size=1,stride=stride, bias=False ),
            nn.BatchNorm2d(F3))
        self.relu = nn.ReLU(inplace=True)
    def forwad(self, x):
        identity = x
        out = self.block(x)
        if self.stride !=1 or self.in_channels != self.out_channels:
            identity =self.downsample(x)
        out += identity
        out = self.relu(out)
        return out
class Resnet50(nn.Module):
    def __init__(self, n_class):
        super(Resnet50, self).__init__()
        self.stage1 = nn.Sequential(
            nn.Conv2d(3, 64, stride=2, padding=3, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(True),
            nn.MaxPool2d (3, 2, padding=1),
        )
        self.stage2 = nn.Sequential(
            Bottleneck(64,[64,64,256], stride=1),
            Bottleneck(256,[64,64,256]),
            Bottleneck(256, [64,64,256])
        )
        self.stage3 = nn.Sequential(
            Bottleneck(256, [128,128,512], stride=2), # 用stride=2是因为图像大小改变了,stride变大下采样操作
            Bottleneck(512,[128,128,512]), # stride默认为1不用进行下采样 操作
            Bottleneck(512,[128,128,512]),
            Bottleneck(512,[128,128,512])
        )
        self.stage4 = nn.Sequential(
            Bottleneck(512, [258, 258, 1024], stride=2),
            Bottleneck(1024, [258, 258, 1024]),
            Bottleneck(1024, [258, 258, 1024]),
            Bottleneck(1024, [258, 258, 1024]),
            Bottleneck(1024, [258, 258, 1024]),
            Bottleneck(1024, [258, 258, 1024])
        )
        self.stage5 = nn.Sequential(
            Bottleneck(1024, [512,512,2028], stride=2),
            Bottleneck(2048, [512, 512, 2048]),
            Bottleneck(2048, [512, 512, 2048])
        )
        self.avgpool = nn.AdaptiveAvgPool2d((1,1)) #最后的输出变成一个1×1的大小,同时通道变成上层的2048个通道
        self.fc = nn.Sequential(
            nn.Linear(2048, n_class)
        )
    def forward(self, x):
        out = self.stage1(x)
        print(out.shape)
        out = self.stage2(out)
        print(out.shape)
        out = self.stage3(out)
        print(out.shape)
        out = self.stage4(out)
        print(out.shape)
        out = self.stage5(out)
        print(out.shape)
        out = self.avgpool(out)  #torch.size([1, 2048,1,1])
        print(out.shape)
        print(out.size(0))
        out = out.view(out.size(0), 2048)  #展平变成一个1x1048
        out = self.fc(out)
        return out
if __name__ == '__main__':
    model = Resnet50(1000)  #实例化类
    # 设置x
    x = torch.randn(1,3, 224,224)
    y = model(x)
    print(y.shape)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值