Skip to content

Yolov5 Why do the following models use the same method but have different results? Is it a C # issue? #7215

Open

Description

Why do the following models use the same method but have different results? Is it a C # issue?

How can C # achieve code with the same effect as Python?

// See https://aka.ms/new-console-template for more information

using System.Drawing;
using ConsoleApp3;
using Microsoft.ML.OnnxRuntime;

InferenceSession session = new InferenceSession("D:\\yolov5\\yolov5x.onnx");
List<NamedOnnxValue> inputs = new List<NamedOnnxValue>();
var bit=(Bitmap)Bitmap.FromFile("C:\\Users\\47013\\Desktop\\2.jpeg");
inputs.Add(NamedOnnxValue.CreateFromTensor<float>("images",test.PreprocessImage(bit)));

var results = session.Run(inputs);
var output=results.First().AsTensor<float>();
var boxes = new List<float[]>();

for (int i = 0; i < output.Dimensions[1]; i++)
{
    var boxData = new float[85];
    for (int j = 0; j < 85; j++)
    {
        boxData[j] = output[0, i, j];
    }
    boxes.Add(boxData);
}

var asd=test.DrawBoundingBoxes(bit,boxes,0.1f);
asd.Save("test.jpg");
var m=boxes.Max(s => s[4]);
List<float> ll = new List<float>();
foreach (var box in boxes)
{
    float confidence = box[4];
    ll.Add(confidence);
}
var sasd=ll.Max();
Console.WriteLine();
import datetime

import torch

# 加载模型
model = torch.hub.load('ultralytics/yolov5', 'yolov5x', pretrained=True)

# 设置模型为评估模式
model.eval()

# 图像文件路径
img_path = 'C:\\Users\\47013\\Desktop\\新建文件夹\\2.jpeg'  # 替换为你自己的图像路径
start_time = datetime.datetime.now()
# 进行推理
results = model(img_path)
print(datetime.datetime.now() - start_time)
# 解析结果
results.print()  # 打印检测结果
results.show()   # 显示带检测框的图像

# 访问结果数据
detections = results.xyxy[0]  # 访问检测结果,格式为 (x1, y1, x2, y2, confidence, class)

# 打印检测框的详细信息
for *box, conf, cls in detections:
    print(f"Detected {model.names[int(cls)]} with confidence {conf:.2f} at [{box[0]:.2f}, {box[1]:.2f}, {box[2]:.2f}, {box[3]:.2f}]")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Deep LearningonnxExporting ONNX models or loading ONNX modelsuntriagedNew issue has not been triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions