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

修改multiArray类型后出现CoreML编译错误,求解决方案

CoreML模型输入类型修改后编译错误问题

我将Google MediaPipe的.tflite模型转换为.mlmodel文件后,尝试把输入类型从multiArray改为image,结果出现无法解决的CoreML编译错误。

修改输入类型的代码

# convert_inputType.py :将multiArray转换为image类型
import coremltools as ct
from coremltools.proto import FeatureTypes_pb2 as ft

spec = ct.utils.load_spec('model_coreml_float32.mlmodel') # 原类型为multiArray
builder = ct.models.neural_network.NeuralNetworkBuilder(spec=spec)

# 查看输入/输出特征
print('--- 修改前:')
builder.inspect_input_features()
builder.inspect_output_features()

# 修改输入以让模型接受256x256的RGB图像
input = spec.description.input[0]
# del input.type.multiArrayType.shape[0]
input.type.imageType.colorSpace = ft.ImageFeatureType.RGB
input.type.imageType.width = 256
input.type.imageType.height = 256

# 查看转换后的输入/输出特征
print('--- 修改后:')
builder.inspect_input_features()
builder.inspect_output_features()

# 保存修改输入类型后的模型
ct.utils.save_spec(spec, 'selfie_segmentation.mlmodel') # 修改类型后的模型

修改前后的特征信息

--- 修改前:

[Id: 0] Name: input_1
          Type: multiArrayType {
  shape: 1
  shape: 256
  shape: 256
  shape: 3
  dataType: FLOAT32
}

[Id: 0] Name: activation_10
          Type: multiArrayType {
  dataType: FLOAT32
}

--- 修改后:

[Id: 0] Name: input_1
          Type: imageType {
  width: 256
  height: 256
  colorSpace: RGB
}

[Id: 0] Name: activation_10
          Type: multiArrayType {
  dataType: FLOAT32
}

模型说明

  • model_coreml_float32.mlmodel:由MediaPipe TFlite转换而来
  • selfie_segmentation.mlmodel:修改输入类型后保存的模型

编译错误信息

Espresso exception: \"Invalid blob shape\": generic_elementwise_kernel: cannot broadcast:
----------------------------------------
SchemeBuildError: Failed to build the scheme \"testSelfieSegmentation\"

compiler error:  Espresso exception: \"Invalid blob shape\": generic_elementwise_kernel: cannot broadcast:

Compile CoreML model selfie_segmentation.mlmodel:
coremlc: error: compiler error:  Espresso exception: \"Invalid blob shape\": generic_elementwise_kernel: cannot broadcast:
 (1, 16, 8, 128)
 (1, 16, 2, 128)

排查情况

用Netron检查模型配置,未找到错误信息中提到的(1,16,8,128)或(1,16,2,128)形状的层。我猜测输入形状(1,256,256,3)应该改为(3,256,256),但不知道如何修改,恳请提供解决建议。

内容的提问来源于stack exchange,提问作者Shin Totoki

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 13:35:44