Rocky Ding
公眾號:WeThinkIn
寫在前面
【人人都是算法專家】欄目專注于分享Rocky在AI行業(yè)中對業(yè)務(wù)/競賽/研究/產(chǎn)品維度的思考與感悟。歡迎大家一起交流學(xué)習(xí)??
大家好,我是Rocky。
之前Rocky詳細(xì)介紹了Stable Diffusion(SD)的核心基礎(chǔ)知識,SD是目前最流行的開源AI繪畫模型,其繁榮的生態(tài)大幅降低了我們進(jìn)行AI繪畫的成本與難度,大家可以查閱Rocky之前的文章:深入淺出解析Stable Diffusion完整核心基礎(chǔ)知識 |【算法兵器譜】了解關(guān)于SD的更多內(nèi)容。
本文主要介紹如何從0到1搭建Stable Diffusion XL進(jìn)行AI繪畫,Stable Diffusion XL是Stable Diffusion的優(yōu)化版本,由Stability AI發(fā)布。比起Stable Diffusion,Stable Diffusion XL做了全方位的優(yōu)化,Rocky相信,Stable Diffusion會是圖像生成領(lǐng)域的“YOLO”,而Stable Diffusion XL那就是“YOLOv3”。
Rocky已經(jīng)在知乎上持續(xù)撰寫Stable Diffusion XL全方位的解析文章:深入淺出完整解析Stable Diffusion XL(SDXL)核心基礎(chǔ)知識(歡迎大家關(guān)注Rocky的知乎號:Rocky Ding,也希望大家給這篇文章多多點贊,讓Rocky的義務(wù)勞動有更多動力?。?,內(nèi)容包括模型結(jié)構(gòu),從0到1訓(xùn)練教程,從0到1搭建推理流程,最新SD資源匯總,相關(guān)插件工具使用等,后續(xù)會將全部內(nèi)容都在公眾號內(nèi)分享!
話不多說,在Rocky毫無保留的分享下,讓我們開始學(xué)習(xí)吧!
So,enjoy:
正文開始
----【目錄先行】----
-
零基礎(chǔ)使用ComfyUI搭建Stable Diffusion XL推理流程
-
零基礎(chǔ)使用SD.Next搭建Stable Diffusion XL推理流程
-
零基礎(chǔ)使用Stable Diffusion WebUI搭建Stable Diffusion XL推理流程
-
零基礎(chǔ)使用diffusers搭建Stable Diffusion XL推理流程
-
Stable Diffusion XL生成示例
零基礎(chǔ)使用ComfyUI搭建Stable Diffusion XL推理流程
ComfyUI是一個基于節(jié)點式的Stable Diffusion AI繪畫工具。和Stable Diffusion WebUI相比,ComfyUI通過將Stable Diffusion模型生成推理的pipeline拆分成獨立的節(jié)點,實現(xiàn)了更加精準(zhǔn)的工作流定制和清晰的可復(fù)現(xiàn)性。
同時其完善的模型加載和圖片生成機(jī)制,讓其能夠在2080Ti顯卡上構(gòu)建Stable Diffusion XL的工作流,并能生成1024x1024分辨率的圖片,如此算力友好,可謂是初學(xué)者的福音。
目前ComfyUI已經(jīng)能夠兼容Stable Diffusion XL的Base模型和Refiner模型,下面兩張圖分別是Rocky使用ComfyUI來加載Stable Diffusion XL Base模型和Stable Diffusion XL Base + Refiner模型并生成圖片的完整Pipeline:
大家如果看了感覺復(fù)雜,不用擔(dān)心,Rocky已經(jīng)為大家保存了這兩個工作流,大家只需關(guān)注Rocky的公眾號WeThinkIn,并回復(fù)“ComfyUI”,就能獲取這兩個工作流以及文生圖,圖生圖,圖像Inpainting,ControlNet以及圖像超分在內(nèi)的所有Stable Diffusion經(jīng)典工作流json文件,大家只需在ComfyUI界面右側(cè)點擊Load按鈕選擇對應(yīng)的json文件,即可加載對應(yīng)的工作流,開始愉快的AI繪畫之旅。
話說回來,下面Rocky將帶著大家一步一步使用ComfyUI搭建Stable Diffusion XL推理流程,從而實現(xiàn)上面兩張圖的生成過程。
首先,我們需要安裝ComfyUI框架,這一步非常簡單,在命令行輸入如下代碼即可:
git clone https://github.com/comfyanonymous/ComfyUI.git
安裝好后,我們可以看到本地的ComfyUI文件夾。
ComfyUI框架安裝到本地后,我們需要安裝其依賴庫,我們只需以下操作:
cd ComfyUI #進(jìn)入下載好的ComfyUI文件夾中
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
完成這些配置工作后,我們就可以配置模型了,我們將Stable Diffusion XL模型放到ComfyUI/models/checkpoints/路徑下。這樣以來,等我們開啟可視化界面后,就可以選擇Stable Diffusion XL模型進(jìn)行AI繪畫了。
接下來,我們就可以啟動ComfyUI了!我們到ComfyUI/路徑下,運行main.py即可:
python main.py --listen --port 8888
運行完成后,可以看到命令行中出現(xiàn)的log:
To see the GUI go to: http://0.0.0.0:8888
我們將http://0.0.0.0:8888輸入到我們本地的網(wǎng)頁中,即可打開如上圖所示的ComfyUI可視化界面,愉快的使用Stable Diffusion XL模型生成我們想要的圖片了。
接下來就是ComfyUI的節(jié)點式模塊講解了,首先是只加載Base模型的情況:
Rocky已經(jīng)進(jìn)行了比較詳細(xì)的注釋,首先大家可以在紅框中選擇我們的模型(Stable Diffusion XL Base),接著填入Prompt和負(fù)向Prompt,并且配置生成推理過程的參數(shù)(迭代次數(shù),CFG,Seed等),然后在綠色框中設(shè)置好生成圖片的分辨率,然后在紫色框中點擊Queue Prompt按鈕,整個推理過程就開始了。等整個推理過程完成之后,生成的圖片會在圖中黃色箭頭所指的地方進(jìn)行展示,并且會同步將生成圖片保存到本地的ComfyUI/output/路徑下。
完成了Stable Diffusion Base模型的推理流程,我們再來看看Base+Refiner模型的推理流程如何搭建:
和Base模型的構(gòu)建十分相似,首先大家可以在紅框中選擇我們的Refiner模型(Stable Diffusion XL Refiner),Refiner模型使用的Prompt和負(fù)向Prompt與Base模型一致,并且配置生成推理過程的參數(shù)(迭代次數(shù),CFG,Seed等),綠色箭頭表示將Base模型輸出的Latent特征作為Refiner模型的輸入,然后在藍(lán)色框中點擊Queue Prompt按鈕,整個Refiner精修過程就開始了。等整個推理過程完成之后,生成的圖片會在圖中紫色箭頭所指的地方進(jìn)行展示,并且會同步將生成圖片保存到本地的ComfyUI/output/路徑下。
到此為止,Rocky已經(jīng)詳細(xì)講解了如何使用ComfyUI來搭建Stable Diffusion XL模型進(jìn)行AI繪畫,大家可以按照Rocky的步驟進(jìn)行嘗試。
零基礎(chǔ)使用SD.Next搭建Stable Diffusion XL推理流程
SD.Next原本是Stable Diffusion WebUI的一個分支,再經(jīng)過不斷的迭代優(yōu)化后,最終成為了一個獨立版本。
SD.Next與Stable Diffusion WebUI相比,包含了更多的高級功能,也兼容Stable Diffusion, Stable Diffusion XL, Kandinsky, DeepFloyd IF等模型結(jié)構(gòu),是一個功能十分強(qiáng)大的AI繪畫框架。
那么我們馬上開始SD.Next的搭建與使用吧。
首先,我們需要安裝SD.Next框架,這一步非常簡單,在命令行輸入如下代碼即可:
git clone https://github.com/vladmandic/automatic
安裝好后,我們可以看到本地的automatic文件夾。
SD.Next框架安裝到本地后,我們需要安裝其依賴庫,我們只需以下操作:
cd automatic #進(jìn)入下載好的automatic文件夾中
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
除了安裝依賴庫之外,還需要配置SD.Next所需的repositories插件,我們需要運行一下代碼:
cd automatic #進(jìn)入下載好的automatic文件夾中
python installer.py
如果發(fā)現(xiàn)extensions插件下載速度較慢,出現(xiàn)很多報錯,大家可以直接使用Rocky已經(jīng)配置好的資源包,可以快速啟動SD.Next框架。SD.Next資源包可以關(guān)注公眾號WeThinkIn,后臺回復(fù)“SD.Next資源”獲取。
在完成了依賴庫和repositories插件的安裝后,我們就可以配置模型了,我們將Stable Diffusion XL模型放到/automatic/models/Stable-diffusion/路徑下。這樣以來,等我們開啟可視化界面后,就可以選擇Stable Diffusion XL模型用于推理生成圖片了。
完成上述的步驟后,我們可以啟動SD.Next了!我們到/automatic/路徑下,運行l(wèi)aunch.py即可:
python launch.py --listen --port 8888
運行完成后,可以看到命令行中出現(xiàn)的log:
To see the GUI go to: http://0.0.0.0:8888
我們將http://0.0.0.0:8888輸入到我們本地的網(wǎng)頁中,即可打開如下圖所示的SD.Next可視化界面,愉快的使用Stable Diffusion XL模型進(jìn)行AI繪畫了。
進(jìn)入SD.Next可視化界面后,我們可以在紅色框中選擇模型,然后需要修改Settings中的配置,來讓SD.Next能夠加載Stable Diffusion XL模型。
我們點擊上圖藍(lán)色框中的Settings,進(jìn)入Settings配置界面:
從上面圖示中可以看到,我們需要做的修改是將Settings -> Stable Diffusion -> Stable Diffusion backend設(shè)置為diffusers,并在Stable Diffusion refiner欄中選擇Refiner模型。
然后我們需要將Settings -> Diffusers Settings-> Select diffuser pipeline when loading from safetensors欄設(shè)置為Stable Diffusion XL。
完成了上述的配置修改后,我們就可以使用SD.Next加載Stable Diffusion XL進(jìn)行AI繪畫了!
零基礎(chǔ)使用Stable Diffusion WebUI搭建Stable Diffusion XL推理流程
目前Stable Diffusion WebUI已經(jīng)支持Stable Diffusion XL中的Base模型,但是對Refiner模型暫不支持。
Stable Diffusion WebUI是AI繪畫領(lǐng)域最為流行的框架,其生態(tài)極其繁榮,非常多的上下游插件能夠與Stable Diffusion WebUI一起完成諸如AI視頻生成,AI證件照生成等工作流,可玩性非常強(qiáng)。
接下來,咱們就使用這個流行框架搭建Stable Diffusion XL推理流程吧。
首先,我們需要下載安裝Stable Diffusion WebUI框架,我們只需要在命令行輸入如下代碼即可:
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
安裝好后,我們可以看到本地的stable-diffusion-webui文件夾。
下面我們需要安裝其依賴庫,我們進(jìn)入Stable Diffusion WebUI文件夾,并進(jìn)行以下操作:
cd stable-diffusion-webui #進(jìn)入下載好的automatic文件夾中
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
和SD.Next的配置流程類似,我們還需要配置Stable Diffusion WebUI的repositories插件,我們需要運行下面的代碼:
sh webui.sh
#主要依賴包括:BLIP CodeFormer generative-models k-diffusion stable-diffusion-stability-ai taming-transformers
如果發(fā)現(xiàn)repositories插件下載速度較慢,出現(xiàn)很多報錯,don’t worry,大家可以直接使用Rocky已經(jīng)配置好的資源包,可以快速啟動與Stable Diffusion XL兼容的Stable Diffusion WebUI框架。Stable Diffusion WebUI資源包可以關(guān)注公眾號WeThinkIn,后臺回復(fù)“WebUI資源”獲取。
在完成了依賴庫和repositories插件的安裝后,我們就可以配置模型了,我們將Stable Diffusion XL模型放到/stable-diffusion-webui/models/Stable-diffusion/路徑下。這樣以來,等我們開啟可視化界面后,就可以選擇Stable Diffusion XL模型用于推理生成圖片了。
完成上述的步驟后,我們可以啟動Stable Diffusion WebUI了!我們到/stable-diffusion-webui/路徑下,運行l(wèi)aunch.py即可:
python launch.py --listen --port 8888
運行完成后,可以看到命令行中出現(xiàn)的log:
To see the GUI go to: http://0.0.0.0:8888
我們將http://0.0.0.0:8888輸入到我們本地的網(wǎng)頁中,即可打開如下圖所示的Stable Diffusion WebUI可視化界面,愉快的使用Stable Diffusion XL模型進(jìn)行AI繪畫了。
進(jìn)入Stable Diffusion WebUI可視化界面后,我們可以在紅色框中選擇SDXL模型,然后在黃色框中輸入我們的Prompt和負(fù)向提示詞,同時在綠色框中設(shè)置我們想要生成的圖像分辨率**(推薦設(shè)置成1024x1024)**,然后我們就可以點擊Generate按鈕,進(jìn)行AI繪畫了。
等待片刻后,圖像就生成好了,并展示在界面的右下角,同時也會保存到/stable-diffusion-webui/outputs/txt2img-images/路徑下,大家可以到對應(yīng)路徑下查看。
零基礎(chǔ)使用diffusers搭建Stable Diffusion XL推理流程
在diffusers中能夠非常好的構(gòu)建Stable Diffusion XL推理流程,由于diffusers目前沒有現(xiàn)成的可視化界面,Rocky將在Jupyter Notebook中搭建完整的Stable Diffusion XL推理工作流,讓大家能夠快速的掌握。
首先,我們需要安裝diffusers庫,并確保diffusers的版本 >= 0.18.0,我們只需要在命令行中輸入一下命令進(jìn)行安裝即可:
pip install diffusers --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
顯示如下log表示安裝成功:
Successfully installed diffusers-0.18.2 huggingface-hub-0.16.4
命令中加入:-i https://pypi.tuna.tsinghua.edu.cn/simple some-package 表示使用清華源下載依賴包,速度非???!
接著,我們繼續(xù)安裝其他的依賴庫:
pip install transformers==4.27.0 accelerate==0.12.0 safetensors==0.2.7 invisible_watermark -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
顯示如下log表示安裝成功:
Successfully installed transformers-4.27.0 accelerate==0.12.0 safetensors==0.2.7 invisible_watermark-0.2.0
注意:想要在diffusers中以fp16的精度加載Stable Diffusion XL模型,必須滿足transformers庫的版本>=4.27.0
完成了上述依賴庫的安裝,我們就可以搭建Stable Diffusion XL模型的完整工作流了。
我們先單獨使用Stable Diffusion XL中的Base模型來生成圖像:
# 加載diffusers和torch依賴庫
from diffusers import DiffusionPipeline
import torch
# 構(gòu)建Stable Diffusion XL Base模型的Pipeline,加載Stable Diffusion XL Base模型
pipe = DiffusionPipeline.from_pretrained("/本地路徑/stable-diffusion-xl-base-0.9",torch_dtype=torch.float16, variant="fp16")
# "本地路徑/stable-diffusion-xl-base-0.9"表示我們需要加載的Stable Diffusion XL Base模型,
# 大家可以關(guān)注Rocky的公眾號WeThinkIn,后臺回復(fù):SDXL模型,即可獲得資源鏈接
# "fp16"代表啟動fp16精度。比起fp32,fp16可以使模型顯存占用減半。
# 使用GPU進(jìn)行Pipeline的推理
pipe.to("cuda")
# 輸入提示詞
prompt = "Watercolor painting of a desert landscape, with sand dunes, mountains, and a blazing sun, soft and delicate brushstrokes, warm and vibrant colors"
# 輸入負(fù)向提示詞,表示我們不想要生成的特征
negative_prompt = "(EasyNegative),(watermark), (signature), (sketch by bad-artist), (signature), (worst quality), (low quality), (bad anatomy), NSFW, nude, (normal quality)"
# 設(shè)置seed,可以固定構(gòu)圖
seed = torch.Generator("cuda").manual_seed(42)
# Pipeline進(jìn)行推理
image = pipe(prompt, negative_prompt=negative_prompt,generator=seed).images[0]
# Pipeline生成的images包含在一個list中:[<PIL.Image.Image image mode=RGB size=1024x1024>]
#所以需要使用images[0]來獲取list中的PIL圖像
# 保存生成圖像
image.save("test.png")
完成上面的整個代碼流程,我們可以生成一張水彩風(fēng)格的沙漠風(fēng)景畫,如果大家按照Rocky的參數(shù)進(jìn)行操作,應(yīng)該能確保生成下面的圖片:
接著,我們將Base模型和Refiner模型級聯(lián)來生成圖像:
from diffusers import DiffusionPipeline
import torch
pipe = DiffusionPipeline.from_pretrained("/本地路徑/stable-diffusion-xl-base-0.9", torch_dtype=torch.float16, variant="fp16")
pipe.to("cuda")
prompt = "Watercolor painting of a desert landscape, with sand dunes, mountains, and a blazing sun, soft and delicate brushstrokes, warm and vibrant colors"
negative_prompt = "(EasyNegative),(watermark), (signature), (sketch by bad-artist), (signature), (worst quality), (low quality), (bad anatomy), NSFW, nude, (normal quality)"
seed = torch.Generator("cuda").manual_seed(42)
# 首先運行Base模型的Pipeline,輸出格式為output_type="latent"
image = pipe(prompt=prompt, negative_prompt=negative_prompt, generator=seed, output_type="latent").images
# 構(gòu)建Stable Diffusion XL Refiner模型的Pipeline,加載Stable Diffusion XL Refiner模型
pipe = DiffusionPipeline.from_pretrained("/本地路徑/stable-diffusion-xl-refiner-0.9", torch_dtype=torch.float16, variant="fp16")
# "本地路徑/stable-diffusion-xl-refiner-0.9"表示我們需要加載的Stable Diffusion XL Refiner模型,
# 大家可以關(guān)注Rocky的公眾號WeThinkIn,后臺回復(fù):SDXL模型,即可獲得資源鏈接
pipe.to("cuda")
images = pipe(prompt=prompt, negative_prompt=negative_prompt, generator=seed, image=image).images
images[0].save("test.png")
完成了上述的代碼流程,我們再來看看這次Base模型和Refiner模型級聯(lián)生成的圖片:
為了更加直觀的對比,我們將剛才生成的兩張圖片放在一起對比:
我們可以清楚的看到,使用了Refiner模型之后,圖片的整體質(zhì)量和細(xì)節(jié)有比較大的增強(qiáng),構(gòu)圖色彩更加柔和。
當(dāng)然,我們也可以單獨使用Refiner模型對圖片進(jìn)行優(yōu)化:
import torch
from diffusers import StableDiffusionXLImg2ImgPipeline
from diffusers.utils import load_image
pipe = DiffusionPipeline.from_pretrained("/本地路徑/stable-diffusion-xl-refiner-0.9", torch_dtype=torch.float16, variant="fp16")
pipe = pipe.to("cuda")
image_path = "/本地路徑/test.png"
init_image = load_image(image_path).convert("RGB")
prompt = "Watercolor painting of a desert landscape, with sand dunes, mountains, and a blazing sun, soft and delicate brushstrokes, warm and vibrant colors"
negative_prompt = "(EasyNegative),(watermark), (signature), (sketch by bad-artist), (signature), (worst quality), (low quality), (bad anatomy), NSFW, nude, (normal quality)"
seed = torch.Generator("cuda").manual_seed(42)
image = pipe(prompt, negative_prompt=negative_prompt, generator=seed, image=init_image).images[0]
image.save("refiner.png")
Rocky這里是使用了未來機(jī)甲風(fēng)格的圖片進(jìn)行測試對比,可以從下圖看到,Refiner模型優(yōu)化圖像質(zhì)量的效果還是非常明顯的,圖像毛刺明顯消除,整體畫面更加自然柔和,細(xì)節(jié)特征也有較好的補(bǔ)充和重建。
Stable Diffusion XL生成示例
示例一:未來主義的城市風(fēng)格
Prompt:Stunning sunset over a futuristic city, with towering skyscrapers and flying vehicles, golden hour lighting and dramatic clouds, high detail, moody atmosphere
Negative Prompt:(EasyNegative),(watermark), (signature), (sketch by bad-artist), (signature), (worst quality), (low quality), (bad anatomy), NSFW, nude, (normal quality)
Stable Diffusion XL Base+Refiner生成結(jié)果:
示例二:天堂海灘風(fēng)格
Prompt:Serene beach scene with crystal clear water and white sand, tropical palm trees swaying in the breeze, perfect paradise, seascape
Negative Prompt:(EasyNegative),(watermark), (signature), (sketch by bad-artist), (signature), (worst quality), (low quality), (bad anatomy), NSFW, nude, (normal quality)
Stable Diffusion XL Base+Refiner生成結(jié)果:
示例三:未來機(jī)甲風(fēng)格
Prompt:Giant robots fighting in a futuristic city, with buildings falling and explosions all around, intense, fast-paced, dramatic, stylized, futuristic
Negative Prompt:(EasyNegative),(watermark), (signature), (sketch by bad-artist), (signature), (worst quality), (low quality), (bad anatomy), NSFW, nude, (normal quality)
Stable Diffusion XL Base+Refiner生成結(jié)果:
示例四:馬斯克風(fēng)格
Prompt:Elon Musk standing in a workroom, in the style of industrial machinery aesthetics, deutscher werkbund, uniformly staged images, soviet, light indigo and dark bronze, new american color photography, detailed facial features
Negative Prompt:(EasyNegative),(watermark), (signature), (sketch by bad-artist), (signature), (worst quality), (low quality), (bad anatomy), NSFW, nude, (normal quality)
Stable Diffusion XL Base+Refiner生成結(jié)果:
精致的結(jié)尾
到這里,從0到1搭建Stable Diffusion XL完整工作流進(jìn)行AI繪畫的過程就完整展現(xiàn)在我們的面前了,希望在AIGC時代中,大家能和Rocky一起,飛速成長!
Rocky將算法高價值面試知識點即“三年面試五年模擬”之獨孤九劍秘籍前十二式進(jìn)行匯總梳理成匯總篇,并制作成pdf版本,大家可在公眾號后臺 【精華干貨】菜單或者回復(fù)關(guān)鍵詞“三年面試五年模擬” 進(jìn)行取用。
除此之外Rocky還將YOLOv1-v7全系列大解析也制作成相應(yīng)的pdf版本,大家可在公眾號后臺 【精華干貨】菜單或者回復(fù)關(guān)鍵詞“YOLO” 進(jìn)行取用。文章來源:http://www.zghlxwxcb.cn/news/detail-647856.html
Rocky一直在運營技術(shù)交流群(WeThinkIn-技術(shù)交流群),這個群的初心主要聚焦于技術(shù)話題的討論與學(xué)習(xí),包括但不限于算法,開發(fā),競賽,科研以及工作求職等。群里有很多人工智能行業(yè)的大牛,歡迎大家入群一起學(xué)習(xí)交流~(請?zhí)砑有≈治⑿臞arvis8866,拉你進(jìn)群~)文章來源地址http://www.zghlxwxcb.cn/news/detail-647856.html
到了這里,關(guān)于保姆級教程:從0到1搭建Stable Diffusion XL完整工作流進(jìn)行AI繪畫的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!