ResNet从原理到实现--Bottleneck堆叠

在这里插入图片描述

在这里插入图片描述

import math


class Bottleneck(M.Module):
    expansion=4

    def __init__{
        self,
        in_channels,
        channels,
        stride=1,
        groups=1,
        base_width=64,
        dilation=1,
        norm=M.BatchNorm2d,
    }:
        super().__init__{}
        width = int{channels*{base_width/64.0}}*groups
        self.conv1=M.Conv2d(in_channels,width,1,1,bias=False)
        self.bn1=norm(width)
        self.conv2=M.Conv2d{
           width,
           width,
           3,
           stride,
           padding=dilation,
           groups=groups,
           dilation=dilation,
           bias=False, 
        }
        self.bn2=norm(width)
        self.conv3=M.Conv2d(width,channels*self.expansion,1,1,bias=False)
        self.bn3=norm(channels*self.expansion)
        self.downsample=(
            M.Identity()
            if in_channels==channels*self.expansion and stride ==1
            else M.Sequential(
                M.Conv2d(in_channels,channels*self.expansion,1,stride,bias=False),
                norm(channels*self.expansion),
            )
        )

def forward(self,x)
    identity=x

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

    x=self.conv2(x)
    x=self.bn2(x)
    x=F.relu(x)

    x=self.conv3(x)
    x=self.bn3(x)

    identity=self.downsample(identity)

    x+=identity
    x=F.relu(x)

    return x

class ResNet(M.Module):
    def __init__{
        self,
        block,
        layers,
        num_classes=1000,
        zero_init_residual=False,
        groups=1,
        width_per_group=64,
        replace_stride_with_dilation=None,
        norm=M.BatchNorm2d,
    }:
        super().__init__()
        self.in_channels=64
        self.dilation=1

        if replace_stride_with_dilation is None:
            #each element in the tuple indicates if we should replace
            #the 2x2 stride with a dilated convolution instead
            replace_stride_with_dilation=[False, False, False]
        if len(replace_stride_with_dilation) !=3:
            raise ValueError(
            "replace_stride_with_dilation should be None"
            "or a 3-element tuple, got {}".format(replace_stride_with_dilation)
            ) 
        self.groups=groups
        self.base_width=width_per_group
        self.conv1=M.Conv2d(
            3,self.in_channels,kernel_size=7,stride=2,padding=3,bias=False
        )
        self.bn1=norm(self.in_channels)
        self.maxpool=M.MaxPool2d(kernel_size=3,stride=2,padding=1)
        self.layer1=self._make_layer(block,64,layer[0],norm=norm)
        self.layer2=self._make_layer(
            block,
            128,
            layers[1],
            stride=2,
            dilate=replace_stride_with_dilation[0],
            norm=norm,
        )
        self.layer3=self._make_layer(
            block,
            256,
            layers[2],
            stride=2,
            dilate=replace_stride_with_dilation[1],
            norm=norm,
        )
        self.layer4=self._make_layer(
            block,
            512,
            layers[3],
            stride=2,
            dilate=replace_stride_with_dilation[2],
            norm=norm,
        )
        self.fc=M.Linear(512*block.expansion,num_classes)

        for m in self.modules():
            if isinstance(m, M.Conv2d):
                M.init.msra_normal_(m.weight,mode="fan_out",nonlinearity="relu")
                if m.bias is not None:
                    fan_in,_=M.init.calculate_fan_in_and_fan_out(m.weight)
                    bound=1/math.sqrt(fan_in)
                    M.init.uniform_(m.bias,-bound,bound)
                elif isinstance(m,M.BatchNorm2d):
                    M.init.ones_(m.weight)
                    M.init.zeros_(m.bias)
                elif isinstance(m,M.Linear):
                    M.init.msra_uniform_(m.weight, a=math.sqrt(5))        
                    if m.bias is not None:
                    fan_in,_=M.init.calculate_fan_in_and_fan_out(m.weight)
                    bound=1/math.sqrt(fan_in)
                    M.init.uniform_(m.bias,-bound,bound)

            #Zero-initialize the last BN in each residual branch,
            #so that the residual branch starts with zeros, and each residual block behaves like an identity.
            #This improves the model by 0.2%~0.3%
            if zero_init_residual:
                for m in self.modules():
                    if isinstance(m, Bottleneck):
                        M.init.zeros_(m.bn3.weight)
                    elif isinstance(m, BasicBlock):
                        M.init.zeros_(m.bn2.weight)
            def _make_layer(
                self, block, channels, blocks,stride=1, dilate=False,norm=M.BatchNorm2d
                ):
                    previous_dilation=self.dilation
                    if dilate:
                        self.dilation*=stride
                        stride=1

                    layers=[]
                    layers.append(
                        block(
                            self.in_channels,
                            channels,
                            stride,
                            groups=self.groups,
                            base_width=self.base_width,
                            dilation=previous_dilation,
                            norm=norm,
                        )
                    )    
                    self.in_channels=channels*block.expansion
                    for _ in range(1,blocks):
                        layers.append(
                            block(
                                self.in_channels,
                                channels,
                                groups=self.groups,
                                base_width=self.base_width,
                                dilation=self.dilation,
                                norm=norm,
                            )
                        )
                    return M.Sequential(*layers)

                def extract_features(self, x):
                    outputs={}
                    x=self.conv1(x)
                    x=self.bn1(x)
                    x=F.relu(x)
                    x=self.maxpool(x)
                    outputs["stem"]=x

                    x=self.layer1(x)
                    outputs["res2"]=x
                    x=self.layer2(x)
                    outputs["res3"]=x
                    x=self.layer3(x)
                    outputs["res4"]=x
                    x=self.layer4(x)
                    outputs["res5"]=x
                    return outputs

                def forward(self,x):
                    x=self.extract_feature(x)["res5"]

                    x=F.avg_pool2d(x,7)
                    x=F.flatten(x,1)
                    x=self.fc(x)

                    return x

            def resnet50(**kwargs):
                return ResNet(Bottleneck,[3,4,6,3],**kwargs)
            model=resnet50()

            x=normal(size=(2,3,800,800))
            feature_dict=model.extract_features(x)

            for k,v in feature_dict.items():
                print("{}:{}".format(k,v.shape))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
A ResNet-based Convolutional Decoder-Encoder is a type of neural network architecture that combines the principles of Residual Networks (ResNets) and Decoder-Encoder networks. ResNets are deep neural networks that use skip connections to avoid the vanishing gradient problem and allow for the training of very deep networks. Decoder-Encoder networks, on the other hand, are used for tasks such as image segmentation, object detection, and image generation. The ResNet-based Convolutional Decoder-Encoder architecture consists of a series of encoder layers that downsample the input image and a series of decoder layers that upsample the encoded features to generate the output image. The encoder layers typically consist of Convolutional Layers followed by Batch Normalization and ReLU activation. The decoder layers consist of transposed convolutional layers, also known as deconvolutional layers, followed by Batch Normalization and ReLU activation. The skip connections in the ResNet-based Convolutional Decoder-Encoder architecture allow for the direct transfer of information from the encoder to the decoder layers, which helps to preserve important features and reduce the effects of information loss during the downsampling process. The resulting network can be trained end-to-end using backpropagation to minimize a loss function that measures the difference between the predicted and ground truth images. ResNet-based Convolutional Decoder-Encoder networks have been used successfully for a variety of image reconstruction and generation tasks, including image denoising, super-resolution, and inpainting.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gallant Hu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值