Yolo v8 模型推理 C#纯手写实现(有惊喜)

yolo 是著名的物体检测系列开源项目,使用python + pytorch实现,支持cpu、gpu训练以及推理,属于计算机视觉对象检测模型天花板,被应用在各行各业的各个角落。yolov8 相对于 yolov5 输出有一些变化。
遗憾的是,官方并没有给出C# SDK。
没有关系、我们dotnet er会把它造出来。

为什么是C#? 你还不明白吗!!!

了解模型的输入和输出形状

Netron 上可以看到我们的模型输入输出如下

Inputs float[1,3,640,640] (人们常叫它 nchw)

  • 1 batch 批次大小 (1表示一次可以传入一张图片)
  • 3 chanel 通道数 (也就是R、G、B)
  • 640 width 图像宽度
  • 640 height 图像高度

组合起来是一个四维数组,举个例子

[
  [
    [
      [ R / 255f, G / 255f, B / 255f ]
    ]
  ]
]

Outputs float[1,31,8400]

  • 1 batch 批次大小 (1表示只会输出一张图片的结果)
  • 31 标签数量
  • 8400 矩形数量

组合起来是一个三维数组,举个例子

[
    [
        [ 27, 28, 29, 30, 0.1, 0.2,...0.31]
    ]
]
  • 27 矩形中心x坐标
  • 28 矩形中心y坐标
  • 29 矩形宽度
  • 30 矩形高度
  • 0.1 对象是class1 的概率
  • 0.2 对象是class2 的概率
  • 0.31 对象是class31 的概率

好的,理论到这里就已经结束了,看起来非常的简单,算法功底好的小伙伴可以 ctrl+w 。

----------华丽的分割线 ---------

代码实现 - 将ECMA标准语言的优势发挥到淋漓尽致

图像转Tensor

private static Tensor<float> ImageToTensor(Image<Rgb24> image)
{
    Tensor<float> tensor = new DenseTensor<float>([1, 3, image.Height, image.Width]);

    for (int h = 0; h < image.Height; h++)
    {
        for (int w = 0; w < image.Width; w++)
        {
            var px = image[w, h];

            tensor[0, 0, h, w] = px.R / 255f;
            tensor[0, 1, h, w] = px.G / 255f;
            tensor[0, 2, h, w] = px.B / 255f;
        }
    }

    return tensor;
}

输出张量解析

private static IEnumerable<ObjectPrediction> ParseOutput(Tensor<float> output, float confidence)
{
    for (int b = 0; b < BATCH_SIZE; b++)
    {
        for (int r = 0; r < RECTANGLES_COUNT; r++)
        {
            float xCenter = output[b, 0, r];
            float yCenter = output[b, 1, r];
            float width = output[b, 2, r];
            float height = output[b, 3, r];

            int bestClass = 0;
            float bestClassScore = output[0, 4, r];

            for (int tc = 1; tc < TAGS_COUNT - 4; tc++)
            {
                float currentScore = output[0, 4 + tc, r];
                if (currentScore > bestClassScore)
                {
                    bestClass = tc;
                    bestClassScore = currentScore;
                }
            }

            if (bestClassScore < confidence)
            {
                continue;
            }

            yield return new ObjectPrediction
            {
                LabelColor = Tags[bestClass],
                RectangleF = new RectangleF(xCenter - width / 2, yCenter - height / 2, width, height),
                Score = bestClassScore
            };
        }
    }
}

IoU

public static float IoU(RectangleF a, RectangleF b)
{
    float xA = MathF.Max(a.Left, b.Left);
    float yA = MathF.Max(a.Top, b.Top);
    float xB = MathF.Min(a.Right, b.Right);
    float yB = MathF.Min(a.Bottom, b.Bottom);

    float interArea = MathF.Abs(MathF.Max(xB - xA, 0) * MathF.Max(yB - yA, 0));

    if (interArea <= 0f)
    {
        return 0f;
    }

    float boxAArea = MathF.Abs((a.Right - a.Left) * (a.Bottom - a.Top));
    float boxBArea = MathF.Abs((b.Right - b.Left) * (b.Bottom - b.Top));

    return interArea / (boxAArea + boxBArea - interArea);
}

NonIoU

private static IEnumerable<ObjectPrediction> NonIoU(IEnumerable<ObjectPrediction> predictions, float maxOverlap = 0.6f)
{
    return predictions
        .GroupBy(x => x.LabelColor)
        .SelectMany(x =>
        {
            List<ObjectPrediction> items = [.. x];

            int length = items.Count;

            bool[] flags = new bool[items.Count];

            for (int i = 0; i < items.Count; i++)
            {
                ObjectPrediction first = items[i];
                RectangleF firstRectangleF = first.RectangleF;

                for (int j = i + 1; j < items.Count; j++)
                {
                    var second = items[j];
                    var secondRectangleF = second.RectangleF;

                    if (flags[j])
                    {
                        continue;
                    }

                    var overlap = IoU(firstRectangleF, secondRectangleF);

                    if (overlap >= maxOverlap)
                    {
                        if (first.Score < second.Score)
                        {
                            flags[i] = true;
                            continue;
                        }

                        flags[j] = true;
                    }
                }
            }

            return flags
              .Select((flag, i) => (flag, i))
              .Where(x => !x.flag)
              .Select((_, i) => items[i]);
        });
}

组合起来

public IEnumerable<ObjectPrediction> Predict(Image<Rgb24> image, float confidence = 0.6f)
{
    using var inputImage = image.Clone(x => x.Resize(new ResizeOptions
    {
        Size = new Size(640, 640),
        Mode = ResizeMode.Pad,
    }));

    Tensor<float> input = ImageToTensor(inputImage);

    List<NamedOnnxValue> inputs = [NamedOnnxValue.CreateFromTensor(INPUT_NAME, input)];

    using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = _session.Run(inputs);

    Tensor<float> output = results[0].AsTensor<float>();

    IEnumerable<ObjectPrediction> predictions = ParseOutput(output, confidence);

    (float neww, float newh, float rate) = CalculateTransform(image.Width, image.Height, inputImage.Width, inputImage.Height);

    float offsetX = (inputImage.Width - neww) / 2f / rate;
    float offsetY = (inputImage.Height - newh) / 2f / rate;

    foreach (var item in NonIoU(predictions))
    {
        item.RectangleF = new RectangleF
        {
            X = item.RectangleF.X / rate - offsetX,
            Y = item.RectangleF.Y / rate - offsetY,
            Width = item.RectangleF.Width / rate,
            Height = item.RectangleF.Height / rate
        };
        yield return item;
    }
}

private static (float neww, float newh, float rate) CalculateTransform(int width, int height, int paddedWidth, int paddedHeight)
{
    float rate = (width > height) ? (float)paddedWidth / width : (float)paddedHeight / height;

    if (width > height)
    {
        return (paddedWidth, height * rate, rate);
    }

    return (width * rate, paddedHeight, rate);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容