Getting Started

Quick Primer

To begin, import the API to your Unity project. Once NatML has been imported, you can run ML models right in the Editor. For this example, we will classify this image using the popular MobileNet classifier architecture:

A Cat

First, import the image (download the image above). Make sure that in the advanced settings, you enable the Read/Write Enabled setting:

Import the cat image.

Now, we can write a small script to classify the image:

Classifier.cs
using UnityEngine;
using NatSuite.ML;
using NatSuite.ML.Features;
using NatSuite.ML.Vision;
public class Classifier : MonoBehaviour {
[Header(@"NatML Hub")]
public string accessKey;
[Header(@"Prediction")]
public Texture2D image;
async void Start () {
// Fetch the model data from Hub
Debug.Log("Fetching model data from Hub");
var modelData = await MLModelData.FromHub("@natsuite/mobilenet-v2", accessKey);
// Deserialize the model
var model = modelData.Deserialize();
// Create the classification predictor
var predictor = new MobileNetv2Predictor(model, modelData.labels);
// Create an image feature and apply normalization
var input = new MLImageFeature(image);
(input.mean, input.std) = modelData.normalization;
input.aspectMode = modelData.aspectMode;
// Classify the image
var (label, confidence) = predictor.Predict(input);
// Log the result
Debug.Log($"Image contains {label} with confidence {confidence}");
// Dispose the model
model.Dispose();
}
}

We load the model from NatML Hub, which is a central repository for finding and downloading NatML models.

Now, let us add our script to the scene and assign the inspector variables:

You can get your access key on NatML Hub. This is how Hub is able to identify you and provide you with models that you have access to.

Now, we run the script to confirm that our model predicted the image correctly!

It's a cat!