Pytorch 无法将训练好的模型导出为 ONNX

2024-01-10

我一直在使用多个卷积层(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 的其他方法吗?


现在,_convolution_mode操作员不支持 https://pytorch.org/docs/stable/onnx.html#supported-operators在 pytorch 中。这是由于使用padding='same'.

您需要将填充更改为整数值或将其更改为其等效值。咨询Pytorch 中相同的填充等效项 https://discuss.pytorch.org/t/same-padding-equivalent-in-pytorch/85121.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pytorch 无法将训练好的模型导出为 ONNX 的相关文章

随机推荐