Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Microsoft.ML.OnnxRuntime.InferenceSample.Maui"
x:Class="Microsoft.ML.OnnxRuntime.InferenceSample.Maui.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Microsoft.ML.OnnxRuntime.InferenceSample.Maui;

public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="Microsoft.ML.OnnxRuntime.InferenceSample.Maui.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Microsoft.ML.OnnxRuntime.InferenceSample.Maui"
Shell.FlyoutBehavior="Disabled">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Microsoft.ML.OnnxRuntime.InferenceSample.Maui;

public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.ML.OnnxRuntime.InferenceSample.Maui.MainPage">

<StackLayout>
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
<Label Text="Inference Sample" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
</Frame>
<Label Text="ONNX Runtime" FontSize="Title" Padding="30,10,30,10"/>
<Button x:Name="Start" Text="Run tests" Clicked="Start_Clicked" FontSize="Large" />
<Label x:Name="OutputLabel" Text="Output" FontSize="Small"/>
</StackLayout>

</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
namespace Microsoft.ML.OnnxRuntime.InferenceSample.Maui;
//using Microsoft.Maui.Controls;

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();

// Best Practice: create the inference session (which loads and optimizes the model) once and not per inference
// as it can be expensive and time consuming.
inferenceSampleApi = new InferenceSampleApi();
}

protected override void OnAppearing()
{
base.OnAppearing();

OutputLabel.Text = "Press 'Run Tests'.\n";
}

private readonly InferenceSampleApi inferenceSampleApi;

private async Task ExecuteTests()
{
Action<Label, string> addOutput = (label, text) =>
{
Application.Current.Dispatcher.Dispatch(() => { label.Text += text; });
//Device.BeginInvokeOnMainThread(() => { label.Text += text; });
Console.Write(text);
};

OutputLabel.Text = "Testing execution\nComplete output is written to Console in this trivial example.\n\n";

// run the testing in a background thread so updates to the UI aren't blocked
await Task.Run(() =>
{
addOutput(OutputLabel, "Testing using default platform-specific session options... ");
inferenceSampleApi.Execute();
addOutput(OutputLabel, "done.\n");
Thread.Sleep(1000); // artificial delay so the UI updates gradually

// demonstrate a range of usages by recreating the inference session with different session options.
addOutput(OutputLabel, "Testing using default platform-specific session options... ");
inferenceSampleApi.CreateInferenceSession(SessionOptionsContainer.Create());
inferenceSampleApi.Execute();
addOutput(OutputLabel, "done.\n");
Thread.Sleep(1000);

addOutput(OutputLabel, "Testing using named platform-specific session options... ");
inferenceSampleApi.CreateInferenceSession(SessionOptionsContainer.Create("ort_with_npu"));
inferenceSampleApi.Execute();
addOutput(OutputLabel, "done.\n");
Thread.Sleep(1000);

addOutput(OutputLabel, "Testing using default platform-specific session options via ApplyConfiguration extension... ");
inferenceSampleApi.CreateInferenceSession(new SessionOptions().ApplyConfiguration());
inferenceSampleApi.Execute();
addOutput(OutputLabel, "done.\n");
Thread.Sleep(1000);

addOutput(OutputLabel, "Testing using named platform-specific session options via ApplyConfiguration extension... ");
inferenceSampleApi.CreateInferenceSession(new SessionOptions().ApplyConfiguration("ort_with_npu"));
inferenceSampleApi.Execute();
addOutput(OutputLabel, "done.\n\n");
Thread.Sleep(1000);
});

addOutput(OutputLabel, "Testing successfully completed! See the Console log for more info.");
}

private async void Start_Clicked(object sender, EventArgs e)
{
await ExecuteTests()
.ContinueWith(
(task) =>
{
if (task.IsFaulted)
MainThread.BeginInvokeOnMainThread(() => DisplayAlert("Error", task.Exception.Message, "OK"));
})
.ConfigureAwait(false);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Microsoft.ML.OnnxRuntime.InferenceSample.Maui;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

return builder.Build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- note net6.0-maccatalyst is not supported currently. requires a new native build to be added. -->
<TargetFrameworks>net6.0-android;net6.0-ios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<RootNamespace>Microsoft.ML.OnnxRuntime.InferenceSample.Maui</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>

<!-- Display name -->
<ApplicationTitle>InferenceSample_Maui</ApplicationTitle>

<!-- App Identifier -->
<ApplicationId>com.microsoft.ml.onnxruntime.inferencesample.maui</ApplicationId>
<ApplicationIdGuid>58af3884-1c25-42b7-b78b-30a65fb3cf69</ApplicationIdGuid>

<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
<DefaultLanguage>en</DefaultLanguage>
</PropertyGroup>

<!-- set ORT SelectedTargets to include .net6 target frameworks -->
<PropertyGroup>
<SelectedTargets>All</SelectedTargets>
</PropertyGroup>

<ItemGroup>
<!-- NOTE:
You need to manually put builds from other platforms such as Android in the correct place for this to work for cross-platform
builds such as running in the Android simulator.
The 'correct' place is defined by the OnnxRuntimeBuildDirectory property in Microsoft.ML.OnnxRuntime.csproj
-->
<ProjectReference Include="..\..\..\src\Microsoft.ML.OnnxRuntime\Microsoft.ML.OnnxRuntime.csproj" />
<ProjectReference Include="..\Microsoft.ML.OnnxRuntime.InferenceSample\Microsoft.ML.OnnxRuntime.InferenceSample.csproj" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="..\..\..\testdata\bench.in">
<Link>TestData\bench.in</Link>
</EmbeddedResource>
<EmbeddedResource Include="..\..\..\testdata\squeezenet.onnx">
<Link>TestData\squeezenet.onnx</Link>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />

<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />

<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31611.283
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.OnnxRuntime.InferenceSample.Maui", "Microsoft.ML.OnnxRuntime.InferenceSample.Maui.csproj", "{00338B12-B291-43A6-9F54-0174660FB70E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{00338B12-B291-43A6-9F54-0174660FB70E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{00338B12-B291-43A6-9F54-0174660FB70E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00338B12-B291-43A6-9F54-0174660FB70E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{00338B12-B291-43A6-9F54-0174660FB70E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00338B12-B291-43A6-9F54-0174660FB70E}.Release|Any CPU.Build.0 = Release|Any CPU
{00338B12-B291-43A6-9F54-0174660FB70E}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61F7FB11-1E47-470C-91E2-47F8143E1572}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Android.App;
using Android.Content.PM;
using Android.OS;

namespace Microsoft.ML.OnnxRuntime.InferenceSample.Maui;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Android.App;
using Android.Runtime;

namespace Microsoft.ML.OnnxRuntime.InferenceSample.Maui;

[Application]
public class MainApplication : MauiApplication
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#512BD4</color>
<color name="colorPrimaryDark">#2B0B98</color>
<color name="colorAccent">#2B0B98</color>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Foundation;

namespace Microsoft.ML.OnnxRuntime.InferenceSample.Maui;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIDeviceFamily</key>
<array>
<integer>1</integer>
<integer>2</integer>
</array>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>arm64</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>XSAppIconAssets</key>
<string>Assets.xcassets/appicon.appiconset</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using ObjCRuntime;
using UIKit;

namespace Microsoft.ML.OnnxRuntime.InferenceSample.Maui;

public class Program
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, typeof(AppDelegate));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<maui:MauiWinUIApplication
x:Class="Microsoft.ML.OnnxRuntime.InferenceSample.Maui.WinUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:maui="using:Microsoft.Maui"
xmlns:local="using:Microsoft.ML.OnnxRuntime.InferenceSample.Maui.WinUI">

</maui:MauiWinUIApplication>
Loading