世間無(wú)限丹青手,遇上AI畫(huà)不成。最近一段時(shí)間,可能所有人類畫(huà)師都得發(fā)出一句“既生瑜,何生亮”的感嘆,因?yàn)锳I 繪畫(huà)通用算法Stable Diffusion已然超神,無(wú)需美術(shù)基礎(chǔ),也不用經(jīng)年累月的刻苦練習(xí),只需要一臺(tái)電腦,人人都可以是丹青圣手。
本次我們?nèi)脚_(tái)構(gòu)建基于Stable-Diffusion算法的Webui可視化圖形界面服務(wù),基于本地模型來(lái)進(jìn)行AI繪畫(huà)操作。
本地安裝Stable-Diffusion-Webui
如果系統(tǒng)之前安裝過(guò)Python3.10或者使用過(guò)Pytorch深度學(xué)習(xí)框架,那么推薦直接本地安裝Stable-Diffusion-Webui,因?yàn)镾table-Diffusion的核心依賴庫(kù)也是Pytorch。
首先拉取官方的項(xiàng)目:
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
隨后進(jìn)入項(xiàng)目的目錄:
cd stable-diffusion-webui
官方文檔建議直接在目錄中運(yùn)行shell腳本:
./webui.sh
但事實(shí)上,shell腳本很容易在過(guò)程中報(bào)錯(cuò),該項(xiàng)目的核心代碼其實(shí)是launch.py,所以理論上,我們只需要正常運(yùn)行l(wèi)aunch.py文件即可。
首先確保本機(jī)的Python版本號(hào)大于等于3.10.9
關(guān)于Python3.10的安裝,請(qǐng)移玉步至:一網(wǎng)成擒全端涵蓋,在不同架構(gòu)(Intel x86/Apple m1 silicon)不同開(kāi)發(fā)平臺(tái)(Win10/Win11/Mac/Ubuntu)上安裝配置Python3.10開(kāi)發(fā)環(huán)境 ,這里不再贅述。
另外確保Pytorch的版本號(hào)大于等于13.1.0,關(guān)于Pytorch,請(qǐng)移步:聞其聲而知雅意,M1 Mac基于PyTorch(mps/cpu/cuda)的人工智能AI本地語(yǔ)音識(shí)別庫(kù)Whisper(Python3.10)
隨后安裝相關(guān)的依賴庫(kù):
pip3 install -r requirements.txt
pip3 install -r requirements_versions.txt
依賴文件中,有一個(gè)庫(kù)可能會(huì)出問(wèn)題,就是GFPGAN,它是騰訊開(kāi)源的人臉識(shí)別模塊,這里推薦使用GFPGAN官方網(wǎng)站(https://github.com/TencentARC/GFPGAN)的安裝方式:
# Install basicsr - https://github.com/xinntao/BasicSR
# We use BasicSR for both training and inference
pip install basicsr
# Install facexlib - https://github.com/xinntao/facexlib
# We use face detection and face restoration helper in the facexlib package
pip install facexlib
pip install -r requirements.txt
python setup.py develop
# If you want to enhance the background (non-face) regions with Real-ESRGAN,
# you also need to install the realesrgan package
pip install realesrgan
安裝成功后,最好驗(yàn)證一下:
? ~ python3
Python 3.10.9 (main, Dec 15 2022, 17:11:09) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gfpgan
>>>
所有依賴安裝成功后,就可以直接運(yùn)行l(wèi)aunch.py文件即可:
python3 launch.py
程序返回:
Python 3.10.9 (main, Dec 15 2022, 17:11:09) [Clang 14.0.0 (clang-1400.0.29.202)]
Commit hash: 0cc0ee1bcb4c24a8c9715f66cede06601bfc00c8
Installing requirements for Web UI
Launching Web UI with arguments: --upcast-sampling --use-cpu interrogate
Warning: caught exception 'Torch not compiled with CUDA enabled', memory monitor disabled
No module 'xformers'. Proceeding without it.
==============================================================================
You are running torch 1.13.0.
The program is tested to work with torch 1.13.1.
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.
==============================================================================
Loading weights [6ce0161689] from /Users/liuyue/wodfan/work/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors
Creating model from config: /Users/liuyue/wodfan/work/stable-diffusion-webui/configs/v1-inference.yaml
LatentDiffusion: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.
Applying cross attention optimization (InvokeAI).
Textual inversion embeddings loaded(0):
Model loaded in 8.2s (create model: 0.6s, apply weights to model: 5.0s, apply half(): 1.9s, move model to device: 0.5s).
Running on local URL: http://127.0.0.1:7860
Stable-Diffusion-Webui服務(wù)會(huì)運(yùn)行在系統(tǒng)的7860端口上。
需要注意的是,如果本地系統(tǒng)不支持cuda模式,需要修改運(yùn)行命令:
python3 launch.py --skip-torch-cuda-test --upcast-sampling --use-cpu interrogate
這里使用CPU來(lái)進(jìn)行模型訓(xùn)練。
另外如果是M系列的Mac,其實(shí)是支持MPS模式的,但Stable Diffusion目前的最新版并不支持MPS,所以需要單獨(dú)設(shè)置環(huán)境變量,關(guān)閉MPS模式:
export PYTORCH_ENABLE_MPS_FALLBACK=1
最后訪問(wèn)http://127.0.0.1:7860即可,本地構(gòu)建Stable-Diffusion-Webui服務(wù)就完成了。
Docker構(gòu)建Stable-Diffusion-Webui
如果不想太折騰,也可以使用Docker容器來(lái)構(gòu)建Stable-Diffusion-Webui,同樣地,需要拉取線上的Docker配置文件項(xiàng)目:
git clone https://github.com/AbdBarho/stable-diffusion-webui-docker.git
隨后進(jìn)入項(xiàng)目的目錄:
stable-diffusion-webui-docker
接著運(yùn)行命令下載相關(guān)的依賴鏡像:
docker compose --profile download up --build
下載完成后,運(yùn)行命令構(gòu)建容器:
docker compose --profile auto up --build
這里需要注意的是,模型數(shù)據(jù)和輸出文件夾會(huì)以/data和/output的形式掛載到容器中,如果想在宿主機(jī)往容器內(nèi)傳入模型或者其他圖片,需要寫(xiě)入項(xiàng)目中的data目錄。
過(guò)程中,可能會(huì)報(bào)錯(cuò):
Found no NVIDIA driver on your system
這是因?yàn)槿萜鲀?nèi)找不到NVIDIA的顯卡驅(qū)動(dòng)。
這里需要單獨(dú)再啟動(dòng)一個(gè)容器服務(wù):
docker run -ti --runtime=nvidia -e NVIDIA_DRIVER_CAPABILITIES=compute,utility -e NVIDIA_VISIBLE_DEVICES=all allennlp/allennlp
總的來(lái)說(shuō),安裝過(guò)程簡(jiǎn)單,但是調(diào)試比較費(fèi)勁,一旦啟動(dòng)出問(wèn)題,就得進(jìn)入容器內(nèi)部修改代碼,或者反復(fù)修改Dockerfile文件,所以Docker比較適合封裝業(yè)務(wù)改動(dòng)頻繁的容器,而不是依賴環(huán)境繁多并且版本需要反復(fù)調(diào)整的場(chǎng)景。
Stable-Diffusion-Webui圖像繪制
配置好Stable-Diffusion-Webui環(huán)境之后,訪問(wèn)http://127.0.0.1:7860:
[圖片上傳失敗...(image-e08742-1677802804064)]
在Prompt文本框中填入引導(dǎo)詞:
Tall buildings, people bustling, heavy traffic, staggered light and shadow, the beauty of the city is conspicuous before.
隨后點(diǎn)擊右側(cè)Generate生成按鈕即可,這里引導(dǎo)詞的意思是:高樓林立,人群熙熙攘攘,車(chē)水馬龍,光影交錯(cuò),城市之美盡顯眼前。
注意引導(dǎo)詞需要使用逗號(hào)分隔。
后端開(kāi)始進(jìn)行訓(xùn)練:
To create a public link, set `share=True` in `launch()`.
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:24<00:00, 1.25s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████| 20/20 [00:19<00:00, 1.00it/s]
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:34<00:00, 1.72s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████| 20/20 [00:22<00:00, 1.11s/it]
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:22<00:00, 1.10s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████| 20/20 [00:20<00:00, 1.00s/it]
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:22<00:00, 1.10s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████| 20/20 [00:22<00:00, 1.13s/it]
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:22<00:00, 1.12s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████| 20/20 [00:21<00:00, 1.07s/it]
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:21<00:00, 1.09s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████| 20/20 [00:19<00:00, 1.03it/s]
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:20<00:00, 1.01s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████| 20/20 [00:19<00:00, 1.03it/s]
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:20<00:00, 1.01s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████| 20/20 [00:19<00:00, 1.02it/s]
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:22<00:00, 1.15s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████| 20/20 [00:21<00:00, 1.07s/it]
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:21<00:00, 1.06s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████| 20/20 [00:20<00:00, 1.00s/it]
片刻之間,揮毫落紙如云煙。
遺憾的是,引導(dǎo)詞不支持中文,但可以配置權(quán)重,數(shù)值從0.1~100,默認(rèn)狀態(tài)是1,低于1就是減弱,大于1就是加強(qiáng):
(Tall buildings:1.1), people bustling(1.61),(heavy traffic:0.3),(staggered light and shadow:1.3)
Stable-Diffusion-Webui也支持Negative prompt(反向引導(dǎo)詞)。
就是用文字描述你不想在圖像中出現(xiàn)的東西:
對(duì)圖片進(jìn)行去噪處理,使其看起來(lái)更像你的提示詞;同樣使其看起來(lái)更像你的反向提示詞。
同時(shí)觀察正方向兩者之間的差異,并利用它來(lái)產(chǎn)生一組對(duì)噪聲圖片的改變,將最終結(jié)果移向前者而遠(yuǎn)離后者。
默認(rèn)通用反向引導(dǎo)詞:
lowres,bad anatomy,bad hands,text,error,missing fingers,
extra digit,fewer digits,cropped,worst quality,
low quality,normal quality,jpeg artifacts,signature,
watermark,username,blurry,missing arms,long neck,
Humpbacked,missing limb,too many fingers,
mutated,poorly drawn,out of frame,bad hands,
owres,unclear eyes,poorly drawn,cloned face,bad face
除了引導(dǎo)詞,還可以調(diào)整采樣迭代步數(shù) (Sampling Steps)。
系統(tǒng)先隨機(jī)生成一個(gè)基礎(chǔ)的圖片,然后一步步的調(diào)整圖片,向引導(dǎo)詞 Prompt 靠攏
Sampling Steps參數(shù)就是告訴人工智能,這樣的步驟應(yīng)該進(jìn)行多少次。
次數(shù)越多,每一步訓(xùn)練也就越小越精確。當(dāng)然了,成本也會(huì)越高,同時(shí)每一次訓(xùn)練的時(shí)間也會(huì)成同比增長(zhǎng)。
除了迭代步數(shù),也可以自由地選擇采樣方法(Sampling method)
也就是讓Stable-Diffusion-Webui具體使用用什么算法來(lái)訓(xùn)練圖片模型。
默認(rèn)算法是Euler a :富有創(chuàng)造力,不同步數(shù)可以生產(chǎn)出不同的圖片。 但是超過(guò)30步左右基本就沒(méi)有實(shí)質(zhì)化的增益效果。
Euler算法:最簡(jiǎn)單的算法,訓(xùn)練速度也是最快的。
LMS算法:Euler的延伸算法,相對(duì)更穩(wěn)定一點(diǎn),30步就比較穩(wěn)定了
PLMS:優(yōu)化過(guò)的LMS算法
其他的一些參數(shù):
生成批次Batch count/n_iter:同樣的配置,循環(huán)跑幾次
每批數(shù)量 Batch size:同時(shí)生成多少個(gè)圖像,增加這個(gè)值可以并行運(yùn)行,但也需要更多的顯卡顯存。
提示詞相關(guān)性 CFG Scale:圖像與引導(dǎo)詞匹配程度。增加這個(gè)值將導(dǎo)致圖像更接近引導(dǎo)詞,但過(guò)高會(huì)讓圖像色彩過(guò)于飽和。一般在5~15之間為好,7,9,12是3個(gè)常見(jiàn)的設(shè)置值。
寬度 X 高度 Width X Height:?jiǎn)挝皇窍袼兀m當(dāng)增加尺寸,后臺(tái)會(huì)試圖填充更多的細(xì)節(jié)進(jìn)來(lái)。
Stable-Diffusion-Webui定制化模型
Stable-Diffusion-Webui默認(rèn)下載的基礎(chǔ)模型在項(xiàng)目目錄的models/Stable-diffusion文件夾中:
/stable-diffusion-webui/models/Stable-diffusion
模型名稱是v1-5-pruned-emaonly.safetensors,體積是4.27GB。
如果需要一些比較有個(gè)性定制化模型,可以在civitai.com平臺(tái)進(jìn)行挑選和下載,需要注意的是,該平臺(tái)上的模型魚(yú)龍混雜,良莠不齊,不能說(shuō)是蔚為大觀,但多多少少有點(diǎn)泥沙俱下的意思,所以最好不要在比較正式的公共(工作)環(huán)境打開(kāi)該平臺(tái),否則結(jié)果可能會(huì)令人非常尷尬。
這里我們選擇相對(duì)比較潮流的賽博朋克風(fēng)格模型:synthwavepunk
將下載的模型放入models/Stable-diffusion目錄。
隨后重啟Stable-Diffusion-Webui服務(wù):
python3 launch.py --skip-torch-cuda-test --upcast-sampling --use-cpu interrogate
在頁(yè)面表單中的Stable Diffusion checkpoint選項(xiàng)里選擇對(duì)應(yīng)的模型:
[圖片上傳失敗...(image-e8d44c-1677802804064)]
引導(dǎo)詞:
concept art, 4k, intricate, pinup, a woman, beautiful face, embroidery, lace, hyper-realistic, highly detailed, octane render, concept art, smooth, 8k, dancing princess, snthwve style, nvinkpunk, by jeremy mann, by sandra chevrier, by dave mckean and richard avedon and maciej kuciara
訓(xùn)練結(jié)果:
[圖片上傳失敗...(image-e57475-1677802804064)]
好了,現(xiàn)在,你已經(jīng)知曉那些網(wǎng)絡(luò)上的漂亮小姐姐是怎么生成的了。
結(jié)語(yǔ)
也許我們只是偶爾被網(wǎng)絡(luò)上那些酷炫而獵奇的AI生成圖所吸引,但如果你真的動(dòng)手去搭建、調(diào)試、甚至開(kāi)始訓(xùn)練屬于自己的繪畫(huà)模型,相信我,你馬上就會(huì)深陷其中,不能自拔,AI仿若可以滿足你所有的幻想,欲望滿溢,又欲言又止,分寸把握之精確,妙入毫顛。什么?你還在玩那些無(wú)聊的電子游戲?相信我,Stable-Diffusion-Webui才是最高級(jí)的精神享受,沒(méi)有之一,也不可替代。