【问题标题】:Re-using a classification CNN model for autoencoding - pytorch重新使用分类 CNN 模型进行自动编码 - pytorch
【发布时间】:2020-06-07 10:40:43
【问题描述】:

我对 pytorch 很陌生,所以我需要一些帮助。我正在尝试重用旧的 CNN 分类模型——重用已经训练好的卷积层作为自动编码器中的编码器,然后训练解码器层。下面的代码就是我所拥有的。

class Autoencoder(nn.Module):
  def __init__(self, model, specs):

    super(Autoencoder, self).__init__()

    self.encoder = nn.Sequential(
        *list(model.conv_layer.children())
        )

    self.decoder = nn.Sequential(
        nn.ConvTranspose2d(in_channels=C7, out_channels=C6, kernel_size=pooling, padding=0),
        nn.ReLU(inplace=True),
        nn.ConvTranspose2d(in_channels=C6, out_channels=C5, kernel_size=pooling, padding=0),
        nn.ReLU(inplace=True),
        nn.ConvTranspose2d(in_channels=C5, out_channels=C4, kernel_size=pooling, padding=0),
        nn.ReLU(inplace=True),
        nn.ConvTranspose2d(in_channels=C4, out_channels=C3, kernel_size=pooling, padding=0),
        nn.ReLU(inplace=True),
        nn.ConvTranspose2d(in_channels=C3, out_channels=C2, kernel_size=pooling, padding=0),
        nn.ReLU(inplace=True),
        nn.ConvTranspose2d(in_channels=C2, out_channels=C1, kernel_size=pooling, padding=0),
        nn.ReLU(inplace=True), 
        nn.ConvTranspose2d(in_channels=C1, out_channels=C0, kernel_size=pooling, padding=0),
        nn.ReLU(inplace=True), 
        nn.ConvTranspose2d(in_channels=C0, out_channels=3, kernel_size=pooling, padding=0),
        nn.ReLU(inplace=True),       
        )
    for param in self.encoder.parameters():
      param.requires_grad = False

    for p in self.decoder.parameters():
      if p.dim() > 1:
        nn.init.kaiming_normal_(p)
        pass

    def forward(self, x):
      x = self.encoder(x)
      x = self.decoder(x)
      return x


但是,我收到“NotImplementedError”。我究竟做错了什么?当我启动该类的一个实例时,我将传递预训练的 CNN 分类模型,并且 self.encoder 应该负责从模型中获取我感兴趣的层(conv_layer 中的那些)。当我:

model = pretrainedCNNmodel
autoencoder = Autoencoder(model, specs)
print(autoencoder)

打印看起来不错,它包含所有层和我希望的所有内容,但是当我尝试对其进行训练时,我得到了“NotImplementedError:”。

编辑

这是整个错误:


---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-20-9adc467b2472> in <module>()
      2 optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, weight_decay=L2_lambda)
      3 
----> 4 train(x, train_loader, test_loader, optimizer, criterion)

2 frames
<ipython-input-5-b25edb14cf5f> in train(model, train_loader, test_loader, optimizer, criterion)
     15       data, target = data.cuda(), target.cuda()
     16       optimizer.zero_grad()
---> 17       output = model(data)
     18       loss = criterion(output, target)
     19       loss.backward()

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in forward(self, *input)
     94             registered hooks while the latter silently ignores them.
     95         """
---> 96         raise NotImplementedError
     97 
     98     def register_buffer(self, name, tensor):

NotImplementedError: 

【问题讨论】:

标签: python pytorch conv-neural-network autoencoder


【解决方案1】:

由于您在此问题上获得了赏金,因此无法关闭。但是,this thread 已经提出并回答了确切相同的问题。

基本上,您的代码中存在缩进问题:您的forward 方法被缩进,使其您的__init__ 方法中,而不是作为Autoencoder 类的一部分。

详情请见my other answer

【讨论】:

  • 谢谢你,成功了。我不敢相信是这样的。我四处寻找,错误就在我面前。一旦我被允许分配赏金,我会把它给你
猜你喜欢
  • 2020-10-31
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
  • 2020-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多