CoreML model: Convert imageType model input to multiArray

安稳与你 提交于 2020-06-16 17:54:07

问题


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.

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!


回答1:


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).

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")


来源:https://stackoverflow.com/questions/59765376/coreml-model-convert-imagetype-model-input-to-multiarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!