当前位置: 首页 > 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/…...

观成科技:隐蔽隧道工具Ligolo-ng加密流量分析

1.工具介绍 Ligolo-ng是一款由go编写的高效隧道工具&#xff0c;该工具基于TUN接口实现其功能&#xff0c;利用反向TCP/TLS连接建立一条隐蔽的通信信道&#xff0c;支持使用Let’s Encrypt自动生成证书。Ligolo-ng的通信隐蔽性体现在其支持多种连接方式&#xff0c;适应复杂网…...

2025年能源电力系统与流体力学国际会议 (EPSFD 2025)

2025年能源电力系统与流体力学国际会议&#xff08;EPSFD 2025&#xff09;将于本年度在美丽的杭州盛大召开。作为全球能源、电力系统以及流体力学领域的顶级盛会&#xff0c;EPSFD 2025旨在为来自世界各地的科学家、工程师和研究人员提供一个展示最新研究成果、分享实践经验及…...

React Native在HarmonyOS 5.0阅读类应用开发中的实践

一、技术选型背景 随着HarmonyOS 5.0对Web兼容层的增强&#xff0c;React Native作为跨平台框架可通过重新编译ArkTS组件实现85%以上的代码复用率。阅读类应用具有UI复杂度低、数据流清晰的特点。 二、核心实现方案 1. 环境配置 &#xff08;1&#xff09;使用React Native…...

【算法训练营Day07】字符串part1

文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接&#xff1a;344. 反转字符串 双指针法&#xff0c;两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...

Redis数据倾斜问题解决

Redis 数据倾斜问题解析与解决方案 什么是 Redis 数据倾斜 Redis 数据倾斜指的是在 Redis 集群中&#xff0c;部分节点存储的数据量或访问量远高于其他节点&#xff0c;导致这些节点负载过高&#xff0c;影响整体性能。 数据倾斜的主要表现 部分节点内存使用率远高于其他节…...

Pinocchio 库详解及其在足式机器人上的应用

Pinocchio 库详解及其在足式机器人上的应用 Pinocchio (Pinocchio is not only a nose) 是一个开源的 C 库&#xff0c;专门用于快速计算机器人模型的正向运动学、逆向运动学、雅可比矩阵、动力学和动力学导数。它主要关注效率和准确性&#xff0c;并提供了一个通用的框架&…...

20个超级好用的 CSS 动画库

分享 20 个最佳 CSS 动画库。 它们中的大多数将生成纯 CSS 代码&#xff0c;而不需要任何外部库。 1.Animate.css 一个开箱即用型的跨浏览器动画库&#xff0c;可供你在项目中使用。 2.Magic Animations CSS3 一组简单的动画&#xff0c;可以包含在你的网页或应用项目中。 3.An…...

SpringAI实战:ChatModel智能对话全解

一、引言&#xff1a;Spring AI 与 Chat Model 的核心价值 &#x1f680; 在 Java 生态中集成大模型能力&#xff0c;Spring AI 提供了高效的解决方案 &#x1f916;。其中 Chat Model 作为核心交互组件&#xff0c;通过标准化接口简化了与大语言模型&#xff08;LLM&#xff0…...

嵌入式面试常问问题

以下内容面向嵌入式/系统方向的初学者与面试备考者,全面梳理了以下几大板块,并在每个板块末尾列出常见的面试问答思路,帮助你既能夯实基础,又能应对面试挑战。 一、TCP/IP 协议 1.1 TCP/IP 五层模型概述 链路层(Link Layer) 包括网卡驱动、以太网、Wi‑Fi、PPP 等。负责…...

【Redis】Redis从入门到实战:全面指南

Redis从入门到实战:全面指南 一、Redis简介 Redis(Remote Dictionary Server)是一个开源的、基于内存的键值存储系统,它可以用作数据库、缓存和消息代理。由Salvatore Sanfilippo于2009年开发,因其高性能、丰富的数据结构和广泛的语言支持而广受欢迎。 Redis核心特点:…...