2023-08-02 17:06:42 +00:00
|
|
|
|
using Microsoft.ML.OnnxRuntime.Tensors;
|
|
|
|
|
|
using System;
|
2021-10-27 10:07:07 +00:00
|
|
|
|
using System.Collections.Generic;
|
2023-06-29 15:59:23 +00:00
|
|
|
|
using System.Diagnostics;
|
2021-10-27 10:07:07 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.ML.OnnxRuntime.InferenceSample
|
|
|
|
|
|
{
|
|
|
|
|
|
public class InferenceSampleApi : IDisposable
|
|
|
|
|
|
{
|
|
|
|
|
|
public InferenceSampleApi()
|
|
|
|
|
|
{
|
2023-06-29 15:59:23 +00:00
|
|
|
|
_model = LoadModelFromEmbeddedResource("TestData.squeezenet.onnx");
|
2021-10-27 10:07:07 +00:00
|
|
|
|
|
|
|
|
|
|
// this is the data for only one input tensor for this model
|
2023-06-29 15:59:23 +00:00
|
|
|
|
var inputData = LoadTensorFromEmbeddedResource("TestData.bench.in");
|
2021-10-27 10:07:07 +00:00
|
|
|
|
|
|
|
|
|
|
// create default session with default session options
|
|
|
|
|
|
// Creating an InferenceSession and loading the model is an expensive operation, so generally you would
|
|
|
|
|
|
// do this once. InferenceSession.Run can be called multiple times, and concurrently.
|
|
|
|
|
|
CreateInferenceSession();
|
|
|
|
|
|
|
|
|
|
|
|
// setup sample input data
|
2023-06-29 15:59:23 +00:00
|
|
|
|
var inputMeta = _inferenceSession.InputMetadata;
|
|
|
|
|
|
_inputData = new List<OrtValue>(inputMeta.Count);
|
|
|
|
|
|
_orderedInputNames = new List<string>(inputMeta.Count);
|
|
|
|
|
|
|
2021-10-27 10:07:07 +00:00
|
|
|
|
foreach (var name in inputMeta.Keys)
|
|
|
|
|
|
{
|
2023-06-29 15:59:23 +00:00
|
|
|
|
// We create an OrtValue in this case over the buffer of potentially different shapes.
|
|
|
|
|
|
// It is Okay as long as the specified shape does not exceed the actual length of the buffer
|
|
|
|
|
|
var shape = Array.ConvertAll<int, long>(inputMeta[name].Dimensions, Convert.ToInt64);
|
2023-08-02 17:06:42 +00:00
|
|
|
|
Debug.Assert(ShapeUtils.GetSizeForShape(shape) <= inputData.LongLength);
|
2023-06-29 15:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
var ortValue = OrtValue.CreateTensorValueFromMemory(inputData, shape);
|
|
|
|
|
|
_inputData.Add(ortValue);
|
|
|
|
|
|
|
|
|
|
|
|
_orderedInputNames.Add(name);
|
2021-10-27 10:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void CreateInferenceSession(SessionOptions options = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Optional : Create session options and set any relevant values.
|
|
|
|
|
|
// If an additional execution provider is needed it should be added to the SessionOptions prior to
|
|
|
|
|
|
// creating the InferenceSession. The CPU Execution Provider is always added by default.
|
|
|
|
|
|
if (options == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
options = new SessionOptions { LogId = "Sample" };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-29 15:59:23 +00:00
|
|
|
|
_inferenceSession = new InferenceSession(_model, options);
|
2021-10-27 10:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Execute()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Run the inference
|
2023-06-29 15:59:23 +00:00
|
|
|
|
// 'results' is an IDisposableReadOnlyCollection<OrtValue> container
|
|
|
|
|
|
using (var results = _inferenceSession.Run(null, _orderedInputNames, _inputData, _inferenceSession.OutputNames))
|
2021-10-27 10:07:07 +00:00
|
|
|
|
{
|
|
|
|
|
|
// dump the results
|
2023-06-29 15:59:23 +00:00
|
|
|
|
for (int i = 0; i < results.Count; ++i)
|
2021-10-27 10:07:07 +00:00
|
|
|
|
{
|
2023-06-29 15:59:23 +00:00
|
|
|
|
var name = _inferenceSession.OutputNames[i];
|
|
|
|
|
|
Console.WriteLine("Output for {0}", name);
|
|
|
|
|
|
// We can now access the native buffer directly from the OrtValue, no copy is involved.
|
|
|
|
|
|
// Spans are structs and are stack allocated. They do not add any GC pressure.
|
|
|
|
|
|
ReadOnlySpan<float> span = results[i].GetTensorDataAsSpan<float>();
|
|
|
|
|
|
Console.Write($"Input {i} results:");
|
|
|
|
|
|
for(int k = 0; k < span.Length; ++k)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.Write($" {span[k]}");
|
|
|
|
|
|
}
|
|
|
|
|
|
Console.WriteLine();
|
2021-10-27 10:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
|
{
|
2023-06-29 15:59:23 +00:00
|
|
|
|
if (disposing && !_disposed)
|
2021-10-27 10:07:07 +00:00
|
|
|
|
{
|
2023-06-29 15:59:23 +00:00
|
|
|
|
_inferenceSession?.Dispose();
|
|
|
|
|
|
_inferenceSession = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (_inputData != null)
|
|
|
|
|
|
foreach(var v in _inputData)
|
|
|
|
|
|
{
|
|
|
|
|
|
v?.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_disposed = true;
|
2021-10-27 10:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
Dispose(true);
|
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static float[] LoadTensorFromEmbeddedResource(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
var tensorData = new List<float>();
|
|
|
|
|
|
var assembly = typeof(InferenceSampleApi).Assembly;
|
|
|
|
|
|
|
|
|
|
|
|
using (var inputFile =
|
|
|
|
|
|
new StreamReader(assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{path}")))
|
|
|
|
|
|
{
|
|
|
|
|
|
inputFile.ReadLine(); // skip the input name
|
|
|
|
|
|
string[] dataStr = inputFile.ReadLine().Split(new char[] { ',', '[', ']' },
|
|
|
|
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
for (int i = 0; i < dataStr.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
tensorData.Add(Single.Parse(dataStr[i]));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return tensorData.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static byte[] LoadModelFromEmbeddedResource(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
var assembly = typeof(InferenceSampleApi).Assembly;
|
|
|
|
|
|
byte[] model = null;
|
|
|
|
|
|
|
|
|
|
|
|
using (Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{path}"))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.CopyTo(memoryStream);
|
|
|
|
|
|
model = memoryStream.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return model;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-29 15:59:23 +00:00
|
|
|
|
private bool _disposed = false;
|
|
|
|
|
|
private readonly byte[] _model;
|
|
|
|
|
|
private readonly List<string> _orderedInputNames;
|
|
|
|
|
|
private readonly List<OrtValue> _inputData;
|
|
|
|
|
|
private InferenceSession _inferenceSession;
|
2021-10-27 10:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|