【问题标题】:Converting from UIImage to MLMultiArray从 UIImage 转换为 MLMultiArray
【发布时间】:2019-09-29 20:41:02
【问题描述】:

我正在使用预训练的 mlmodel 进行图像分类。该模型将 3 x 224 x 224 MultiArray 作为图像格式的输入。对于我当前的应用程序,我正在使用 UIImage。有没有办法将 UIImage 转换为 MLMultiArray?

我已经看到一些关于从 Keras 模型转换为 CoreML 模型的答案,但我的模型已经是 mlmodel 格式并且无法访问数据。

【问题讨论】:

    标签: ios swift multidimensional-array coreml coremltools


    【解决方案1】:

    最简单的解决方案是更改 mlmodel 文件中的输入格式。即使您没有原始的 Keras 模型,您也可以这样做。

    在 Python 脚本中执行以下操作:

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

    也可以将 UIImage 转换为 MLMultiArray,但如果您的模型确实适用于图像,最好将输入类型更改为图像。

    顺便说一句,如果您仍然拥有原始 Keras 模型,您可以通过向 coremltools Keras 转换器提供 image_input_names="your_input" 来自动执行此操作。在这种情况下无需编写新的 Python 脚本。

    【讨论】:

    • 感谢您的回答,但是在我的模型上运行此脚本后,输入类型是 CVPixelBuffer 而不是 UIImage,这是预期的行为吗?
    • 是的,Core ML 使用 CVPixelBuffer 而不是 UIImage。但是将 UIImage 转换为 CVPixelBuffer 相对简单。见github.com/hollance/CoreMLHelpers
    猜你喜欢
    • 2017-11-15
    • 2021-08-30
    • 2020-08-25
    • 2020-11-03
    • 2018-09-16
    • 2020-09-03
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多