pytorch从零实现resnet50

pytorch从零实现resnet_pytorch实现resnet_两只蜡笔的小新的博客-CSDN博客

前言:

之前博主写过一个ResNet34, ResNet18的实现方法,对于ResNet50的实现方法有点不太一样,之前的实现方法参考上面的链接。下面介绍ResNet50的实现方法。

基本结构示意图

 发现ResNet50,其基本模块是三个,1*1 3*3 1*1 的卷积层,在向前推进的时候,需要特征图的通道数降维,所以与ResNet34不同的地方是BasicBlock,和make_layer

二、构建BasicBlock

import torch
import torch.nn as nn
import torch.nn.functional as F

# 定义残差块
class ResidualBlock(nn.Module):
    def __init__(self, in_channels, out_channels, stride=1, downsample=None):
        super(ResidualBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(out_channels)
        self.conv3 = nn.Conv2d(out_channels, out_channels * 4, kernel_size=1, stride=1, padding=0, bias=False)
        self.bn3 = nn.BatchNorm2d(out_channels * 4)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample

    def forward(self, x):
        identity = 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)
        if self.downsample is not None:
            identity = self.downsample(x)
        out += identity
        out = self.relu(out)
        return out

三、残差块的实现,

由于renset残差单元可能连接两个不同维度的特征图,所以要接一个降采样操作self.downsample = shortcut,有没有取决于输入维度与输出维度是否相同,还取决于特征图的尺寸是否发生变化。

    def _make_layer(self, out_channels, blocks, stride=1):
        downsample = None
        if stride != 1 or 64 != out_channels * 4:
            downsample = nn.Sequential(
                nn.Conv2d(64, out_channels * 4, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(out_channels * 4)
            )
        layers = []
        layers.append(ResidualBlock(64, out_channels, stride, downsample))
        for i in range(1, blocks):
            layers.append(ResidualBlock(out_channels * 4, out_channels))
        return nn.Sequential(*layers)

四、下面构造类class ResNet50(nn.Module)

1.构造类方法1

 定义 ResNet50 模型
class ResNet50(nn.Module):
    def __init__(self, num_classes=1000):
        super(ResNet50, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(64, 3)
        self.layer2 = self._make_layer(128, 4, stride=2)
        self.layer3 = self._make_layer(256, 6, stride=2)
        self.layer4 = self._make_layer(512, 3, stride=2)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * 4, num_classes)

    def _make_layer(self, out_channels, blocks, stride=1):
        downsample = None
        if stride != 1 or 64 != out_channels * 4:
            downsample = nn.Sequential(
                nn.Conv2d(64, out_channels * 4, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(out_channels * 4)
            )
        layers = []
        layers.append(ResidualBlock(64, out_channels, stride, downsample))
        for i in range(1, blocks):
            layers.append(ResidualBlock(out_channels * 4, out_channels))
        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)

你可能感兴趣的:(源码简介-pytorch,pytorch,深度学习,人工智能)