Examples

The following are code example snippets and full examples of using Core ML Tools to convert models.

TopicExample
For a quick startGetting Started: Demonstrates how to convert an image classifier model trained using the TensorFlow Keras API to the Core ML format.
ML program with typed executionTyped Execution Workflow Example: Demonstrates a workflow for checking accuracy using ML Programs with Typed Execution.
TensorFlow 2Load and Convert a Model (in Unified Conversion API)
TensorFlow 2 Workflow
Convert a Pre-trained Model
Convert a User-defined Model

Full examples:
Getting Started: Demonstrates how to convert an image classifier model trained using the TensorFlow Keras API to the Core ML format.
Converting TensorFlow 2 BERT Transformer Models: Converts an object of the tf.keras.Model class and a SavedModel in the TensorFlow 2 format.
TensorFlow 1Convert From TensorFlow 1 (in Unified Conversion API)
Export as Frozen Graph and Convert
Convert a Pre-trained Model

Full examples:
Converting a TensorFlow 1 Image Classifier: Demonstrates the importance of setting the image preprocessing parameters correctly during conversion to get the right results.
Converting a TensorFlow 1 DeepSpeech Model: Demonstrates automatic handling of flexible shapes using automatic speech recognition.
PyTorchConvert from PyTorch (in Unified Conversion API)
Converting from PyTorch
Model Tracing
Model Scripting

Full examples:
Converting a Natural Language Processing Model: Combines tracing and scripting to convert a PyTorch natural language processing model.
Converting a torchvision Model from PyTorch: Traces a torchvision MobileNetV2 model, adds preprocessing for image input, and then converts it to Core ML.
Converting a PyTorch Segmentation Model: Converts a PyTorch segmentation model that takes an image and outputs a class prediction for each pixel of the image.
Model Intermediate Language (MIL)Model Intermediate Language: Construct a MIL program using the Python builder.
Conversion OptionsImage Inputs:
Convert a Model with a MultiArray
Convert a Model with an ImageType
Add Image Preprocessing Options

Classifiers: Produce a Classifier Model

Flexible Input Shapes:
Select from Predetermined Shapes
Set the Range for Each Dimension
Update a Core ML Model to Use Flexible Input shapes

Composite Operators: Defining a composite operation by decomposing it into MIL operations.

Full examples:
Custom Operators: Augment Core ML with your own operators and implement them in Swift.
OptimizationTraining-Time Compression Examples: Use magnitude pruning, linear quantization, or palettization while training your model, or start from a pre-trained model and fine-tune it with training data.
Compressing Neural Network Weights: Reduce the size of a neural network by reducing the number of bits that represent a number.
Other ConvertersMulti-backend Keras
ONNX
Caffe
Trees and Linear ModelsLibSVM
Scikit-learn
XGBoost
MLModelMLModel Overview:
Load and save the MLModel
Use the MLModel for Prediction
Work with the spec Object
Update the Metadata and Input/output Descriptions

Model Prediction:
Make Predictions
Multi-array Prediction
Image Prediction
Image Prediction for a Multi-array Model

Xcode Model Preview Types:
Segmentation Example
BERT QA Example
Body Pose Example

MLModel Utilities:
Rename a Feature
Convert All Double Multi-array Feature Descriptions to Float
Evaluate Classifier, Regressor, and Transformer models
Updatable ModelsNearest Neighbor Classifier: Create an updatable empty k-nearest neighbor.
Neural Network Classifier: Create a simple convolutional model with Keras, convert the model to Core ML, and make the model updatable.
Pipeline Classifier: Use a pipeline composed of a drawing-embedding model and a nearest neighbor classifier to create a model for training a sketch classifier.

If you have a code example you'd like to submit, see Contributing.