unity sentis 2.1.0

unity sentis 2.1.0学习记录帖

unity sentis 2.1.0手写字体识别



前言

随着unity6的发布 记录自己学习历程


一、使用步骤

1. 引入库

代码如下(省去安装步骤):

using UnityEngine;
using Unity.Sentis;

2. 将 softmax 层添加到模型的末尾

代码如下(示例):

FunctionalGraph graph = new FunctionalGraph();
// Set the inputs of the graph from the original model inputs and return an array of functional tensors
FunctionalTensor[] inputs = graph.AddInputs(runtimeModel);
FunctionalTensor[] outputs = Functional.Forward(runtimeModel, inputs);
// Calculate the softmax of the first output with the functional API.
FunctionalTensor softmax = Functional.Softmax(outputs[0]);

// Create a model with softmax by compiling the functional graph.
runtimeModel = graph.Compile(softmax);

3. 实现

// 将输入数据创建为张量
Tensor inputTensor = TextureConverter.ToTensor(inputTexture, width: 28, height: 28, channels: 1);
// 创建引擎
worker = new Worker(runtimeModel, BackendType.GPUCompute);
// 使用输入数据运行模型
worker.Schedule(inputTensor);
//Get the result
Tensor<float> outputTensor = worker.PeekOutput() as Tensor<float>;
results = outputTensor.DownloadToArray();
Debug.Log("识别结果:" + GetValue(results));

private int GetValue(float[] results)
{
    int index = 0;
    float value = 0;
    for (int i = 0; i < results.Length; i++)
    {
        if (value < results[i])
        {
            value = results[i];
            index = i;
        }
    }
    return index;
}

调整输出得到结果,索引值为结果值


在这里插入图片描述

总结

just soso

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值