如何在带有GPU的C#中使用Yolo V5?

问题描述 投票:0回答:2

我花了最后几天的时间尝试让 Yolo 在我的 GPU 上工作。我尝试使用 https://github.yuuza.net/mentalstack/yolov5-net,我按照他们的指南使用 GPU,但它不起作用。所有其他 C# Yolo 包装器均未使用 Yolov5 版本,而我想使用这个版本。所以我的问题是,如何在我的 GPU 上使用 C# (.net 5.0) 中的 YoloV5。这是我在 yolov5-net 中使用的代码:

using var image = Image.FromFile(path);

            using var scorer = new YoloScorer<YoloCocoP5Model>("tinyyolov2-8.onnx");

            List<YoloPrediction> predictions = scorer.Predict(image);
            using var graphics = Graphics.FromImage(image);

            foreach (var prediction in predictions)
            {
                double score = Math.Round(prediction.Score, 2);

                graphics.DrawRectangles(new Pen(prediction.Label.Color, 8),
                    new[] { prediction.Rectangle });

                var (x, y) = (prediction.Rectangle.X - 3, prediction.Rectangle.Y - 23);
                graphics.DrawString($"{prediction.Label.Name} ({score})",
                    new Font("Arial", 40, GraphicsUnit.Pixel), new SolidBrush(prediction.Label.Color),
                    new PointF(x, y));
            }
            Console.WriteLine(outputPath);
            image.Save(outputPath);

上面的代码可以工作,但是它会占用我的CPU,而且显然不可能用它来快速处理许多图像。

c# .net gpu yolo yolov5
2个回答
0
投票

你可以试试这个:

using var image = Image.FromFile(path);
var options = SessionOptions.MakeSessionOptionWithCudaProvider(0);
using var scorer = new YoloScorer<YoloCocoP5Model>("Assets/Weights/yolov5n.onnx", options);

确保您安装了 CUDA 工具包和 cudnn。 这段代码可能有效或无效,我不确定,但你可以尝试一下,这就是我能提供的帮助


0
投票

@赛义德·沙扬·哈桑 有几个选项,您可以使用以下命令将 yolov5n.pt 模型转换为 yolov5n.onnx 格式:

使用 GPU: python export.py --weights yolov5n.pt --img 640 --batch 1 --device 0 --opset 12 --simplify --include onnx

不带 GPU,带 CPU: python export.py --weights yolov5n.pt --img 640 --batch 1 --device cpu --opset 12 --simplify --include onnx

运行 shell 命令,例如:

python export.py --weights yolov5n.pt --img 640 --batch 1 --device 0 --opset 12 --simplify --include onnx

在 Jupyter Notebook 环境中需要稍微不同的方法。在 Jupyter Notebook 中,您可以通过在命令前添加感叹号 (!) 来执行 shell 命令。这告诉 Jupyter 在系统 shell 中运行命令,而不是作为 Python 代码运行。

© www.soinside.com 2019 - 2024. All rights reserved.