前言: 这篇文章适合有过搭建AI模型基础的人(安装过cuda、pytorch等)。
1.环境配置
首先我们需要安装一个Anaconda或者miniconda,这里我用的是miniconda。
创建虚拟环境
打开miniconda的cmd工具,创建一个新的环境。python环境一定要是3.10.6的,官方推荐这个版本我们就用这个版本。
conda create -n stablediffusion python=3.10.6
出现提示问你要不要安装,输入y就好了。
--切换环境
activate stablediffusion
2.下载代码
这里我们下载这个开源共享,因为带webui十分好用。
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
3.下载权重文件
直接点击下面的链接即可下载,下载后放到项目目录下的models/Stable-diffusion文件夹中。
https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/resolve/main/sd-v1-4.ckpt
4.下载模型
可以去C站(需要科学上网)或者Hugging Face、liblibai(国内的网站)找自己喜欢的模型下载下来然后放到项目目录models/Stable-diffusion这个文件夹加下,网站在下面。
Hugging Face – The AI community building the future.
LiblibAI·哩布哩布AI-中国领先原创AI模型分享社区
5. 启动
到stablediffusion项目路径下面, 编辑一下 webui-user.bat文件,将conda虚拟环境中的python.exe文件放到 set PYTHON= 这一行,如果有的话就不用改了。
set PYTHON=D:\miniconda\envs\stablediffusion\python.exe
然后在stablediffusion项目路径下命令行启动 webui-user.bat,这里会自动下载很多库耐心等待即可。
webui-user.bat
启动成功后,输入 http://127.0.0.1:7860,就能看到web页面啦。
6.可能存在的问题
我在运行的过程中出现了一个问题,当我输入我的Prompt点击生成的时候,报了这样的错。
modules.devices.NansException: A tensor with all NaNs was produced in Unet. This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the "Upcast cross attention layer to float32" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this. Use --disable-nan-check commandline argument to disable this check.
谷歌了一下发现改一处代码就好了,贴在下面。
sd_model.py文件下。
#修改前:
sd_model = None
try:
with sd_disable_initialization.DisableInitialization(disable_clip=clip_is_included_into_sd):
sd_model = instantiate_from_config(sd_config.model)
except Exception:
pass
if 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 = instantiate_from_config(sd_config.model)
修改后再重新运行webui-user.bat,就可以愉快的生成图片了。