Converting from UIImage to MLMultiArray

I am using a pre-trained mlmodel for image classification. The model takes in as input a 3 x 224 x 224 MultiArray as the format for the image. For my current application, I am working with a UIImage. Is there a way to convert a UIImage to a MLMultiArray?

I have seen some answers regarding converting from a Keras model to a CoreML model, but my model is already in the mlmodel format and don't have access to the data.

Answer

The easiest solution is to change the format of the input in the mlmodel file. You can do this even if you don't have the original Keras model.

Do the following in a Python script:

import coremltools
import coremltools.proto.FeatureTypes_pb2 as ft 

spec = coremltools.utils.load_spec("YourModel.mlmodel")

input = spec.description.input[0]
input.type.imageType.colorSpace = ft.ImageFeatureType.RGB
input.type.imageType.height = 224 
input.type.imageType.width = 224

coremltools.utils.save_spec(spec, "YourNewModel.mlmodel")

It's also possible to convert the UIImage into an MLMultiArray, but if your model really works on images anyway, it's best to change the input type to an image.

By the way, if you still do have the original Keras model, you can automatically do this by providing image_input_names="your_input" to the coremltools Keras converter. No need to write a new Python script in that case.

扫描二维码关注公众号,回复: 8619506 查看本文章
发布了1636 篇原创文章 · 获赞 341 · 访问量 221万+

猜你喜欢

转载自blog.csdn.net/tony2278/article/details/103895142