Core ML enables app to use Machine Learning models with less power consumption, efficient processing speed and low memory usage. Core ML supports various models including neural networks, tree ensembles, support vector machines, generalized linear models, feature engineering and pipeline models. However, all of the models need to be converted to .mlmodel file extension.
Basically, Core ML model is the conversion of a trained machine learning model to .mlmodel file.
Apple supports below mentioned formats for conversion as of now:
To convert already trained models to .mlmodel format, Apple has introduced coremltools, which is open source tool.
Note : Try coremltools with Python 2.7 as other versions in macOS have some errors while configuration.
Try this commands:
pip install virtualenv
virtualenv --python=/usr/bin/python2.7 python27
Check the current version of Python installed on your system:
Install coremltools in system:
pip install -U coremltools
- U is for temporary environment. Also our installation part is over here.
As of now, we are ready to convert already trained models in .mlmodel file.
Files required: bvlc_alexnet.caffemodel, deploy.prototxt, class_labels.txt
So choose the dictionary in terminal,
# Convert a caffe model to a classifier in Core ML
coreml_model = coremltools.converters.caffe.convert(('bvlc_alexnet.caffemodel', 'deploy.prototxt'), predicted_feature_name='class_labels.txt')
deploy.prototxt – which contains the structure of neural network inside.
‘bvlc_alexnet.caffemodel – already trained caffe model.
class_labels.txt – It contains list of names of flowers.
To begin with conversion, you just need to press ENTER and the system will start its operation of converting the model.
It will be >>> sign when conversion gets completed. So save it by,
# Now save the model
And you’ll see a ‘Flowers.mlmodel’ file in the same folder. That’s it. Integrate recently created model in Xcode and start identifying objects.
Integrating Core ML Model into Xcode Project
Drag the Core ML model to Xcode navigator. Xcode will detect it as Core ML model and will display its description, input – output parameters for the model with other necessary information.
It’ll show a message like ‘Model is not part of any target…’. Just add the model to Target by selecting the target.
Now it’ll generate a new object with the name of your model and its properties will be automatically generated.
Now, in the ViewController, Add the code to pick image and after that predict the result.
Now, run the app and pick image from Photos. The model will predict the age of the portrait picked will display with probability of it.
Limitations of Core ML :
- At present apple only supports trained model.
- Not able to train models inside app.
- Conversion of models is limited to few formats only.
- Developers will not have option to choose between CPU or GPU.
- It’ll increase the size of the app.
- It may result in different result on different platform as .mlmodel is not supported on all platforms.
Latest posts by Milan Manwar (see all)
- Introduction to ARKit 2 and Hands-on using ARKit 2 with Vision - September 20, 2019
- Basics of Create ML and Turi Create - September 17, 2019
- Auto Renewable Subscriptions in iOS and Android - June 19, 2019