NatML
Search…
MLModelData
class NatSuite.ML.MLModelData
The MLModelData class is a self-contained archive containing an MLModel along with supplemental data that is useful to make predictions with the model.

Fetching Model Data

NatML supports fetching model data from multiple sources.

From NatML Hub

1
/// <summary>
2
/// Fetch ML model data from NatML hub.
3
/// </summary>
4
/// <param name="tag">Model tag.</param>
5
/// <param name="accessKey">Hub access key.</param>
6
/// <returns>ML model data.</returns>
7
static Task<MLModelData> FromHub (string tag, string accessKey = ...);
Copied!
NatML provides a model hosting, delivery, and analytics service called NatML Hub. Hub is crucial when taking ML into production, because embedding ML models in the app can very quickly explode the bundle size. Furthermore, Hub is able to deliver models optimized specifically for individual devices, and provide important analytics on model performance.
NatML caches models that are loaded from Hub, meaning that a user only has to download the model once.

From File

1
/// <summary>
2
/// Fetch ML model data from a local file.
3
/// </summary>
4
/// <param name="path">Path to ONNX model file.</param>
5
/// <returns>ML model data.</returns>
6
static Task<MLModelData> FromFile (string path);
Copied!
NatML allows for loading ML model data from files. Currently, only .onnx model files are supported.
Model data loaded from a file will contain no supplemental data. As such, it is highly recommended to use NatML Hub instead.

From StreamingAssets

1
/// <summary>
2
/// Fetch ML model data from StreamingAssets.
3
/// </summary>
4
/// <param name="path">Relative path to ONNX model file in `StreamingAssets` folder.</param>
5
/// <returns>ML model data.</returns>
6
static Task<MLModelData> FromStreamingAssets (string relativePath);
Copied!
ML model data can also be loaded from StreamingAssets. This function accepts the relative path of the model file.
Model data loaded from StreamingAssets will contain no supplemental data. As such, it is highly recommended to use NatML Hub instead.

Creating a Model

1
/// <summary>
2
/// Deserialize the model data to create an ML model that can be used for prediction.
3
/// You MUST dispose the model once you are done with it.
4
/// </summary>
5
/// <returns>ML model.</returns>
6
MLModel Deserialize ();
Copied!
An MLModel is created from model data. The model can then be used with a predictor to make predictions.
You must Dispose the model when you are done with it. Failing to do so will result in severe resource leaks.

Using Supplemental Data

Model data contains additional information needed to make a prediction with a model.

Classification Labels

1
/// <summary>
2
/// Model classification labels.
3
/// This is `null` if the model does not have any classification labels.
4
/// </summary>
5
string[] labels { get; }
Copied!
For classification and detection models, this field contains the list of class labels associated with each class in the output distribution. If class labels don't apply to the model, it will return null.

Feature Normalization

1
/// <summary>
2
/// Expected feature normalization for predictions with this model.
3
/// </summary>
4
Normalization normalization { get; }
Copied!
Vision models often require that images be normalized to a specific mean and standard deviation. As such, MLModelData includes an opaque normalization struct that can be deconstructed like so:
1
// Get the model's preferred image normalization
2
Vector3 mean, std;
3
(mean, std) = modelData.normalization;
Copied!

Image Aspect Mode

1
/// <summary>
2
/// Expected image aspect mode for predictions with this model.
3
/// </summary>
4
MLImageFeature.AspectMode aspectMode { get; }
Copied!
Vision models might require that input image features be scaled a certain way when they are resized to fit the model's input size. The aspectMode can be passed directly to an MLImageFeature.

Audio Format

1
/// <summary>
2
/// Expected audio format for predictions with this model.
3
/// </summary>
4
AudioFormat audioFormat { get; }
Copied!
Audio and speech models often require or produce audio data with a specific sample rate and channel count. As such, MLModelData provides an audio format struct that can be deconstructed like so:
1
// Get the model's audio format
2
int sampleRate, channelCount;
3
(sampleRate, channelCount) = modelData.audioFormat;
Copied!
Last modified 2mo ago