ONNX runtime本地终端部署

news/2024/7/21 6:47:21 标签: python, matplotlib, 深度学习, 图像处理

1、class_index.csv文件:

ID,English,Chinese
0,A,你
1,B,我
2,C,他
3,D,她

2、classification.onnx
3、单张图像处理代码如下:

import onnxruntime
import torch
import torch.nn.functional as F
import pandas as pd
from PIL import Image
from torchvision import transforms
import matplotlib.pyplot as plt

def predict_class(model_path, image_path, class_index_path, top_n=1):
    # Load the ONNX model and create an ONNX Runtime inference session
    ort_session = onnxruntime.InferenceSession(model_path, providers=['CUDAExecutionProvider'])

    # Load the test image and apply transformations
    img_pil = Image.open(image_path).convert('RGB')
    test_transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406],
                             [0.229, 0.224, 0.225])
    ])
    input_img = test_transform(img_pil)
    input_tensor = input_img.unsqueeze(0).numpy()

    # Perform inference
    ort_inputs = {'input': input_tensor}
    pred_logits = ort_session.run(['output'], ort_inputs)[0]
    pred_logits = torch.tensor(pred_logits)

    # Apply softmax to get class probabilities
    pred_softmax = F.softmax(pred_logits, dim=1)

    # Load the class index mapping
    df = pd.read_csv(class_index_path)
    idx_to_labels = {row['ID']: row['English'] for idx, row in df.iterrows()}
    # idx_to_labels = {row['ID']: row['Chinese'] for idx, row in df.iterrows()}

    # Get the top predicted classes and their confidence scores
    top_n_results = torch.topk(pred_softmax, top_n)
    pred_ids = top_n_results.indices.numpy()[0]
    confs = top_n_results.values.numpy()[0]

    # Translate class IDs to class names
    predicted_classes = [idx_to_labels[class_id] for class_id in pred_ids]

    # Create a list of class names and their corresponding confidence scores
    results = []
    for i in range(top_n):
        class_name = predicted_classes[i]
        confidence = confs[i] * 100
        results.append((class_name, confidence))
    return results,img_pil

if __name__ == '__main__':
    model_path = 'classification.onnx'
    image_path = 'E:/Python_Project/classification/21t1Gdxsagittal0143.png'
    class_index_path = 'class_index.csv'

    top_n = 1  # Adjust the number of top predictions you want
    predictions,img = predict_class(model_path, image_path, class_index_path, top_n)

    for class_name, confidence in predictions:
        text = class_name + ': {:.3f}%'.format(confidence)
        print(text)

    # Display the image input
    plt.rcParams['font.sans-serif'] = 'SimHei'  # 黑体

    plt.figure(figsize=(6,6))
    plt.imshow(img)
    plt.axis('off')

    # add the predicted class and confidence as a title
    class_name, confidence = predictions[0]
    title_text = f'Predicted Class: {class_name}\nAccuracy: {confidence:.3f}%'
    plt.title(title_text)
    plt.show()

4、批量图像处理代码如下:

import onnxruntime
import pandas
import torch
from PIL import Image
import os
from torchvision import transforms
import torch.nn.functional as F
import matplotlib.pyplot as plt

def batch_prediction(model_path,class_index_path,top_n,input_folder,output_folder):

    # 列出输出文件夹所有图片
    input_files=os.listdir(input_folder)
    # print(input_files)

    # 针对每个文件进行处理
    for input_file in input_files:
        # 构建一个完整的路径
        input_file_path=os.path.join(input_folder,input_file)
        # 打开图像并转换成RGB格式
        img_pil=Image.open(input_file_path).convert('RGB')
        # print(image)
        # 图像预处理
        test_transform = transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406],
                                 [0.229, 0.224, 0.225])
        ])

        input_img=test_transform(img_pil)
        # 增加维度
        input_tensor=input_img.unsqueeze(0).numpy()
        # print(input_tensor.shape)

        # 加载ONNX模型并创建onnx_runtime推理
        ort_session = onnxruntime.InferenceSession(model_path, providers=['CUDAExecutionProvider'])
        # print(ort_session)

        # 平台推理
        ort_inputs={'input':input_tensor}
        pre_logits=ort_session.run(['output'],ort_inputs)[0]
        pre_logits=torch.Tensor(pre_logits)
        # print(pre_logits)

        # 应用softmax去做类别预测
        pred_softmax=F.softmax(pre_logits,dim=1)# dim=1 按行进行归一化
        # print(pred_softmax)

        # 加载类别索引
        df=pandas.read_csv(class_index_path)
        # print(df)
        idx_to_labels={row['ID'] : row['English'] for idx, row in df.iterrows()}

        # 去得到准确率和得分
        top_n_results=torch.topk(pred_softmax,top_n)
        # print(top_n_results)
        pred_ids=top_n_results.indices.numpy()[0]# 得到id
        # print(pred_ids)
        confs=top_n_results.values.numpy()[0]# 得到分数 numpy格式
        # print(type(confs))

        # 预测类别
        predicted_class=[idx_to_labels[class_id] for class_id in pred_ids]#列表格式
        # print(predicted_class)

        # 输出类别和相应的得分
        # print(predicted_class,confs)

        for i in range(top_n):
            class_name=predicted_class[i]
            confidence=confs[i]*100
            plt.rcParams['font.sans-serif'] = 'SimHei'  # 黑体中文字体

            plt.imshow(img_pil)#
            plt.axis('off')#
            title_text = f'Predicted Class: {class_name}\nAccuracy: {confidence:.3f}%'
            plt.title(title_text)
            print(title_text)

            # 确保输出文件夹存在,如果不存在则创建
            if not os.path.exists(output_folder):
                os.makedirs(output_folder)

            # 构建输出文件夹的完整路径并保存绘制的图像
            plot_output_file = os.path.join(output_folder,input_file)
            plt.savefig(plot_output_file, bbox_inches='tight', pad_inches=0.1)  # 保存绘制的图像
            plt.close()  # 关闭当前绘图以便处理下一个图像

if __name__ == '__main__':
    model_path='classification.onnx'
    class_index_path='class_index.csv'
    top_n=1
    input_folder="C:/Users/XUB/Desktop/A"
    output_folder="C:/Users/XUB/Desktop/B"
    batch_prediction(model_path,class_index_path,top_n,input_folder,output_folder)

http://www.niftyadmin.cn/n/5107833.html

相关文章

3DCAT实时云渲染赋能聚好看科技,打造3D沉浸式互动视频云平台

随着5G、云计算、XR等技术的发展,3D沉浸式互动视频已经成为了新一代的数字媒体形式,为各行各业带来了新的创新机遇和价值。然而,要实现高效、高质、高效率的3D沉浸式互动视频生产和传播,还需要强大的技术支撑和平台服务。 作为海…

代码随想录算法训练营第五十八天| 583. 两个字符串的删除操作 72. 编辑距离

今日学习的文章链接和视频链接 两个字符串的删除操作 https://programmercarl.com/0583.%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C.html 编辑距离 https://programmercarl.com/0072.%E7%BC%96%E8%BE%91%E8%B7%9D%E7%A6%BB…

【Java 进阶篇】Bootstrap 快速入门

Bootstrap 是一个流行的开源前端框架,它使网页开发更加容易和高效。无论您是一个有经验的开发者还是一个初学者,本文将带您深入了解 Bootstrap,从基础概念到实际示例,以帮助您快速入门这个强大的工具。 什么是 Bootstrap&#xf…

【五:Httprunner的介绍使用】

接口自动化框架封装思想的建立。httprunner(热加载:动态参数),去应用 意义不大。 day1 一、什么是Httprunner? 1.httprunner是一个面向http协议的通用测试框架,目前最新的版本3.X。以前比较流行的 2.X的版本。2.它的…

git笔记 - 常用记录

第1阶段 - Git简介 什么是Git及其重要性?基本的Git概念和术语 仓库(Repository):也称为 repo,是存储代码和版本历史的地方。它可以是本地仓库(在本地计算机上)或远程仓库(在服务器…

[AutoSAR系列] 1.1 AutoSar 发展历史

AUTOSAR,全称为Automotive Open System Architecture,即汽车开放系统架构。 AutoSar 是一项开源的汽车软件标准,旨在提高汽车电子系统的互操作性和可重用性。AutoSar 成员通常是汽车制造商、电子元件制造商、软件供应商和工具供应商等公司,他们在共同开发和推进 AutoSar 标…

性能测试常见故障和解决思路

一、性能问题分析流程 1、查看服务器的CPU、内存 、负载等情况,包括应用服务器和数据库服务器 2、查看数据库健康状态,数据库死锁、连接池不释放 3、查看项目日志(查看无报错现象) 4、查看jvm的gc等情况 二、内存溢出 &…

【网络安全】网站被攻击了怎么办?怎么防护DDOS、CC、XSS、ARP等攻击?

网站被攻击了怎么办? 六字真言:认怂、关站、睡觉 如果你对网络安全入门感兴趣,那么你需要的话可以点击这里👉【入门&进阶全套282G学习资源包免费分享!】 常见的网络攻击 XSS攻击 XSS 攻击可以分为 3 类&#…