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