C#Onnx模型信息查看工具-CSDN博客

25 阅读1分钟

效果

Netron效果

项目

代码

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包下载

源码下载