Debug Stable Diffusion webui

文章目录

以下内容是最近的学习笔记,如果有不对的地方,还望同志们指出~共勉

SD

前期预备

深入Stable diffusion时,可以不按照官方指导来。官方指导对于AIGC爱好者比较的友好~.

可以选择Anaconda 按照之前AI传统,安装环境(需要从代码包里找到所有需要安装的库,有点麻烦,但是能用),也可以直接运行webui.sh安装虚拟的python环境(个人推荐VENV,非常省事,删掉VENV重新安装,科学上网后耗时只在半小时内)。

安装完环境后,可以设置IDE的python环境,然后debug 'launch.py', voila, 你可以开始各种探索了。

一些惊喜

TorchHijackForUnet

CondFunc('modules.models.diffusion.ddpm_edit.LatentDiffusion.decode_first_stage', first_stage_sub, first_stage_cond)

用于dynamicly import modules.

Txt2Img 搭配 Lora 使用

单独运行 txt2img.py

可以自行改写以下(放在txt2img.py的最后):

python 复制代码
if __name__ == "__main__":
    txt2img(id_task= 'task(lt4vr6pvvx26gfm)', 
            prompt= 'pandas', 
            negative_prompt= 'cats', 
            prompt_styles=[], 
            steps= 20, 
            sampler_index= 0, 
            restore_faces= False, 
            tiling= False, 
            n_iter= 1, #(对应GUI上的Batch count)
            batch_size= 1, 
            cfg_scale= 7, 
            seed= -1, 
            subseed= -1, 
            subseed_strength= 0, 
            seed_resize_from_h= 0, 
            seed_resize_from_w= 0, 
            seed_enable_extras= False, 
            height= 512, 
            width= 512, 
            enable_hr= False, 
            denoising_strength= 0.7, 
            hr_scale= 2, 
            hr_upscaler= 'Latent', 
            hr_second_pass_steps= 0, 
            hr_resize_x= 0, 
            hr_resize_y= 0, 
            hr_sampler_index= 0, 
            hr_prompt= '', 
            hr_negative_prompt='', 
            override_settings_texts=[], 
    )

获取所有资源

代码地址

modules\txt2img.py

参数

是一个Object processing.StableDiffusionProcessingTxt2Img

参数地址: Line 871 和 Line 105

modules\processing.py

python 复制代码
p = processing.StableDiffusionProcessingTxt2Img(
        sd_model=shared.sd_model,
        outpath_samples=opts.outdir_samples or opts.outdir_txt2img_samples,
        outpath_grids=opts.outdir_grids or opts.outdir_txt2img_grids,
        prompt=prompt,
        styles=prompt_styles,
        negative_prompt=negative_prompt,
        seed=seed,
        subseed=subseed,
        subseed_strength=subseed_strength,
        seed_resize_from_h=seed_resize_from_h,
        seed_resize_from_w=seed_resize_from_w,
        seed_enable_extras=seed_enable_extras,
        sampler_name=sd_samplers.samplers[sampler_index].name,
        batch_size=batch_size,
        n_iter=n_iter,
        steps=steps,
        cfg_scale=cfg_scale,
        width=width,
        height=height,
        restore_faces=restore_faces,
        tiling=tiling,
        enable_hr=enable_hr,
        denoising_strength=denoising_strength if enable_hr else None,
        hr_scale=hr_scale,
        hr_upscaler=hr_upscaler,
        hr_second_pass_steps=hr_second_pass_steps,
        hr_resize_x=hr_resize_x,
        hr_resize_y=hr_resize_y,
        hr_sampler_name=sd_samplers.samplers_for_img2img[hr_sampler_index - 1].name if hr_sampler_index != 0 else None,
        hr_prompt=hr_prompt,
        hr_negative_prompt=hr_negative_prompt,
        override_settings=override_settings,
    )

sd model

除了:

python 复制代码
unet = p.sd_model.model.diffusion_model
text_encoder = p.sd_model.cond_stage_model

sd model还包含:

  • first_stage_model
  • configure_shareded_model

主程序

  • process_images()
    • process_images_inner()

代码地址

modules\processing.py

参数(同上)

是一个Object processing.StableDiffusionProcessingTxt2Img

参数地址: Line 871 和 Line 105

modules\processing.py

模型Inference

LORA应用

地址:Line 186

extensions\sd-webui-additional-networks\scripts\additional_networks.py

Line 236 - Line 251:

  • 加载Lora模型到latest_networks 这个list中

  • 通过 du_state_dict = load_file(model_path)导入LORA模型每层权重(类似于一个ZIP里包含每一个layer的权重文件,然后通过文件名的index对应到每一层的名字上(单独一个name list))

  • network, info = lora_compvis.create_network_and_apply_compvis(du_state_dict, weight_tenc, weight_unet, text_encoder, unet) 将权重和LORA模型进行结合:

    dimension: {96}, alpha: {48.0}, multiplier_unet: 1, multiplier_tenc: 1
    create LoRA for Text Encoder: 72 modules.
    create LoRA for U-Net: 192 modules.
    original forward/weights is backed up.
    enable LoRA for text encoder
    enable LoRA for U-Net
    shapes for 0 weights are converted.

重构并使用LORA模型

地址:

extensions\sd-webui-additional-networks\scripts\lora_compvis.py

靠 model里是否包含"MultiheadAttention"判断LORA的版本。有:V2,没有:V1

modules 里的dim 和 alpha 都是传入的一个list,可以潜在不唯一(如果满足网络结构)

python 复制代码
comp_vis_loras_dim_alpha[comp_vis_lora_name] = (dim, alpha)

把SD与lora结合就需要根据几点去筛选出Unet和CLIP里有用的那几层, 条件:

  • 选择:Linear or Conv2d相关的
    • 不选 _resblocks_23_, 即StabilityAi Text Encoder最后一个block
    • 不选不在comp_vis_loras_dim_alpha这个dict的keys里的
  • 选择:MultiheadAttention相关的
    • 在这个范围内的每一层,都会通过加入 ["q_proj", "k_proj", "v_proj", "out_proj"] 这些suffix 来构建关于 Q,K, V, O 的重复项
    • 不选 _resblocks_23_, 即StabilityAi Text Encoder最后一个block
    • 不选不在comp_vis_loras_dim_alpha这个dict的keys里的

以上条件都满足的会通过:

python 复制代码
lora = LoRAModule(lora_name, child_module, multiplier, dim, alpha)

重构针对Lora使用的Unet 或者 CLIP的每一层/block,形成新的Unet & CLIP,也因此,我们的SD model 融入好了我们自己的LORA

用Lora重构后的网络 做 sampler

地址:

modules\processing.py

通过 def sample() 来运行:

  • 组装sampler
  • 设定latent scale mode
  • create_random_tensors
    • noise的shape =(4, 64, 64)

    • noise 被 slerp(subseed_strength, noise, subnoise)做interpolation的平滑优化

      samples = self.sampler.sample(self, x, conditioning, unconditional_conditioning, image_conditioning=self.txt2img_image_conditioning(x)

后处理

地址:

modules\processing.py

通过以下代码得到最终模型输出得结果:

python 复制代码
with devices.without_autocast() if devices.unet_needs_upcast else devices.autocast():
                samples_ddim = p.sample(conditioning=p.c, unconditional_conditioning=p.uc, seeds=p.seeds, subseeds=p.subseeds, subseed_strength=p.subseed_strength, prompts=p.prompts)

从Line 741 开始进入后处理得阶段(根据用户需要进行处理),以下为一个例子:

改颜色范围以及调换shape(从(3, 512, 512)==> (512, 512, 3)).

python 复制代码
x_sample = 255. * np.moveaxis(x_sample.cpu().numpy(), 0, 2)
相关推荐
love530love1 天前
【避坑指南】提示词“闹鬼”?Stable Diffusion 自动注入神秘词汇 xiao yi xian 排查全记录
人工智能·windows·stable diffusion·model keyword
世界尽头与你1 天前
Stable Diffusion web UI 未授权访问漏洞
安全·网络安全·stable diffusion·渗透测试
love530love1 天前
【故障解析】Stable Diffusion WebUI 更换主题后启动报 JSONDecodeError?可能是“主题加载”惹的祸
人工智能·windows·stable diffusion·大模型·json·stablediffusion·gradio 主题
ai_xiaogui6 天前
Stable Diffusion Web UI 绘世版 v4.6.1 整合包:一键极速部署,深度解决 AI 绘画环境配置与 CUDA 依赖难题
人工智能·stable diffusion·环境零配置·高性能内核优化·全功能插件集成·极速部署体验
微学AI7 天前
金仓数据库的新格局:以多模融合开创文档数据库
人工智能·stable diffusion
我的golang之路果然有问题7 天前
开源绘画大模型简单了解
人工智能·ai作画·stable diffusion·人工智能作画
我的golang之路果然有问题7 天前
comfyUI中的动作提取分享
人工智能·stable diffusion·ai绘画·人工智能作画·comfy
stephen one11 天前
2026 AI深度伪造危机:实测 Midjourney v7 与 Flux 2 Max 识别,谁才是 AI 检测的天花板?
人工智能·ai作画·stable diffusion·aigc·midjourney
长不大的蜡笔小新14 天前
基于Stable Diffusion的多模态图像生成与识别系统
stable diffusion
米汤爱学习14 天前
stable-diffusion-webui【笔记】
笔记·stable diffusion