Stable Diffusion的安裝和問(wèn)題解決

小時(shí)候比較喜歡畫(huà)畫(huà),如今AI盛行,各種繪畫(huà)工具層出不窮,網(wǎng)上也看了很多,stable diffusion(sd)和midjunery(mj)算是主流,國(guó)內(nèi)的文心、天工、甚至抖音什么的,我也嘗試過(guò),但是總的來(lái)說(shuō),跟sd和mj相比真的不好用。后來(lái)我找了網(wǎng)上一些運(yùn)營(yíng)號(hào)問(wèn),大多采用的是mj,不過(guò)我個(gè)人還是想試試,親自對(duì)比一下,鑒于mj的付費(fèi),我先試了sd,下面是我安裝使用過(guò)程中的一些經(jīng)歷。

我的是Mac,所以就按照這個(gè)系統(tǒng)來(lái)了

一、安裝homebrew

$ /bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"
// 等待即可,檢查是否安裝成功
brew -V
// 打印出 homebrew 4.0.11 表示安裝成功

二、安裝python

brew install cmake protobuf rust python@3.10 git wget

也可以去官網(wǎng)下載安裝包3.10.9 或者3.10.10,手動(dòng)安裝

三、下載SD包

1、下載資料包,解壓安裝。鏈接: https://pan.baidu.com/s/1_a2dnt8ABG1txxmUSG9QOg?pwd=v1yz 提取碼: v1yz

2、從git倉(cāng)庫(kù)直接下載

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

四、下載模型

可以使用上面資料包里的模型,也可以自己從官網(wǎng)下載https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/v1-5-pruned-emaonly.ckpt
等模型。

這里需要注意的是,下載后放置目錄stable-diffusion-webui/models/Stable-diffusion/下。模型有ckpt文件,也有safetensors文件,統(tǒng)統(tǒng)放進(jìn)去就行。

五、運(yùn)行

在終端中進(jìn)入你安裝的“stable-diffusion-webui”目錄,運(yùn)行 ./webui.sh

第一次運(yùn)行會(huì)安裝一些依賴,這個(gè)過(guò)程會(huì)遇到諸多問(wèn)題,下面我列舉幾個(gè)。

六、運(yùn)行問(wèn)題解決

1、python版本問(wèn)題 ??

sd運(yùn)行python是3,但是默認(rèn)是2,一般來(lái)說(shuō)是不進(jìn)行覆蓋的,覆蓋它可能會(huì)導(dǎo)致系統(tǒng)腳本和其他依賴默認(rèn) Python 的軟件出現(xiàn)問(wèn)題。一般我們采用軟連的方式,如:sudo ln -s /usr/local/bin/python3 /usr/bin/python,但是你會(huì)發(fā)現(xiàn)報(bào)錯(cuò)了ln: /usr/bin/python: Operation not permitted,也查過(guò)其他各種刪除方式都不得行。

首先安裝高版本的python后,按照下面的步驟來(lái):

(1、查看版本

which python 
// /usr/bin/python
which python3
// /usr/local/bin/python3

(2、編輯profile文件

vi ~/.bash_profile
// 末尾追加內(nèi)容
alias python="/usr/local/bin/python3" 
alias python2="/usr/bin/python"

(3、生效啟用

source ~/.bash_profile

這里還有個(gè)問(wèn)題,如果新開(kāi)一個(gè)終端界面,需要重啟這個(gè)命令才能生效。

2、No SDP backend available ??

Launching Web UI with arguments: --skip-torch-cuda-test --upcast-sampling --no-half-vae --use-cpu interrogate
no module 'xformers'. Processing without...
No SDP backend available, likely because you are running in pytorch versions < 2.0. In fact, you are using PyTorch 1.12.1. You might want to consider upgrading.
no module 'xformers'. Processing without...
No module 'xformers'. Proceeding without it.
Style database not found: /Users/villen/stable-diffusion-webui/styles.csv
Warning: caught exception 'Torch not compiled with CUDA enabled', memory monitor disabled
==============================================================================
You are running torch 1.12.1.
The program is tested to work with torch 2.0.0.
To reinstall the desired version, run with commandline flag --reinstall-torch.
Beware that this will cause a lot of large files to be downloaded, as well as
there are reports of issues with training tab on the latest version.

Use --skip-version-check commandline argument to disable this check.
==============================================================================
Downloading: "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors" to /Users/villen/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors

WARNING:modules.mac_specific:MPS garbage collection failed
Traceback (most recent call last):
  File "/Users/villen/stable-diffusion-webui/modules/mac_specific.py", line 38, in torch_mps_gc
    from torch.mps import empty_cache
ModuleNotFoundError: No module named 'torch.mps'                

原因:torch 的版本不對(duì)
解決方案:在安裝目錄stable-diffusion-webui 下,編輯webui-macos-env.sh文件
我的原來(lái)的配置文件如下;

export install_dir="$HOME"
export COMMANDLINE_ARGS="--skip-torch-cuda-test --upcast-sampling --no-half-vae --use-cpu interrogate"
export TORCH_COMMAND="pip install torch==1.12.1 torchvision==0.13.1"
export K_DIFFUSION_REPO="https://github.com/brkirch/k-diffusion.git"
export K_DIFFUSION_COMMIT_HASH="51c9778f269cedb55a4d88c79c0246d35bdadb71"
export PYTORCH_ENABLE_MPS_FALLBACK=1

修改好的配置文件如下:

(1、在COMMANDLINE_ARGS 參數(shù)的后面添加了–reinstall-torch
(2、export TORCH_COMMAND=“pip install torch2.0.1 torchvision0.15.2”,將torch的版本修改成2.0的

export install_dir="$HOME"
export COMMANDLINE_ARGS="--skip-torch-cuda-test --upcast-sampling --no-half-vae --use-cpu interrogate --reinstall-torch"
export TORCH_COMMAND="pip install torch==2.0.1 torchvision==0.15.2"
#export TORCH_COMMAND="pip install torch==1.12.1 torchvision==0.13.1"
export K_DIFFUSION_REPO="https://github.com/brkirch/k-diffusion.git"
export K_DIFFUSION_COMMIT_HASH="51c9778f269cedb55a4d88c79c0246d35bdadb71"
export PYTORCH_ENABLE_MPS_FALLBACK=1

3、Symbol not found: _VTRegisterSupplementalVideoDecoderIfAvailable??

這個(gè)提示其實(shí)還是跟依賴版本有關(guān),opencv版本太老,可以這樣手工更新一下:

cd venv/bin 
source activate 
./python3.10 -m pip install --upgrade pip 
pip3 install opencv-python==4.6.0.66 deactivate

4、"LayerNormKernelImpl" not implemented for 'Half’??

或者也會(huì)有這樣的提示Failed to create model quickly; will retry using slow method.其實(shí)都是顯卡的問(wèn)題,一般我們工作自用的電腦的顯存都不太能夠的,所以這里需要修改一下運(yùn)行方式:

編輯webui-user.sh,添加:

export COMMANDLINE_ARGS="--precision full --no-half --skip-torch-cuda-test

或者我個(gè)人采用以下運(yùn)行方式:

./webui.sh --precision full --no-half

大家注意這里的half,如果你有足夠的計(jì)算資源和內(nèi)存,最簡(jiǎn)單的解決方案是將數(shù)據(jù)類型更改為全精度(float32),我們這里強(qiáng)調(diào)一半,其實(shí)就是半精度替代實(shí)現(xiàn)方案。有興趣可以去了解一下精度問(wèn)題。

七、總結(jié)

本地跑起來(lái)了,確實(shí)比較慢,風(fēng)扇都要轉(zhuǎn)費(fèi)了。SD跑圖除了需要通常所說(shuō)的描述要到位,還要選合適的模型或者插件,才能做出理想的圖片。

以上是本地SD安裝過(guò)程中遇到的問(wèn)題,畢竟免費(fèi),隨后會(huì)跟大家聊一下它和midjunery的區(qū)別以及我個(gè)人的取舍。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容