MLEdgeModel

class NatML.MLEdgeModel : MLModel

The MLEdgeModel represents an ML model that makes predictions on the local device. As such, it forms the basis for implementing edge predictors in code.

Creating the Model

The edge model can be created from a NatML Hub predictor, from a file, or from model data:

From NatML Hub

/// <summary>
/// Create an edge ML model.
/// </summary>
/// <param name="tagOrPath">Predictor tag or path to model file.</param>
/// <param name="configuration">Optional model configuration.</param>
/// <param name="accessKey">NatML access key.</param>
static Task<MLEdgeModel> Create (string tagOrPath, Configuration configuration = null, string accessKey = null);

The model can be created from a predictor on NatML Hub:

NatML Hub predictor catalog.

The predictor MUST have an active graph for your current platform.

From a Model File

The model can be created from an ML model file. Simply call the Create method and pass in a file path:

From Model Data

The model can be created from MLModelData instances, which are opaque representations of an ML model file in your Unity project.

Inspecting Feature Types

Edge models provide information about their expected input and output feature types. This type information is crucial for writing Edge predictors.

Input Features

Refer to the Input Features section of the MLModel class for more information.

Output Features

Refer to the Output Features section of the MLModel class for more information.

Inspecting Metadata

Refer to the Inspecting Metadata section of the MLModel class for more information.

Inspecting Classification Labels

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.

Inspecting Feature Normalization

Vision models often require that images be normalized to a specific mean and standard deviation. As such, MLEdgeModel defines a Normalization struct:

Normalization

When working with image features, the Normalization struct can be easily deconstructed:

Inspecting the Aspect Mode

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.

Inspecting the Audio Format

Audio and speech models often require or produce audio data with a specific sample rate and channel count. As such, MLEdgeModel defines an AudioFormat struct:

Audio Format

When working with audio features, the AudioFormat struct can be easily deconstructed like so:

Making Predictions

The MLEdgeModel exposes a Predict method which makes predictions on one or more MLEdgeFeature instances.

Instead of using MLEdgeFeature instances directly, we highly recommend using the managed feature classes instead (MLArrayFeature, MLAudioFeature, and so on).

Disposing the Model

Refer to the Disposing the Model section of the MLModel class for more information.

Embedding the Model

When fetching edge models from NatML Hub, the model graph must first be downloaded to the device before it is cached and loaded. For larger ML models or for users with poor internet, this download can take a long time. As such, the MLEdgeModel class defines the EmbedAttribute to embed the ML graph into the app binary at build time.

The attribute can be placed on any class or struct definition. At build time, NatML will find all such attributes, and embed the corresponding model data in the build. The attribute can be used like so:

Last updated

Was this helpful?