最近生成式 AI 非常火爆,特别是 Stable Diffusion、Midjourney 等工具涌现,让互联网圈子话题不断。一个疲倦的周末,我决心在自己的电脑上尝尝鲜,因此有了这个文章。网上很多文章都说 Stable Diffusion 需要 Windows 10 + Python 3.10 环境才能使用,测试后才发现 Windows 7 + Python 3.8 也不是不可能,咱们唯一需要的是升级显存和内存!

操作系统配置

  1. 升级显卡驱动:Stable Diffusion WebUI 要求 CUDA 版本为 11.4,通过查询 https://www.nvidia.cn/Download/Find.aspx?lang=cn 找到本机对应的硬件驱动,安装升级。

    驱动下载页

  1. 安装 Python 3.8.10:通过官网下载 setup 包进行安装。

    • 建议人工下载安装 torch-1.13.1+cu117-cp38-cp38-win_amd64.whl 包,阿里云地址为 https://mirrors.aliyun.com/pytorch-wheels/cu117/ ,下载完成后人工安装到 Python 环境中。

        D:\stable-diffusion-webui\venv\Scripts\python.exe -m pip install   torch-1.13.0+cu117-cp38-cp38-win_amd64.whl
    • 安装 xformers 插件,解决 Installation of xformers is not supported in this version of Python. 错误。

        D:\stable-diffusion-webui\venv\Scripts\python.exe -m pip install   xformers==0.0.16
      
        # 检查功能是否正常
        D:\stable-diffusion-webui\venv\Scripts\python.exe  -m xformers.info

      上述插件版本适用于 stable-diffusion-webui v1.0及以下版本。1.1.0 版本需要 torch 2.0 、CUDA 11.8、xformers 0.0.18,插件地址见 https://mirrors.aliyun.com/pytorch-wheels/cu118/

Stable Diffusion 环境配置

  1. 下载 stable-diffusion-webui 文件,如下:

     cd D:\
     git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui

    这是假设文件的根路径为 D:\ ,后续调用时不再赘述。

  2. D:\stable-diffusion-webui\webui-user.bat 修改以下变量。

     set PYTHON="C:\Program Files\Python38\python.exe"
     set COMMANDLINE_ARGS=--xformers --lowvram
  3. "D:\stable-diffusion-webui\launch.py" 进行修改。

    • 删除 Python 版本检测:将 prepare_environment 函数中 if (not is_installed("xformers") or args.reinstall_xformers) and args.xformers: 下的 else 子句进行注释。

    • 更改 GitHub 路径:将 prepare_environment 函数中的每个 https://github.com/ 替换为 https://ghproxy.com/https://github.com/

    • 更改 torch==1.13.1+cu117 下载路径:将 torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117") 修改为 torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.13.1 torchvision==0.14.1")

      如果安装过程中出现异常,重命名 D:\stable-diffusion-webui\venv 文件夹,即可重新安装相关环境。

  4. 修改 webui-user.bat 文件以下参数:

     set PYTHON="C:\Program Files\Python38\python.exe"
     set PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:32
     set COMMANDLINE_ARGS=--lowvram --xformers --skip-python-version-check --skip-torch-cuda-test --port 6006 --opt-split-attention --always-batch-cond-uncond

    注意,上边修改了端口为 6006,后续网页访问时注意更改。

  5. 运行 webui-user.bat(首次将初始化安装环境,慢慢等待若干小时即可)。

    stable diffusion webui 初始化示意图

    若出现 RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory错误,这是 pytorch 1.6 起 torch.save 增加了参数所致,而 torch.load 使用的是旧格式,修改函数即可。即 D:\stable-diffusion-webui\venv\lib\site-packages\torch\serialization.py
    _use_new_zipfile_serialization: bool = True 修改为 _use_new_zipfile_serialization: bool = False ,并重启 webui-user.bat 。

Stable Diffusion 模型导入

  1. 下载 基础模型 ,并拷贝到 D:\stable-diffusion-webui\models\Stable-diffusion 下。

  2. 添加 lora 模型

    • 下载基础模型 ChilloutMix ,移动至 *\stable-diffusion-webui\models\Stable-diffusion 目录。

    • 下载需要的角色模型,如 taiwanDollLikeness_v10,并移动至 *\stable-diffusion-webui\models\Lora 目录。

      重启Stable Diffusion WebUI主界面,然后在左上角可以加载使用基础模型 ChilloutMix ;如果没有必要使用东方人脸模型,直接使用 v1-5-pruned 基础模型也是 OK 的。

  3. (可选)下载 codeformerD:\stable-diffusion-webui\models\Codeformer\codeformer-v0.1.0.pth

Stable Diffusion 生成图片

  1. 运行 webui-user.bat 启动 Stable Diffusion 系统。

  2. 访问系统 http://127.0.0.1:6006

    • 在提示词处输入相关语句,如 masterpiece, best quality,Beautiful blonde with red eyes ,dress ,walking ,on the beach,night,设置步骤数为 25~30,点击开始即可。

      演示效果1

    • 采用 Protogen 8.6(Infinity) 模型,提示词输入 Chiri-jiang Mountain View, Blue Sky, White Cloud, Oil Painting Style, skillful,serene pastoral scenes, light green and orange,效果如下:

      演示效果2

      演示效果3

参考文献