ResNet代码实现

        import torch
        import torch.nn as nn
        from torch.hub import load_state_dict_from_url    
        model_urls = {
            'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
            'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
            'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
            'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
            'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
            'resnext50_32x4d': 'https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth',
            'resnext101_32x8d': 'https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth',
            'wide_resnet50_2': 'https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pth',
            'wide_resnet101_2': 'https://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth',
        }
# 封装标准卷积
def conv3x3(in_planes, out_planes, stride=1,padding=1):
   return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=padding, bias=False)
        
def conv1x1(in_planes, out_planes, stride=1):
    # 当卷积层后面跟着bn层时,卷积层不需要bias
    # 因为在bn层重新学习均值和方差
   return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
class BasicBolck(nn.Module):

    # 通道放大倍数
    expansion = 1
    def __init__(self, inplanes, planes, stride=1, downsample=None, norm_layer=None):

    # 调用父类的初始化函数
    super(BasicBlock, self).__init__()

    # 如果没有指定Batchnormalization
    if norm_layer is None:
      # 使用标准的BatchNorm
      norm_layer = nn.BatchNorm2d

    self.conv1 = conv3x3(inplanes, planes, stride)
    self.bn1 = norm_layer(planes)
    self.relu = nn.ReLU(inplace=True)
    self.conv2 = conv3x3(planes, planes)
    self.bn2 = norm_layer(planes)
    self.downsample = downsample
    self.stride = stride

  # forward用来调用,x是输入
  def forward(self, x):
    indentity = x
    out = self.conv1(x)
    out = self.bn1(out)
    out = self.relu(out)

    out = self.conv2(out)
    out = self.bn2(out)
    # 下采样
    if self.downsample is not None:
      identity = self.downsample(x)

    # 为什么out不下采样呢?stride=1就不下采样,stride=2已经下采样了

    # f(x)+x
    out += identity
    # 在加和之后再调用激活函数
    out = self.relu(out)

    return out
class BottleNeck(nn.Module):

  # 放大倍数
  expansion = 4

  def __init__(self, inplanes, planes, stride=1, downsample=None, norm_layer=None):
    super(BottleNeck, self).__init___()
    if norm_layer is None:
      norm_layer = nn.BatchNorm2d
    
    self.conv1 = conv1x1(inplanes, planes)
    self.bn1 = norm_layer(planes)
    self.conv2 = conv3x3(planes, planes)
    self.bn2 = norm_layer(planes)
    self.conv3 = conv1x1(planes, planes * self.expansion)
    self.bn3 = norm_layer(planes * self.expansion)
    self.relu = nn.ReLU(inplace=True)
    self.downsample = downsample
    self.stride = stride
  
  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.downasample is not None:
      identity = self.downsample(x)
    
    out += identity
    out = self.relu(out)

    return out
class ResNet(nn.Module):
  # num_class 分多少类
  def __init__(self, block, layers, num_class=1000, norm_layer=None):
    super(ResNet, self).__init__()
    if norm_layer is None:
      norm_layer = nn.BatchNorm2d
    
    self.inplanes = 64
    self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False)
    self.bn1 = norm_layer(self.inplanes)
    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])
    self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
    self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
    self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
    # 平均池化
    self.avgpool = nn.AdaptiveAvgPool2d((1,1))
    # 分类层
    self.fc = nn.Linear(512*block.expansion, num_class)

    # 参数初始化
    for m in self.modules():
      # 如果是卷积层
      if isinstance(m, nn.Conv2d):
        # 凯明初始化 
        # 主要的应用场景是搭配RElu激活函数
        nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
      # 如果是Batch层
      elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
        # 常量初始化 把权重和误差初始化为0,1
        nn.init.constant_(m.weight, 1)
        nn.init.constant_(m.bias, 0)

  def _make_layer(self, block, stride=1):
    # 生成stage里的内容
    norm_layer = self._norm_layer
    downsample = None

    # 判断是否需要下采样 stride!=1发生下采样
    if stride != 1 or self.inplanes != planes*bloack.expansion:
      # downsample 用1x1的卷积来调整维度
      # downsample是一个小的网络,由1x1卷积层和normlazition组成
      downsample = nn.Sequential(conv1x1(self.inplanes, planes*block.expansion, stride),norm_layer(planes*block.expansion))
    
    layers=[]
    layers.append(block(self.inplanes, planes, stride, downsample, norm_layer))
    self.inplanes = planes * self.expansion
    for _ in range(1, blocks):
      layer.append(block(self.inplanes, planes, norm_layer=norm_layer))
    # *list 是一个语法, 对list降维
    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)
    x = self.layer4(x)
    x = self.avgpool(x)
    # 展平
    x = torch.flattern(x,1)
    x = self.fc(x)

    return x
# 封装预加载训练
def _resnet(arch, block, layers, pretrained, progress, **kwargs):
  model = ResNet(block, layers, **kwargs)
  # 如果需要预训练
  if pretrained:
    state_dict = load_state_dict_from_url(model_urls[arch], progress=progress)
    model.load_state_dict(state_dict)

  return model
def resnet(pretrained=False, progress=True, **kwargs):
  return _resnet('resnet152', BottleNeck, [3,8,36,3], pretrained, progress, **kwargs)
model = resnet152(pretrained = True)
model.eval()

你可能感兴趣的:(ResNet代码实现)