Example #1
0
        public IList <BoundingBox> ParseOutputs(float[] yoloModelOutputs, float threshold = .2F)
        {
            var boxes = new List <BoundingBox>();

            for (int cy = 0; cy < RowCount; cy++)
            {
                for (int cx = 0; cx < ColCount; cx++)
                {
                    for (int b = 0; b < BoxesPerCell; b++)
                    {
                        var channel = (b * (ClassCount + BoxInfoCount));

                        var tx = yoloModelOutputs[GetOffset(cx, cy, channel)];
                        var ty = yoloModelOutputs[GetOffset(cx, cy, channel + 1)];
                        var tw = yoloModelOutputs[GetOffset(cx, cy, channel + 2)];
                        var th = yoloModelOutputs[GetOffset(cx, cy, channel + 3)];
                        var tc = yoloModelOutputs[GetOffset(cx, cy, channel + 4)];

                        var x      = (cx + MathMethods.Sigmoid(tx)) * CellWidth;
                        var y      = (cy + MathMethods.Sigmoid(ty)) * CellHeight;
                        var width  = (float)Math.Exp(tw) * CellWidth * anchors[b * 2];
                        var height = (float)Math.Exp(th) * CellHeight * anchors[b * 2 + 1];

                        var confidence = MathMethods.Sigmoid(tc);
                        if (confidence < threshold)
                        {
                            continue;
                        }

                        var classes     = new float[ClassCount];
                        var classOffset = channel + BoxInfoCount;

                        for (int i = 0; i < ClassCount; i++)
                        {
                            classes[i] = yoloModelOutputs[GetOffset(cx, cy, i + classOffset)];
                        }

                        var results = MathMethods.Softmax(classes)
                                      .Select((v, iexp) => new { Value = v, Index = iexp });

                        var labelClass = results.OrderByDescending(r => r.Value).First().Index;
                        var scoreClass = results.OrderByDescending(r => r.Value).First().Value *confidence;
                        var testSum    = results.Sum(r => r.Value);

                        if (scoreClass > threshold)
                        {
                            boxes.Add(new BoundingBox()
                            {
                                Confidence = scoreClass,
                                X          = (x - width / 2),
                                Y          = (y - height / 2),
                                Width      = width,
                                Height     = height,
                                Label      = Enum.GetName(typeof(ObjectLabels), labelClass).ToLower()
                            });
                        }
                    }
                }
            }

            var filteredBoxes = NonMaxSuppression(boxes, 5, .5F);

            return(filteredBoxes);
        }