Last active
March 1, 2020 04:57
-
-
Save n-taku/c63256872e4d55fe0d2c8f556c9b8477 to your computer and use it in GitHub Desktop.
UnityでOnnx Runtimeを使ってMnistの手書き文字認識をする [blog](https://unitech.hatenablog.com/entry/2020/02/29/144249)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.ML.OnnxRuntime; | |
using Microsoft.ML.OnnxRuntime.Tensors; | |
public class MnistInference | |
{ | |
private readonly InferenceSession session; | |
public MnistInference(string modelPath) | |
{ | |
session = new InferenceSession(modelPath); | |
} | |
//推論 | |
//Mnistは28x28のfloat値(0~1)のinputで推論できる,左上が原点で右下に向かう座標系 | |
public int Inference(float[] input_floats) | |
{ | |
//推論する | |
var scores = InferenceOnnx(input_floats); | |
//最大のIndexを求める.Indexが推論した数字 | |
var maxScore = float.MinValue; | |
int maxIndex = 0; | |
for (int i = 0; i < scores.Length; i++) | |
{ | |
float score = scores[i]; | |
if (maxScore < score) | |
{ | |
maxScore = score; | |
maxIndex = i; | |
} | |
} | |
return maxIndex; | |
} | |
private float[] InferenceOnnx(float[] input) | |
{ | |
var inputName = session.InputMetadata.First().Key; | |
var inputDim = session.InputMetadata.First().Value.Dimensions; | |
var inputTensor = new DenseTensor<float>(new System.Memory<float>(input), inputDim); | |
// OnnxRuntimeでの入力形式であるNamedOnnxValueを作成する | |
var inputOnnxValues = new List<NamedOnnxValue> { | |
NamedOnnxValue.CreateFromTensor (inputName, inputTensor) | |
}; | |
// 推論を実行 | |
var results = session.Run(inputOnnxValues); | |
var scores = results.First().AsTensor<float>().ToArray(); | |
return scores; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment