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 an image using the popular MobileNet classifier architecture:
@natsuite/mobilenet-v2 - NatML
Get the MobileNet v2 predictor from NatML Hub.
First, 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:
using UnityEngine;
using NatML;
using NatML.Features;
using NatML.Vision;
public class Classifier : MonoBehaviour {
public string accessKey;
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 = modelData.Deserialize();
// 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(quot;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!
Copy link