PyTorch - 如何手动推导 ResNet?

PyTorch - How to derive manually ResNet?

我设法找到了 ResNet-18 的实现:

def __init__(self): 
    super(ResNet18,self).__init__()
    self.block1 = nn.Sequential(
        nn.Conv2d(1,64,kernel_size=2,stride=2,padding=3,bias=False),
        nn.BatchNorm2d(64),
        nn.ReLU(True)
    )
    self.block2 = nn.Sequential(
       nn.MaxPool2d(1,1),
       ResidualBlock(64,64),
       ResidualBlock(64,64,2)
    )
    self.block3 = nn.Sequential(
        ResidualBlock(64,128),
        ResidualBlock(128,128,2)
    )
    self.block4 = nn.Sequential(
        ResidualBlock(128,256),
        ResidualBlock(256,256,2)
    )
    self.block5 = nn.Sequential(
        ResidualBlock(256,512),
        ResidualBlock(512,512,2)
    )
    self.avgpool = nn.AvgPool2d(2) 
    # vowel_diacritic 
    self.fc1 = nn.Linear(512,11)
    # grapheme_root
    self.fc2 = nn.Linear(512,168)
    # consonant_diacritic
    self.fc3 = nn.Linear(512,7)

def forward(self,x):
    x = self.block1(x)
    x = self.block2(x)
    x = self.block3(x)
    x = self.block4(x)
    x = self.block5(x)
    x = self.avgpool(x)
    x = x.view(x.size(0),-1)
    x1 = self.fc1(x)
    x2 = self.fc2(x)
    x3 = self.fc3(x)
    return x1,x2,x3

class ResidualBlock(nn.Module):

    def __init__(self,in_channels,out_channels,stride=1,kernel_size=3,padding=1,bias=False):
        super(ResidualBlock,self).__init__()
        self.cnn1 = nn.Sequential(nn.Conv2d(in_channels,out_channels,kernel_size,stride,padding,bias=False), nn.BatchNorm2d(out_channels), nn.ReLU(True) )

        self.cnn2 = nn.Sequential( nn.Conv2d(out_channels,out_channels,kernel_size,1,padding,bias=False), nn.BatchNorm2d(out_channels) ) 

        if stride != 1 or in_channels != out_channels: 
            self.shortcut = nn.Sequential( nn.Conv2d(in_channels,out_channels,kernel_size=1,stride=stride,bias=False), nn.BatchNorm2d(out_channels) ) 
        else: 
            self.shortcut = nn.Sequential() 

    def forward(self,x): 
        residual = x 
        x = self.cnn1(x) 
        x = self.cnn2(x) 
        x += self.shortcut(residual) 
        x = nn.ReLU(True)(x) 
        return x

我还是 PyTorch 的新手,我正在尝试以与上述 ResNet18 类似的格式获取 ResNets 34、50 和 101 的架构。最初我认为我必须做的唯一更改是根据 https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py 处的源 GitHub 代码每个块的最后一个参数,其中对于 ResNet34 我们有 return _resnet('resnet34', BasicBlock, [3, 4, 6, 3], pretrained, progress, **kwargs)

我做错了吗?我仍在阅读文档并试图理解这些方法,因此将非常感谢一些指导。 :')

来自具有 FastAi 风格的 TensorFlow 背景:如果直接从 torchvision 模块导入它们,则可以使用所有这些 ResNet 变体:

#Download the pretrained model
import torch
import torchvision.models as models
model = models.resnet18(pretrained = True) #change here to whatever model you want

#Switch device to gpu if available
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")