当前位置:   article > 正文

C# RAM Stable Diffusion 提示词反推 Onnx Demo

C# RAM Stable Diffusion 提示词反推 Onnx Demo

目录

介绍

效果

模型信息

项目

代码

下载


C# RAM Stable Diffusion 提示词反推 Onnx Demo

介绍

github地址:GitHub - xinyu1205/recognize-anything: Open-source and strong foundation image recognition models.

Open-source and strong foundation image recognition models.

效果

模型信息

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[1, 3, 384, 384]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[1, 4585]
---------------------------------------------------------------

项目

代码

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

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

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        string model_path;
        Mat image;

        SessionOptions options;
        InferenceSession onnx_session;
        Tensor<float> input_tensor;
        List<NamedOnnxValue> input_container;
        IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
        DisposableNamedOnnxValue[] results_onnxvalue;

        Tensor<float> result_tensors;

        StringBuilder sbTags = new StringBuilder();
        StringBuilder sbTagsCN = new StringBuilder();
        StringBuilder sb = new StringBuilder();

        public string[] class_names;

        List<Tag> ltTag = new List<Tag>();

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            image = new Mat(image_path);
        }

        float[] mean = { 0.485f, 0.456f, 0.406f };
        float[] std = { 0.229f, 0.224f, 0.225f };

        public void Normalize(Mat src)
        {
            src.ConvertTo(src, MatType.CV_32FC3, 1.0 / 255);
            Mat[] bgr = src.Split();
            for (int i = 0; i < bgr.Length; ++i)
            {
                bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1 / std[i], (0.0 - mean[i]) / std[i]);
            }
            Cv2.Merge(bgr, src);
            foreach (Mat channel in bgr)
            {
                channel.Dispose();
            }
        }

        public float[] ExtractMat(Mat src)
        {
            OpenCvSharp.Size size = src.Size();
            int channels = src.Channels();
            float[] result = new float[size.Width * size.Height * channels];
            GCHandle resultHandle = default;
            try
            {
                resultHandle = GCHandle.Alloc(result, GCHandleType.Pinned);
                IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
                for (int i = 0; i < channels; ++i)
                {
                    Mat cmat = new Mat(
                       src.Height, src.Width,
                       MatType.CV_32FC1,
                       resultPtr + i * size.Width * size.Height * sizeof(float));

                    Cv2.ExtractChannel(src, cmat, i);
                    cmat.Dispose();
                }
            }
            finally
            {
                resultHandle.Free();
            }
            return result;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            button2.Enabled = false;
            textBox1.Text = "";
            sb.Clear();
            sbTagsCN.Clear();
            sbTags.Clear();
            Application.DoEvents();

            image = new Mat(image_path);

            //图片缩放
            Mat resize_image = new Mat();
            Cv2.Resize(image, resize_image, new OpenCvSharp.Size(384, 384));

            Normalize(resize_image);

            var data = ExtractMat(resize_image);

            resize_image.Dispose();
            image.Dispose();

            // 输入Tensor
            input_tensor = new DenseTensor<float>(data, new[] { 1, 3, 384, 384 });

            //将 input_tensor 放入一个输入参数的容器,并指定名称
            input_container.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));

            dt1 = DateTime.Now;
            //运行 Inference 并获取结果
            result_infer = onnx_session.Run(input_container);
            dt2 = DateTime.Now;

            // 将输出结果转为DisposableNamedOnnxValue数组
            results_onnxvalue = result_infer.ToArray();

            // 读取第一个节点输出并转为Tensor数据
            result_tensors = results_onnxvalue[0].AsTensor<float>();

            var result_array = result_tensors.ToArray();

            double[] scores = new double[result_array.Length];
            for (int i = 0; i < result_array.Length; i++)
            {
                double score = 1 / (1 + Math.Exp(result_array[i] * -1));
                scores[i] = score;
            }
            List<Tag> tags = new List<Tag>(ltTag);

            List<Tag> topTags = new List<Tag>();
            for (int i = 0; i < scores.Length; i++)
            {
                if (scores[i] > tags[i].Threshold)
                {
                    tags[i].Score = scores[i];
                    topTags.Add(tags[i]);
                }
            }
            topTags.OrderByDescending(x => x.Score).ToList();

            foreach (var item in topTags)
            {
                sbTagsCN.Append(item.NameCN + ",");
                sbTags.Append(item.Name + ",");
            }
            sbTagsCN.Length--;
            sbTags.Length--;

            sb.AppendLine("Tags:" + sbTags.ToString());
            sb.AppendLine("标签:" + sbTagsCN.ToString());
            sb.AppendLine("------------------");
            sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
            textBox1.Text = sb.ToString();
            button2.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            model_path = "model/ram.onnx";

            // 创建输出会话,用于输出模型读取信息
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行
            // 创建推理模型类,读取本地模型文件
            onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径

            // 创建输入容器
            input_container = new List<NamedOnnxValue>();

            image_path = "test_img/1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);

            string[] thresholdLines = File.ReadAllLines("model/ram_tag_list_threshold.txt");
            string[] tagChineseLines = File.ReadAllLines("model/ram_tag_list_chinese.txt");
            string[] tagLines = File.ReadAllLines("model/ram_tag_list.txt");

            for (int i = 0; i < tagLines.Length; i++)
            {
                ltTag.Add(new Tag { NameCN = tagChineseLines[i], Name = tagLines[i], Threshold = double.Parse(thresholdLines[i]) });
            }
        }

    }
}

  1. using Microsoft.ML.OnnxRuntime;
  2. using Microsoft.ML.OnnxRuntime.Tensors;
  3. using OpenCvSharp;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. namespace Onnx_Demo
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  21. string image_path = "";
  22. DateTime dt1 = DateTime.Now;
  23. DateTime dt2 = DateTime.Now;
  24. string model_path;
  25. Mat image;
  26. SessionOptions options;
  27. InferenceSession onnx_session;
  28. Tensor<float> input_tensor;
  29. List<NamedOnnxValue> input_container;
  30. IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
  31. DisposableNamedOnnxValue[] results_onnxvalue;
  32. Tensor<float> result_tensors;
  33. StringBuilder sbTags = new StringBuilder();
  34. StringBuilder sbTagsCN = new StringBuilder();
  35. StringBuilder sb = new StringBuilder();
  36. public string[] class_names;
  37. List<Tag> ltTag = new List<Tag>();
  38. private void button1_Click(object sender, EventArgs e)
  39. {
  40. OpenFileDialog ofd = new OpenFileDialog();
  41. ofd.Filter = fileFilter;
  42. if (ofd.ShowDialog() != DialogResult.OK) return;
  43. pictureBox1.Image = null;
  44. image_path = ofd.FileName;
  45. pictureBox1.Image = new Bitmap(image_path);
  46. textBox1.Text = "";
  47. image = new Mat(image_path);
  48. }
  49. float[] mean = { 0.485f, 0.456f, 0.406f };
  50. float[] std = { 0.229f, 0.224f, 0.225f };
  51. public void Normalize(Mat src)
  52. {
  53. src.ConvertTo(src, MatType.CV_32FC3, 1.0 / 255);
  54. Mat[] bgr = src.Split();
  55. for (int i = 0; i < bgr.Length; ++i)
  56. {
  57. bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1 / std[i], (0.0 - mean[i]) / std[i]);
  58. }
  59. Cv2.Merge(bgr, src);
  60. foreach (Mat channel in bgr)
  61. {
  62. channel.Dispose();
  63. }
  64. }
  65. public float[] ExtractMat(Mat src)
  66. {
  67. OpenCvSharp.Size size = src.Size();
  68. int channels = src.Channels();
  69. float[] result = new float[size.Width * size.Height * channels];
  70. GCHandle resultHandle = default;
  71. try
  72. {
  73. resultHandle = GCHandle.Alloc(result, GCHandleType.Pinned);
  74. IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
  75. for (int i = 0; i < channels; ++i)
  76. {
  77. Mat cmat = new Mat(
  78. src.Height, src.Width,
  79. MatType.CV_32FC1,
  80. resultPtr + i * size.Width * size.Height * sizeof(float));
  81. Cv2.ExtractChannel(src, cmat, i);
  82. cmat.Dispose();
  83. }
  84. }
  85. finally
  86. {
  87. resultHandle.Free();
  88. }
  89. return result;
  90. }
  91. private void button2_Click(object sender, EventArgs e)
  92. {
  93. if (image_path == "")
  94. {
  95. return;
  96. }
  97. button2.Enabled = false;
  98. textBox1.Text = "";
  99. sb.Clear();
  100. sbTagsCN.Clear();
  101. sbTags.Clear();
  102. Application.DoEvents();
  103. image = new Mat(image_path);
  104. //图片缩放
  105. Mat resize_image = new Mat();
  106. Cv2.Resize(image, resize_image, new OpenCvSharp.Size(384, 384));
  107. Normalize(resize_image);
  108. var data = ExtractMat(resize_image);
  109. resize_image.Dispose();
  110. image.Dispose();
  111. // 输入Tensor
  112. input_tensor = new DenseTensor<float>(data, new[] { 1, 3, 384, 384 });
  113. //input_tensor 放入一个输入参数的容器,并指定名称
  114. input_container.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));
  115. dt1 = DateTime.Now;
  116. //运行 Inference 并获取结果
  117. result_infer = onnx_session.Run(input_container);
  118. dt2 = DateTime.Now;
  119. // 将输出结果转为DisposableNamedOnnxValue数组
  120. results_onnxvalue = result_infer.ToArray();
  121. // 读取第一个节点输出并转为Tensor数据
  122. result_tensors = results_onnxvalue[0].AsTensor<float>();
  123. var result_array = result_tensors.ToArray();
  124. double[] scores = new double[result_array.Length];
  125. for (int i = 0; i < result_array.Length; i++)
  126. {
  127. double score = 1 / (1 + Math.Exp(result_array[i] * -1));
  128. scores[i] = score;
  129. }
  130. List<Tag> tags = new List<Tag>(ltTag);
  131. List<Tag> topTags = new List<Tag>();
  132. for (int i = 0; i < scores.Length; i++)
  133. {
  134. if (scores[i] > tags[i].Threshold)
  135. {
  136. tags[i].Score = scores[i];
  137. topTags.Add(tags[i]);
  138. }
  139. }
  140. topTags.OrderByDescending(x => x.Score).ToList();
  141. foreach (var item in topTags)
  142. {
  143. sbTagsCN.Append(item.NameCN + ",");
  144. sbTags.Append(item.Name + ",");
  145. }
  146. sbTagsCN.Length--;
  147. sbTags.Length--;
  148. sb.AppendLine("Tags:" + sbTags.ToString());
  149. sb.AppendLine("标签:" + sbTagsCN.ToString());
  150. sb.AppendLine("------------------");
  151. sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
  152. textBox1.Text = sb.ToString();
  153. button2.Enabled = true;
  154. }
  155. private void Form1_Load(object sender, EventArgs e)
  156. {
  157. model_path = "model/ram.onnx";
  158. // 创建输出会话,用于输出模型读取信息
  159. options = new SessionOptions();
  160. options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
  161. options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行
  162. // 创建推理模型类,读取本地模型文件
  163. onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径
  164. // 创建输入容器
  165. input_container = new List<NamedOnnxValue>();
  166. image_path = "test_img/1.jpg";
  167. pictureBox1.Image = new Bitmap(image_path);
  168. image = new Mat(image_path);
  169. string[] thresholdLines = File.ReadAllLines("model/ram_tag_list_threshold.txt");
  170. string[] tagChineseLines = File.ReadAllLines("model/ram_tag_list_chinese.txt");
  171. string[] tagLines = File.ReadAllLines("model/ram_tag_list.txt");
  172. for (int i = 0; i < tagLines.Length; i++)
  173. {
  174. ltTag.Add(new Tag { NameCN = tagChineseLines[i], Name = tagLines[i], Threshold = double.Parse(thresholdLines[i]) });
  175. }
  176. }
  177. }
  178. }

下载

源码下载(带模型)

模型下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/233054
推荐阅读
相关标签
  

闽ICP备14008679号