【问题标题】:AttributeError: 'Conv3d' object has no attribute 'padding_mode'AttributeError:“Conv3d”对象没有属性“padding_mode”
【发布时间】:2020-09-05 07:29:49
【问题描述】:

您好,任何可能看到此内容的人。我正在尝试将 c3d 模型转换为 onnx 以用于一般用途。

C3D 训练代码取自:https://github.com/jfzhang95/pytorch-video-recognition

所以 c3d 模型的原始 env 是 torch=0.4.0 python=3.5.2 所要求的 README.MD。

from network import C3D_model
import torch
import torch.onnx

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("Device being used:", device)


def test():

    model = C3D_model.C3D(num_classes=2)
    checkpoint = torch.load('C3D-ucf101_epoch-3.pth.tar',
                        map_location=lambda storage, loc: storage)
    model.load_state_dict(checkpoint['state_dict'])
    torch.save(model, 'c3d_thunder.pkl')
    dummy_input1 = torch.randn(1, 3, 16, 112, 112)
    torch.onnx.export(model, dummy_input1, "C3D.onnx", verbose=True)

if __name__ == "__main__":
    test()

一开始我尝试在原始环境下转换这个模型,但是遇到了这个错误:

Traceback (most recent call last):
  File "/workspace/pytorch-video-recognition/model2onnx.py", line 31, in <module>
    test()
  File "/workspace/pytorch-video-recognition/model2onnx.py", line 27, in test
    torch.onnx.export(model, dummy_input1, "C3D.onnx", verbose=True)
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/__init__.py", line 25, in export
    return utils.export(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py", line 84, in export
    _export(model, args, f, export_params, verbose, training, input_names, output_names)
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py", line 154, in _export
    _onnx_opset_version, defer_weight_export)
RuntimeError: ONNX export failed: Couldn't export operator aten::max_pool3d

经过一番努力寻找解决方案,我决定将整个模型保存到 .pkl 并在 torch=1.1.0 下重新加载。它适用于加载模型,但在转换中仍然存在一些问题。

import torch
import torch.onnx

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


def test():
    PATH = 'c3d_thunder.pkl'
    model = torch.load(PATH)
    dummy_input1 = torch.randn(1, 3, 16, 112, 112)
    torch.onnx.export(model, dummy_input1, "C3D.onnx", verbose=True)


if __name__ == "__main__":
    test()

还是报错:

Traceback (most recent call last):
  File "C:/Users/google/Desktop/onnx_test/t1.py", line 33, in <module>
    test()
  File "C:/Users/google/Desktop/onnx_test/t1.py", line 29, in test
    torch.onnx.export(model, dummy_input1, "C3D.onnx", verbose=True)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\onnx\__init__.py", line 25, in export
    return utils.export(*args, **kwargs)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\onnx\utils.py", line 131, in export
    strip_doc_string=strip_doc_string)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\onnx\utils.py", line 363, in _export
    _retain_param_name, do_constant_folding)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\onnx\utils.py", line 266, in _model_to_graph
    graph, torch_out = _trace_and_get_graph_from_model(model, args, training)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\onnx\utils.py", line 225, in _trace_and_get_graph_from_model
    trace, torch_out = torch.jit.get_trace_graph(model, args, _force_outplace=True)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\jit\__init__.py", line 231, in get_trace_graph
    return LegacyTracedModule(f, _force_outplace, return_inputs)(*args, **kwargs)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\jit\__init__.py", line 294, in forward
    out = self.inner(*trace_inputs)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\nn\modules\module.py", line 491, in __call__
    result = self._slow_forward(*input, **kwargs)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\nn\modules\module.py", line 481, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "C:\Users\google\Desktop\onnx_test\network\C3D_model.py", line 46, in forward
    x = self.relu(self.conv1(x))
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\nn\modules\module.py", line 491, in __call__
    result = self._slow_forward(*input, **kwargs)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\nn\modules\module.py", line 481, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\nn\modules\conv.py", line 468, in forward
    if self.padding_mode == 'circular':
  File "D:\Anaconda3\envs\torch_learn\lib\site-packages\torch\nn\modules\module.py", line 539, in __getattr__
    type(self).__name__, name))
AttributeError: 'Conv3d' object has no attribute 'padding_mode'

谁能告诉我如何解决这个问题?感谢您的宝贵时间!~

【问题讨论】:

  • 你应该发布你的代码,而不是多张图片,这样会更容易理解。
  • 对不起,我已经更新了代码和错误内容。

标签: pytorch onnx


【解决方案1】:

您是否尝试过使用最新版本的 PyTorch?

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多