stable-diffusion-webui sdxl模型代码分析

采样器这块基本都是用的k-diffusion,模型用的是stability的原生项目generative-models中的sgm,这点和fooocus不同,fooocus底层依赖comfyui中的models,comfy是用load_state_dict的方式解析的,用的load_checkpoint_guess_config函数,这个函数webui中也有。

webui在paths中导入了generative-models,在sd_model_config中导入了config.sdxl和config.sdxl_refiner两个config,模型使用sgm下的models/diffusion/DiffusionEngine初始化,refiner和base的模型几乎是一致的。

python webui.py --port 6006 --no-half-vae

python 复制代码
webui()->

initialize()->
initialize_rest()->
- sd_samplers.py -> set_samplers()->sd_samplers_kdiffusion.py->
- extensions.py -> list_extensions()
- initialize_util.py -> restore_config_state_file()
- sd_models.py -> list_models()
- localization.py -> list_localizations()
- scripts.load_scripts() -> scripts.py 
-- scripts_txt2img=ScriptRunner()/scripts_img2img=ScriptRunner()/scripts_postpro=scripts_postprocessing.ScriptPostprocessingRunner()(scripts_postprocessing.py)
- modelloader.py -> load_upscaler()
- sd_vae.py -> refresh_vae_list()
- textual_inversion/textual_inversion.py -> list_textual_inversion_templates() 
- script_callbacks.py -> on_list_optimizers(sd_hijack_optimizations.list_optimizers)
- sd_hijack.py -> list_optimizers()
- sd_unet.py -> list_unets()
- load_model -> shared.py 
- shared_items.py -> reload_hypernetworks() # 这种方式现在几乎不用了
- ui_extra_networks.py -> initialize()/register_default_pages()
- extra_networks.py -> initialize()/register_default_extra_networks()

ui.py -> ui.create_ui()

ui.py

一些基础参数的初始化也在这里,关于ui设计在webui中代码还是挺多的

ui_components.py 一些设计的ui组件

shared_items.py 重复的一些item

下面就是一个FormRow:

python 复制代码
elif category == "dimensions":
    with FormRow():
        with gr.Column(elem_id="txt2img_column_size", scale=4):
            width = gr.Slider(minimum=64, maximum=2048, step=8, label="Width", value=512, elem_id="txt2img_width")
            height = gr.Slider(minimum=64, maximum=2048, step=8, label="Height", value=512, elem_id="txt2img_height")

....

调用接口入口:

python 复制代码
txt2img_args = dict(
                fn=wrap_gradio_gpu_call(modules.txt2img.txt2img, extra_outputs=[None, '', '']),
                _js="submit",
                inputs=[
                    dummy_component,
                    toprow.prompt,
                    toprow.negative_prompt,
                    toprow.ui_styles.dropdown,
                    steps,
                    sampler_name,
                    batch_count,
                    batch_size,
                    cfg_scale,
                    height,
                    width,
                    enable_hr,
                    denoising_strength,
                    hr_scale,
                    hr_upscaler,
                    hr_second_pass_steps,
                    hr_resize_x,
                    hr_resize_y,
                    hr_checkpoint_name,
                    hr_sampler_name,
                    hr_prompt,
                    hr_negative_prompt,
                    override_settings,

                ] + custom_inputs,

txt2img.py

python 复制代码
p = processing.StableDiffusionProcessingTxt2Img(sd_model,,prompt,negative_prompt,sampler_name,...)->
processed = processing.process_images(p)

processing.py

python 复制代码
res = process_image_inner(p)
- sample_ddim = p.sample(conditioning,unconditional_conditioning,seeds,subseeds,subseed_strength,prompts)-> StableDiffusionProcessingTxt2Img.sample()
-- self.sampler = sd_sampler.create_sampler(self.sampler_name,self.sd_model)
-- samples = self.samplers.sample(c,uc,image_encoditioning=self.txt2img_image_conditioning(x))

sd_samplers_kdiffusion.py

python 复制代码
sample()->
samples = self.launch_sampling(steps,lambda:self.func(self.model_wrap_cfg,x,self.sampler_extra_args,...))
model_rap_cfg:CFGDenoiseKDiffusion->sd_samplers_cfg_denoiser.CFGDenoiser

sd_samplers_common.py

python 复制代码
func() = sample_dpmpp_2m ->

repositories/k-diffusion/k_diffusion/sampling.py

python 复制代码
sample_dpmpp_2m()->
- denoised = model(x,sigmas[i]*s_in,**extra_args)->
...
# 此处就是去噪产生图片的过程

modules/sd_samplers_cfg_denoiser.py model =

python 复制代码
CFGDenoiser()->
forward(x:2x4x128x128,sigma:[14.6146,14.6146],uncond:SchedulePromptConditionings,cond:MulticondLearnedConditioning,cond_scale:7,s_min_uncond:0,image_cond:2x5x1x1)->
denoised:2x4x128x128
# unet预测都被封装在这里

modules/sd_models.py 这块主要是

python 复制代码
reload_model_weights()->
sd_model = reuse_model_from_already_loaded(sd_model,checkpoint_info,...)
load_model()
....

modules/sd_samplers_common.py

python 复制代码
sd_models.reload_model_weights(refiner_checkpoint_info)
cfg_denoiser.update_inner_model()->

modules/sd_samplers_cfg_denoiser.py

python 复制代码
forward()->
相关推荐
空白诗3 天前
CANN ops-nn 算子解读:Stable Diffusion 图像生成中的 Conv2D 卷积实现
深度学习·计算机视觉·stable diffusion
学易3 天前
第十五节.别人的工作流,如何使用和调试(上)?(2类必现报错/缺失节点/缺失模型/思路/实操/通用调试步骤)
人工智能·ai作画·stable diffusion·报错·comfyui·缺失节点
心疼你的一切3 天前
基于CANN仓库算力手把手实现Stable Diffusion图像生成(附完整代码+流程图)
数据仓库·深度学习·stable diffusion·aigc·流程图·cann
Niuguangshuo4 天前
DALL-E 3:如何通过重构“文本描述“革新图像生成
人工智能·深度学习·计算机视觉·stable diffusion·重构·transformer
Niuguangshuo4 天前
深入解析 Stable Diffusion XL(SDXL):改进潜在扩散模型,高分辨率合成突破
stable diffusion
Niuguangshuo4 天前
深入解析Stable Diffusion基石——潜在扩散模型(LDMs)
人工智能·计算机视觉·stable diffusion
迈火4 天前
SD - Latent - Interposer:解锁Stable Diffusion潜在空间的创意工具
人工智能·gpt·计算机视觉·stable diffusion·aigc·语音识别·midjourney
迈火12 天前
Facerestore CF (Code Former):ComfyUI人脸修复的卓越解决方案
人工智能·gpt·计算机视觉·stable diffusion·aigc·语音识别·midjourney
重启编程之路13 天前
Stable Diffusion 参数记录
stable diffusion
孤狼warrior16 天前
图像生成 Stable Diffusion模型架构介绍及使用代码 附数据集批量获取
人工智能·python·深度学习·stable diffusion·cnn·transformer·stablediffusion