首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CoreMLTools转换为MLModel: reName_Feature不起作用

CoreMLTools转换为MLModel: reName_Feature不起作用
EN

Stack Overflow用户
提问于 2021-02-22 06:39:47
回答 1查看 127关注 0票数 1

问题: CoreMLTools reName_feature不更新模型中的功能名称,仅更新模型的“规范”中的功能名称!

我为一个图像分类问题创建了一个连续的Tensorflow模型。最初,在转换时(根据Apple文档):

代码语言:javascript
复制
# https://coremltools.readme.io/docs/tensorflow-2

mlmodel2 = ct.convert(model, 
                      inputs=[ct.ImageType()],
                      input_names=['image'],
                      image_input_names='image')

生成了一个具有名为"conv2d_3_input“的输入的MLModel,并且是一个MultiArray (Float33。1x299x299x1)和名为“MultiArray”的输出,该输出也是Float32的身份

幸运的是,位于:https://coremltools.readme.io/docs/introductory-quickstart#download-the-model的快速入门文档阐明了输入类型需要向ImageType添加形状参数的修复:

代码语言:javascript
复制
# Define the input type as image, 
# set pre-processing parameters to normalize the image 
# to have its values in the interval [0,1] 

image_input = ct.ImageType(shape=(1, 299, 299, 1,))
mlmodel3 = ct.convert(model, 
                      inputs=[image_input],
                      input_names=['image'],
                      image_input_names='image')

因此,现在得到的图像的输入类型是MLModel (灰度299x299)(棒极了!),但它仍然被称为"conv2d_3_input“。

对于样式点,我想将输入特性重命名为"image“。convert函数的名称参数(上面)没有效果。接下来,我尝试直接更改模型的规范:

代码语言:javascript
复制
spec = mlmodel.get_spec()
#spec.description.input
ct.utils.rename_feature(spec, 'conv2d_3_input', 'image')

#rename change spec but does not push new spec into model
spec.description.input

这将正确地更改规范中的输入名称:

代码语言:javascript
复制
[name: "image"
type {
  imageType {
    width: 299
    height: 299
    colorSpace: GRAYSCALE
  }
}
]

然而,这显然不会将更改推入模型中!下面是mlmodel的清单:

代码语言:javascript
复制
input {
  name: "conv2d_3_input"
  type {
    imageType {
      width: 299
      height: 299
      colorSpace: GRAYSCALE
    }
  }
}
output {
  name: "Identity"
  shortDescription: "Most likely ....."
  type {
    multiArrayType {
      dataType: FLOAT32
    }
  }
}
metadata {
  shortDescription: "Converts image ........."

如何将特性名称中的更改推送到实际的mlmodel中?

EN

回答 1

Stack Overflow用户

发布于 2021-02-23 04:42:53

在coreMLtools团队的Aseem的帮助下,以下是将修订后的protobuf规范推回到模型中的机制:

代码语言:javascript
复制
spec = mlmodel3.get_spec()
ct.utils.rename_feature(spec, 'conv2d_3_input', 'image')
ct.utils.rename_feature(spec, 'Identity', 'output')


# reload the model with the updated spec and re-save
model = ct.models.MLModel(spec)
model.save("mlModel3.mlmodel")

#observe the correct model

model

在Aseem的WWDC2020谈话中对此进行了更详细的介绍:https://developer.apple.com/videos/play/wwdc2020/10153/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66307905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档