当前位置:   article > 正文

C# danbooru Stable Diffusion 提示词反推 OpenVINO Demo

C# danbooru Stable Diffusion 提示词反推 OpenVINO Demo

C# danbooru Stable Diffusion 提示词反推 OpenVINO Demo

目录

说明

效果

模型信息

项目

代码

下载 


说明

 模型下载地址:https://huggingface.co/deepghs/ml-danbooru-onnx

效果

模型信息

OVVersion { BuildNumber = 2023.1.0-12185-9e6b00e51cd-releases/2023/1, Description = OpenVINO Runtime }
---------------------------------------------------------------
本机可用设备
CPU
GNA
GPU
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:F32[?, 3, ?, ?]

---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:F32[?, 12547]

---------------------------------------------------------------

项目

代码

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace C__danbooru_Stable_Diffusion_提示词反推_OpenVINO__Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

        StringBuilder sb = new StringBuilder();
        public string[] class_names;

        Model rawModel;
        PrePostProcessor pp;
        Model m;
        CompiledModel cm;
        InferRequest ir;

        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);
        }

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

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

            image = new Mat(image_path);


            image = new Mat(image_path);
            int w = image.Width;
            int h = image.Height;

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            float[] input_tensor_data;

            image.ConvertTo(image, MatType.CV_32FC3, 1.0 / 255);
            input_tensor_data = Common.ExtractMat(image);

            Tensor input_tensor = Tensor.FromArray(input_tensor_data, new Shape(1, 3, h, w));

            ir.Inputs[0] = input_tensor;

            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            ir.Run();

            double inferTime = stopwatch.Elapsed.TotalMilliseconds;

            stopwatch.Restart();

            var result_array = ir.Outputs[0].GetData<float>().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<ScoreIndex> ltResult = new List<ScoreIndex>();
            ScoreIndex temp;
            for (int i = 0; i < scores.Length; i++)
            {
                temp = new ScoreIndex(i, scores[i]);
                ltResult.Add(temp);
            }

            //根据分数倒序排序,取前10个
            var SortedByScore = ltResult.OrderByDescending(p => p.Score).ToList().Take(10);

            foreach (var item in SortedByScore)
            {
                sb.Append(class_names[item.Index] + ",");
            }
            sb.Length--; // 将长度减1来移除最后一个字符

            sb.AppendLine("");
            sb.AppendLine("------------------");


            double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Stop();

            double totalTime = preprocessTime + inferTime + postprocessTime;

            sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
            sb.AppendLine($"Infer: {inferTime:F2}ms");
            sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
            sb.AppendLine($"Total: {totalTime:F2}ms");
            textBox1.Text = sb.ToString();
            button2.Enabled = true;
        }

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

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

            List<string> str = new List<string>();
            StreamReader sr = new StreamReader("model/lable.txt");
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                str.Add(line);
            }
            class_names = str.ToArray();


            rawModel = OVCore.Shared.ReadModel(model_path);
            pp = rawModel.CreatePrePostProcessor();

            m = pp.BuildModel();
            cm = OVCore.Shared.CompileModel(m, "CPU");
            ir = cm.CreateInferRequest();

        }
    }
}

  1. using OpenCvSharp;
  2. using Sdcb.OpenVINO;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. namespace C__danbooru_Stable_Diffusion_提示词反推_OpenVINO__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. string model_path;
  23. Mat image;
  24. StringBuilder sb = new StringBuilder();
  25. public string[] class_names;
  26. Model rawModel;
  27. PrePostProcessor pp;
  28. Model m;
  29. CompiledModel cm;
  30. InferRequest ir;
  31. private void button1_Click(object sender, EventArgs e)
  32. {
  33. OpenFileDialog ofd = new OpenFileDialog();
  34. ofd.Filter = fileFilter;
  35. if (ofd.ShowDialog() != DialogResult.OK) return;
  36. pictureBox1.Image = null;
  37. image_path = ofd.FileName;
  38. pictureBox1.Image = new Bitmap(image_path);
  39. textBox1.Text = "";
  40. image = new Mat(image_path);
  41. }
  42. private void button2_Click(object sender, EventArgs e)
  43. {
  44. if (image_path == "")
  45. {
  46. return;
  47. }
  48. button2.Enabled = false;
  49. textBox1.Text = "";
  50. sb.Clear();
  51. Application.DoEvents();
  52. image = new Mat(image_path);
  53. image = new Mat(image_path);
  54. int w = image.Width;
  55. int h = image.Height;
  56. Stopwatch stopwatch = new Stopwatch();
  57. stopwatch.Start();
  58. float[] input_tensor_data;
  59. image.ConvertTo(image, MatType.CV_32FC3, 1.0 / 255);
  60. input_tensor_data = Common.ExtractMat(image);
  61. Tensor input_tensor = Tensor.FromArray(input_tensor_data, new Shape(1, 3, h, w));
  62. ir.Inputs[0] = input_tensor;
  63. double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
  64. stopwatch.Restart();
  65. ir.Run();
  66. double inferTime = stopwatch.Elapsed.TotalMilliseconds;
  67. stopwatch.Restart();
  68. var result_array = ir.Outputs[0].GetData<float>().ToArray();
  69. double[] scores = new double[result_array.Length];
  70. for (int i = 0; i < result_array.Length; i++)
  71. {
  72. double score = 1 / (1 + Math.Exp(result_array[i] * -1));
  73. scores[i] = score;
  74. }
  75. List<ScoreIndex> ltResult = new List<ScoreIndex>();
  76. ScoreIndex temp;
  77. for (int i = 0; i < scores.Length; i++)
  78. {
  79. temp = new ScoreIndex(i, scores[i]);
  80. ltResult.Add(temp);
  81. }
  82. //根据分数倒序排序,取前10
  83. var SortedByScore = ltResult.OrderByDescending(p => p.Score).ToList().Take(10);
  84. foreach (var item in SortedByScore)
  85. {
  86. sb.Append(class_names[item.Index] + ",");
  87. }
  88. sb.Length--; // 将长度减1来移除最后一个字符
  89. sb.AppendLine("");
  90. sb.AppendLine("------------------");
  91. double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
  92. stopwatch.Stop();
  93. double totalTime = preprocessTime + inferTime + postprocessTime;
  94. sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
  95. sb.AppendLine($"Infer: {inferTime:F2}ms");
  96. sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
  97. sb.AppendLine($"Total: {totalTime:F2}ms");
  98. textBox1.Text = sb.ToString();
  99. button2.Enabled = true;
  100. }
  101. private void Form1_Load(object sender, EventArgs e)
  102. {
  103. model_path = "model/ml_danbooru.onnx";
  104. image_path = "test_img/2.jpg";
  105. pictureBox1.Image = new Bitmap(image_path);
  106. image = new Mat(image_path);
  107. List<string> str = new List<string>();
  108. StreamReader sr = new StreamReader("model/lable.txt");
  109. string line;
  110. while ((line = sr.ReadLine()) != null)
  111. {
  112. str.Add(line);
  113. }
  114. class_names = str.ToArray();
  115. rawModel = OVCore.Shared.ReadModel(model_path);
  116. pp = rawModel.CreatePrePostProcessor();
  117. m = pp.BuildModel();
  118. cm = OVCore.Shared.CompileModel(m, "CPU");
  119. ir = cm.CreateInferRequest();
  120. }
  121. }
  122. }

下载 

源码下载

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号