【问题标题】:How to create new layer for Resnet?如何为 Resnet 创建新层?
【发布时间】:2021-06-09 18:26:41
【问题描述】:

我通过为 CIFAR10 调整和微调 ResNet18 来练习迁移学习。我想用新的 fc 层替换最后一个 fc 层。所以,我想创建一个新层,但我没有。如何创建新图层?

下载 Resnet18

OrigResNet18 = None
OrigResNet18 = torch.hub.load('pytorch/vision:v0.9.0', 'resnet18', pretrained=True)

快速层

(fc):线性(in_features=512,out_features=1000,bias=True)

我试过了,但我不确定这段代码:

num_in_features=OrigResNet18.fc.in_features
num_out_features=OrigResNet18.fc.out_features
NewResNet18.conv1=nn.Conv2d(in_channels=1,out_channels=16, kernel_size= 
(3,3)) 
NewResNet18.fc=nn.Linear(in_features=num_in_features,out_features=num_out_features)

我有错误

    ---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-58-f8fe748d1e75> in <module>()
     32 NewResNet18 = NewResNet18.to(device)
     33 epochs = 1
---> 34 loss_history = train(NewResNet18, criterion, optimizer, epochs, trainloader)

6 frames
<ipython-input-57-a35bfc25b940> in train(model, criterion, optimizer, epochs, dataloader, verbose)
     19 
     20       # Obtain the scores
---> 21       outputs = model(inputs)
     22 
     23       # Calculate loss

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

/usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py in forward(self, x)
    247 
    248     def forward(self, x: Tensor) -> Tensor:
--> 249         return self._forward_impl(x)
    250 
    251 

/usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py in _forward_impl(self, x)
    230     def _forward_impl(self, x: Tensor) -> Tensor:
    231         # See note [TorchScript super()]
--> 232         x = self.conv1(x)
    233         x = self.bn1(x)
    234         x = self.relu(x)

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in forward(self, input)
    397 
    398     def forward(self, input: Tensor) -> Tensor:
--> 399         return self._conv_forward(input, self.weight, self.bias)
    400 
    401 class Conv3d(_ConvNd):

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    394                             _pair(0), self.dilation, self.groups)
    395         return F.conv2d(input, weight, bias, self.stride,
--> 396                         self.padding, self.dilation, self.groups)
    397 
    398     def forward(self, input: Tensor) -> Tensor:

RuntimeError: Given groups=1, weight of size [16, 1, 3, 3], expected input[8, 3, 224, 224] to have 1 channels, but got 3 channels instead

【问题讨论】:

  • 通常如果我想对已经存在的架构进行更改,我必须从头开始重新制作。由于您没有看到所有抽象,对导入进行更改会带来很多问题。 Resnet18 并不太复杂,所以如果你决定走这条路,希望它不会有太大的问题。

标签: python neural-network pytorch conv-neural-network


【解决方案1】:

错误来自这一行:

NewResNet18.conv1=nn.Conv2d(in_channels=1,out_channels=16, kernel_size= 
(3,3)) 

您将第一个卷积更改为具有 1 个输入通道(即灰度图像),但您正在为其提供 3 通道图像(即 RGB 图像)。

如果你只是想改变分类器的大小,你可以使用:

num_in_features=OrigResNet18.fc.in_features
num_out_features=OrigResNet18.fc.out_features
NewResNet18.fc=nn.Linear(in_features=num_in_features,out_features=num_out_features)

【讨论】:

    猜你喜欢
    • 2020-05-02
    • 2017-05-15
    • 2017-06-09
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    相关资源
    最近更新 更多