NatML
Search…
Getting Started
Quick Primer
To begin, import NatML 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")]
9
public string accessKey;
10
11
[Header(@"Prediction")]
12
public Texture2D image;
13
14
async void Start () {
15
Debug.Log("Fetching model data from NatML");
16
// Fetch the model data from NatML
17
var modelData = await MLModelData.FromHub("@natsuite/mobilenet-v2", accessKey);
18
// Deserialize the model
19
using var model = modelData.Deserialize();
20
// Create the MobileNet predictor
21
using var predictor = new MobileNetv2Predictor(model, modelData.labels);
22
// Create an image feature
23
var imageFeature = new MLImageFeature(image);
24
(imageFeature.mean, imageFeature.std) = modelData.normalization;
25
imageFeature.aspectMode = modelData.aspectMode;
26
// Classify the image feature
27
var (label, confidence) = predictor.Predict(imageFeature);
28
// Log the result
29
Debug.Log(quot;Image contains {label} with confidence {confidence}");
30
}
31
}
Copied!
You can get your accessKey on NatML Hub. This is how NatML 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!
Copy link