1

Problem: CoreMLTools reName_feature does not update feature name in model, only in "spec" of model!

I have created a sequential Tensorflow model for an image classification problem. Initially, upon conversion (per Apple docs):

# https://coremltools.readme.io/docs/tensorflow-2

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

produced an MLModel which had an input named "conv2d_3_input" and was a MultiArray (Float33. 1x299x299x1) and an output named "Identity" which was also a MultiArray of Float32

Fortunately, the Quickstart Doc at: https://coremltools.readme.io/docs/introductory-quickstart#download-the-model elucidated the fix for input type requires adding a shape arg to 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')

So now the resulting MLModel has an input type of Image (Grayscale 299 x 299) (Great!), but it is still called "conv2d_3_input"

For style points, I want to rename the input feature to "image". The name arguments to the convert function (above) had no effect. I next tried directly changing the spec of the model:

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

This correctly changes the input name in the spec:

[name: "image"
type {
  imageType {
    width: 299
    height: 299
    colorSpace: GRAYSCALE
  }
}
]

However, this apparently does not push the change into the model! Here is the listing for 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 ........."

How can one push the change in the feature name into the actual mlmodel?

BlueskyMed
  • 765
  • 7
  • 24

1 Answers1

1

With the help of Aseem of the coreMLtools team, here is the mechanism to push the revised protobuf spec back into the model:

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

This is covered in a bit more detail in Aseem's WWDC2020 talk: https://developer.apple.com/videos/play/wwdc2020/10153/

BlueskyMed
  • 765
  • 7
  • 24