I am trying to make a Core ML model from Places205 - GoogLeNet, as described by Apple here.
I don't want to use the ready-made model by Apple, so I got the original model from this link: https://developer.apple.com/machine-learning/model-details/Places205-GoogLeNet.txt
As per Apple's WWDC session we can convert that model using Coremltools. In their session we don't know which IDE they are using for Python coding but currently I am using Pycharm for Python coding.
Using the link to the model, we can get two things .caffemodel and .prototext. I tried to convert these using coremltools and received 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 given models are not converting then how we can say other open source model will work?
You can see my code base in attached screenshot.
How can this error be fixed?
It looks like Matthijs solved your core problem with his comment above, but I can expand on that for future people who encounter this with other models.
When converting a Caffe model to a .mlmodel file using Apple's coremltools, Apple's converter takes in both the binary .caffemodel (containing the model structure and weights), as well as a text-based .prototxt model description (which might have a little more context than the .caffemodel does).
Often, trained Caffe models will have a few different .prototxt files along with them (deploy.prototxt
, solver.prototxt
, train.prototxt
), and you generally want to use the deploy.prototxt
(or a similarly named prototxt).
Even if you do, you still may encounter an error about coremltools being "Unable to infer input name and dimensions". Caffe models don't have an explicit requirement to state the input dimensions the model will accept, so coremltools tries to infer that from a few different sources. If those sources are missing, you may need to edit them into the .prototxt yourself.
In the above-linked deploy_places205.protxt
, this input size is specified by 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 may also see the following in one of these .prototxt descriptions:
input: "data"
input_shape {
dim: 1
dim: 3
dim: 227
dim: 227
}
If you have a .prototxt file that is missing this for its input, you could add either of the above right before the first layer {
instance in the file. The dimensions are, in order: batch size (ignored by Core ML), color channels, image width, image height. You can adjust these to match what the network expects.
You may also need to verify that "data" is the input layer used by the model, so look for the first layer {
instance and check that it says bottom: "data"
within it. If the layer name is something different, you may need to either change it there or in your data layer name.
You may also need to set your data layer name in the coremltools converter to make sure it picks up the right one. For reference, here's the little Python script I use for converting a 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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With