当前位置: 首页 > news >正文

stable-diffusion-webui 的模型更新

shared.py和sd_models.py中

shared.py:

options_templates.update(options_section(('sd', "Stable Diffusion"), {"sd_model_checkpoint": OptionInfo(None, "Stable Diffusion checkpoint", gr.Dropdown, lambda: {"choices": list_checkpoint_tiles()}, refresh=refresh_checkpoints),"sd_checkpoint_cache": OptionInfo(0, "Checkpoints to cache in RAM", gr.Slider, {"minimum": 0, "maximum": 10, "step": 1}),"sd_vae_checkpoint_cache": OptionInfo(0, "VAE Checkpoints to cache in RAM", gr.Slider, {"minimum": 0, "maximum": 10, "step": 1}),"sd_vae": OptionInfo("Automatic", "SD VAE", gr.Dropdown, lambda: {"choices": shared_items.sd_vae_items()}, refresh=shared_items.refresh_vae_list).info("choose VAE model: Automatic = use one with same filename as checkpoint; None = use VAE from checkpoint"),"sd_vae_as_default": OptionInfo(True, "Ignore selected VAE for stable diffusion checkpoints that have their own .vae.pt next to them"),"sd_unet": OptionInfo("Automatic", "SD Unet", gr.Dropdown, lambda: {"choices": shared_items.sd_unet_items()}, refresh=shared_items.refresh_unet_list).info("choose Unet model: Automatic = use one with same filename as checkpoint; None = use Unet from checkpoint"),"inpainting_mask_weight": OptionInfo(1.0, "Inpainting conditioning mask strength", gr.Slider, {"minimum": 0.0, "maximum": 1.0, "step": 0.01}),"initial_noise_multiplier": OptionInfo(1.0, "Noise multiplier for img2img", gr.Slider, {"minimum": 0.5, "maximum": 1.5, "step": 0.01}),"img2img_color_correction": OptionInfo(False, "Apply color correction to img2img results to match original colors."),"img2img_fix_steps": OptionInfo(False, "With img2img, do exactly the amount of steps the slider specifies.").info("normally you'd do less with less denoising"),"img2img_background_color": OptionInfo("#ffffff", "With img2img, fill image's transparent parts with this color.", ui_components.FormColorPicker, {}),"enable_quantization": OptionInfo(False, "Enable quantization in K samplers for sharper and cleaner results. This may change existing seeds. Requires restart to apply."),"enable_emphasis": OptionInfo(True, "Enable emphasis").info("use (text) to make model pay more attention to text and [text] to make it pay less attention"),"enable_batch_seeds": OptionInfo(True, "Make K-diffusion samplers produce same images in a batch as when making a single image"),"comma_padding_backtrack": OptionInfo(20, "Prompt word wrap length limit", gr.Slider, {"minimum": 0, "maximum": 74, "step": 1}).info("in tokens - for texts shorter than specified, if they don't fit into 75 token limit, move them to the next 75 token chunk"),"CLIP_stop_at_last_layers": OptionInfo(1, "Clip skip", gr.Slider, {"minimum": 1, "maximum": 12, "step": 1}).link("wiki", "https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#clip-skip").info("ignore last layers of CLIP network; 1 ignores none, 2 ignores one layer"),"upcast_attn": OptionInfo(False, "Upcast cross attention layer to float32"),"randn_source": OptionInfo("GPU", "Random number generator source.", gr.Radio, {"choices": ["GPU", "CPU"]}).info("changes seeds drastically; use CPU to produce the same picture across different videocard vendors"),
}))

模型的列表在list_checkpoint_tiles()中,更新在refresh_checkpoints中,282行

def list_checkpoint_tiles():import modules.sd_modelsreturn modules.sd_models.checkpoint_tiles()def refresh_checkpoints():import modules.sd_modelsreturn modules.sd_models.list_models()

点击选中更改,sd_model.py的519行:

def reload_model_weights(sd_model=None, info=None):from modules import lowvram, devices, sd_hijackcheckpoint_info = info or select_checkpoint()if not sd_model:sd_model = model_data.sd_modelif sd_model is None:  # previous model load failedcurrent_checkpoint_info = Noneelse:current_checkpoint_info = sd_model.sd_checkpoint_infoif sd_model.sd_model_checkpoint == checkpoint_info.filename:returnsd_unet.apply_unet("None")if shared.cmd_opts.lowvram or shared.cmd_opts.medvram:lowvram.send_everything_to_cpu()else:sd_model.to(devices.cpu)sd_hijack.model_hijack.undo_hijack(sd_model)timer = Timer()state_dict = get_checkpoint_state_dict(checkpoint_info, timer)checkpoint_config = sd_models_config.find_checkpoint_config(state_dict, checkpoint_info)timer.record("find config")if sd_model is None or checkpoint_config != sd_model.used_config:del sd_modelload_model(checkpoint_info, already_loaded_state_dict=state_dict)return model_data.sd_modeltry:load_model_weights(sd_model, checkpoint_info, state_dict, timer)except Exception:print("Failed to load checkpoint, restoring previous")load_model_weights(sd_model, current_checkpoint_info, None, timer)raisefinally:sd_hijack.model_hijack.hijack(sd_model)timer.record("hijack")script_callbacks.model_loaded_callback(sd_model)timer.record("script callbacks")if not shared.cmd_opts.lowvram and not shared.cmd_opts.medvram:sd_model.to(devices.device)timer.record("move model to device")print(f"Weights loaded in {timer.summary()}.")return sd_model

其中sd_model.py的165行中的select_checkpoint

def select_checkpoint():"""Raises `FileNotFoundError` if no checkpoints are found."""model_checkpoint = shared.opts.sd_model_checkpointcheckpoint_info = checkpoint_alisases.get(model_checkpoint, None)if checkpoint_info is not None:return checkpoint_infoif len(checkpoints_list) == 0:error_message = "No checkpoints found. When searching for checkpoints, looked at:"if shared.cmd_opts.ckpt is not None:error_message += f"\n - file {os.path.abspath(shared.cmd_opts.ckpt)}"error_message += f"\n - directory {model_path}"if shared.cmd_opts.ckpt_dir is not None:error_message += f"\n - directory {os.path.abspath(shared.cmd_opts.ckpt_dir)}"error_message += "Can't run without a checkpoint. Find and place a .ckpt or .safetensors file into any of those locations."raise FileNotFoundError(error_message)checkpoint_info = next(iter(checkpoints_list.values()))if model_checkpoint is not None:print(f"Checkpoint {model_checkpoint} not found; loading fallback {checkpoint_info.title}", file=sys.stderr)return checkpoint_info

实际如果在代码中想要更改权重:

 reload_model_weights(model_checkpoint=input_json.get('model', "chilloutmix_NiPrunedFp32Fix.safetensors"))

即可。

初始化在webui.py的270行

modules.sd_models.list_models()

中,主要Stable-diffusion下的模型都是提前初始化,在整个工程启动前,sd_model.py的113行

def list_models():checkpoints_list.clear()checkpoint_alisases.clear()cmd_ckpt = shared.cmd_opts.ckptif shared.cmd_opts.no_download_sd_model or cmd_ckpt != shared.sd_model_file or os.path.exists(cmd_ckpt):model_url = Noneelse:model_url = "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors"model_list = modelloader.load_models(model_path=model_path, model_url=model_url, command_path=shared.cmd_opts.ckpt_dir, ext_filter=[".ckpt", ".safetensors"], download_name="v1-5-pruned-emaonly.safetensors", ext_blacklist=[".vae.ckpt", ".vae.safetensors"])if os.path.exists(cmd_ckpt):checkpoint_info = CheckpointInfo(cmd_ckpt)checkpoint_info.register()shared.opts.data['sd_model_checkpoint'] = checkpoint_info.titleelif cmd_ckpt is not None and cmd_ckpt != shared.default_sd_model_file:print(f"Checkpoint in --ckpt argument not found (Possible it was moved to {model_path}: {cmd_ckpt}", file=sys.stderr)for filename in sorted(model_list, key=str.lower):checkpoint_info = CheckpointInfo(filename)checkpoint_info.register()

被注册在checkpoint_info中

webui.py的311行,加载模型:

def load_model():"""Accesses shared.sd_model property to load model.After it's available, if it has been loaded before this access by some extension,its optimization may be None because the list of optimizaers has neet been filledby that time, so we apply optimization again."""shared.sd_model  # noqa: B018if modules.sd_hijack.current_optimizer is None:modules.sd_hijack.apply_optimizations()Thread(target=load_model).start()

在shared.py的714行中

class Shared(sys.modules[__name__].__class__):"""this class is here to provide sd_model field as a property, so that it can be created and loaded on demand rather thanat program startup."""sd_model_val = None@propertydef sd_model(self):import modules.sd_modelsreturn modules.sd_models.model_data.get_sd_model()@sd_model.setterdef sd_model(self, value):import modules.sd_modelsmodules.sd_models.model_data.set_sd_model(value)sd_model: LatentDiffusion = None  # this var is here just for IDE's type checking; it cannot be accessed because the class field above will be accessed instead
sys.modules[__name__].__class__ = Shared

sd_model的getter/setter属性,在sd_model.py的406行

class SdModelData:def __init__(self):self.sd_model = Noneself.was_loaded_at_least_once = Falseself.lock = threading.Lock()def get_sd_model(self):if self.was_loaded_at_least_once:return self.sd_modelif self.sd_model is None:with self.lock:if self.sd_model is not None or self.was_loaded_at_least_once:return self.sd_modeltry:load_model()except Exception as e:errors.display(e, "loading stable diffusion model", full_traceback=True)print("", file=sys.stderr)print("Stable diffusion model failed to load", file=sys.stderr)self.sd_model = Nonereturn self.sd_modeldef set_sd_model(self, v):self.sd_model = v

在sd_model.py的438行,加载模型:


def load_model(checkpoint_info=None, already_loaded_state_dict=None):from modules import lowvram, sd_hijackcheckpoint_info = checkpoint_info or select_checkpoint()if model_data.sd_model:sd_hijack.model_hijack.undo_hijack(model_data.sd_model)model_data.sd_model = Nonegc.collect()devices.torch_gc()do_inpainting_hijack()timer = Timer()if already_loaded_state_dict is not None:state_dict = already_loaded_state_dictelse:state_dict = get_checkpoint_state_dict(checkpoint_info, timer)checkpoint_config = sd_models_config.find_checkpoint_config(state_dict, checkpoint_info)clip_is_included_into_sd = sd1_clip_weight in state_dict or sd2_clip_weight in state_dicttimer.record("find config")sd_config = OmegaConf.load(checkpoint_config)repair_config(sd_config)timer.record("load config")print(f"Creating model from config: {checkpoint_config}")sd_model = Nonetry:with sd_disable_initialization.DisableInitialization(disable_clip=clip_is_included_into_sd):sd_model = instantiate_from_config(sd_config.model)except Exception:passif sd_model is None:print('Failed to create model quickly; will retry using slow method.', file=sys.stderr)sd_model = instantiate_from_config(sd_config.model)sd_model.used_config = checkpoint_configtimer.record("create model")load_model_weights(sd_model, checkpoint_info, state_dict, timer)if shared.cmd_opts.lowvram or shared.cmd_opts.medvram:lowvram.setup_for_low_vram(sd_model, shared.cmd_opts.medvram)else:sd_model.to(shared.device)timer.record("move model to device")sd_hijack.model_hijack.hijack(sd_model)timer.record("hijack")sd_model.eval()model_data.sd_model = sd_modelmodel_data.was_loaded_at_least_once = Truesd_hijack.model_hijack.embedding_db.load_textual_inversion_embeddings(force_reload=True)  # Reload embeddings after model load as they may or may not fit the modeltimer.record("load textual inversion embeddings")script_callbacks.model_loaded_callback(sd_model)timer.record("scripts callbacks")with devices.autocast(), torch.no_grad():sd_model.cond_stage_model_empty_prompt = sd_model.cond_stage_model([""])timer.record("calculate empty prompt")print(f"Model loaded in {timer.summary()}.")return sd_model

相关文章:

stable-diffusion-webui 的模型更新

shared.py和sd_models.py中 shared.py: options_templates.update(options_section((sd, "Stable Diffusion"), {"sd_model_checkpoint": OptionInfo(None, "Stable Diffusion checkpoint", gr.Dropdown, lambda: {"choices": list_…...

Gin模板语法

Gin模板语法 文章目录 <center> Gin模板语法前提提醒Gin框架启动服务器模板解析模板渲染遇到不同目录下相同的文件如何加载和渲染自定义函数加载静态文件 前提提醒 由于有了前面template包的基础,所以该笔记不再过多详细分析 Gin框架启动服务器 语法: r:gin.Default()/…...

Go http.Handle和http.HandleFunc的路由问题

Golang的net/http包提供了原生的http服务&#xff0c;其中http.Handle和http.HandleFunc是两个重要的路由函数。 1. 函数介绍 http.HandleFunc和http.Handle的函数原型如下&#xff0c;其中DefaultServeMux是http包提供的一个默认的路由选择器。 func HandleFunc(pattern st…...

如何使用Kali Linux进行渗透测试?

1. 渗透测试简介 渗透测试是通过模拟恶意攻击&#xff0c;评估系统、应用或网络的安全性的过程。Kali Linux为渗透测试人员提供了丰富的工具和资源&#xff0c;用于发现漏洞、弱点和安全风险。 2. 使用Kali Linux进行渗透测试的步骤 以下是使用Kali Linux进行渗透测试的基本…...

简单易用且高效的跨平台开发工具:Xojo 2023 for Mac

Xojo for Mac是Mac平台上一个跨平台的针对桌面、Web、移动和Raspberry Pi的快速应用程序开发软件。与其他多平台开发工具相比&#xff0c;Xojo for Mac为开发人员提供了显着的生产率提高。 Xojo for Mac具有拖放功能&#xff0c;使您能够快速创建用户界面设计&#xff0c;然后…...

HIVE SQL实现分组字符串拼接concat

在Mysql中可以通过group_concat()函数实现分组字符串拼接&#xff0c;在HIVE SQL中可以使用concat_ws()collect_set()/collect_list()函数实现相同的效果。 实例&#xff1a; abc2014B92015A82014A102015B72014B6 1.concat_wscollect_list 非去重拼接 select a ,concat_ws(-…...

【问心篇】渴望、热情和选择

加班太严重完全没有时间学习&#xff0c;怎么办&#xff1f; 我真的不算聪明的人&#xff0c;但是&#xff0c;我对学习真的是有渴望的。说得好听一点&#xff0c;我希望自己在不停地成长&#xff0c;不辜负生活在这个信息化大变革的时代。说得不好的一点&#xff0c;就是我从…...

【贪心】CF1841 D

Codeforces 题意&#xff1a; 思路&#xff1a; 首先模拟一下样例 并没有发现什么 那么就去考虑特殊情况&#xff0c;看看有没有什么启发 考虑一个大区间包含所有小区间的情形&#xff0c;这种情况就是在这么多区间中找出两个区间 换句话说&#xff0c;这么多区间组成一个…...

移动端预览指定链接的pdf文件流

场景 直接展示外部系统返回的获取文件流时出现了跨域问题&#xff1a; 解决办法 1. 外部系统返回的请求头中调整&#xff08;但是其他系统不会给你改的&#xff09; 2. 我们系统后台获取文件流并转为新的文件流提供给前端 /** 获取传入url文件流 */ GetMapping("/get…...

【Go 基础篇】Go语言字符类型:解析字符的本质与应用

介绍 字符类型是计算机编程中用于表示文本和字符的数据类型&#xff0c;是构建字符串的基本单位。在Go语言&#xff08;Golang&#xff09;中&#xff0c;字符类型具有独特的特点和表示方式&#xff0c;包括Unicode编码、字符字面值以及字符操作。本篇博客将深入探讨Go语言中的…...

Java基础(十二)面向对象编程 OOP

一、抽象数据类型 1.面向对象基本概念 1. 面向对象 面向对象程序设计&#xff08;OOP&#xff09;是一种基于对象概念的软件开发方法&#xff0c;是目前软件开发的主流方式。 常见面向对象的语言&#xff1a;C 、Python 、Java 常见面向过程的语言&#xff1a;C 面向对象的三…...

在阿里云服务器上安装Microsoft SharePoint 2016流程

本教程阿里云百科分享如何在阿里云ECS上搭建Microsoft SharePoint 2016。Microsoft SharePoint是Microsoft SharePoint Portal Server的简称。SharePoint Portal Server是一个门户站点&#xff0c;使得企业能够开发出智能的门户站点。 目录 背景信息 步骤一&#xff1a;添加…...

Ubuntu设置定时重启

1.安装/更新 cron 安装crontab sudo apt-get install cron更新命令 sudo apt-get update2.配置cron定时任务 sudo nano /etc/crontab* * * * * root reboot(从左到右&#xff0c;五个 * 依次是 分&#xff0c;时 &#xff0c;天&#xff0c;月&#xff0c;星期)下列命令表示…...

sqlloader学习笔记

INFILE的用法 1&#xff09;模糊导入多个数据的文件。 可以在文件名中使用通配符。 星号 &#xff08;*&#xff09; 表示复数字符&#xff0c;问号 &#xff08;&#xff1f;&#xff09; 表示单个字符。 INFILE emp*.dat INFILE m?emp.dat 2&#xff09;如果不需要导入数据…...

内网ip与外网ip

一、关于IP地址 我们平时直接接触最多的是内网IP。而且还可以自己手动修改ip地址。而外网ip&#xff0c;我们很少直接接触&#xff0c;都是间接接触、因为外网ip一般都是运营商管理&#xff0c;而且是全球唯一的&#xff0c;一般我们自己是无法修改的。 内网IP和外网IP是指在…...

分布式 - 消息队列Kafka:Kafka消费者和消费者组

文章目录 1. Kafka 消费者是什么&#xff1f;2. Kafka 消费者组的概念&#xff1f;3. Kafka 消费者和消费者组有什么关系&#xff1f;4. Kafka 多个消费者如何同时消费一个分区&#xff1f; 1. Kafka 消费者是什么&#xff1f; 消费者负责订阅Kafka中的主题&#xff0c;并且从…...

feign调用和被调用者字段名称不对应解决

如果您在使用Feign时&#xff0c;尝试使用SerializedName("id")或JsonAlias("id")修饰字段&#xff0c;但仍然无法正常生效&#xff0c;可能是由于以下原因&#xff1a; Feign不会直接使用Gson库进行序列化和反序列化&#xff0c;而是使用了默认的Jackson库…...

【UE4 RTS】07-Camera Boundaries

前言 本篇实现的效果是当CameraPawn移动到地图边缘时会被阻挡。 效果 步骤 1. 打开项目设置&#xff0c;在“引擎-碰撞”中&#xff0c;点击“新建Object通道” 新建通道命名为“MapBoundaries”&#xff0c;然后点击接受 2. 向视口中添加 阻挡体积 调整阻挡体积的缩放 向四…...

大语言模型之二 GPT发展史简介

得益于数据、模型结构以及并行算力的发展&#xff0c;大语言模型应用现今呈井喷式发展态势&#xff0c;大语言神经网络模型成为了不可忽视的一项技术。 GPT在自然语言处理NLP任务上取得了突破性的进展&#xff0c;扩散模型已经拥有了成为下一代图像生成模型的代表的潜力&#x…...

前后端分离------后端创建笔记(09)密码加密网络安全

本文章转载于【SpringBootVue】全网最简单但实用的前后端分离项目实战笔记 - 前端_大菜007的博客-CSDN博客 仅用于学习和讨论&#xff0c;如有侵权请联系 源码&#xff1a;https://gitee.com/green_vegetables/x-admin-project.git 素材&#xff1a;https://pan.baidu.com/s/…...

RestClient

什么是RestClient RestClient 是 Elasticsearch 官方提供的 Java 低级 REST 客户端&#xff0c;它允许HTTP与Elasticsearch 集群通信&#xff0c;而无需处理 JSON 序列化/反序列化等底层细节。它是 Elasticsearch Java API 客户端的基础。 RestClient 主要特点 轻量级&#xff…...

基础测试工具使用经验

背景 vtune&#xff0c;perf, nsight system等基础测试工具&#xff0c;都是用过的&#xff0c;但是没有记录&#xff0c;都逐渐忘了。所以写这篇博客总结记录一下&#xff0c;只要以后发现新的用法&#xff0c;就记得来编辑补充一下 perf 比较基础的用法&#xff1a; 先改这…...

TRS收益互换:跨境资本流动的金融创新工具与系统化解决方案

一、TRS收益互换的本质与业务逻辑 &#xff08;一&#xff09;概念解析 TRS&#xff08;Total Return Swap&#xff09;收益互换是一种金融衍生工具&#xff0c;指交易双方约定在未来一定期限内&#xff0c;基于特定资产或指数的表现进行现金流交换的协议。其核心特征包括&am…...

CSS | transition 和 transform的用处和区别

省流总结&#xff1a; transform用于变换/变形&#xff0c;transition是动画控制器 transform 用来对元素进行变形&#xff0c;常见的操作如下&#xff0c;它是立即生效的样式变形属性。 旋转 rotate(角度deg)、平移 translateX(像素px)、缩放 scale(倍数)、倾斜 skewX(角度…...

数据结构第5章:树和二叉树完全指南(自整理详细图文笔记)

名人说&#xff1a;莫道桑榆晚&#xff0c;为霞尚满天。——刘禹锡&#xff08;刘梦得&#xff0c;诗豪&#xff09; 原创笔记&#xff1a;Code_流苏(CSDN)&#xff08;一个喜欢古诗词和编程的Coder&#x1f60a;&#xff09; 上一篇&#xff1a;《数据结构第4章 数组和广义表》…...

StarRocks 全面向量化执行引擎深度解析

StarRocks 全面向量化执行引擎深度解析 StarRocks 的向量化执行引擎是其高性能的核心设计&#xff0c;相比传统行式处理引擎&#xff08;如MySQL&#xff09;&#xff0c;性能可提升 5-10倍。以下是分层拆解&#xff1a; 1. 向量化 vs 传统行式处理 维度行式处理向量化处理数…...

用鸿蒙HarmonyOS5实现国际象棋小游戏的过程

下面是一个基于鸿蒙OS (HarmonyOS) 的国际象棋小游戏的完整实现代码&#xff0c;使用Java语言和鸿蒙的Ability框架。 1. 项目结构 /src/main/java/com/example/chess/├── MainAbilitySlice.java // 主界面逻辑├── ChessView.java // 游戏视图和逻辑├── …...

ffmpeg(三):处理原始数据命令

FFmpeg 可以直接处理原始音频和视频数据&#xff08;Raw PCM、YUV 等&#xff09;&#xff0c;常见场景包括&#xff1a; 将原始 YUV 图像编码为 H.264 视频将 PCM 音频编码为 AAC 或 MP3对原始音视频数据进行封装&#xff08;如封装为 MP4、TS&#xff09; 处理原始 YUV 视频…...

Shell 解释器​​ bash 和 dash 区别

bash 和 dash 都是 Unix/Linux 系统中的 ​​Shell 解释器​​&#xff0c;但它们在功能、语法和性能上有显著区别。以下是它们的详细对比&#xff1a; ​​1. 基本区别​​ ​​特性​​​​bash (Bourne-Again SHell)​​​​dash (Debian Almquist SHell)​​​​来源​​G…...

2025年全国I卷数学压轴题解答

第19题第3问: b b b 使得存在 t t t, 对于任意的 x x x, 5 cos ⁡ x − cos ⁡ ( 5 x t ) < b 5\cos x-\cos(5xt)<b 5cosx−cos(5xt)<b, 求 b b b 的最小值. 解: b b b 的最小值 b m i n min ⁡ t max ⁡ x g ( x , t ) b_{min}\min_{t} \max_{x} g(x,t) bmi…...