CoreML 模型:将 imageType 模型输入转换为 multiArray [英] CoreML model: Convert imageType model input to multiArray

查看:80
本文介绍了CoreML 模型:将 imageType 模型输入转换为 multiArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PyTorch,我训练了一个简单的多类分类器,我想将其转换为 CoreML 模型格式.它已转换,但有一个问题.

Using PyTorch, I have trained a simple multiclass classifier and I want to convert it to CoreML model format. It is converted, but there's an issue.

我已经搜索了非常详尽但最常见的问题,与 mlmodel 的输入有关,只是关于如何将 mlmodel 的输入格式从 MLMultiArray 更改为 UIImage,因为它们必须处理图像.但是,如果我的模型需要 UIImage 作为输入,而我有多数组类型的数据,我该如何更改模型的输入以使其需要多数组数据?

I've searched quite exhaustively but most frequent questions, pertaining to mlmodel's inputs, are only about how to change the format of the input of mlmodel from MLMultiArray to UIImage because they must be working with images. But if my model expects a UIImage as input whereas I have multiarray type data, how can I change the model's input so it expects multiarray data?

转换后得到的模型输入规范:

Model input spec I get after conversion:

input {
  name: "input"
  type {
    imageType {
      width: 3
      height: 150
      colorSpace: GRAYSCALE
    }
  }
}

我想要的模型输入规范:

Model input spec I want to have:

input {
  name: "input"
  type {
      multiArrayType {
        shape: 3
        shape: 1
        shape: 150
        dataType: DOUBLE
    }
  }
}

任何帮助将不胜感激.谢谢!

Any help will be really appreciated. Thanks!

推荐答案

通常,当您转换模型时,您会得到一个 MLMultiArray 除非您指定要将其作为图像.不确定您如何转换模型,但您可能会说您不希望输入是图像(即不要为转换器指定 image_input_names 参数).

Usually when you convert a model you get an MLMultiArray unless you specify you want it to be an image. Not sure how you converted the model, but you can probably say you don't want the input to be an image (i.e. don't specify an image_input_names argument to the converter).

如果您无权访问原始模型,您可以更改 mlmodel 文件执行以下操作(可能有错别字):

In case you don't have access to the original model, you can change the mlmodel file doing something like this (there may be typos):

import coremltools
mlmodel = coremltools.models.MLModel("YourModel.mlmodel")
spec = mlmodel._spec
spec.description.input[0].type.multiArrayType.shape.extend([3, 1, 150])
coremltools.util.save_spec(spec, "YourNewModel.mlmodel")

这篇关于CoreML 模型:将 imageType 模型输入转换为 multiArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆