将 pytorch 模型转换为 core-ml 时出错

Error while converting pytorch model to core-ml

C = torch.cat((A,B),1)

张量形状:

A is (1, 128, 128, 256)
B is (1, 1, 128, 256)

预期 C 值为 (1, 129, 128, 256)

此代码适用于 pytorch,但在转换为 core-ml 时出现以下错误:

"Error while converting op of type: {}. Error message: {}\n".format(node.op_type, err_message, )
TypeError: Error while converting op of type: Concat. Error message: unable to translate constant array shape to CoreML shape"

这是 coremltools 版本相关的问题。尝试使用最新的 beta coremltools 3.0b2。

以下在最新的测试版中没有任何错误。

import torch

class cat_model(torch.nn.Module):
    def __init__(self):
        super(cat_model, self).__init__()

    def forward(self, a, b):
        c = torch.cat((a, b), 1)
        # print(c.shape)
        return c

a = torch.randn((1, 128, 128, 256))
b = torch.randn((1, 1, 128, 256))

model = cat_model()
torch.onnx.export(model, (a, b), 'cat_model.onnx')

import onnx
model = onnx.load('cat_model.onnx')
onnx.checker.check_model(model)
print(onnx.helper.printable_graph(model.graph))

from onnx_coreml import convert
mlmodel = convert(model)