-
Notifications
You must be signed in to change notification settings - Fork 61
/
DetectorYolo2.cs
294 lines (227 loc) · 9.52 KB
/
DetectorYolo2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using UnityEngine;
using Unity.Barracuda;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class DetectorYolo2 : MonoBehaviour, Detector
{
public NNModel modelFile;
public TextAsset labelsFile;
private const int IMAGE_MEAN = 0;
private const float IMAGE_STD = 255.0F;
// ONNX model input and output name. Modify when switching models.
//These aren't const values because they need to be easily edited on the component before play mode
public string INPUT_NAME;
public string OUTPUT_NAME;
//This has to stay a const
private const int _image_size = 416;
public int IMAGE_SIZE { get => _image_size; }
// Minimum detection confidence to track a detection
public float MINIMUM_CONFIDENCE;
private IWorker worker;
public const int ROW_COUNT = 13;
public const int COL_COUNT = 13;
public const int BOXES_PER_CELL = 5;
public const int BOX_INFO_FEATURE_COUNT = 5;
//Update this!
public int CLASS_COUNT;
public const float CELL_WIDTH = 32;
public const float CELL_HEIGHT = 32;
private string[] labels;
private float[] anchors = new float[]
{
// 1.08F, 1.19F, 3.42F, 4.41F, 6.63F, 11.38F, 9.42F, 5.11F, 16.62F, 10.52F // yolov2-tiny-voc
//0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov2-tiny
0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov2-tiny-food
};
public void Start()
{
this.labels = Regex.Split(this.labelsFile.text, "\n|\r|\r\n")
.Where(s => !String.IsNullOrEmpty(s)).ToArray();
var model = ModelLoader.Load(this.modelFile);
// https://docs.unity3d.com/Packages/com.unity.barracuda@1.0/manual/Worker.html
//These checks all check for GPU before CPU as GPU is preferred if the platform + rendering pipeline support it
this.worker = GraphicsWorker.GetWorker(model);
}
public IEnumerator Detect(Color32[] picture, System.Action<IList<BoundingBox>> callback)
{
using (var tensor = TransformInput(picture, IMAGE_SIZE, IMAGE_SIZE))
{
var inputs = new Dictionary<string, Tensor>();
inputs.Add(INPUT_NAME, tensor);
yield return StartCoroutine(worker.StartManualSchedule(inputs));
//worker.Execute(inputs);
var output = worker.PeekOutput(OUTPUT_NAME);
Debug.Log("Output: " + output);
var results = ParseOutputs(output, MINIMUM_CONFIDENCE);
var boxes = FilterBoundingBoxes(results, 5, MINIMUM_CONFIDENCE);
callback(boxes);
}
}
public static Tensor TransformInput(Color32[] pic, int width, int height)
{
float[] floatValues = new float[width * height * 3];
for (int i = 0; i < pic.Length; ++i)
{
var color = pic[i];
floatValues[i * 3 + 0] = (color.r - IMAGE_MEAN) / IMAGE_STD;
floatValues[i * 3 + 1] = (color.g - IMAGE_MEAN) / IMAGE_STD;
floatValues[i * 3 + 2] = (color.b - IMAGE_MEAN) / IMAGE_STD;
}
return new Tensor(1, height, width, 3, floatValues);
}
private IList<BoundingBox> ParseOutputs(Tensor yoloModelOutput, float threshold)
{
var boxes = new List<BoundingBox>();
for (int cy = 0; cy < COL_COUNT; cy++)
{
for (int cx = 0; cx < ROW_COUNT; cx++)
{
for (int box = 0; box < BOXES_PER_CELL; box++)
{
var channel = (box * (CLASS_COUNT + BOX_INFO_FEATURE_COUNT));
var bbd = ExtractBoundingBoxDimensions(yoloModelOutput, cx, cy, channel);
float confidence = GetConfidence(yoloModelOutput, cx, cy, channel);
if (confidence < threshold)
{
continue;
}
float[] predictedClasses = ExtractClasses(yoloModelOutput, cx, cy, channel);
var (topResultIndex, topResultScore) = GetTopResult(predictedClasses);
var topScore = topResultScore * confidence;
Debug.Log("DEBUG: results: " + topResultIndex.ToString());
if (topScore < threshold)
{
continue;
}
var mappedBoundingBox = MapBoundingBoxToCell(cx, cy, box, bbd);
boxes.Add(new BoundingBox
{
Dimensions = new BoundingBoxDimensions
{
X = (mappedBoundingBox.X - mappedBoundingBox.Width / 2),
Y = (mappedBoundingBox.Y - mappedBoundingBox.Height / 2),
Width = mappedBoundingBox.Width,
Height = mappedBoundingBox.Height,
},
Confidence = topScore,
Label = labels[topResultIndex],
Used = false
});
}
}
}
return boxes;
}
private float Sigmoid(float value)
{
var k = (float)Math.Exp(value);
return k / (1.0f + k);
}
private float[] Softmax(float[] values)
{
var maxVal = values.Max();
var exp = values.Select(v => Math.Exp(v - maxVal));
var sumExp = exp.Sum();
return exp.Select(v => (float)(v / sumExp)).ToArray();
}
private BoundingBoxDimensions ExtractBoundingBoxDimensions(Tensor modelOutput, int x, int y, int channel)
{
return new BoundingBoxDimensions
{
X = modelOutput[0, x, y, channel],
Y = modelOutput[0, x, y, channel + 1],
Width = modelOutput[0, x, y, channel + 2],
Height = modelOutput[0, x, y, channel + 3]
};
}
private float GetConfidence(Tensor modelOutput, int x, int y, int channel)
{
// Debug.Log("ModelOutput " + modelOutput);
return Sigmoid(modelOutput[0, x, y, channel + 4]);
}
private CellDimensions MapBoundingBoxToCell(int x, int y, int box, BoundingBoxDimensions boxDimensions)
{
return new CellDimensions
{
X = ((float)y + Sigmoid(boxDimensions.X)) * CELL_WIDTH,
Y = ((float)x + Sigmoid(boxDimensions.Y)) * CELL_HEIGHT,
Width = (float)Math.Exp(boxDimensions.Width) * CELL_WIDTH * anchors[box * 2],
Height = (float)Math.Exp(boxDimensions.Height) * CELL_HEIGHT * anchors[box * 2 + 1],
};
}
public float[] ExtractClasses(Tensor modelOutput, int x, int y, int channel)
{
float[] predictedClasses = new float[CLASS_COUNT];
int predictedClassOffset = channel + BOX_INFO_FEATURE_COUNT;
for (int predictedClass = 0; predictedClass < CLASS_COUNT; predictedClass++)
{
predictedClasses[predictedClass] = modelOutput[0, x, y, predictedClass + predictedClassOffset];
}
return Softmax(predictedClasses);
}
private ValueTuple<int, float> GetTopResult(float[] predictedClasses)
{
return predictedClasses
.Select((predictedClass, index) => (Index: index, Value: predictedClass))
.OrderByDescending(result => result.Value)
.First();
}
private float IntersectionOverUnion(Rect boundingBoxA, Rect boundingBoxB)
{
var areaA = boundingBoxA.width * boundingBoxA.height;
if (areaA <= 0)
return 0;
var areaB = boundingBoxB.width * boundingBoxB.height;
if (areaB <= 0)
return 0;
var minX = Math.Max(boundingBoxA.xMin, boundingBoxB.xMin);
var minY = Math.Max(boundingBoxA.yMin, boundingBoxB.yMin);
var maxX = Math.Min(boundingBoxA.xMax, boundingBoxB.xMax);
var maxY = Math.Min(boundingBoxA.yMax, boundingBoxB.yMax);
var intersectionArea = Math.Max(maxY - minY, 0) * Math.Max(maxX - minX, 0);
return intersectionArea / (areaA + areaB - intersectionArea);
}
private IList<BoundingBox> FilterBoundingBoxes(IList<BoundingBox> boxes, int limit, float threshold)
{
var activeCount = boxes.Count;
var isActiveBoxes = new bool[boxes.Count];
for (int i = 0; i < isActiveBoxes.Length; i++)
{
isActiveBoxes[i] = true;
}
var sortedBoxes = boxes.Select((b, i) => new { Box = b, Index = i })
.OrderByDescending(b => b.Box.Confidence)
.ToList();
var results = new List<BoundingBox>();
for (int i = 0; i < boxes.Count; i++)
{
if (isActiveBoxes[i])
{
var boxA = sortedBoxes[i].Box;
results.Add(boxA);
if (results.Count >= limit)
break;
for (var j = i + 1; j < boxes.Count; j++)
{
if (isActiveBoxes[j])
{
var boxB = sortedBoxes[j].Box;
if (IntersectionOverUnion(boxA.Rect, boxB.Rect) > threshold)
{
isActiveBoxes[j] = false;
activeCount--;
if (activeCount <= 0)
break;
}
}
}
if (activeCount <= 0)
break;
}
}
return results;
}
}