Skip to content

在 NVidia GPU 上安装并运行

自动安装

Windows(方法1)

在Windows 10/11 NVIDIA GPU上启动并运行Stable Diffusion Web UI的非常基本的指南。

  1. 从这里下载sd.webui.zip,此软件包来自v1.0.0-pre,我们将在步骤3中将其更新到最新的webui版本。
  2. 在您想要的位置提取zip文件。
  3. 双击update.bat将Web UI更新到最新版本,等待完成,然后关闭窗口。
  4. Double click the run.bat to launch web UI, during the first launch it will download large amounts of files. After everything has been downloaded and installed correctly, you should see a message "Running on local URL: http://127.0.0.1:7860", opening the link will present you with the web UI interface.

你应该能够开始生成图像

额外的配置通过COMMANDLINE_ARGS

您可能希望将一些配置选项应用于Web UI,为了配置这些选项,您需要编辑在sd.webui\webui\webui-user.bat中找到的启动脚本,编辑文件,在set COMMANDLINE_ARGS=后添加选定的参数,例如:

set COMMANDLINE_ARGS=--autolaunch --update-check

每个单独的参数都需要用空格分隔,上述示例很好地配置了Web UI,以便在浏览器页面完成加载后自动启动,并在启动时检查新版本的Web UI。

故障排除

Web UI的默认配置应该在大多数现代GPU上运行,但在某些情况下,您可能需要一些额外的参数才能使其正常工作。

  1. 对于VRAM较少的GPU,您可能需要--medvram--lowvram,这些优化降低了VRAM要求,但牺牲了性能。如果您没有足够的VRAM,Web UI可能会因内存不足错误而拒绝启动或无法生成图像。所需的VRAM量在很大程度上取决于所需的图像分辨率,有关更多详细信息,请参阅故障排除

平铺VAE扩展可以帮助减少VRAM要求。

  1. 如果您生成的结果是黑色或绿色图像,请尝试添加--precision full--no-half

  2. 模型和VAE的一些组合容易产生NansException: A tensor with all NaNs was produced in VAE导致图像为黑色,使用选项--no-half-vae可能有助于缓解这个问题。

额外选项

  1. 有几种交叉衰减优化方法,如--xformers--opt-sdp-attention,这些方法可以大幅提高性能,请参阅优化以了解更多详细信息,尝试不同的选项,因为不同的硬件适合不同的优化。如果您想衡量系统的性能,请尝试使用sd-extension-system-info扩展,该扩展具有基准测试工具和用户提交结果的数据库
  2. 添加--autolaunch,让Web UI在Web UI启动后自动启动Web浏览器。
  3. 当有新版本的webui时,添加--update-check会通知您。
  4. 有关更多配置选项,请参阅命令行参数和设置

提示

如果您已经下载了稳定的扩散模型,您可以在第3步中运行run.bat之前将模型移动到sd.webui\webui\models\Stable-diffusion\,这将跳过自动下载香草稳定-diffusion-v1-5模型

Windows(方法2)

  1. 安装Python 3.10.6(打卡添加到PATH)和git
  2. 从搜索栏打开命令提示符,然后键入git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
  3. 双击webui-user.bat

安装视频,以防您卡住:
解决#8229

视频:(点击展开:) webui_h264.mp4 替代Powershell启动脚本:

webui.ps1

if ($env:PYTHON -eq "" -or $env:PYTHON -eq $null) {
    $PYTHON = "Python.exe"
} else {
    $PYTHON = $env:PYTHON
}

if ($env:VENV_DIR -eq "" -or $env:VENV_DIR -eq $null) {
    $VENV_DIR = "$PSScriptRoot\venv"
} else {
    $VENV_DIR = $env:VENV_DIR
}

if ($env:LAUNCH_SCRIPT -eq "" -or $env:LAUNCH_SCRIPT -eq $null) {
    $LAUNCH_SCRIPT = "$PSScriptRoot\launch.py"
} else {
    $LAUNCH_SCRIPT = $env:LAUNCH_SCRIPT
}

$ERROR_REPORTING = $false

mkdir tmp 2>$null

function Start-Venv {
    if ($VENV_DIR -eq '-') {
        Skip-Venv
    }

    if (Test-Path -Path "$VENV_DIR\Scripts\$python") {
        Activate-Venv
    } else {
        $PYTHON_FULLNAME = & $PYTHON -c "import sys; print(sys.executable)"
        Write-Output "Creating venv in directory $VENV_DIR using python $PYTHON_FULLNAME"
        Invoke-Expression "$PYTHON_FULLNAME -m venv $VENV_DIR > tmp/stdout.txt 2> tmp/stderr.txt"
        if ($LASTEXITCODE -eq 0) {
            Activate-Venv
        } else {
            Write-Output "Unable to create venv in directory $VENV_DIR"
        }
    }
}

function Activate-Venv {
    $PYTHON = "$VENV_DIR\Scripts\Python.exe"
    $ACTIVATE = "$VENV_DIR\Scripts\activate.bat"
    Invoke-Expression "cmd.exe /c $ACTIVATE"
    Write-Output "Venv set to $VENV_DIR."
    if ($ACCELERATE -eq 'True') {
        Check-Accelerate
    } else {
        Launch-App
    }
}

function Skip-Venv {
    Write-Output "Venv set to $VENV_DIR."
    if ($ACCELERATE -eq 'True') {
        Check-Accelerate
    } else {
        Launch-App
    }
}

function Check-Accelerate {
    Write-Output 'Checking for accelerate'
    $ACCELERATE = "$VENV_DIR\Scripts\accelerate.exe"
    if (Test-Path -Path $ACCELERATE) {
        Accelerate-Launch
    } else {
        Launch-App
    }
}

function Launch-App {
    Write-Output "Launching with python"
    Invoke-Expression "$PYTHON $LAUNCH_SCRIPT"
    #pause
    exit
}

function Accelerate-Launch {
    Write-Output 'Accelerating'
    Invoke-Expression "$ACCELERATE launch --num_cpu_threads_per_process=6 $LAUNCH_SCRIPT"
    #pause
    exit
}


try {
    if(Get-Command $PYTHON){
        Start-Venv
    }
} Catch {
    Write-Output "Couldn't launch python."
}

webui-user.ps1

[Environment]::SetEnvironmentVariable("PYTHON", "")
[Environment]::SetEnvironmentVariable("GIT", "")
[Environment]::SetEnvironmentVariable("VENV_DIR","")

## Commandline arguments for webui.py, for example: [Environment]::SetEnvironmentVariable("COMMANDLINE_ARGS", "--medvram --opt-split-attention")
[Environment]::SetEnvironmentVariable("COMMANDLINE_ARGS", "")

## script to launch to start the app
## [Environment]::SetEnvironmentVariable("LAUNCH_SCRIPT", "launch.py")

## install command for torch
## [Environment]::SetEnvironmentVariable("TORCH_COMMAND", "pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113")

## Requirements file to use for stable-diffusion-webui
## [Environment]::SetEnvironmentVariable("REQS_FILE", "requirements_versions.txt")

## [Environment]::SetEnvironmentVariable("GFPGAN_PACKAGE", "")
## [Environment]::SetEnvironmentVariable("CLIP_PACKAGE", "")
## [Environment]::SetEnvironmentVariable("OPENCLIP_PACKAGE", "")

## URL to a WHL if you wish to override default xformers windows
## [Environment]::SetEnvironmentVariable("XFORMERS_WINDOWS_PACKAGE", "")

## Uncomment and set to enable an alternate repository URL
## [Environment]::SetEnvironmentVariable("STABLE_DIFFUSION_REPO", "")
## [Environment]::SetEnvironmentVariable("TAMING_TRANSFORMERS_REPO", "")
## [Environment]::SetEnvironmentVariable("K_DIFFUSION_REPO", "")
## [Environment]::SetEnvironmentVariable("CODEFORMER_REPO", "")
## [Environment]::SetEnvironmentVariable("BLIP_REPO", "")

## Uncomment and set to enable a specific revision of a repository
## [Environment]::SetEnvironmentVariable("STABLE_DIFFUSION_COMMIT_HASH", "")
## [Environment]::SetEnvironmentVariable("TAMING_TRANSFORMERS_COMMIT_HASH", "")
## [Environment]::SetEnvironmentVariable("K_DIFFUSION_COMMIT_HASH", "")
## [Environment]::SetEnvironmentVariable("CODEFORMER_COMMIT_HASH", "")
## [Environment]::SetEnvironmentVariable("BLIP_COMMIT_HASH", "")


## Uncomment to enable accelerated launch
## [Environment]::SetEnvironmentVariable("ACCELERATE", "True")

$SCRIPT = "$PSScriptRoot\webui.ps1"
Invoke-Expression "$SCRIPT"

请参阅故障排除部分,了解如果出现问题该怎么办。

Linux

(基于Debian)

  1. 输入这些命令,这些命令会将webui安装到您当前的目录:
sudo apt install git python3.10-venv -y
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui && cd stable-diffusion-webui
python3.10 -m venv venv
  1. 安装并运行:

    ./webui.sh {your_arguments}

其他分布:用于安装Python 3.10

第三方安装指南/脚本:

在没有虚拟环境的情况下安装和运行

要在不创建虚拟环境的情况下通过pip安装所需的软件包,请运行:

python launch.py

命令行参数可以直接传递,例如:

python launch.py --opt-split-attention --ckpt ../secret/anime9999.ckpt

手动安装

手动安装非常过时,可能不起作用。在repo的readme中查看colab以获取说明。

以下过程在Windows或Linux上手动安装所有内容(后者要求替换dir):

## install torch with CUDA support. See https://pytorch.org/get-started/locally/ for more instructions if this fails.
pip install torch --extra-index-url https://download.pytorch.org/whl/cu113

## check if torch supports GPU; this must output "True". You need CUDA 11. installed for this. You might be able to use
## a different version, but this is what I tested.
python -c "import torch; print(torch.cuda.is_available())"

## clone web ui and go into its directory
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui

## clone repositories for Stable Diffusion and (optionally) CodeFormer
mkdir repositories
git clone https://github.com/CompVis/stable-diffusion.git repositories/stable-diffusion-stability-ai
git clone https://github.com/CompVis/taming-transformers.git repositories/taming-transformers
git clone https://github.com/sczhou/CodeFormer.git repositories/CodeFormer
git clone https://github.com/salesforce/BLIP.git repositories/BLIP

## install requirements of Stable Diffusion
pip install transformers==4.19.2 diffusers invisible-watermark --prefer-binary

## install k-diffusion
pip install git+https://github.com/crowsonkb/k-diffusion.git --prefer-binary

## (optional) install GFPGAN (face restoration)
pip install git+https://github.com/TencentARC/GFPGAN.git --prefer-binary

## (optional) install requirements for CodeFormer (face restoration)
pip install -r repositories/CodeFormer/requirements.txt --prefer-binary

## install requirements of web ui
pip install -r requirements.txt  --prefer-binary

## update numpy to latest version
pip install -U numpy  --prefer-binary

## (outside of command line) put stable diffusion model into web ui directory
## the command below must output something like: 1 File(s) 4,265,380,512 bytes
dir model.ckpt

安装完成,要启动web ui,请运行:

python webui.py

Windows 11 WSL2指令

要在Windows 11的WSL2中的Linux发行版下安装:

## install conda (if not already done)
wget https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh
chmod +x Anaconda3-2022.05-Linux-x86_64.sh
./Anaconda3-2022.05-Linux-x86_64.sh

## Clone webui repo
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui

## Create and activate conda env
conda env create -f environment-wsl2.yaml
conda activate automatic

此时,可以从步骤## clone repositories for Stable Diffusion and (optionally) CodeFormer开始应用手动安装说明。

使用Conda在Windows上进行替代安装

  • 先决条件(仅当您没有这些先决条件时才需要)。假设安装了巧克力

    ```shell

    install git

    choco install git

    install conda

    choco install anaconda3 ```

    可选参数:gitconda * 安装(警告:一些文件超过几千兆字节,首先确保您有空间) 1. 下载为.zip并提取或使用git进行克隆。

    ```shell
    git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
    ```
    
    1. 启动Anaconda提示。应该注意的是,您可以使用较旧的Python版本,但您可能会被强迫手动删除缓存优化等功能,这将降低您的性能。

      ```shell

      cd "GIT\StableDiffusion"

      Create environment

      conda create -n StableDiffusion python=3.10.6

      Activate environment

      conda activate StableDiffusion

      Validate environment is selected

      conda env list

      Start local webserver

      webui-user.bat

      Wait for "Running on local URL: http://127.0.0.1:7860" and open that URI.

      ```

    2. (可选)转到CompVis并下载最新型号,例如1.4,并将其解压为:

      shell GIT\StableDiffusion\models\Stable-diffusion

      之后,通过重新启动Anaconda提示来重新启动服务器,然后

      shell webui-user.bat

  • 值得尝试的替代默认值:

    1. 尝试具有更高采样步骤的euler a(Ancestral Euler),例如:40或其他具有100。
    2. 将“设置>用户界面>每N个采样步骤显示图像创建进度”设置为1,并选择一个确定性的种子值。可以直观地看到图像扩散是如何发生的,并使用ScreenToGif记录.gif。
    3. 使用恢复面孔。一般来说,效果更好,但质量要以速度为代价。

我们一直在努力

apachecn/AiLearning

【布客】中文翻译组