Core ML model conversion fails with "Cannot output name and input dimensions"

I am trying to make a Core ML model from Places205 - GoogLeNet as described by Apple here .

I don't want to use Apple's off-the-shelf model, so I got the original model from this link: https://developer.apple.com/machine-learning/model-details/Places205-GoogLeNet.txt

According to Apple WWDC session, we can transform this model using Coremltools. In their session, we don't know which IDEs they are using for Python coding, but I am currently using Pycharm for Python coding.

Using the reference to the model, we can get two things: caffemodel and .prototext. I tried to convert them using coremltools and got the following error:

RuntimeError: Unable to infer input name and dimensions. Please provide a .prototxt file with 'Input' layer and dimensions defined.

      

So, if Apple's model data is not transformed, then how can we tell another open source model will work?

You can see my codebase in the attached screenshot.

How can this error be resolved?

enter image description here

+3


source to share


1 answer


It looks like Matthijs solved your main problem with his comment above, but I can expand on this for future people who come across this with other models.

When converting a Caffe model to a .mlmodel file using Apple's coremltools, Apple's converter accepts both a binary .caffemodel (containing the model and weights structure) and a textual description of the .prototxt model (which can have a little more context than .caffemodel does).

Often trained Caffe models will have several different .prototxt files with them ( deploy.prototxt

, solver.prototxt

, train.prototxt

), and you normally want to use deploy.prototxt

(or similar prototype).

Even if you do this, you can still run into the error that coremltools is "Unable to deduce input names and sizes". Caffe models have no explicit requirement to specify the input dimensions that the model will take, so coremltools tries to infer that from several different sources. If these sources are missing, you may need to edit them in the .prototxt yourself.

In the above, deploy_places205.protxt

this input size is listed in the following code at the top of the file:

input: "data"
input_dim: 10
input_dim: 3
input_dim: 224
input_dim: 224

      



but you can also see the following in one of the following .prototxt descriptions:

input: "data"
input_shape {
  dim: 1
  dim: 3
  dim: 227
  dim: 227
}

      

If you have a .prototxt file that is missing to enter it, you can add any of the above permissions before the first instance layer {

in the file. Dimensions are: batch size (ignored by Core ML), color channels, image width, image height. You can customize them to be what they expect.

You may also need to check that "data" is the input layer used by the model, so find the first instance layer {

and check what it says bottom: "data"

inside it. If the layer name is something else, you may need to either change it there or the name of your data layer.

You may also need to set the name of your data layer in the coremltools converter to make sure it picks up the correct one. For reference, here's a little Python script I use to transform the model like this:

import coremltools

coreml_model = coremltools.converters.caffe.convert(('mymodel.caffemodel', 'deploy.prototxt'),
                                                    image_input_names = "data",
                                                    is_bgr = True,
                                                    class_labels='labels.txt'
                                                   )


coreml_model.save('MyModel.mlmodel')

      

+4


source







All Articles