Member-only story

How would I convert this TensorFlow image classification model to Core ML?

Mustafa Celik
3 min readJul 26, 2023

SOLUTION-1

This code shows how to use the coremltools library to convert a machine learning model (e.g., Keras, TensorFlow, PyTorch, or scikit-learn) into the Core ML format, which is suitable for deployment on Apple platforms (iOS, macOS, etc.). It also configures the Core ML model as a classifier by providing class labels, allowing you to map the model's output to human-readable class names. The resulting Core ML model is then saved to a file with the '.mlmodel' extension.

# install coremltools
!pip install coremltools

# import coremltools
import coremltools as ct

# define the input type
# This line creates an ImageType object, which specifies the type and format of the model's input.
# In this case, it's an image input, and the input is expected to be an image.
image_input = ct.ImageType()

# create classifier configuration with the class labels
# The 'classifier_config' is used to specify the class labels for a classifier model.
# It takes a list of class names as input, which will be used to map the model's output to human-readable class labels.
# Make sure 'class_names' is a list that contains the names of the classes in the same order as the model's output.
classifier_config = ct.ClassifierConfig(class_names)

# perform the conversion
# The 'convert' function is used to convert the input model to Core ML format.
# 'model' is the input model, which should be a Keras, TensorFlow, PyTorch, or scikit-learn model.
# 'inputs' is a list that specifies…

--

--

No responses yet