Converting Teachable Machine trained model to CoreML model

Pranav S Khodanpur (anushku)
2 min readDec 20, 2022

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.

https://github.com/jeffheaton/t81_558_deep_learning/blob/master/install/tensorflow-install-mac-metal-jul-2021.ipynb

Use python scripts supported with corelmltools , keras.models and tensorflow modules for conversion.

labels.txt file that contains the class names, above has 3 classes as I had trained my model for 3 celebrities
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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response