Class ONNXImporter<T>
- Namespace
- AiDotNet.ModelLoading
- Assembly
- AiDotNet.dll
Imports weights from ONNX model files.
public class ONNXImporter<T>
Type Parameters
TThe numeric type used for calculations.
- Inheritance
-
ONNXImporter<T>
- Inherited Members
Remarks
For Beginners: ONNX (Open Neural Network Exchange) is a standard format for representing machine learning models.
Many pretrained models are distributed in ONNX format. This class extracts the learned weights from ONNX files so you can use them in your models.
Example usage:
var importer = new ONNXImporter<float>();
// Load weights from ONNX file
var weights = importer.LoadWeights("model.onnx");
// Apply to your model
var layer = new DenseLayer<float>(inputSize, outputSize);
importer.ApplyWeights(layer, weights);
Constructors
ONNXImporter(bool)
Initializes a new instance of the ONNXImporter class.
public ONNXImporter(bool verbose = false)
Parameters
verboseboolWhether to log import progress.
Methods
ApplyWeights(IWeightLoadable<T>, Dictionary<string, Tensor<T>>, Func<string, string?>?, bool)
Applies loaded weights to a model using IWeightLoadable.
public WeightLoadResult ApplyWeights(IWeightLoadable<T> model, Dictionary<string, Tensor<T>> weights, Func<string, string?>? mapping = null, bool strict = false)
Parameters
modelIWeightLoadable<T>The model to load weights into.
weightsDictionary<string, Tensor<T>>Dictionary of weights from LoadWeights.
mappingFunc<string, string>Optional name mapping function.
strictboolIf true, fails when weights can't be loaded.
Returns
- WeightLoadResult
Load result with statistics.
GetTensorInfo(string)
Gets information about tensors in an ONNX file without loading them.
public List<ONNXTensorInfo> GetTensorInfo(string onnxPath)
Parameters
onnxPathstringPath to the .onnx file.
Returns
- List<ONNXTensorInfo>
List of tensor metadata.
LoadWeights(string)
Loads all initializer tensors from an ONNX file.
public Dictionary<string, Tensor<T>> LoadWeights(string onnxPath)
Parameters
onnxPathstringPath to the .onnx file.
Returns
- Dictionary<string, Tensor<T>>
Dictionary mapping initializer names to tensors.
Exceptions
- ArgumentNullException
Thrown when onnxPath is null.
- FileNotFoundException
Thrown when the file doesn't exist.
- InvalidOperationException
Thrown when the file is not a valid ONNX file.