![]() |
VOOZH | about |
dotnet add package Plugin.Maui.ML --version 1.2.0
NuGet\Install-Package Plugin.Maui.ML -Version 1.2.0
<PackageReference Include="Plugin.Maui.ML" Version="1.2.0" />
<PackageVersion Include="Plugin.Maui.ML" Version="1.2.0" />Directory.Packages.props
<PackageReference Include="Plugin.Maui.ML" />Project file
paket add Plugin.Maui.ML --version 1.2.0
#r "nuget: Plugin.Maui.ML, 1.2.0"
#:package Plugin.Maui.ML@1.2.0
#addin nuget:?package=Plugin.Maui.ML&version=1.2.0Install as a Cake Addin
#tool nuget:?package=Plugin.Maui.ML&version=1.2.0Install as a Cake Tool
A comprehensive .NET MAUI plugin that provides ML inference capabilities with support for multiple backends including ONNX Runtime, CoreML, and platform-native acceleration.
๐ NuGet Version
๐ GitHub Actions Workflow Status
graph TB
A["IMLInfer Interface"] --> B["OnnxRuntimeInfer"]
A --> C["CoreMLInfer (iOS/macOS)"]
A --> D["MLKitInfer (Android) - Coming Soon"]
A --> E["WindowsMLInfer (Windows) - Coming Soon"]
B --> F["Cross-Platform ONNX Models"]
C --> G["Apple Vision/CoreML Models"]
D --> H["TFLite/MLKit Models"]
E --> I["Windows ML Models"]
B -.->|Uses| J["CoreML EP (iOS/macOS)"]
B -.->|Uses| K["NNAPI EP (Android)"]
B -.->|Uses| L["DirectML EP (Windows)"]
style A fill:#4CAF50,stroke:#2E7D32,color:#fff
style B fill:#2196F3,stroke:#1565C0,color:#fff
style C fill:#FF9800,stroke:#E65100,color:#fff
style D fill:#9C27B0,stroke:#4A148C,color:#fff
style E fill:#00BCD4,stroke:#006064,color:#fff
Legend:
<PackageReference Include="Plugin.Maui.ML" Version="1.0.0" />
// In your MauiProgram.cs
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
// Register ML services with platform-optimized defaults
builder.Services.AddMauiML(config =>
{
config.EnablePerformanceLogging = true;
config.MaxConcurrentInferences = 2;
});
// Or specify a backend explicitly
// builder.Services.AddMauiML(MLBackend.CoreML); // iOS/macOS only
return builder.Build();
}
}
public class MainPage : ContentPage
{
private readonly IMLInfer _mlService;
public MainPage(IMLInfer mlService)
{
_mlService = mlService;
InitializeComponent();
}
private async void OnPredictClicked(object sender, EventArgs e)
{
try
{
// Check which backend is being used
Console.WriteLine($"Using backend: {_mlService.Backend}");
// Load your model (ONNX or platform-specific format)
await _mlService.LoadModelFromAssetAsync("my-model.onnx");
// Prepare input tensors
var inputData = new float[] { 1.0f, 2.0f, 3.0f, 4.0f };
var inputTensor = TensorHelper.CreateTensor(inputData, new int[] { 1, 4 });
var inputs = new Dictionary<string, Tensor<float>>
{
["input"] = inputTensor
};
// Run inference
var results = await _mlService.RunInferenceAsync(inputs);
// Process results
foreach (var output in results)
{
var outputData = TensorHelper.ToArray(output.Value);
Console.WriteLine($"Output '{output.Key}': [{string.Join(", ", outputData)}]");
}
}
catch (Exception ex)
{
await DisplayAlert("Error", ex.Message, "OK");
}
}
}
Before using Plugin.Maui.ML, you'll need ONNX model files. You can:
We provide the HFOnnxTool to make this easy:
# Download a model that already has ONNX files
cd tools/HFOnnxTool
dotnet run -- fetch --repo sentence-transformers/all-MiniLM-L6-v2 --output ./models
# Or convert a model to ONNX format
dotnet run -- convert --repo d4data/biomedical-ner-all --task token-classification --output ./onnx
๐ See the for detailed instructions on:
inspect, fetch, and convert commands| Backend | Platforms | Status | Hardware Acceleration |
|---|---|---|---|
| ONNX Runtime | All | โ Stable | CoreML (iOS/macOS), NNAPI (Android), DirectML (Windows) |
| CoreML | iOS/macOS | โ Stable | Apple Neural Engine (A12+, M1+) |
| ML Kit | Android | ๐ง Coming Soon | NNAPI, GPU delegates |
| Windows ML | Windows | ๐ง Coming Soon | DirectML, DirectX 12 |
// Automatic platform-optimized selection (recommended)
builder.Services.AddMauiML();
// Explicit backend
builder.Services.AddMauiML(MLBackend.CoreML); // iOS/macOS only
// Configuration-based
builder.Services.AddMauiML(config =>
{
config.PreferredBackend = MLBackend.CoreML; // Falls back to default if not available
});
// Runtime selection
IMLInfer onnxInfer = new OnnxRuntimeInfer();
#if IOS || MACCATALYST
IMLInfer coreMLInfer = new CoreMLInfer();
#endif
| Backend | Formats | Notes |
|---|---|---|
| ONNX Runtime | .onnx |
Cross-platform, largest model zoo |
| CoreML | .mlmodel, .mlmodelc |
Native iOS/macOS format |
| ML Kit | .tflite |
Android-optimized |
See for detailed information about backends, model conversion, and performance optimization.
The main interface for ML inference operations.
MLBackend Backend { get; } - Get the current backend typebool IsModelLoaded { get; } - Check if a model is currently loadedTask LoadModelAsync(string modelPath, CancellationToken cancellationToken = default)
Task LoadModelAsync(Stream modelStream, CancellationToken cancellationToken = default)
Task LoadModelFromAssetAsync(string assetName, CancellationToken cancellationToken = default)
Task<Dictionary<string, Tensor<float>>> RunInferenceAsync(Dictionary<string, Tensor<float>> inputs, CancellationToken cancellationToken = default)
Task<Dictionary<string, Tensor<float>>> RunInferenceLongInputsAsync(Dictionary<string, Tensor<long>> inputs, CancellationToken cancellationToken = default)
Dictionary<string, NodeMetadata> GetInputMetadata()
Dictionary<string, NodeMetadata> GetOutputMetadata()
void UnloadModel()
Helper utilities for working with tensors.
static Tensor<float> CreateTensor(float[] data, int[] dimensions)
static Tensor<float> CreateTensor(float[,] data)
static Tensor<float> CreateTensor(float[,,] data)
static float[] ToArray(Tensor<float> tensor)
static string GetShapeString(Tensor<float> tensor)
static Tensor<float> Reshape(Tensor<float> tensor, int[] newDimensions)
static Tensor<float> Normalize(Tensor<float> tensor)
static Tensor<float> Softmax(Tensor<float> tensor)
Configure the ML services with MLConfiguration:
builder.Services.AddMauiML(config =>
{
config.UseTransientService = false; // Use singleton (default)
config.EnablePerformanceLogging = true;
config.MaxConcurrentInferences = Environment.ProcessorCount;
config.DefaultModelAssetPath = "models/default-model.onnx";
config.PreferredBackend = MLBackend.CoreML; // Optional: specify preferred backend
});
PreferredBackend: Preferred ML backend (default: null = platform default)UseTransientService: Whether to use transient service lifetime (default: false, uses singleton)EnablePerformanceLogging: Enable performance logging (default: false)MaxConcurrentInferences: Maximum number of concurrent inference operations (default: processor count)DefaultModelAssetPath: Default model asset path (default: null)PlatformMLInfer.IsNnapiAvailable().mlmodel files with CoreMLInferPlatformMLInfer.IsNeuralEngineAvailable()PlatformMLInfer.IsDirectX12Available(), PlatformMLInfer.GetSystemInfo()Check out the sample projects in the samples/ directory:
Run the comprehensive test suite:
dotnet test tests/Plugin.Maui.ML.Tests/
The test suite includes:
UnloadModel() when done with a model// Check backend capabilities
#if IOS
if (Platforms.iOS.PlatformMLInfer.IsNeuralEngineAvailable())
{
Console.WriteLine("Running on Apple Neural Engine!");
}
#endif
// Profile inference time
var sw = Stopwatch.StartNew();
var result = await _mlService.RunInferenceAsync(inputs);
sw.Stop();
Console.WriteLine($"Inference took: {sw.ElapsedMilliseconds}ms");
Model Loading Errors
Backend Not Available
_mlService.Backend to see what's being usedMLPlugin.Default for automatic platform selectionMemory Issues
UnloadModel() when done with a modelPlatform-Specific Issues
IsNnapiAvailable()IsDirectX12Available()We welcome contributions! Please see our for details.
This project is licensed under the MIT License - see the file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-android35.0 net9.0-android35.0 is compatible. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-ios18.0 net9.0-ios18.0 is compatible. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-maccatalyst18.0 net9.0-maccatalyst18.0 is compatible. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net9.0-windows10.0.19041 net9.0-windows10.0.19041 is compatible. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.