Last active
July 29, 2021 16:47
-
-
Save pranavsharma/66186e986101022a7bb43201cd1e685e to your computer and use it in GitHub Desktop.
Test program for Issue 4070
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) Microsoft Corporation. All rights reserved. | |
// Licensed under the MIT License. | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.IO; | |
using Microsoft.ML.OnnxRuntime; | |
using Microsoft.ML.OnnxRuntime.Tensors; | |
namespace Issue4070 | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Using API"); | |
UseApi(); | |
Console.WriteLine("Done"); | |
} | |
static void UseApi() | |
{ | |
string modelPath = "C:\\Users\\prs\\work_projects\\ort_main\\csharp\\testdata\\squeezenet.onnx"; | |
// Optional : Create session options and set the graph optimization level for the session | |
SessionOptions options = new SessionOptions(); | |
options.GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_EXTENDED; | |
options.InterOpNumThreads = 1; | |
using (var session = new InferenceSession(modelPath, options)) | |
{ | |
var inputMeta = session.InputMetadata; | |
var container = new List<NamedOnnxValue>(); | |
float[] inputData = | |
LoadTensorFromFile("C:\\Users\\prs\\work_projects\\ort_main\\csharp\\testdata\\bench.in"); // this is the data for only one input tensor for this model | |
foreach (var name in inputMeta.Keys) | |
{ | |
var tensor = new DenseTensor<float>(inputData, inputMeta[name].Dimensions); | |
container.Add(NamedOnnxValue.CreateFromTensor<float>(name, tensor)); | |
} | |
// Run the inference | |
using (var results = session.Run(container)) // results is an IDisposableReadOnlyCollection<DisposableNamedOnnxValue> container | |
{ | |
// dump the results | |
foreach (var r in results) | |
{ | |
Console.WriteLine("Output for {0}", r.Name); | |
Console.WriteLine(r.AsTensor<float>().GetArrayString()); | |
} | |
} | |
} | |
} | |
static float[] LoadTensorFromFile(string filename) | |
{ | |
var tensorData = new List<float>(); | |
// read data from file | |
using (var inputFile = new System.IO.StreamReader(filename)) | |
{ | |
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment