Examples — Guide to Core ML Tools (original) (raw)
Contents
- For a Quick Start
- ML Program with Typed Execution
- TensorFlow 2
- TensorFlow 1
- PyTorch
- Model Intermediate Language (MIL)
- Conversion Options
- Optimization
- Trees and Linear Models
- MLModel
- Updatable Models
Examples#
The following are code example snippets and full examples of using Core ML Tools to convert models.
For a Quick Start#
Full example:
- Getting Started: Demonstrates how to convert an image classifier model trained using the TensorFlow Keras API to the Core ML format.
ML Program with Typed Execution#
Full example:
- Typed Execution Workflow Example: Demonstrates a workflow for checking accuracy using ML Programs with Typed Execution.
TensorFlow 2#
- Load and Convert Model Workflow
- 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 1#
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.
PyTorch#
Full examples:
- Converting a torchvision Model from PyTorch: Traces / Exports 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.
- Converting an Open Efficient Language Model: Converts a PyTorch Open Efficient Language Model to Core ML
Model Intermediate Language (MIL)#
Full example:
- Model Intermediate Language: Construct a MIL program using the Python builder.”
Conversion Options#
Image Inputs#
Classifiers#
Flexible Input Shapes#
- Select from Predetermined Shapes.
- Set the Range for Each Dimension.
- Update a Core ML Model to Use Flexible Input shapes
Composite and Custom Operators#
- Composite Operators: Defining a composite operation by decomposing it into MIL operations.
Full example:
- Custom Operators: Augment Core ML with your own operators and implement them in Swift.
Optimization#
Full examples:
- Training-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.
Trees and Linear Models#
MLModel#
MLModel Overview#
- Load and save the MLModel.
- Use the MLModel for Prediction.
- Work with the Spec.
- Update the Metadata and Input/output Descriptions.
Model Prediction#
- Make Predictions
- Multi-array Prediction
- Image Prediction
- Image Prediction for a Multi-array Model
- Predict From the Compiled Model
Full example:
- Compiled Model Timing Example: Demonstrates timing differences with calling a large model.
Xcode Model Preview Types#
Full examples:
MLModel Utilities#
- Rename a Feature.
- Convert All Double Multi-array Feature Descriptions to Float.
- Evaluate Classifier, Regressor, and Transformer models.
Updatable Models#
Full examples:
- Nearest 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.