You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中如何将MLMultiArray转UIImage?Axis参数报错

问题解决:CoreMLHelpers中MLMultiArray转图像的axes参数错误

你的崩溃原因是传入的axes参数索引超出了MLMultiArray的维度范围:

  • 你的MLMultiArray形状为[1, 256, 256, 3],对应4个维度,索引从0开始计数,各维度含义是:
    • 索引0:batch维度(大小1)
    • 索引1:图像高度(256)
    • 索引2:图像宽度(256)
    • 索引3:颜色通道数(3)
  • 你传入的(4, 2, 3)中,4大于最大维度索引3,触发了参数校验错误。

CoreMLHelpers的image(min:max:axes:)方法要求axes的三个元素依次对应通道轴、高度轴、宽度轴的索引。针对你的MLMultiArray格式,正确的参数应该是(3, 1, 2)。

修改后的代码如下:

func prediction(input: MLModelProviderInput) throws -> MLModelProviderOutput {
    let outFeatures = try model.prediction(from: input)
    let multiArray = outFeatures.featureValue(for: outputName)!.multiArrayValue!
    
    let outputImage = multiArray.image(min: 0, max: 255, axes: (3, 1, 2))
    let result = MLModelProviderOutput(outputImage: outputImage!.pixelBuffer()!, outputName: outputName)
    
    return result
}

说明:batch维度(索引0)因为只有1个样本,会被方法自动忽略,无需传入到axes参数中。

内容的提问来源于stack exchange,提问作者Sachinthana Aluvihare

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:35:28