【问题标题】:Pytorch unable to export trained model as ONNXPytorch 无法将训练好的模型导出为 ONNX
【发布时间】:2021-10-04 11:24:18
【问题描述】:

我一直在使用多个卷积层(3x3,步幅 1,填充相同)在 Pytorch 框架中训练模型。该模型表现良好,我想在 Matlab 中使用它进行推理。为此,框架之间用于 NN 交换的 ONNX 格式似乎是(唯一的?)解决方案。可以使用以下命令导出模型:

torch.onnx.export(net.to('cpu'), test_input,'onnxfile.onnx')

这是我的 CNN 架构定义:

class Encoder_decoder(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.Sequential(
        nn.Conv2d(2,8, (3, 3),stride = 1, padding='same'),
        nn.ReLU(),
        nn.Conv2d(8,8, (3, 3),stride = 1, padding='same'),
        nn.ReLU(),
        nn.Conv2d(8,16, (3, 3),stride = 1, padding='same'),
        nn.ReLU(),
        nn.Conv2d(16,16, (3, 3),stride = 1, padding='same'),
        nn.ReLU(),
        nn.Conv2d(16,32, (3, 3),stride = 1, padding='same'),
        nn.ReLU(),
        nn.Conv2d(32,32, (3, 3),stride = 1, padding='same'),
        nn.ReLU(),
        nn.Conv2d(32,64, (3, 3),stride = 1, padding='same'),
        nn.ReLU(),
        nn.Conv2d(64,64, (3, 3),stride = 1, padding='same'),
        nn.ReLU(),
        nn.Conv2d(64,128, (3, 3),stride = 1, padding='same'),
        nn.ReLU(),
        nn.Conv2d(128,128, (3, 3),stride = 1, padding='same'),
        nn.ReLU(),
        nn.Conv2d(128,1, (1, 1))
        )


    def forward(self, x):
        x = self.model(x)
        
        return x

但是,当我运行 torch.onnx.export 命令时,我收到以下错误:

RuntimeError: Exporting the operator _convolution_mode to ONNX opset version 9 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub.

我已尝试更改 opset,但这并不能解决问题。 ONNX 完全支持卷积神经网络。另外,我正在 google colab 中训练网络。

你知道将模型转移到matlab的其他方法吗?

【问题讨论】:

    标签: python-3.x matlab pytorch conv-neural-network onnx


    【解决方案1】:

    目前,_convolution_mode 操作员 isn't supported 在 pytorch 中。这是由于使用了padding='same'

    您需要将填充更改为整数值或将其更改为等效值。咨询Same padding equivalent in Pytorch

    【讨论】:

      猜你喜欢
      • 2021-04-06
      • 2021-09-01
      • 2019-06-12
      • 2019-11-10
      • 1970-01-01
      • 2019-11-06
      • 2021-09-02
      • 1970-01-01
      • 2022-10-13
      相关资源
      最近更新 更多