本文为原创文章,若需要转载,请注明出处。
原文地址: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这个变乱可以看到正在进行的步数和去噪的图像结果。
- using StableDiffusionSharp;
- using System.Diagnostics;
- namespace StableDiffusionDemo_Winform
- {
- public partial class FormMain : Form
- {
- string modelPath = string.Empty;
- string vaeModelPath = string.Empty;
- StableDiffusionSharp.StableDiffusion sd;
- public FormMain()
- {
- InitializeComponent();
- }
- private void FormMain_Load(object sender, EventArgs e)
- {
- ComboBox_Device.SelectedIndex = 0;
- ComboBox_Precition.SelectedIndex = 0;
- }
- private void Button_ModelScan_Click(object sender, EventArgs e)
- {
- FileDialog fileDialog = new OpenFileDialog();
- fileDialog.Filter = "Model files|*.safetensors;*.ckpt;*.pt;*.pth|All files|*.*";
- if (fileDialog.ShowDialog() == DialogResult.OK)
- {
- TextBox_ModelPath.Text = fileDialog.FileName;
- modelPath = fileDialog.FileName;
- }
- }
- private void Button_ModelLoad_Click(object sender, EventArgs e)
- {
- if (File.Exists(modelPath))
- {
- SDDeviceType deviceType = ComboBox_Device.SelectedIndex == 0 ? SDDeviceType.CUDA : SDDeviceType.CPU;
- SDScalarType scalarType = ComboBox_Precition.SelectedIndex == 0 ? SDScalarType.Float16 : SDScalarType.Float32;
- Task.Run(() =>
- {
- base.Invoke(() => Button_ModelLoad.Enabled = false);
- sd = new StableDiffusionSharp.StableDiffusion(deviceType, scalarType);
- sd.StepProgress += Sd_StepProgress;
- sd.LoadModel(modelPath, vaeModelPath);
- base.Invoke(() =>
- {
- Button_ModelLoad.Enabled = true;
- Button_Generate.Enabled = true;
- Label_State.Text = "Model loaded.";
- });
- });
- }
- }
- private void Button_VAEModelScan_Click(object sender, EventArgs e)
- {
- FileDialog fileDialog = new OpenFileDialog();
- fileDialog.Filter = "Model files|*.safetensors;*.ckpt;*.pt;*.pth|All files|*.*";
- if (fileDialog.ShowDialog() == DialogResult.OK)
- {
- TextBox_VaePath.Text = fileDialog.FileName;
- vaeModelPath = fileDialog.FileName;
- }
- }
- private void Sd_StepProgress(object? sender, StableDiffusionSharp.StableDiffusion.StepEventArgs e)
- {
- base.Invoke(() =>
- {
- Label_State.Text = $"Progress: {e.CurrentStep}/{e.TotalSteps}";
- if (e.VaeApproxImg != null)
- {
- MemoryStream memoryStream = new MemoryStream();
- e.VaeApproxImg.Write(memoryStream, ImageMagick.MagickFormat.Jpg);
- base.Invoke(() =>
- {
- PictureBox_Output.Image = Image.FromStream(memoryStream);
- });
- }
- });
- }
- private void Button_Generate_Click(object sender, EventArgs e)
- {
- string prompt = TextBox_Prompt.Text;
- string nprompt = TextBox_NPrompt.Text;
- int step = (int)NumericUpDown_Step.Value;
- float cfg = (float)NumericUpDown_CFG.Value;
- long seed = 0;
- int width = (int)NumericUpDown_Width.Value;
- int height = (int)NumericUpDown_Height.Value;
- int clipSkip = (int)NumericUpDown_ClipSkip.Value;
- Task.Run(() =>
- {
- Stopwatch stopwatch = Stopwatch.StartNew();
- base.Invoke(() =>
- {
- Button_ModelLoad.Enabled = false;
- Button_Generate.Enabled = false;
- Label_State.Text = "Generating...";
- });
- ImageMagick.MagickImage image = sd.TextToImage(prompt, nprompt, clipSkip, width, height, step, seed, cfg);
- MemoryStream memoryStream = new MemoryStream();
- image.Write(memoryStream, ImageMagick.MagickFormat.Jpg);
- base.Invoke(() =>
- {
- PictureBox_Output.Image = Image.FromStream(memoryStream);
- Button_ModelLoad.Enabled = true;
- Button_Generate.Enabled = true;
- Label_State.Text = $"Done. It takes {stopwatch.Elapsed.TotalSeconds.ToString("f2")} s";
- });
- GC.Collect();
- });
- }
- }
- }
复制代码 写在末了
使用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企服之家,中国第一个企服评测及商务社交产业平台。 |