使用自定义输入将onnx模型转换为mlmodel时出错

时间:2019-01-13 15:58:33

标签: deep-learning pytorch coreml mlmodel onnx-coreml

我正在尝试将Pytorch模型转换为.mlmodel。所以我先将.pth文件转换为.onnx文件,然后将.onnx文件转换为.mlmodel文件。

我的输入大小是:(1,3,299,299)

这是我的转换代码:

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

        # get the pretrained DenseNet model
        densenet = models.densenet161(pretrained=True)

        # freeze the gradients to avoid training
        for i, param in enumerate(densenet.parameters()):
            param.requires_grad_(False)

        # transfer learning procedure
        # take everything before the 12th layer of the densenet
        modules = list(densenet.children())[0][:12]
        self.densenet = nn.Sequential(*modules)

        # transfer the classifier
        self.classifier1 = nn.Linear(in_features=2208, out_features=4096)
        self.classifier2 = nn.Linear(in_features=4096, out_features=2887)

        # relu activation
        self.prelu = nn.PReLU()

        # dropout
        self.dropout = nn.Dropout(p=0.5)

        # max pool
        self.avg_pool = nn.AvgPool2d(kernel_size=7)

    def forward(self, x):

        # get the features from the original VGG
        features = self.densenet(x)
        # flat the features
        features = self.avg_pool(features).squeeze()
        # out
        features = self.dropout(self.prelu(self.classifier1(features)))
        logits = self.classifier2(features)
        return logits


def load_checkpoint(checkpoint_path):
    checkpoint = torch.load(checkpoint_path,map_location="cpu")
    model = DenseNet()
    model.load_state_dict(checkpoint)
    return model

model = load_checkpoint('model.pth')
dummy_input = torch.randn(1, 3, 299, 299, device='cpu')
torch.onnx.export(model, dummy_input, "model_output.onnx")

当.onnx转换为.mlmodel脚本时,此脚本运行成功。

这是我正在使用的脚本:

import sys
from onnx import onnx_pb
from onnx_coreml import convert

model_in = 'model_output.onnx'
model_out = 'model_output.mlmodel'

model_file = open(model_in, 'rb')
model_proto = onnx_pb.ModelProto()
model_proto.ParseFromString(model_file.read())
coreml_model = convert(model_proto)
coreml_model.save(model_out)

此脚本给我以下错误:

  

570/574:转换节点类型MatMul   追溯(最近一次通话):     在第84行中输入文件“ densenet.py”       coreml_model = convert(model_proto)     在转换中的文件“ /home/ubuntu/anaconda3/envs/py36/lib/python3.6/site-packages/onnx_coreml/converter.py”中,第458行       _convert_node(生成器,节点,图形,错误)     _convert_node中的文件“ /home/ubuntu/anaconda3/envs/py36/lib/python3.6/site-packages/onnx_coreml/_operators.py”,行1755       返回converter_fn(生成器,节点,图形,错误)     文件“ /home/ubuntu/anaconda3/envs/py36/lib/python3.6/site-packages/onnx_coreml/_operators.py”,行1091,位于_convert_matmul中       返回err.unsupported_op_configuration(生成器,节点,图形,“ CoreML不兼容的轴放置”)     文件“ /home/ubuntu/anaconda3/envs/py36/lib/python3.6/site-packages/onnx_coreml/_error_utils.py”,第56行,位于unsupported_op_configuration中       “转换类型为{}的op时出错。错误消息:{} \ n” .format(node.op_type,err_message,)   TypeError:转换类型为MatMul的op时出错。错误消息:CoreML不兼容的轴放置

但是当我将pth中的虚拟输入大小更改为onnx模型为

  

dummy_input = torch.randn(10,3,299,299,device ='cpu')

或大于1的任何数字。 但是我需要输入的是大小为(299,299)的单色图像。

0 个答案:

没有答案