onnxruntime/csharp/test/Microsoft.AI.MachineLearning.Tests.CSharp/main.cs
Sheil Kumar fdb4a3a2e8
Add cppwinrt and cswinrt tests in windowsai nuget pipeline (#4381)
* build e2e cppwinrt tests

* add use nuget task

* make all referenced to package version prop/target-ified

* remove dupe props/targets reference

* work around project.assets.json error by deleting it

* powershell test invocation

* switch to batch script

* print debug info

* update x86->x64

* stdio.h

* pushd/popd

* add csharp tests

* package.config -> packages.config

* typo

* x86 -> anycpu

* debug is default

* add test path

* update csproj as well

* debug

* really replace all package versions

* debug output

* really use [PackageVersion]

* sleep intead of converting async operation to task and waiting

* dont close software bitmap

* switch to powershell script

* remove binding check

* continue on failure

* continuse on error action

* continueOnError and errorActionPreference

* tabbing

Co-authored-by: Sheil Kumar <sheilk@microsoft.com>
2020-07-07 09:36:42 -07:00

92 lines
5.5 KiB
C#

using System;
using System.IO;
using Microsoft.AI.MachineLearning;
using WinRT;
namespace Microsoft.AI.MachineLearning.Tests
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Load squeezenet.onnx.");
using (var model = LearningModel.LoadFromFilePath("squeezenet.onnx"))
{
Console.WriteLine("Load kitten_224.png as StorageFile.");
var name = AppDomain.CurrentDomain.BaseDirectory + "kitten_224.png";
var image_task = Windows.Storage.StorageFile.GetFileFromPathAsync(name);
image_task.AsTask().Wait();
var image = image_task.GetResults();
Console.WriteLine("Load StorageFile into Stream.");
var stream_task = image.OpenReadAsync();
System.Threading.Thread.Sleep(1000);
// stream_task.AsTask().Wait();
//
// Unable to call AsTask on IAsyncOperation<IRandomAccessStreamWithContentType>...
// System.TypeInitializationException: 'The type initializer for 'ABI.Windows.Foundation.AsyncOperationCompletedHandler`1' threw an exception.'
// This exception was originally thrown at this call stack:
// System.RuntimeType.ThrowIfTypeNeverValidGenericArgument(System.RuntimeType)
// System.RuntimeType.SanityCheckGenericArguments(System.RuntimeType[], System.RuntimeType[])
// System.RuntimeType.MakeGenericType(System.Type[])
// System.Linq.Expressions.Compiler.DelegateHelpers.MakeNewDelegate(System.Type[])
// System.Linq.Expressions.Compiler.DelegateHelpers.MakeDelegateType(System.Type[])
// ABI.Windows.Foundation.AsyncOperationCompletedHandler<TResult>.AsyncOperationCompletedHandler()
//
// So sleep instead...
using (var stream = stream_task.GetResults())
{
Console.WriteLine("Create SoftwareBitmap from decoded Stream.");
var decoder_task = Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
System.Threading.Thread.Sleep(1000);
// decoder_task.AsTask().Wait();
//
// Unable to call AsTask on IAsyncOperation<SoftwareBitmap>...
// System.TypeInitializationException: 'The type initializer for 'ABI.Windows.Foundation.AsyncOperationCompletedHandler`1' threw an exception.'
// This exception was originally thrown at this call stack:
// System.RuntimeType.ThrowIfTypeNeverValidGenericArgument(System.RuntimeType)
// System.RuntimeType.SanityCheckGenericArguments(System.RuntimeType[], System.RuntimeType[])
// System.RuntimeType.MakeGenericType(System.Type[])
// System.Linq.Expressions.Compiler.DelegateHelpers.MakeNewDelegate(System.Type[])
// System.Linq.Expressions.Compiler.DelegateHelpers.MakeDelegateType(System.Type[])
// ABI.Windows.Foundation.AsyncOperationCompletedHandler<TResult>.AsyncOperationCompletedHandler()
//
// So sleep instead...
var decoder = decoder_task.GetResults();
var software_bitmap_task = decoder.GetSoftwareBitmapAsync();
System.Threading.Thread.Sleep(1000);
// software_bitmap_task.AsTask().Wait();
//
// Unable to call AsTask on IAsyncOperation<SoftwareBitmap>...
// System.TypeInitializationException: 'The type initializer for 'ABI.Windows.Foundation.AsyncOperationCompletedHandler`1' threw an exception.'
// This exception was originally thrown at this call stack:
// System.RuntimeType.ThrowIfTypeNeverValidGenericArgument(System.RuntimeType)
// System.RuntimeType.SanityCheckGenericArguments(System.RuntimeType[], System.RuntimeType[])
// System.RuntimeType.MakeGenericType(System.Type[])
// System.Linq.Expressions.Compiler.DelegateHelpers.MakeNewDelegate(System.Type[])
// System.Linq.Expressions.Compiler.DelegateHelpers.MakeDelegateType(System.Type[])
// ABI.Windows.Foundation.AsyncOperationCompletedHandler<TResult>.AsyncOperationCompletedHandler()
//
// So sleep instead...
using (var software_bitmap = software_bitmap_task.GetResults())
{
Console.WriteLine("Create VideoFrame.");
var frame = Windows.Media.VideoFrame.CreateWithSoftwareBitmap(software_bitmap);
Console.WriteLine("Create LearningModelSession.");
using (var session = new LearningModelSession(model))
{
Console.WriteLine("Create LearningModelBinding.");
var binding = new LearningModelBinding(session);
Console.WriteLine("Bind data_0.");
binding.Bind("data_0", frame);
Console.WriteLine("Evaluate.");
var results = session.Evaluate(binding, "");
}
Console.WriteLine("Success!\n");
}
}
}
}
}
}