Apple's CoreML was first introduced at the WWDC 2017 conference. It brings ML (machine learning) models to Apple's devices using: iOS, iPadOS, watchOS, macOS and tvOS. — i.e without backend support.  

However all models to be used by Apple's devices need to be in CoreML format. Aside from pre-converted models, Apple also provides a coremltools package to convert trained ML models to CoreML models.

Here is an example of converting a Caffee Model to CoreML ML.

What we need for converting

  1. Python: since coremltools is a python package. Version 3.8.5 is used in this example.
  2. coremltools: version 4.0b2
  3. oxford102.caffemodel
  4. deploy.prototxt
  5. A file contains class labels used in a training model. Class labels map the index of the output of a neural network to labels in a classifier.

For #4 and #5, we will be using these files. 

Step by step

  • Create a folder/directory on a computer: convertmodel. Note: all files will be installed or added to the same folder.
                  cd convertmodel
  • Install coremltools: from a terminal:
                 python -m pip install coremltools==4.0b2 
  • Download oxford102.caffemodel.
  • Download deploy.prototxt
  • Download class_lables.py
  • Generate a file that contains class labels from the trained model. Normally, this file can be provided by the people/team who train the model. In our case, the Github repo used in this example provided a class_labels.py but we need to modify it a bit.
  • Open class_labels.py and add the following line at the end of the file.
        		for label in labels:  
				print(label) 
  • From a terminal:
				python class_labels.py > flower_lables.txt

you should see something that looks like this:

  • Create a python script: covert.py. Again, make sure all files used are in the same folder. Open a text editor of your choice. As an important note: generated CoreML model MUST have the extension .mlmodel.
import coremltools 
  
caffe_modle = ('oxford102.caffemodel', 'deploy.prototxt') 
model_labels = 'class_labels.txt' 
  
# look into deploy.prototxt file 
# find input: "data" 
image_input = 'data' 
  
coreml_model = coremltools.converters.caffe.convert(caffe_modle, class_labels=model_labels, 
                                                    image_input_names=image_input) 
coreml_model.save('Oxford102.mlmodel') 

 Please note: For the image_input field - find this info in deploy.prototxt look for input: 

At this point, we have all info we needed to generate a CoreML model from the terminal:

$> python covert.py 

 

Finally, you have successfully converted a trained model to a CoreML model!

coremltools
Explore

Technologies