【C#深度学习之路】如何使用C#实现Stable Diffusion的文生图功能 ...

打印 上一主题 下一主题

主题 1650|帖子 1650|积分 4950

本文为原创文章,若需要转载,请注明出处。
原文地址:https://blog.csdn.net/qq_30270773/article/details/147002073
项目对应的Github地址:https://github.com/IntptrMax/StableDiffusionSharp
项目打包的Nuget地址:https://github.com/IntptrMax/StableDiffusionSharp
C#深度学习之路专栏地址:https://blog.csdn.net/qq_30270773/category_12829217.html
关注我的Github,可以获取更多资料,请为你感爱好的项目奉上一颗小星星:https://github.com/IntptrMax
另外本人已经在多平台上发现了不做任何修改直接照抄就发布我的文章的盗版举动,还将我的开源免费资源当成付费资源发布的举动,对此表示强烈的不满。这种“偷窃知识”的举动严峻侵害了开源项目作者的个人利益以及开源共享精力。
项目背景

本人已经在Github及CSDN上一连发布了C#训练和推理主流的Yolo版本的代码,受到了许多小搭档的关注。由于YoloSharp的基本功能已经完成,本人对Stable Diffusion又比较感爱好,以是开始了StableDiffusionSharp的开辟。本文并不主要讲StableDiffusionSharp代码,而是先做个基本功能演示,代码会另外再讲解。
如果该资料对你有资助,请在我的Github上送我一颗小星星。该项目标Github链接为https://github.com/IntptrMax/StableDiffusionSharp
项目实现

本文主要借助了本人开辟的StableDiffusionSharp,该包已经发布到了Nuget上,以是直接拉取就可以用了。另外还需要环境依赖项,这个依赖项也需要再Nuget上拉取。

拉取完成后,就可以调用了。为了更直观,本文使用了Winform作为演示。需要使用.Net 8.0以上的环境支持。如果想要使用低版本的.net,请自行修改StableDiffusionSharp源码编译。
作为演示,主界面绘制得较为简单。

主要功能会合在Load Model 和 Generate两个按钮中。全部的功能代码均贴在下面了。此中Sd_StepProgress这个变乱可以看到正在进行的步数和去噪的图像结果。
  1. using StableDiffusionSharp;
  2. using System.Diagnostics;
  3. namespace StableDiffusionDemo_Winform
  4. {
  5.         public partial class FormMain : Form
  6.         {
  7.                 string modelPath = string.Empty;
  8.                 string vaeModelPath = string.Empty;
  9.                 StableDiffusionSharp.StableDiffusion sd;
  10.                 public FormMain()
  11.                 {
  12.                         InitializeComponent();
  13.                 }
  14.                 private void FormMain_Load(object sender, EventArgs e)
  15.                 {
  16.                         ComboBox_Device.SelectedIndex = 0;
  17.                         ComboBox_Precition.SelectedIndex = 0;
  18.                 }
  19.                 private void Button_ModelScan_Click(object sender, EventArgs e)
  20.                 {
  21.                         FileDialog fileDialog = new OpenFileDialog();
  22.                         fileDialog.Filter = "Model files|*.safetensors;*.ckpt;*.pt;*.pth|All files|*.*";
  23.                         if (fileDialog.ShowDialog() == DialogResult.OK)
  24.                         {
  25.                                 TextBox_ModelPath.Text = fileDialog.FileName;
  26.                                 modelPath = fileDialog.FileName;
  27.                         }
  28.                 }
  29.                 private void Button_ModelLoad_Click(object sender, EventArgs e)
  30.                 {
  31.                         if (File.Exists(modelPath))
  32.                         {
  33.                                 SDDeviceType deviceType = ComboBox_Device.SelectedIndex == 0 ? SDDeviceType.CUDA : SDDeviceType.CPU;
  34.                                 SDScalarType scalarType = ComboBox_Precition.SelectedIndex == 0 ? SDScalarType.Float16 : SDScalarType.Float32;
  35.                                 Task.Run(() =>
  36.                                 {
  37.                                         base.Invoke(() => Button_ModelLoad.Enabled = false);
  38.                                         sd = new StableDiffusionSharp.StableDiffusion(deviceType, scalarType);
  39.                                         sd.StepProgress += Sd_StepProgress;
  40.                                         sd.LoadModel(modelPath, vaeModelPath);
  41.                                         base.Invoke(() =>
  42.                                         {
  43.                                                 Button_ModelLoad.Enabled = true;
  44.                                                 Button_Generate.Enabled = true;
  45.                                                 Label_State.Text = "Model loaded.";
  46.                                         });
  47.                                 });
  48.                         }
  49.                 }
  50.                 private void Button_VAEModelScan_Click(object sender, EventArgs e)
  51.                 {
  52.                         FileDialog fileDialog = new OpenFileDialog();
  53.                         fileDialog.Filter = "Model files|*.safetensors;*.ckpt;*.pt;*.pth|All files|*.*";
  54.                         if (fileDialog.ShowDialog() == DialogResult.OK)
  55.                         {
  56.                                 TextBox_VaePath.Text = fileDialog.FileName;
  57.                                 vaeModelPath = fileDialog.FileName;
  58.                         }
  59.                 }
  60.                 private void Sd_StepProgress(object? sender, StableDiffusionSharp.StableDiffusion.StepEventArgs e)
  61.                 {
  62.                         base.Invoke(() =>
  63.                         {
  64.                                 Label_State.Text = $"Progress: {e.CurrentStep}/{e.TotalSteps}";
  65.                                 if (e.VaeApproxImg != null)
  66.                                 {
  67.                                         MemoryStream memoryStream = new MemoryStream();
  68.                                         e.VaeApproxImg.Write(memoryStream, ImageMagick.MagickFormat.Jpg);
  69.                                         base.Invoke(() =>
  70.                                         {
  71.                                                 PictureBox_Output.Image = Image.FromStream(memoryStream);
  72.                                         });
  73.                                 }
  74.                         });
  75.                 }
  76.                 private void Button_Generate_Click(object sender, EventArgs e)
  77.                 {
  78.                         string prompt = TextBox_Prompt.Text;
  79.                         string nprompt = TextBox_NPrompt.Text;
  80.                         int step = (int)NumericUpDown_Step.Value;
  81.                         float cfg = (float)NumericUpDown_CFG.Value;
  82.                         long seed = 0;
  83.                         int width = (int)NumericUpDown_Width.Value;
  84.                         int height = (int)NumericUpDown_Height.Value;
  85.                         int clipSkip = (int)NumericUpDown_ClipSkip.Value;
  86.                         Task.Run(() =>
  87.                         {
  88.                                 Stopwatch stopwatch = Stopwatch.StartNew();
  89.                                 base.Invoke(() =>
  90.                                 {
  91.                                         Button_ModelLoad.Enabled = false;
  92.                                         Button_Generate.Enabled = false;
  93.                                         Label_State.Text = "Generating...";
  94.                                 });
  95.                                 ImageMagick.MagickImage image = sd.TextToImage(prompt, nprompt, clipSkip, width, height, step, seed, cfg);
  96.                                 MemoryStream memoryStream = new MemoryStream();
  97.                                 image.Write(memoryStream, ImageMagick.MagickFormat.Jpg);
  98.                                 base.Invoke(() =>
  99.                                 {
  100.                                         PictureBox_Output.Image = Image.FromStream(memoryStream);
  101.                                         Button_ModelLoad.Enabled = true;
  102.                                         Button_Generate.Enabled = true;
  103.                                         Label_State.Text = $"Done. It takes {stopwatch.Elapsed.TotalSeconds.ToString("f2")} s";
  104.                                 });
  105.                                 GC.Collect();
  106.                         });
  107.                 }
  108.         }
  109. }
复制代码
写在末了

使用C#深度学习项目是许多人所盼望的。不过在该方向上资料很少,开辟难度大。常规使用C#进行深度学习项目标方法为使用Python训练,转为Onnx模子再用C#调用。
目前我盼望能够改变这一现象,盼望能用纯C#平台进行训练和推理。这条路还很长,也很困难,盼望有爱好的读者能跟我一起让让C#的深度学习开辟环境更为完善,以此能资助到更多的人。
另外随着项目标关注度增多,已经开始有人盗版我的项目并将免费开源的项目当成付费项目在卖了。这种举动极其恶劣,请各位小搭档积极抵制这种举动,还开源项目一片干净的环境,也让开源项目开辟者有动力继续贡献更多的项目。
我在Github上已经将完备的代码发布了,项目地址为:https://github.com/IntptrMax/StableDiffusionSharp,期待你能在Github上送我一颗小星星。在我的Github里还GGMLSharp这个项目,这个项目也是C#平台下深度学习的开辟包,盼望能得到你的支持。
项目下载链接

https://download.csdn.net/download/qq_30270773/90566451

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

民工心事

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表