python - CoreML 输出类型

标签 python keras coreml

是否可以更改 CoreML 模型的输出类型?我的模型将图像作为输入,将图像作为输出,但是当我将 Keras 模型转换为 mlmodel 时,我得到:

coreml_model = coremltools.converters.keras.convert('/Users/user/Desktop/model.h5',input_names='input_img',image_input_names='input_img',output_names='image')
coreml_model.save('/Users/user/Desktop/model.mlmodel')

enter image description here

输出是 MultiArray 类型,但我想要 Image 类型,如何更改它?

最佳答案

是的,这是可能的。不过,您之后需要手动更改转换后的 Core ML 模型,因为从 2.1 版开始,coremltools 并未为此提供任何转换选项。

简而言之,以下是将模型转换为 Core ML 格式后需要执行的操作。这些应该在 Python 端通过调用 coremltools 的较低级别 API 来完成。

  1. 使用 coremltools 将转换后的 CoreML 模型加载到 Python 中
  2. 在链的末尾附加一个新的 ActivationLinear 层,就在原始模型的输出层之后。您还可以使用该层执行线性变换,例如将范围从 0..1 转换为 0..255 和/或添加偏差。
  3. 通过设置其 type 属性,将该新图层配置为图像输出图层
  4. 将更新后的模型保存到新的 Core ML 模型中。
  5. 重新加载它并使用训练数据集中的样本进行测试,作为健全性检查。

要使第 5 步生效,您需要在 Mac 上运行 Python 脚本,因为它使用 native Core ML 库来运行模型。

详情可以阅读我的帖子getting Core ML to produce images as output .

关于python - CoreML 输出类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46093797/

相关文章:

java - 集成 Spring Boot 项目中的 Python 代码

python - 为什么我们使用strategy.num_replicas_in_sync

machine-learning - Keras 如何更新多标签学习中的权重(实现方面)

ios - 物体检测 : Output different from CreateML vs programatically

swift - 浓缩咖啡异常 : "Invalid argument":general shape kernel while loading mlmodel

python - 字典列表中的 Pandas DataFrame

python - Pymqi 在库导入时抛出异常 ImportError

python - 如何在没有双斜杠的情况下获取相对于项目根目录的绝对路径?

python - Keras属性错误: 'Tensor' object has no attribute 'log'

augmented-reality - 使用 ARKit 和 CoreML 从远距离检测物体