• 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]
    ---------------------------------------------------------------

    项目

    代码

    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. }

    下载

    源码下载(带模型)

    模型下载

  • 相关阅读:
    C++输入输出流解析
    遍历完全二叉树节点
    实操自动生成接口自动化测试用例
    qt-C++笔记之Qt中的时间与定时器
    【设计模式】建造者模式
    AddressSanitizer failed to allocate 0xdfff0001000 (15392894357504) bytes解决方法
    智慧医院解决方案
    find 与 cp 命令组合使用
    21天学习挑战赛——Python爬虫 requests库
    初步读懂linux内核结构
  • 原文地址:https://blog.csdn.net/weixin_46771779/article/details/136669983