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.

Inspecting Model Data

1
/// <summary>
2
/// NatML Hub predictor tag.
3
/// </summary>
4
string tag { get; }
Copied!
The model data exposes the NatML Hub predictor tag that it corresponds to.

Fetching Model Data

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. NatML Hub provides model data for both Edge (on-device) and Hub (server-side) predictors.
You can get your access key from your profile page on NatML Hub.
When loading Edge predictors, NatML caches the model graph on device. This means that a user only has to download the model graph once.

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 predictor does not have use 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 a Normalization struct:
1
struct Normalization {
2
/// <summary>
3
/// Per-channel normalization means.
4
/// </summary>
5
float[] mean { get; }
6
/// <summary>
7
/// Per-channel normalization standard deviations.
8
/// </summary>
9
float[] std { get; }
10
}
Copied!
When working with image features, the Normalization struct can be easily 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:
1
struct AudioFormat {
2
/// <summary>
3
/// Sample rate.
4
/// </summary>
5
int sampleRate { get; }
6
/// <summary>
7
/// Channel count.
8
/// </summary>
9
int channelCount { get; }
10
}
Copied!
When working with audio features, the AudioFormat struct can be easily deconstructed like so:
1
// Get the model's audio format
2
int sampleRate, channelCount;
3
(sampleRate, channelCount) = modelData.audioFormat;
Copied!
Last modified 18h ago