通过 ONNX 从 PyTorch 转换为 CoreML 时缺少权重向量

Posted

技术标签:

【中文标题】通过 ONNX 从 PyTorch 转换为 CoreML 时缺少权重向量【英文标题】:Missing Weight Vectors when converting from PyTorch to CoreML via ONNX 【发布时间】:2018-11-14 07:09:03 【问题描述】:

我正在尝试通过 ONNX 将 PyTorch 模型转换为 CoreML,但是 ONNX-->CoreML 转换缺少权重向量?

我正在关注here 的教程,该教程发表了以下声明:

第 3 步:将模型转换为 CoreML

就像运行convert 函数一样简单。生成的对象是一个 coremltools MLModel 对象,您可以将其保存到文件中并稍后在 XCode 中导入。cml = onnx_coreml.convert(model)

不幸的是,当我尝试这样做时,它失败了。

这是我的代码:

# convert.py
import torch
import torch.onnx
from torch.autograd import Variable

import onnx
from onnx_coreml import convert

from hourglass_model import Hourglass

model_no = 1
torch_model = Hourglass(joint_count=14, size=256)
state_dict = torch.load("hourglass_model_.model".format(model_no))
torch_model.load_state_dict(state_dict)
torch_model.train(False)
torch_model.eval()

# Dummy Input to the model
x = Variable(torch.randn(1,3,256,256,dtype=torch.float32))

# Export the model
onnx_filename = "test_hourglass.onnx"
torch_out = torch.onnx.export(torch_model, x, onnx_filename, export_params=False) 

# Load back in ONNX model
onnx_model = onnx.load(onnx_filename)

# Check that the IR is well formed
onnx.checker.check_model(onnx_model)

# Print a human readable representation of the graph
graph = onnx.helper.printable_graph(onnx_model.graph)
print(graph)

coreml_model = convert(onnx_model,
    add_custom_layers=True,
    image_input_names=["input"], 
    image_output_names=["output"])
coreml_model.save('test_hourglass.mlmodel')

这是print(graph) 行给出的内容。

graph torch-jit-export (
  %0[FLOAT, 1x3x256x256]
  %1[FLOAT, 64x3x5x5]
  %2[FLOAT, 64]
  %3[FLOAT, 64x64x5x5]
  %4[FLOAT, 64]
  %5[FLOAT, 64x64x5x5]
  %6[FLOAT, 64]
  %7[FLOAT, 64x64x5x5]
  %8[FLOAT, 64]
  %9[FLOAT, 64x64x5x5]
  %10[FLOAT, 64]
  %11[FLOAT, 64x64x5x5]
  %12[FLOAT, 64]
  %13[FLOAT, 64x64x5x5]
  %14[FLOAT, 64]
  %15[FLOAT, 64x64x1x1]
  %16[FLOAT, 64]
  %17[FLOAT, 14x64x1x1]
  %18[FLOAT, 14]
) 
  %19 = Conv[dilations = [1, 1], group = 1, kernel_shape = [5, 5], pads = [2, 2, 2, 2], strides = [1, 1]](%0, %1, %2)
  %20 = Relu(%19)
  %21 = MaxPool[kernel_shape = [4, 4], pads = [0, 0, 0, 0], strides = [4, 4]](%20)
  %22 = Conv[dilations = [1, 1], group = 1, kernel_shape = [5, 5], pads = [2, 2, 2, 2], strides = [1, 1]](%21, %3, %4)
  %23 = Relu(%22)
  %24 = MaxPool[kernel_shape = [4, 4], pads = [0, 0, 0, 0], strides = [4, 4]](%23)
  %25 = Conv[dilations = [1, 1], group = 1, kernel_shape = [5, 5], pads = [2, 2, 2, 2], strides = [1, 1]](%24, %5, %6)
  %26 = Relu(%25)
  %27 = Conv[dilations = [1, 1], group = 1, kernel_shape = [5, 5], pads = [2, 2, 2, 2], strides = [1, 1]](%26, %7, %8)
  %28 = Relu(%27)
  %29 = Conv[dilations = [1, 1], group = 1, kernel_shape = [5, 5], pads = [2, 2, 2, 2], strides = [1, 1]](%28, %9, %10)
  %30 = Relu(%29)
  %31 = Upsample[height_scale = 4, mode = 'nearest', width_scale = 4](%30)
  %32 = Add(%31, %23)
  %33 = Conv[dilations = [1, 1], group = 1, kernel_shape = [5, 5], pads = [2, 2, 2, 2], strides = [1, 1]](%32, %11, %12)
  %34 = Relu(%33)
  %35 = Upsample[height_scale = 4, mode = 'nearest', width_scale = 4](%34)
  %36 = Add(%35, %20)
  %37 = Conv[dilations = [1, 1], group = 1, kernel_shape = [5, 5], pads = [2, 2, 2, 2], strides = [1, 1]](%36, %13, %14)
  %38 = Relu(%37)
  %39 = Conv[dilations = [1, 1], group = 1, kernel_shape = [1, 1], pads = [0, 0, 0, 0], strides = [1, 1]](%38, %15, %16)
  %40 = Relu(%39)
  %41 = Conv[dilations = [1, 1], group = 1, kernel_shape = [1, 1], pads = [0, 0, 0, 0], strides = [1, 1]](%40, %17, %18)
  %42 = Relu(%41)
  return %42

这是错误信息:

1/24: Converting Node Type Conv
Traceback (most recent call last):
  File "convert.py", line 38, in <module>
    image_output_names=["output"])
  File "/Users/stephenf/Developer/miniconda3/envs/pytorch/lib/python3.6/site-packages/onnx_coreml/converter.py", line 396, in convert
    _convert_node(builder, node, graph, err)
  File "/Users/stephenf/Developer/miniconda3/envs/pytorch/lib/python3.6/site-packages/onnx_coreml/_operators.py", line 994, in _convert_node
    return converter_fn(builder, node, graph, err)
  File "/Users/stephenf/Developer/miniconda3/envs/pytorch/lib/python3.6/site-packages/onnx_coreml/_operators.py", line 31, in _convert_conv
    "Weight tensor:  not found in the graph initializer".format(weight_name,))
  File "/Users/stephenf/Developer/miniconda3/envs/pytorch/lib/python3.6/site-packages/onnx_coreml/_error_utils.py", line 71, in missing_initializer
    format(node.op_type, node.inputs[0], node.outputs[0], err_message)
ValueError: Missing initializer error in op of type Conv, with input name = 0, output name = 19. Error message: Weight tensor: 1 not found in the graph initializer

据我所知,它说权重张量 %1[FLOAT, 64x3x5x5] 丢失。这就是我保存模型的方式:

torch.save(model.state_dict(), "hourglass_model_.model".format(epoch))

ONNX 加载良好 - 这只是我从 ONNX 转换为 CoreML 的步骤。

任何帮助解决这个问题将不胜感激。我确定我做错了很多其他事情,但我现在只需要导出这个东西。

谢谢,

【问题讨论】:

您好,我不是 CoreML 专家,但您在致电 torch.onnx.export 时尝试过 export_params=True 吗?也许这就是问题所在,因为您尝试从 ONNX 而不是火炬模型进行转换 嗨!有任何更新吗? @fr_andres 就是这样。您可以将其发布为答案,以便我接受并为您加分吗? 那太好了^^ 【参考方案1】:

您正在使用export_params=False 调用torch.onnx.export,正如0.3.1 doc 所读取的那样,它正在保存没有实际参数张量的模型架构。最新的文档没有具体说明这一点,但我们可以假设这是由于您遇到的 Weight tensor not found 错误。

export_params=True试试吧,你应该会看到保存的模型的大小是如何显着增加的。

很高兴它有帮助! 安德烈斯

【讨论】:

以上是关于通过 ONNX 从 PyTorch 转换为 CoreML 时缺少权重向量的主要内容,如果未能解决你的问题,请参考以下文章

YOLOv5的pytorch模型文件转换为ONNX文件

PyTorch 1.0 中文官方教程:使用ONNX将模型从PyTorch传输到Caffe2和移动端

pytorch 转tensorflow注意

pytorch模型转换为rknn模型,使用npu推理

对象检测模型 (PyTorch) 到 ONNX:ONNX 推理的空输出

来自 PyTorch 模型的 ONNX 对象,无需导出