NatML
Search…
Fetching Predictors
Where it All Begins
The very first step in using ML in your app is fetching a predictor. NatML supports fetching models from different sources, and this functionality is encapsulated in the MLModelData class.

Fetching from Hub

NatML Hub is a machine learning management service built specifically for NatML. Hub uses high-speed networks to deliver machine learning to your users when and where they need it:
1
// Fetch model data from NatML Hub
2
var modelData = await MLModelData.FromHub("@natsuite/tiny-yolo-v3");
3
// Create a model from model data
4
var model = modelData.Deserialize();
Copied!
Predictors are loaded from Hub and cached on device, so your users only ever have to download the predictor and model graph once.

Using Supplemental Data

The MLModelData class is more versatile than simply loading models. Usually, supplemental data is needed to properly use an ML model. For example, when using a classification model, you will need a list of class labels which correspond to the model's output probabilities. The MLModelData class encapsulates all of the information needed to both load a model, and make predictions with it.

Class Labels

Object classification and detection models output probabilities corresponding to a set of predefined class labels. The MLModelData class can provide these class labels, depending on how it is fetched:
1
// Create a classification model and get its corresponding labels
2
MLModel model = modelData.Deserialize();
3
string[] labels = modelData.labels;
4
// Create a classification predictor using the model's class labels
5
var predictor = new MLClassificationPredictor(model, labels);
Copied!

Image Normalization

Some vision models require input images to have certain normalization, which is a mathematical transformation of the input data. This normalization information can be used by an MLImageFeature when creating input data for a model:
1
// Create an input feature from an image
2
Texture2D image = ...;
3
var feature = new MLImageFeature(image);
4
// Apply normalization coefficients from model data
5
(feature.mean, feature.std) = modelData.normalization;
Copied!

Image Aspect Mode

Certain vision models, like object detectors, require input images to be cropped in a specific way so as to preserve all the information in the image while making a prediction. This aspect mode can be used by an MLImageFeature when creating input data for a model:
1
// Create an input feature from an image
2
Texture2D image = ...;
3
var feature = new MLImageFeature(image);
4
// Apply the proper aspect mode expected by the model
5
feature.aspectMode = modelData.aspectMode;
Copied!
Last modified 1d ago