Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert the following Python code into C # code to make it the same code? #7212

Open
bbhxwl opened this issue Aug 5, 2024 · 0 comments
Labels
Deep Learning onnx Exporting ONNX models or loading ONNX models question Further information is requested untriaged New issue has not been triaged

Comments

@bbhxwl
Copy link

bbhxwl commented Aug 5, 2024

import torch
import torchvision.transforms as transforms
from torchvision.models.detection import ssd300_vgg16
from PIL import Image
import numpy as np


def preprocess_image(image_path):
    """
    预处理输入图像,将其调整为模型需要的输入格式。
    """
    # 定义图像转换
    transform = transforms.Compose([
        transforms.Resize((300, 300)),  # 调整图像大小
        transforms.ToTensor(),  # 转换为张量
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # 标准化
    ])

    # 打开图像并应用转换
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)  # 添加批次维度

    return image_tensor

def run_inference(image_tensor, threshold=0.5):
    """
    使用 SSD 模型进行推理,并根据置信度分数过滤结果。
    """
    # 加载预训练的 SSD 模型
    model = ssd300_vgg16(pretrained=True)
    model.eval()  # 切换模型到评估模式

    with torch.no_grad():
        # 进行推理
        predictions = model(image_tensor)

    # 提取检测结果
    boxes = predictions[0]['boxes'].numpy()
    labels = predictions[0]['labels'].numpy()
    scores = predictions[0]['scores'].numpy()

    # 根据置信度分数过滤结果
    filtered_boxes = boxes[scores > threshold]
    filtered_labels = labels[scores > threshold]
    filtered_scores = scores[scores > threshold]

    return filtered_boxes, filtered_labels, filtered_scores

def print_results(boxes, labels, scores):
    """
    打印检测结果。
    """
    print("检测结果:")
    for i in range(len(labels)):
        print(f"Box: {boxes[i]}")
        print(f"Label: {labels[i]}")
        print(f"Score: {scores[i]:.2f}\n")

image_path = "/Users/xuzhibin/Downloads/6ee927a0d4f2c9862a918798de175f5.jpg"  
image_tensor = preprocess_image(image_path)
boxes, labels, scores = run_inference(image_tensor)
print_results(boxes, labels, scores)

using ConsoleApp10;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;

string modelPath = "/Users/xuzhibin/Desktop/python/myScript/ssd300_vgg16.onnx";
using var session = new InferenceSession(modelPath);
var inputData=test.PreprocessImage("/Users/xuzhibin/Downloads/6ee927a0d4f2c9862a918798de175f5.jpg");
// 创建张量
var inputTensor = new DenseTensor<float>(inputData, new[] { 1, 3, 300, 300 });

// 设置输入
var inputs = new List<NamedOnnxValue>
{
    NamedOnnxValue.CreateFromTensor("input", inputTensor)
};

// 运行推理
using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = session.Run(inputs);

// 获取输出
var boxes = results.FirstOrDefault(x => x.Name == "boxes").AsEnumerable<float>().ToArray();
var labels = results.FirstOrDefault(x => x.Name == "labels").AsEnumerable<long>().ToArray();
var scores = results.FirstOrDefault(x => x.Name == "scores").AsEnumerable<float>().ToArray();

// 输出前几个检测结果
Console.WriteLine("检测结果:");
for (int i = 0; i < Math.Min(5, labels.Length); i++)
{
    Console.WriteLine($"Box: {boxes[i * 4 + 0]}, {boxes[i * 4 + 1]}, {boxes[i * 4 + 2]}, {boxes[i * 4 + 3]}");
    Console.WriteLine($"Label: {labels[i]}");
    Console.WriteLine($"Score: {scores[i]}");
}
using System.Drawing;
using System.Drawing.Imaging;
using SkiaSharp;

namespace ConsoleApp10;

public class test
{
    public static float[] PreprocessImage(string imagePath)
    {
        // 读取图像为 SKBitmap
        using var inputStream = File.OpenRead(imagePath);
        using var originalBitmap = SKBitmap.Decode(inputStream);
        // 创建新的 Bitmap,调整到模型需要的大小
        using var resizedBitmap = originalBitmap.Resize(new SKImageInfo(300, 300), SKFilterQuality.High);
        // 创建数据数组
        var inputData = new float[1 * 3 * 300 * 300];
        int index = 0;

        // 遍历每个像素,将颜色值归一化到 [0, 1] 范围
        for (int y = 0; y < resizedBitmap.Height; y++)
        {
            for (int x = 0; x < resizedBitmap.Width; x++)
            {
                var pixel = resizedBitmap.GetPixel(x, y);
                inputData[index++] = pixel.Red / 255.0f;
                inputData[index++] = pixel.Green / 255.0f;
                inputData[index++] = pixel.Blue / 255.0f;
            }
        }

        return inputData;
    }
}
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged label Aug 5, 2024
@luisquintanilla luisquintanilla added question Further information is requested onnx Exporting ONNX models or loading ONNX models Deep Learning labels Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Deep Learning onnx Exporting ONNX models or loading ONNX models question Further information is requested untriaged New issue has not been triaged
Projects
None yet
Development

No branches or pull requests

2 participants