Getting Started

Quick Primer

Importing NatML

To begin, import NatML to your Unity project by adding the following lines to your Unity project's Packages > manifest.json file:

{
  "scopedRegistries": [
   {
    "name": "NatML",
    "url": "https://registry.npmjs.com",
    "scopes": ["ai.natml"]
   }
  ],
  "dependencies": {
    "ai.natml.natml": "1.1.8",
    ...
  }
}

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:

Get the MobileNet v2 predictor from NatML Hub.

Importing the MobileNet Predictor

First, we'll import the MobileNet predictor into our project by following the import instructions:

Packages/manifest.json
{
  ...,
  "dependencies": {
    "ai.natml.natml": "1.1.8",
    "ai.natml.vision.mobilenet-v2": "1.0.3",
    ...
  }
}

The recommended way to make predictions with ML models is using Predictors. These are lightweight classes that 'know how' to format the model's input and output features.

Specifying your NatML Access Key

We will fetch our models from NatML Hub, . To do so, we need to retrieve our access key:

Get your access key from NatML Hub

Once you have an access key, you can add it to your Unity project in Project Settings > NatML:

Specifying your access key in a Unity project.

Importing a Sample Image

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.

Making a Prediction

Now, we can write a small script to classify the image:

Classifier.cs
using UnityEngine;
using NatML.Vision;

public class Classifier : MonoBehaviour {
    
    [Header(@"Prediction")]
    public Texture2D image;
    
    async void Start () {
        // Create the MobileNet predictor
        using var predictor = await MobileNetv2Predictor.Create();
        // Classify the image feature
        var (label, confidence) = predictor.Predict(image);
        // Log the result
        Debug.Log($"Image contains {label} with confidence {confidence}");     
    }
}

Now, let us add our script to the scene and assign the inspector variables:

Create an empty object and add the Classifier script we just created.

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

It's a cat!

Last updated

Was this helpful?