Converting Teachable Machine trained model to CoreML model
Teachable Machine
Teachable machine is a web based tool to train ML models using Tensorflow.
Training the model
- Create classes, and name the classes with classifying labels.
- Train the model, if required you can change parameter Learning rate and number of Epochs.
- We can upload the trained model to the cloud and then access it using the link provided or download the Tensorflow models locally and use it for further implementation.
- Tensorflow’s downloaded model can be saved/downloaded in two formats.
Tensorflow SavedModel (.pb extension)
Keras .h5 model
to know more about training on Teachable Machine you can read this tutorial.
Converting the model
- If you are working on mac then to support the script that uses Tensorflow packages, you can refer to the link below for setup instructions.
Use python scripts supported with corelmltools , keras.models and tensorflow modules for conversion.


from keras.models import load_model
import coremltools as ct
import tensorflow as tf
model = tf.keras.models.load_model('converted_keras/keras_model.h5')
image_input = ct.ImageType()
f=open("converted_keras/labels.txt", "r")
contents = f.read().splitlines()
for i, label in enumerate(contents):
if isinstance(label, bytes):
contents[i] = label.decode("utf8")
classifier_config = ct.ClassifierConfig(contents)
image_input = ct.ImageType(shape=(1, 224, 224, 3,),bias=[-1,-1,-1], scale=1/127)
mlmodel = ct.convert(
model,
source="tensorflow",
inputs=[image_input],
classifier_config=classifier_config,
)
mlmodel.author = 'Pranav'
mlmodel.short_description = 'Celebrity Recognition with TeachableMachine'
mlmodel.save('celebrityDetection.mlpackage')
- In the above script I have used the Keras (.h5) model and converted it to the CoreML model.
- Once you get the CoreML model from above script you can import it to desired applications and use it for predictions.
- The model takes images of size( 224 X 224 ) as input for predictions and gives labels as output, hence before giving input to model for predictions.
> Run a face detection function on the desired image.
> Crop faces to face bounds(You can use CIFeatureDetection or VisionFramework)
> Resize(224 x 224) the image(face) and convert to CVPixelBuffer
> Make the predictions