docs.unity3d.com
    Show / Hide Table of Contents

    Run a model

    After you create a worker, use Execute to run the model.

    worker.Execute(inputTensor);
    

    You can enable verbose mode when you create a worker. Sentis logs execution to the Console window when you run the model.

    worker = WorkerFactory.CreateWorker(BackendType.GPUCompute, runtimeModel, verbose: true);
    

    When you run a model for the first time in the Unity Editor, it might be slow because Sentis needs to compile code and shaders. Later runs are faster.

    Refer to the ModelExecution example in the sample scripts for an example.

    Run a model a layer at a time

    To run a model a layer at a time, use the StartManualSchedule method of the worker. The method creates an IEnumerator object.

    The following code sample runs the model one layer per frame, and executes the rest of the Update method only after the model finishes.

    using UnityEngine;
    using Unity.Sentis;
    using System.Collections;
    
    public class StartManualSchedule : MonoBehaviour
    {
        public ModelAsset modelAsset;
        IWorker worker;
        Tensor inputTensor;
    
        public Texture2D inputTexture;
        public RenderTexture outputTexture;
    
        IEnumerator modelEnumerator;
        bool started = false;
        bool hasMoreModelToRun = true;
    
        void Start()
        {
            Model runtimeModel = ModelLoader.Load(modelAsset);
            worker = WorkerFactory.CreateWorker(BackendType.GPUCompute, runtimeModel);
            inputTensor = TextureConverter.ToTensor(inputTexture);
        }
    
        void Update()
        {
            if (!started)
            {
                modelEnumerator = worker.StartManualSchedule(inputTensor);
                started = true;
            }
    
            // Iterate running the model once per frame
            // In each iteration of the do-while loop, use the IEnumerator object to run the next layer of the model
            if (hasMoreModelToRun)
            {
                // Use MoveNext() to run the next layer of the model. MoveNext() returns true if there's more work to do
                hasMoreModelToRun = modelEnumerator.MoveNext();
    
                // Log the progress so far as a float value. 0 means no progress, 1 means complete
                Debug.Log(worker.scheduleProgress);
            }
    
            else
            {
                // Get the output tensor
                var outputTensor = worker.PeekOutput() as TensorFloat;
                // Move the output tensor to CPU before reading the data
                outputTensor.MakeReadable();
                float[] outputArray = outputTensor.ToReadOnlyArray();
    
                // turn off the component once the tensor is obtained
                enabled = false;
            }
        }
    
        void OnDisable()
        {
            worker.Dispose();
            inputTensor.Dispose();
        }
    }
    

    Refer to the ModelExecutionInParts example in the sample scripts for an example.

    Additional resources

    • Understand models in Sentis
    • Create an engine to run a model
    • Profile a model
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023