docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Workflow example

    The following example includes a simple script that takes an image of a handwritten digit and predicts the likelihood of the image representing a digit.

    Use this example to Understand the sentis workflow

    Use the example

    To use the example, follow these steps:

    1. Attach the following script to a GameObject in your scene:
    using UnityEngine;
    using Unity.Sentis;
    
    public class ClassifyHandwrittenDigit : MonoBehaviour
    {
        public Texture2D inputTexture;
        public ModelAsset modelAsset;
        Model runtimeModel;
        IWorker worker;
        public float[] results;
    
        void Start()
        {
            var sourceModel = ModelLoader.Load(modelAsset);
    
            // Create the runtime model
            runtimeModel = Functional.Compile(
                inputs =>
                {
                    var output = FunctionalTensor.FromModel(sourceModel, inputs)[0];
                    // rescale output of source model
                    return new[] { Functional.Softmax(output) };
                },
                InputDef.FromModel(sourceModel)
            );
    
            // Create input data as a tensor
            using Tensor inputTensor = TextureConverter.ToTensor(inputTexture, width: 28, height: 28, channels: 1);
    
            // Create an engine
            worker = WorkerFactory.CreateWorker(BackendType.GPUCompute, runtimeModel);
    
            // Run the model with the input data
            worker.Execute(inputTensor);
    
            // Get the result
            using TensorFloat outputTensor = worker.PeekOutput() as TensorFloat;
    
            // Move the tensor data to the CPU before reading it
            outputTensor.CompleteOperationsAndDownload();
    
            results = outputTensor.ToReadOnlyArray();
        }
    
        void OnDisable()
        {
            // Tell the GPU we're finished with the memory the engine used
            worker.Dispose();
        }
    }
    
    1. Download a handwriting recognition ONNX model file from the ONNX Model Zoo (GitHub). For example, the MNIST Handwritten Digit Recognition model mnist-8.onnx.

    2. Drag the downloaded model file into the Assets folder of the Project window.

    3. Open the Inspector window of the GameObject and drag the model asset into the Model Asset field.

    4. Download the digit.png image below and drag it into the Assets folder of the Project window.

      A handwritten number 7

    5. Open the Inspector window of the imported image.

    6. In Import Settings, expand the Advanced section to reveal more settings.

    7. Set Non-Power of 2 to None and click Apply.

    8. Open the Inspector window of the GameObject and drag the digit asset into the Input Texture field.

    9. Click Play to run the project.

    In the Inspector window of the GameObject, each item of the Results array shows how highly the model predicts the image to be a digit. For example, item 0 of the array represents how highly the model predicts the image being a handwritten zero.

    Additional resources

    • Samples
    • Understand the sentis workflow
    • Create a model
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)