C#Onnx模型信息查看工具
  yqdtHKhvd9Ja 2023年12月15日 79 0


目录

背景

效果

Netron效果

项目

代码

下载


背景

在程序调用Onnx模型时,一般都会查看一下模型的属性、输入、输出信息,一般可以不看模型具体的信息。

可以使用Netron查看,Netron地址 https://netron.app/  在线查看时需要有网,模型越大加载越慢。

大多时候只想快速查看一下模型的输入、输出信息,所以自己写了一个C#Onnx模型信息查看工具。

效果

C#Onnx模型信息查看工具_C#Onnx模型信息查看工具

Netron效果

C#Onnx模型信息查看工具_Text_02

项目

VS2022

.net framework 4.8

Microsoft.ML.OnnxRuntime 1.16.2

C#Onnx模型信息查看工具_Text_03

代码

核心代码
加载模型
InferenceSession   onnx_session = new InferenceSession(model_path);
属性信息
 Dictionary<string, string> CustomMetadataMap = onnx_session.ModelMetadata.CustomMetadataMap;foreach (string key in CustomMetadataMap.Keys)
 {
     sb.AppendLine(String.Format("{0}:{1}",key, CustomMetadataMap[key])) ;
 }输入信息
 IReadOnlyDictionary<string, NodeMetadata> InputMetadata = onnx_session.InputMetadata;foreach (var item in InputMetadata)
 {
     sb.AppendLine("name:" + item.Key);
     NodeMetadata  nmData= item.Value;
     int[] dim = nmData.Dimensions;
     sb.AppendLine("tensor:" + nmData.ElementDataType.ToString()+"["+ String.Join(", ", dim)+"]");
 }输出信息
 IReadOnlyDictionary<string, NodeMetadata> OutputMetadata = onnx_session.OutputMetadata;foreach (var item in OutputMetadata)
 {
     sb.AppendLine("name:" + item.Key);
     NodeMetadata nmData = item.Value;
     int[] dim = nmData.Dimensions;
     sb.AppendLine("tensor:" + nmData.ElementDataType.ToString() + "[" + String.Join(", ", dim) + "]");
 }using Microsoft.ML.OnnxRuntime;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Onnx_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.onnx;";
        string model_path;

        StringBuilder sb = new StringBuilder();
        InferenceSession onnx_session;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            txtInfo.Text = "";
            model_path = ofd.FileName;
            txtPath.Text = model_path;

            txtInfo.Text = "正在读取,请稍后……";
            Application.DoEvents();

            ShowInfo(model_path);
        }

        void ShowInfo(string model_path)
        {
            try
            {
                onnx_session = new InferenceSession(model_path);
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取模型异常:" + ex.Message);
            }
            sb.Clear();

            //Model Properties
            sb.AppendLine("Model Properties");
            sb.AppendLine("-------------------------");
            Dictionary<string, string> CustomMetadataMap = onnx_session.ModelMetadata.CustomMetadataMap;
            foreach (string key in CustomMetadataMap.Keys)
            {
                sb.AppendLine(String.Format("{0}:{1}",key, CustomMetadataMap[key])) ;
            }
            sb.AppendLine("---------------------------------------------------------------");

            IReadOnlyList<string> InputNames = onnx_session.InputNames;
            IReadOnlyDictionary<string, NodeMetadata> InputMetadata = onnx_session.InputMetadata;
            //Inputs
            sb.AppendLine("");
            sb.AppendLine("Inputs");
            sb.AppendLine("-------------------------");

            foreach (var item in InputMetadata)
            {
                sb.AppendLine("name:" + item.Key);
                NodeMetadata  nmData= item.Value;
                int[] dim = nmData.Dimensions;
                sb.AppendLine("tensor:" + nmData.ElementDataType.ToString()+"["+ String.Join(", ", dim)+"]");
            }
            sb.AppendLine("---------------------------------------------------------------");


            IReadOnlyList<string> OutputNames = onnx_session.OutputNames;
            IReadOnlyDictionary<string, NodeMetadata> OutputMetadata = onnx_session.OutputMetadata;
            //Outputs
            sb.AppendLine("");
            sb.AppendLine("Outputs");
            sb.AppendLine("-------------------------");

            foreach (var item in OutputMetadata)
            {
                sb.AppendLine("name:" + item.Key);
                NodeMetadata nmData = item.Value;
                int[] dim = nmData.Dimensions;
                sb.AppendLine("tensor:" + nmData.ElementDataType.ToString() + "[" + String.Join(", ", dim) + "]");
            }
            sb.AppendLine("---------------------------------------------------------------");

            txtInfo.Text = sb.ToString();

        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

    }
}

下载

可执行程序exe包下载

源码下载

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月15日 0

暂无评论

推荐阅读
yqdtHKhvd9Ja