This repository was archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathOnnxModelSession.cs
137 lines (110 loc) · 4.31 KB
/
OnnxModelSession.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using Microsoft.ML.OnnxRuntime;
using OnnxStack.Core.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace OnnxStack.Core.Model
{
public class OnnxModelSession : IDisposable
{
private readonly SessionOptions _options;
private readonly OnnxModelConfig _configuration;
private OnnxMetadata _metadata;
private InferenceSession _session;
/// <summary>
/// Initializes a new instance of the <see cref="OnnxModelSession"/> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <exception cref="System.IO.FileNotFoundException">Onnx model file not found</exception>
public OnnxModelSession(OnnxModelConfig configuration)
{
if (!File.Exists(configuration.OnnxModelPath))
throw new FileNotFoundException("Onnx model file not found", configuration.OnnxModelPath);
_configuration = configuration;
_options = configuration.GetSessionOptions();
_options.RegisterOrtExtensions();
}
/// <summary>
/// Gets the SessionOptions.
/// </summary>
public SessionOptions Options => _options;
/// <summary>
/// Gets the InferenceSession.
/// </summary>
public InferenceSession Session => _session;
/// <summary>
/// Gets the configuration.
/// </summary>
public OnnxModelConfig Configuration => _configuration;
/// <summary>
/// Loads the model session.
/// </summary>
public async Task LoadAsync()
{
if (_session is not null)
return; // Already Loaded
_session = await Task.Run(() => new InferenceSession(_configuration.OnnxModelPath, _options));
}
/// <summary>
/// Unloads the model session.
/// </summary>
/// <returns></returns>
public async Task UnloadAsync()
{
// TODO: deadlock on model dispose when no synchronization context exists(console app)
// Task.Yield seems to force a context switch resolving any issues, revist this
await Task.Yield();
if (_session is not null)
{
_session.Dispose();
_metadata = null;
_session = null;
}
}
/// <summary>
/// Gets the metadata.
/// </summary>
/// <returns></returns>
public async Task<OnnxMetadata> GetMetadataAsync()
{
if (_metadata is not null)
return _metadata;
if (_session is null)
await LoadAsync();
_metadata = new OnnxMetadata
{
Inputs = _session.InputMetadata.Select(OnnxNamedMetadata.Create).ToList(),
Outputs = _session.OutputMetadata.Select(OnnxNamedMetadata.Create).ToList()
};
return _metadata;
}
/// <summary>
/// Runs inference on the model with the suppied parameters, use this method when you do not have a known output shape.
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public IDisposableReadOnlyCollection<OrtValue> RunInference(OnnxInferenceParameters parameters)
{
return _session.Run(parameters.RunOptions, parameters.InputNameValues, parameters.OutputNames);
}
/// <summary>
/// Runs inference on the model with the suppied parameters, use this method when the output shape is known
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public Task<IReadOnlyCollection<OrtValue>> RunInferenceAsync(OnnxInferenceParameters parameters)
{
return _session.RunAsync(parameters.RunOptions, parameters.InputNames, parameters.InputValues, parameters.OutputNames, parameters.OutputValues);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
_options?.Dispose();
_session?.Dispose();
}
}
}