NatML
Search…
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
1
using UnityEngine;
2
using NatSuite.ML;
3
using NatSuite.ML.Features;
4
using NatSuite.ML.Vision;
5
6
public class Classifier : MonoBehaviour {
7
8
[Header(@"NatML Hub")]
9
public string accessKey;
10
11
[Header(@"Prediction")]
12
public Texture2D image;
13
14
async void Start () {
15
// Fetch the model data from Hub
16
Debug.Log("Fetching model data from Hub");
17
var modelData = await MLModelData.FromHub("@natsuite/mobilenet-v2", accessKey);
18
// Deserialize the model
19
var model = modelData.Deserialize();
20
// Create the classification predictor
21
var predictor = new MobileNetv2Predictor(model, modelData.labels);
22
// Create an image feature and apply normalization
23
var input = new MLImageFeature(image);
24
(input.mean, input.std) = modelData.normalization;
25
input.aspectMode = modelData.aspectMode;
26
// Classify the image
27
var (label, confidence) = predictor.Predict(input);
28
// Log the result
29
Debug.Log(quot;Image contains {label} with confidence {confidence}");
30
// Dispose the model
31
model.Dispose();
32
}
33
}
Copied!
You can get your accessKey on NatML Hub. This is how Hub is able to identify you and provide you with models that you have access to.
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!
Last modified 1mo ago
Copy link