How can use combine
Asked on 08/04/2024
1 search
To combine machine learning models using Core ML, you can utilize the new support for multifunction models introduced at WWDC 2024. This feature allows you to merge multiple models into a single Core ML model, which can perform multiple tasks using a shared feature extractor. Here’s a step-by-step guide based on the sessions:
-
Convert Models to Core ML: First, convert your individual models to Core ML models using
ct convert
and save them as individual ML packages. -
Create a Multifunction Descriptor: Use Core ML tools to create a multifunction descriptor. This descriptor specifies which models to merge and the new function names in the merged model.
-
Merge Models: Use the
save multifunctionutil
to produce a merged multifunction Core ML model. During this process, Core ML tools will deduplicate shared weights by calculating the hash values of weights. -
Load and Use the Multifunction Model: When loading the multifunction model via the Core ML tools Python API, you can specify the function name to load the specific function and then perform predictions as usual.
Here is a sample code snippet for merging models:
import coremltools as ct
# Convert individual models
model1 = ct.convert('model1.onnx')
model2 = ct.convert('model2.onnx')
# Save models as ML packages
model1.save('Model1.mlpackage')
model2.save('Model2.mlpackage')
# Create a multifunction descriptor
descriptor = ct.models.MLModelDescriptor(
models=['Model1.mlpackage', 'Model2.mlpackage'],
function_names=['function1', 'function2']
)
# Merge models
multifunction_model = ct.models.MLModel(descriptor)
multifunction_model.save('MultifunctionModel.mlpackage')
# Load and use the multifunction model
loaded_model = ct.models.MLModel('MultifunctionModel.mlpackage')
prediction = loaded_model.predict({'input': input_data}, function_name='function1')
For more detailed information, you can refer to the session Bring your machine learning and AI models to Apple silicon (26:31).
Additionally, the session Deploy machine learning and AI models on-device with Core ML (14:15) demonstrates a practical example of using multifunction models in an app, showcasing how different functions can be used to generate different styles of images.
Relevant Sessions:

Render Metal with passthrough in visionOS
Get ready to extend your Metal experiences for visionOS. Learn best practices for integrating your rendered content with people’s physical environments with passthrough. Find out how to position rendered content to match the physical world, reduce latency with trackable anchor prediction, and more.

Compose interactive 3D content in Reality Composer Pro
Discover how the Timeline view in Reality Composer Pro can bring your 3D content to life. Learn how to create an animated story in which characters and objects interact with each other and the world around them using inverse kinematics, blend shapes, and skeletal poses. We’ll also show you how to use built-in and custom actions, sequence your actions, apply triggers, and implement natural movements.

Explore object tracking for visionOS
Find out how you can use object tracking to turn real-world objects into virtual anchors in your visionOS app. Learn how you can build spatial experiences with object tracking from start to finish. Find out how to create a reference object using machine learning in Create ML and attach content relative to your target object in Reality Composer Pro, RealityKit or ARKit APIs.