问题: CoreMLTools reName_feature不更新模型中的功能名称,仅更新模型的“规范”中的功能名称!
我为一个图像分类问题创建了一个连续的Tensorflow模型。最初,在转换时(根据Apple文档):
# 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添加形状参数的修复:
# 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函数的名称参数(上面)没有效果。接下来,我尝试直接更改模型的规范:
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这将正确地更改规范中的输入名称:
[name: "image"
type {
imageType {
width: 299
height: 299
colorSpace: GRAYSCALE
}
}
]然而,这显然不会将更改推入模型中!下面是mlmodel的清单:
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中?
发布于 2021-02-23 04:42:53
在coreMLtools团队的Aseem的帮助下,以下是将修订后的protobuf规范推回到模型中的机制:
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/
https://stackoverflow.com/questions/66307905
复制相似问题