Commit
·
cf5ca42
1
Parent(s):
1f4ae4c
cleanup syntax
Browse files- RunYOLO.cs +15 -13
RunYOLO.cs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
-
using System
|
|
|
|
|
|
|
| 2 |
using Unity.InferenceEngine;
|
| 3 |
using UnityEngine;
|
| 4 |
using UnityEngine.UI;
|
| 5 |
using UnityEngine.Video;
|
| 6 |
-
using System.IO;
|
| 7 |
-
using FF = Unity.InferenceEngine.Functional;
|
| 8 |
|
| 9 |
/*
|
| 10 |
* YOLO Inference Script
|
|
@@ -51,10 +51,12 @@ public class RunYOLO : MonoBehaviour
|
|
| 51 |
List<GameObject> boxPool = new();
|
| 52 |
|
| 53 |
[Tooltip("Intersection over union threshold used for non-maximum suppression")]
|
| 54 |
-
[SerializeField, Range(0, 1)]
|
|
|
|
| 55 |
|
| 56 |
[Tooltip("Confidence score threshold used for non-maximum suppression")]
|
| 57 |
-
[SerializeField, Range(0, 1)]
|
|
|
|
| 58 |
|
| 59 |
Tensor<float> centersToCorners;
|
| 60 |
//bounding box data
|
|
@@ -103,15 +105,15 @@ public class RunYOLO : MonoBehaviour
|
|
| 103 |
//Here we transform the output of the model1 by feeding it through a Non-Max-Suppression layer.
|
| 104 |
var graph = new FunctionalGraph();
|
| 105 |
var inputs = graph.AddInputs(model1);
|
| 106 |
-
var modelOutput =
|
| 107 |
var boxCoords = modelOutput[0, 0..4, ..].Transpose(0, 1); //shape=(8400,4)
|
| 108 |
var allScores = modelOutput[0, 4.., ..]; //shape=(80,8400)
|
| 109 |
-
var scores =
|
| 110 |
-
var classIDs =
|
| 111 |
-
var boxCorners =
|
| 112 |
-
var indices =
|
| 113 |
-
var coords =
|
| 114 |
-
var labelIDs =
|
| 115 |
|
| 116 |
//Create worker to run model
|
| 117 |
worker = new Worker(graph.Compile(coords, labelIDs), backend);
|
|
@@ -247,7 +249,7 @@ public class RunYOLO : MonoBehaviour
|
|
| 247 |
}
|
| 248 |
}
|
| 249 |
|
| 250 |
-
|
| 251 |
{
|
| 252 |
centersToCorners?.Dispose();
|
| 253 |
worker?.Dispose();
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Collections.Generic;
|
| 3 |
+
using System.IO;
|
| 4 |
using Unity.InferenceEngine;
|
| 5 |
using UnityEngine;
|
| 6 |
using UnityEngine.UI;
|
| 7 |
using UnityEngine.Video;
|
|
|
|
|
|
|
| 8 |
|
| 9 |
/*
|
| 10 |
* YOLO Inference Script
|
|
|
|
| 51 |
List<GameObject> boxPool = new();
|
| 52 |
|
| 53 |
[Tooltip("Intersection over union threshold used for non-maximum suppression")]
|
| 54 |
+
[SerializeField, Range(0, 1)]
|
| 55 |
+
float iouThreshold = 0.5f;
|
| 56 |
|
| 57 |
[Tooltip("Confidence score threshold used for non-maximum suppression")]
|
| 58 |
+
[SerializeField, Range(0, 1)]
|
| 59 |
+
float scoreThreshold = 0.5f;
|
| 60 |
|
| 61 |
Tensor<float> centersToCorners;
|
| 62 |
//bounding box data
|
|
|
|
| 105 |
//Here we transform the output of the model1 by feeding it through a Non-Max-Suppression layer.
|
| 106 |
var graph = new FunctionalGraph();
|
| 107 |
var inputs = graph.AddInputs(model1);
|
| 108 |
+
var modelOutput = Functional.Forward(model1, inputs)[0]; //shape=(1,84,8400)
|
| 109 |
var boxCoords = modelOutput[0, 0..4, ..].Transpose(0, 1); //shape=(8400,4)
|
| 110 |
var allScores = modelOutput[0, 4.., ..]; //shape=(80,8400)
|
| 111 |
+
var scores = Functional.ReduceMax(allScores, 0); //shape=(8400)
|
| 112 |
+
var classIDs = Functional.ArgMax(allScores, 0); //shape=(8400)
|
| 113 |
+
var boxCorners = Functional.MatMul(boxCoords, Functional.Constant(centersToCorners)); //shape=(8400,4)
|
| 114 |
+
var indices = Functional.NMS(boxCorners, scores, iouThreshold, scoreThreshold); //shape=(N)
|
| 115 |
+
var coords = Functional.IndexSelect(boxCoords, 0, indices); //shape=(N,4)
|
| 116 |
+
var labelIDs = Functional.IndexSelect(classIDs, 0, indices); //shape=(N)
|
| 117 |
|
| 118 |
//Create worker to run model
|
| 119 |
worker = new Worker(graph.Compile(coords, labelIDs), backend);
|
|
|
|
| 249 |
}
|
| 250 |
}
|
| 251 |
|
| 252 |
+
void OnDestroy()
|
| 253 |
{
|
| 254 |
centersToCorners?.Dispose();
|
| 255 |
worker?.Dispose();
|