AI大模型教程
一起来学习

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().ToArray();

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

            List ltResult = new List();
            ScoreIndex temp;
            for (int i = 0; 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 str = new List();
            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();

        }
    }
}

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().ToArray();

            double[] scores = new double[result_array.Length];
            for (int i = 0; i  ltResult = new List();
            ScoreIndex temp;
            for (int i = 0; i  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 str = new List();
            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();

        }
    }
}

下载 

源码下载

文章来源于互联网:C# danbooru Stable Diffusion 提示词反推 OpenVINO Demo

相关推荐: 大厂实战!如何帮用户一小时轻松搭建智能体?

前言 我们身处 AI 的浪潮中,智能体已在各行各业为大家带来便利,如基金经理可以利用智能体帮自己分析市场趋势、出租屋中介可以让智能体替自己回答咨询问题。现在,在百度营销平台,广告主也可以搭建属于自己的品牌智能体,让品牌与用户能产生更直接的互动,从而激发品牌潜能…

赞(0)
未经允许不得转载:5bei.cn大模型教程网 » C# danbooru Stable Diffusion 提示词反推 OpenVINO Demo
分享到: 更多 (0)

AI大模型,我们的未来

小欢软考联系我们