简体   繁体   中英

CoreML model: Convert imageType model input to multiArray

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM