CoreML integration for iOS.
This module provides an Elixir API for using Apple's CoreML framework on iOS devices. CoreML is optimized by Apple and can use the Neural Engine for hardware-accelerated ML inference.
Prerequisites
- iOS device or simulator (CoreML is iOS-only)
- CoreML model file (.mlmodel or .mlpackage)
Converting models
You can convert models from other formats to CoreML:
Axon → ONNX → CoreML:
# Train with Axon model = Axon.input("input", shape: {nil, 784}) |> Axon.dense(10, activation: :softmax) {init_fn, predict_fn} = Axon.build(model) params = init_fn.(Nx.template({1, 784}, :f32), %{}) # Export to ONNX (requires ortex or onnx package) # Then convert ONNX to CoreML using Apple's coremltools (Python)Use pre-trained CoreML models from Apple or third parties.
Usage
# Load a model
:ok = Dala.ML.CoreML.load_model("/path/to/model.mlmodel", "my_model")
# Check if loaded
true = Dala.ML.CoreML.loaded?("my_model")
# Make prediction
{:ok, result_json} = Dala.ML.CoreML.predict("my_model", %{
"input": [1.0, 2.0, 3.0]
})
# Unload when done
:ok = Dala.ML.CoreML.unload_model("my_model")
Summary
Functions
Loads a CoreML model from the given path.
Checks if a model is loaded.
Lists all loaded model identifiers.
Makes a prediction using a loaded model.
Convenience function to load and predict in one call.
Unloads a previously loaded model.
Functions
Loads a CoreML model from the given path.
Parameters
model_path: Path to the .mlmodel or .mlpackage fileidentifier: A unique identifier for this model (used in later calls)
Returns
:okif successful{:error, reason}if failed
Checks if a model is loaded.
Parameters
identifier: The model identifier
Returns
true if the model is loaded, false otherwise.
Returns false on non-iOS platforms.
Lists all loaded model identifiers.
Returns
A list of model identifiers, or :none on non-iOS platforms.
Makes a prediction using a loaded model.
Parameters
identifier: The model identifierinputs: A map of input names to values
Input values can be:
- Numbers (floats/integers)
- Strings
- Lists (converted to MLMultiArray)
- Base64-encoded data (for large multi-array inputs)
Returns
{:ok, result_json} where result_json is a JSON string of the outputs.
{:error, reason} if the prediction fails.
:not_supported on non-iOS platforms.
Example
inputs = %{
"input1" => 1.0,
"input2" => [1.0, 2.0, 3.0]
}
case Dala.ML.CoreML.predict("my_model", inputs) do
{:ok, json} ->
result = Jason.decode!(json)
# Use result...
{:error, reason} ->
# Handle error...
:not_supported ->
# CoreML not available
end
Convenience function to load and predict in one call.
Note: Model must be loaded first using load_model/2.
Unloads a previously loaded model.
Parameters
identifier: The identifier used when loading the model