resnet代码详解

class Bottleneck(nn.Module):
    //一个残差块的结构。第一个1*1卷积会缩小通道数,第二个3*3卷积,第三个1*1卷积会变回原来的通道数
    expansion = 4

    //inplanes:输入通道; planes*expansion:输出通道; 
    def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None):
        super(Bottleneck, self).__init__()
        //第一个1*1卷积,stride一定为1
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
        self.bn1 = BatchNorm2d(planes)
        //第二个3*3卷积,stride不一定为1,可以有空洞
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
                               dilation=dilation, padding=dilation, bias=False)
        self.bn2 = BatchNorm2d(planes)
        //第三个1*1卷积,stride一定为1,通道数扩大4倍
        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = BatchNorm2d(planes * 4)
        self.relu = nn.ReLU(inplace=True)
        //下采样通道数
        self.downsample = downsample
        self.stride = stride
        self.dilation = dilation

    def forward(self, x):
        residual = 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)

        //如果输入residual与经过1*1,3*3,1*1之后的输出out通道数不同,对输入residual进行下采样,再与输出相加
        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

 

class ResNet(nn.Module):
    //nInputChannels为输入通道,block为待传的Bottleneck
    def __init__(self, nInputChannels, block, layers, os=16, pretrained=False):
        //self.inplanes是传给self._make_layer函数的输入通道数
        self.inplanes = 64
        super(ResNet, self).__init__()
        //在不同的os下,四个layer的参数,output stride为该矩阵经过多次卷积pooling操作后,尺寸缩小的值,例如:
input image为224 * 224,经过多次卷积pooling操作后,feature map为7*7,那么output stride为224/7 = 32.
        //这里的blocks指的是每个layer中的残差块个数
        if os == 16:
            strides = [1, 2, 2, 1]
            dilations = [1, 1, 1, 2]
            blocks = [1, 2, 4]
        elif os == 8:
            strides = [1, 2, 1, 1]
            dilations = [1, 1, 2, 2]
            blocks = [1, 2, 1]
        else:
            raise NotImplementedError

        # Modules
        //输入通道数为nInputChannels,输出为64
        self.conv1 = nn.Conv2d(nInputChannels, 64, kernel_size=7, stride=2, padding=3,
                                bias=False)
        self.bn1 = BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

        self.layer1 = self._make_layer(block, 64, layers[0], stride=strides[0], dilation=dilations[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=strides[1], dilation=dilations[1])
        self.layer3 = self._make_layer(block, 256, layers[2], stride=strides[2], dilation=dilations[2])
        self.layer4 = self._make_layer(block, 512, blocks=blocks, stride=strides[3], dilation=dilations[3])

        self._init_weight()

        if pretrained:
            self._load_pretrained_model()

    //block是待传递的bottlenet,planes为输入通道数,不同的layers分别为64、128、156,blocks指的是每个layer中的残差块
    //个数,不为1的stride传入会用某一个layer每个bottlenet中的3*3卷积进行下采样
    def _make_layer(self, block, planes, blocks, stride=1, dilation=1):
        downsample = None
        //如果马上使用的bottlenet中3*3卷积的stride不是1(意思是需要在这个layer的第一层进行一次图片尺寸的下采样)或者
        //输出通道数planes*expansion与inplanes不同(意思是如果每个layer的输出通道数planes*expansion和输入通道数不同
        //(输入通道数第一次是64,第二次是64*4,第三次是128*4)
        if stride != 1 or self.inplanes != planes * block.expansion:
            //这个函数在这里没有使用,只是定义了一下。对图片尺寸进行下采样,另外把输入通道数inplanes变成输出通道数
            //planes * block.expansion
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),
                BatchNorm2d(planes * block.expansion),
            )

        layers = []
        //在这里调用第一个Bottleneck(),形成第一个block,每个layer是多个block组成,但是只对第一个block传入stride
        //(只对第一个block可能进行下采样),当layer的第一个block的输入通道inplanes不等于输出通道planes*expansion,
        //进行downsample。第一层输入为64,经过1*1卷积,3*3卷积,1*1卷积后输出64*4,输入和输出不同维度
        //(另外如果输出是经过了stride下采样,输出图片尺寸也和输入不同)不能相加,所以在downsample中对输入进行
        //stride下采样,对输入图片的通道数进行升维,从64变到64*4 
        layers.append(block(self.inplanes, planes, stride, dilation, downsample))
        self.inplanes = planes * block.expansion
        //对每个layer中的除过第一个bottlenet外的bottlenet们,stride为默认值1,不进行下采样,另外因为已经定义了
        //self.inplanes = planes * block.expansion,所以剩余的这些block的输入通道和输出通道是一样的,所以不再使用
        //downsample()
        for i in range(1, blocks):
        layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)


    def forward(self, input):
        x = self.conv1(input)
        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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值