一、问题描述:
在没有WebUI的情况下,StableDiffusion如何正确加载单个模型文件。
二、部分python代码
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_single_file(
'path/to/model.safetensors',
config='path/to/stable_diffusion_v1_5',
torch_dtype=torch.float16,
local_files_only=True,
use_safetensors=True,
)
三、来自运行时的提示
Detected legacy `from_single_file` loading behavior. Attempting to create the pipeline based on inferred components.
This may lead to errors if the model components are not correctly inferred.
To avoid this warning, please explicity pass the `config` argument to `from_single_file` with a path to a local diffusers model repo
e.g. `from_single_file(<my model checkpoint path>, config=<path to local diffusers model repo>)
or run `from_single_file` with `local_files_only=False` first to update the local cache directory with the necessary config files.
四、一些不必要的坑列一下
1.文件夹结构与单个模型文件
官方模型里有文件夹以及单个的模型文件,非官方模型一般是单个文件。
*无需转换单个模型文件为diffusers的文件夹模式(含有index_model.json文件结构)。
2.from_pretrained()
若使用官方的模型,比如Stable-Diffusion-v1-5,
可以用StableDiffusionPipeline.from_pretrained()加载文件夹地址。
*若把单个模型放进官方模型的文件夹里,调用from_pretrained加载,最终效果还是官方的模型。
3.from_single_file()
我们常常使用非官方的模型,比如单个safetensors模型文件,
可以用StableDiffusionPipeline.from_single_file()加载文件及后缀。
*要设置config参数为原有官方模型的路径,这个路径里含有所需的组件。
五、解决这个问题的初衷
本机显卡是Intel Arc A770 16GB,在用了WebUI启动器的方式体验过StableDiffusion后,想要试试WSL的方式搭建。手里有官方模型,如果换成非官方的模型该怎么做?这篇文章就记录一下关键的单个模型文件的加载过程。