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

news/2024/9/24 21:59:52/

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

        }
    }
}

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

下载 

源码下载


http://www.ppmy.cn/news/1427376.html

相关文章

安卓Dagger框架:依赖注入实践与应用

摘要 Dagger是适用于Android和Java生态系统的强大依赖注入(Dependency Injection, DI)工具&#xff0c;以其编译时生成代码和高效的运行时性能而著称。本文旨在深入探讨Dagger的核心概念、工作流程、优缺点以及实际代码示例&#xff0c;以便开发者更好地理解并有效利用这一框架…

EFK安装与使用!!!

一、将你的项目进行打包。 二、上传到docker&#xff0c; 启动项目 三、修改前端的代理路径 四、EFK相关配置 1、docker-compose.yml&#xff1a; version: 3 services:kibana:image: kibana:7.14.0ports:- "5601:5601"environment:- ELASTICSEARCH_HOSTShttp://19…

Webservice使用

Webservice使用教程 Webservice的交互模式是一个类似于CS结构的模式&#xff0c;因此它需要一个Server端与一个Client端。在Client端访问Server端的接口来实现Webservice的功能。 Server端 打开IDEA创建gradle模块 webservice-01-server1 然后再build.gradle.kts文件中添加以…

Windows10安装配置nodejs环境

一、下载 下载地址&#xff1a;https://nodejs.cn/download/ ​ 二、安装 1、找到node-v16.17.0-x64.msi安装包, 根据默认提示安装, 过程中间的弹窗不勾选 2、安装完成后, 打开powershell(管理员身份) ​ 3、命令行输入 node -v 和 npm -v 如下图所示则nodejs安装成功 ​ 三…

Mac下删除旧版本.net sdk

参照微软官网给的方法&#xff0c;Releases dotnet/cli-lab (github.com) 好像不能直接的解决问题&#xff0c;我做一下补充&#xff0c;希望对需要删除旧版本sdk的小伙伴们有所帮助 1:下载工具包 Releases dotnet/cli-lab (github.com) 2:打开终端&#xff0c;cd切换到该…

ES主要功能特性和使用场景

ES主要功能特性和使用场景 ES 通常指的是 Elasticsearch&#xff0c;这是一个高度可扩展的开源全文搜索引擎和数据分析平台。Elasticsearch 以其强大的实时搜索、分析和数据可视化能力而闻名&#xff0c;广泛应用于日志分析、应用程序监控、全文检索、商业智能、点击流分析等多…

Unity HDRP Water Surface 水系统 基础教程

Unity HDRP Water Surface 水系统 基础教程 Unity Water SurfaceUnity 项目创建Unity Water Surface&#xff1a;Ocean&#xff08;海洋&#xff09;简介Ocean&#xff1a;Transform、GeneralOcean&#xff1a;Simulation&#xff08;仿真模拟&#xff09;Ocean&#xff1a;Sim…

wx小程序-input事件改变数据

一、input标签 在index.xwml文件夹下写出input标签&#xff0c;并给它绑定一个处理函数inputTTT&#xff0c;用来改变msg的数据值。 <input value"{{msg}}" bindinput"inputTTT"/> 二、样式 和web一样&#xff0c;为了让input文本输入框好看一点…