NatML
Search…
MLEdgeModel
class NatML.Internal.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.

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

/// <summary>
/// Model input feature types.
/// </summary>
MLFeatureType[] inputs { get; }
Refer to the Input Features section of the MLModel class for more information.

Output Features

/// <summary>
/// Model output feature types.
/// </summary>
MLFeatureType[] outputs { get; }
Refer to the Output Features section of the MLModel class for more information.

Inspecting Metadata

/// <summary>
/// Get the model metadata dictionary.
/// </summary>
IReadOnlyDictionary<string, string> metadata { get; }
Refer to the Inspecting Metadata section of the MLModel class for more information.

Making Edge Predictions

/// <summary>
/// Make a prediction on one or more Edge ML features.
/// Input and output features MUST be disposed when no longer needed.
/// </summary>
/// <param name="inputs">Input Edge ML features.</param>
/// <returns>Output Edge ML features.</returns>
MLFeatureCollection<MLEdgeFeature> Predict (params MLEdgeFeature[] inputs);
The Edge model exposes a Predict method which makes predictions on one or more MLEdgeFeature instances. It returns a collection of MLEdgeFeature instances, which can then be used with corresponding feature classes (MLArrayFeature, MLAudioFeature, and so on).
The input and output features MUST be disposed when they are no longer needed. Call Dispose on the individual features, or on the returned feature collection to do so.

Disposing the Model

/// <summary>
/// Dispose the model and release resources.
/// </summary>
void Dispose ();
Refer to the Disposing the Model section of the MLModel class for more information.

Implementing Edge Predictors

As you might have noticed above, MLEdgeModel instances typically won't be used directly. Instead, they will be used through Edge Predictors, which are lightweight classes that can transform input data into the model's expected input features; and can transform the model's output features into easily usable types. Below are the general steps in implementing Edge predictors:

Defining the Predictor

All Edge predictors must inherit from the IMLPredictor<TOutput> interface. The predictor has a single generic type argument, TOutput, which is a developer-friendly type that is returned when a prediction is made. For example, the MobileNetv2Predictor predictor class which classifies an image uses a tuple for its output type:
// The MobileNetv2 classification predictor returns a tuple
class MobileNetv2Predictor : IMLPredictor<(string label, float confidence)> { ... }

Defining Constructors

All Edge predictors must define one or more constructors that accept one or more MLModel instances, along with any other supplemental data needed to make predictions with the model(s). For example:
/// <summary>
/// Create a custom predictor.
/// </summary>
/// <param name="model">ML model used to make predictions.</param>
public CustomPredictor (MLModel model) { ... }
Within the constructor, the model should cast the MLModel to an MLEdgeModel and store a readonly reference to the edge model:
// Store a reference to the Edge model
private readonly MLEdgeModel model;

Making Predictions

All Edge predictors must implement a public Predict method which accepts a params MLFeature[] and returns a TOutput, for example:
/// <summary>
/// Make a prediction with the model.
/// </summary>
/// <param name="inputs">Input feature.</param>
/// <returns>Output label with unnormalized confidence value.</returns>
public (string label, float confidence) Predict (params MLFeature[] inputs);
Within the Predict method, the predictor should do three things:

Input Checking

The predictor should check that the client has provided the correct number of input features, and that the features have the model's expected types.
If these checks fail, an appropriate exception should be thrown. Do this instead of returning an un-initialized output.

Prediction

To make predictions, the predictor must create MLEdgeFeature instances from input features. Creating an MLEdgeFeature typically requires a corresponding MLFeatureType which dictates any required pre-processing when creating the edge feature. You will typically use the model's input feature types for this purpose:
// Get or create the native feature type which the model expects
MLFeatureType inputType = model.inputs[0];
// Create an Edge feature from the input feature
using MLEdgeFeature edgeFeature = (inputFeature as IMLEdgeFeature).Create(inputType);
To check if a feature can be used for Edge predictions, cast it to an IMLEdgeFeature and check that the result of the cast is not null.
Once you have created all the required Edge features, you can then make predictions with the MLEdgeModel:
// Make a prediction with one or more native input features
using var outputFeatures = model.Predict(edgeFeature);

Marshaling

Once you have output Edge features from the model, you can then marshal the feature data into a more developer-friendly type. This is where most of the heavy-lifting happens in a predictor:
// Marshal the output feature data into a developer-friendly type
var arrayFeature = new MLArrayFeature<float>(outputFeatures[0]);
// Do stuff with this data...
...
Finally, return your predictor's output:
// Create the prediction result from the output data
TOutput result = ...;
// Return it
return result;

Disposing the Predictor

All Edge predictors must define a Dispose method, because IMLPredictor implements the IDisposable interface. This method should be used to dispose any explicitly-managed resources used by the predictor. If a predictor does not have any explicitly-managed resources to dispose, then the predictor should hide the Dispose method using interface hiding:
// Hide the `Dispose` method so that clients cannot use it directly
void IDisposable.Dispose () { }
The predictor must not Dispose any models provided to it. This is the responsibility of the client.
Copy link
On this page
Inspecting Feature Types
Input Features
Output Features
Inspecting Metadata
Making Edge Predictions
Disposing the Model
Implementing Edge Predictors
Defining the Predictor
Defining Constructors
Making Predictions
Disposing the Predictor