NatML
Search…
⌃K

Getting Started

Quick Primer
To begin, import NatML to your Unity project by adding the following items to your Unity project's Packages > manifest.json file:
Packages/manifest.json
1
{
2
"scopedRegistries": [
3
{
4
"name": "NatML",
5
"url": "https://registry.npmjs.com",
6
"scopes": ["ai.natml"]
7
}
8
],
9
"dependencies": {
10
"ai.natml.natml": "1.0.19",
11
...
12
}
13
}
Once NatML has been imported, you can run ML models right in the Editor. For this example, we will classify an image using the popular MobileNet classifier architecture:
@natsuite/mobilenet-v2 - NatML
Get the MobileNet v2 predictor from NatML Hub.
First, we'll import the MobileNet predictor into our project by following the import instructions:
Packages/manifest.json
1
{
2
...,
3
"dependencies": {
4
"ai.natml.natml": "1.0.19",
5
"ai.natml.vision.mobilenet-v2": "1.0.0",
6
...
7
}
8
}
Next, import the image below (download it into your Unity project):
A Cat
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 NatML;
using NatML.Features;
using NatML.Vision;
public class Classifier : MonoBehaviour {
[Header(@"NatML")]
public string accessKey;
[Header(@"Prediction")]
public Texture2D image;
async void Start () {
Debug.Log("Fetching model data from NatML");
// Fetch the model data from NatML
var modelData = await MLModelData.FromHub("@natsuite/mobilenet-v2", accessKey);
// Deserialize the model
using var model = new MLEdgeModel(modelData);
// Create the MobileNet predictor
using var predictor = new MobileNetv2Predictor(model, modelData.labels);
// Create an image feature
var imageFeature = new MLImageFeature(image);
(imageFeature.mean, imageFeature.std) = modelData.normalization;
imageFeature.aspectMode = modelData.aspectMode;
// Classify the image feature
var (label, confidence) = predictor.Predict(imageFeature);
// Log the result
Debug.Log($"Image contains {label} with confidence {confidence}");
}
}
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!