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:

using UnityEngine;
using NatSuite.ML;
using NatSuite.ML.Features;
using NatSuite.ML.Vision;
public class Classifier : MonoBehaviour {
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");
// Deserialize the model
var model = modelData.Deserialize();
// Create the classification predictor
var predictor = new MLClassificationPredictor(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

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:

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

It's a cat!