iTranslated by AI
Common Pitfalls for iOS Developers Creating CoreML Models
Hello, I'm noppe.
I've been trying to create CoreML models, but things haven't been going smoothly, so I'll share some challenges I faced while getting about halfway through the process.
Don't Create Models as Keras Subclasses
To use coremltools for converting models to CoreML, the Keras structure needs to be a Sequential model or a Functional model.
Therefore, if you build a model using a Keras subclass, you won't be able to convert it to a CoreML model even after training it.
Specify the Filename When You Want to Create an mlpackage
It's not very well-known, but if you include .mlpackage in the filename, it will automatically save it as an mlpackage.
ml_model = ct.convert(model)
ml_model.save("MLModel.mlpackage")
Explicitly Specify ImageType When Input is an Image
If you don't specify ImageType, you'll have to deal with an array called MLMultiArray directly from Swift.
ct.convert(
model,
inputs=[
ct.ImageType()
],
)
Name Your Layers
By naming your layers, you can determine the property names used when calling them from Swift.
In particular, layer names like dense_1_input, dense_2_input, etc., change every time you run it, so it's better to define them explicitly.
layer1 = tf.keras.Input(name="input", shape=(82,))
layer2 = tf.keras.layers.Embedding(input_dim=100, output_dim=10, input_length=82)(layer1)
layer3 = tf.keras.layers.LSTM(name="output", units=200, return_sequences=False)(layer2)
Note that if you don't use Input, the name will be the first layer's name plus _input.
I generally recommend adding Input at the beginning.
I'll add more when I think of them.
Discussion