Stable Diffusion模型的安裝與使用教程

加入 Stable Diffusion 體驗無限免費的 AI 作圖

Chris Lee
20 min readMar 13, 2023

本篇提供給不想看或沒辦法看 Youtube 教學影片的人,手把手帶你安裝Stable Diffusion。對於 AI 製圖工具及 Stable Diffusion 原理有興趣可以參考之前的幾篇文章。

有一定的基礎後,讓我們開始建造美少女夢工廠吧!

硬體需求

欲善其事先利其器,首先你需要一台有 GPU 的 PC 或 MAC,GPU 建議是 1050 以上,其中 VRAM 越大越好,規格太差使用體驗天壤之別。GPU 用 Nvidia、AMD、M1 晶片都沒差,Nvidia 30 系列的算力會更穩一點。

Mac玩家可以參考:

PC玩家可以參考:

後面的說明都是用 Windows PC 做範例,範例設備為筆電,規格如下:

AMD Ryzen 9 5900HS
16G Ram
Nvidia RTX3060 6G

1. 安裝 Python

根據官方建議,請到 Python 網站下載 Python 3.10,不要安裝太低版本,容易有未預期的錯誤,高版本也不建議,可能導致電腦 cuda 不支援等問題,可以從下面網站找到 installer 安裝程式,無腦下一步安裝。

針對熟悉 Python 的人可能會裝 Anaconda,想說可否用虛擬環境來做,其實是可以的,但多數玩家還是建議乖乖裝 Python 即可。

2. 下載 AUTOMATIC1111 的 Stable Diffusion

將 Github 的內容下載至電腦,可以在終端機下 git 指令,或是使用 Download ZIP 也可以,放到一個空間夠大的路徑中,因為未來你可能會加入很多模型來玩。

3. 設定 Python 路徑

找到你的 Python 路徑,可以在開始列中從開啟檔案位置一直找,或是根據你安裝的路徑找,找到後複製一下路徑。

打開剛剛下載的 AUTOMATIC1111 Stable Diffusion,找到資料夾內的 webui-user.bat,右鍵編輯,把 Python 路徑貼上去,並且在下面加上 git pull,讓他可以自動更新,存檔後關閉。

@echo off

set PYTHON="你的 Python 路徑"
set GIT=
set VENV_DIR=
set COMMANDLINE_ARGS=
git pull
call webui.bat

4. 加入模型檔

接下來要將模型檔加入到 Stable Diffusion 的資料夾中,模型可以從幾個地方抓,最有名的是 Civitai 和 Huggingface,記得都要註冊喔:

https://civitai.com

我們以C站為例,找到 CHECKPOINT 字樣的模型,假設我要喜歡 Realistic Vision 風格,點進去後下載即可,這些都是模型主檔,檔案動輒幾 GB,所以才說要一定的空間放置。

這是開啟Safe mode,另一個模式大家知道的🧐

下載完後丟到剛剛 stable diffustion\stable-diffusion-webui\models\Stable-diffusion 的資料夾中,這裡是模型檔的放置處。

你可以看到 models 裡面有很多子資料夾,這個未來有機會再說,總之目前只會用到放主檔的 Stable-diffusion。

5. 啟動 Stable Diffustion

萬事俱備後,打開剛剛編輯好的 webui-user.bat,就會開始進入安裝畫面,通常安裝要一點時間,可以先去喝杯茶。

完成後會如下,顯示 http://127.0.0.1:7860 可以使用,基本上第一次比較久,之後都會滿快的。

打開瀏覽器貼上網址,就進入到生成圖片的主控台囉~

6. 功能說明

模型選擇

最上面的Stable Diffusion checkpoint,可以選擇你的主模型,也就是對應到stable-diffusion-webui\models\Stable-diffusion 資料夾的內容,如果丟進去沒顯示,可以點一下旁邊的重新整理。

工具列

工具列上有很多選項,如果安裝插件會更多,下面講幾個常用的:

  • txt2img - 文字生成圖像的地方,主要工作區
  • img2img - 根據圖像成範本,變出更多圖像的地方
  • Extras - 根據需求優化圖像的地方,一般少用
  • PNG Info - 圖像剖析的地方,一般少用
  • Checkpoint Merger - 模型合併的地方,一般少用
  • Ttain - 訓練模型的地方,想建自己的客製化模型在這裡
  • Settings - 顧名思義就是設定
  • Extensions - 安裝插件的地方
如果裝了插件,就會多一些選項出來

txt2img

文字輸入區塊:

  • Prompt - 對於圖像的描述,可以去網路上抄咒語來改
  • Negative prompt - 不想出現的關鍵字,例如不想出現 18 禁的圖,可以把 nsfw 加在這

參數調整區塊:

  • Sampling method、Sampling steps - 採樣方法及生成步數,採樣方法有簡單有複雜,效果因想產生的圖像而異。步數越多則越細緻,但也會比較久,通常高出某個數值差異就不大,個人習慣 DPM 配上 20~30 steps
  • Restore faces - 臉部優化,圖有人臉建議打開
  • Tiling - 對圖做堆疊,做出拼貼的效果,個人幾乎沒用
  • Highres. fix - 高解析度修復,會先用低解析度製作,再用高解析度修復,因為做了兩次,所以會更花時間,個人少用
  • Width、Height - 圖片長寬尺寸,通常512x512,尺寸太大會影響效能
  • Batch count、 Batch size - 一次產生幾張圖,一次多少張一起算,設定2、2就會有4張圖
  • CFG Scale - 越高會越接近關鍵字,越低就是讓AI自由發揮,通常在7-12之間,太高有機率變合成獸
  • Seed - 所有程式都會需要的種子參數,同一個種子可以產出相似的風格,-1代表隨機,可以骰到喜歡的圖,再點綠色按鈕設定種子

下完咒語按下 Generate 鍵即可開始生成:

咒語怎麼下:

Prompt 下法有很多,可以找你載主模型的地方,通常會有範例,抄來改即可,如下圖是 C 站的範例,可以看到咒語內容、模型、步數、種子等資訊,但通常即使全抄,也不一定能產出一樣的圖,基本上都要靠 SL 大法。

其他功能未必會用到,有機會再說明。

7. 範例演示

# Prompt
a beatuiful female play guitar <lora:taiwanDollLikeness_v10:0.3> <lora:koreanDollLikeness_v15:0.5> <lora:japaneseDollLikeness_v10:0.2>

# Negative prompt
EasyNegative disfigured,bad anatomy,futa,sketches,paintings, multiple nippals, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, bad anatomy,(long hair:1.4),(fat:1.2),lowres,bad anatomy,bad hands, text, error, missing fingers,extra digit, fewer digits, cropped, worstquality, low quality, normal quality,jpegartifacts,signature, watermark, username,blurry,bad feet,cropped,poorly drawn hands,poorly drawn face,mutation,deformed,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,extra fingers,fewer digits,extra limbs,extra arms,extra legs,malformed limbs,fused fingers,too many fingers,long neck,cross-eyed,mutated hands,polar lowres,bad body,bad proportions,gross proportions,text,error,missing fingers,missing arms,missing legs,extra digit, extra arms, extra leg, extra foot, monotone, ugly, disfigured face, diformed face, EasyNegative disfigured,bad anatomy,futa,sketches,paintings, multiple nippals, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, bad anatomy,(long hair:1.4),(fat:1.2),lowres,bad anatomy,bad hands, text, error, missing fingers,extra digit, fewer digits, cropped, worstquality, low quality, normal quality,jpegartifacts,signature, watermark, username,blurry,bad feet,cropped,poorly drawn hands,poorly drawn face,mutation,deformed,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,extra fingers,fewer digits,extra limbs,extra arms,extra legs,malformed limbs,fused fingers,too many fingers,long neck,cross-eyed,mutated hands,polar lowres,bad body,bad proportions,gross proportions,text,error,missing fingers,missing arms,missing legs,extra digit, extra arms, extra leg, extra foot, monotone, ugly, disfigured face, diformed face, EasyNegative disfigured,bad anatomy,futa,sketches,paintings, multiple nippals, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, bad anatomy,(long hair:1.4),(fat:1.2),lowres,bad anatomy,bad hands, text, error, missing fingers,extra digit, fewer digits, cropped, worstquality, low quality, normal quality,jpegartifacts,signature, watermark, username,blurry,bad feet,cropped,poorly drawn hands,poorly drawn face,mutation,deformed,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,extra fingers,fewer digits,extra limbs,extra arms,extra legs,malformed limbs,fused fingers,too many fingers,long neck,cross-eyed,mutated hands,polar lowres,bad body,bad proportions,gross proportions,text,error,missing fingers,missing arms,missing legs,extra digit, extra arms, extra leg, extra foot, monotone, ugly, disfigured face, diformed face, EasyNegative disfigured,bad anatomy,futa,sketches,paintings, multiple nippals, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, bad anatomy,(long hair:1.4),(fat:1.2),lowres,bad anatomy,bad hands, text, error, missing fingers,extra digit, fewer digits, cropped, worstquality, low quality, normal quality,jpegartifacts,signature, watermark, username,blurry,bad feet,cropped,poorly drawn hands,poorly drawn face,mutation,deformed,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,extra fingers,fewer digits,extra limbs,extra arms,extra legs,malformed limbs,fused fingers,too many fingers,long neck,cross-eyed,mutated hands,polar lowres,bad body,bad proportions,gross proportions,text,error,missing fingers,missing arms,missing legs,extra digit, extra arms, extra leg, extra foot, monotone, ugly, disfigured face, diformed face, EasyNegative disfigured,bad anatomy,futa,sketches,paintings, multiple nippals, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, bad anatomy,(long hair:1.4),(fat:1.2),lowres,bad anatomy,bad hands, text, error, missing fingers,extra digit, fewer digits, cropped, worstquality, low quality, normal quality,jpegartifacts,signature, watermark, username,blurry,bad feet,cropped,poorly drawn hands,poorly drawn face,mutation,deformed,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,extra fingers,fewer digits,extra limbs,extra arms,extra legs,malformed limbs,fused fingers,too many fingers,long neck,cross-eyed,mutated hands,polar lowres,bad body,bad proportions,gross proportions,text,error,missing fingers,missing arms,missing legs,extra digit, extra arms, extra leg, extra foot, monotone, ugly, disfigured face, diformed face, EasyNegative disfigured,bad anatomy,futa,sketches,paintings, multiple nippals, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, bad anatomy,(long hair:1.4),(fat:1.2),lowres,bad anatomy,bad hands, text, error, missing fingers,extra digit, fewer digits, cropped, worstquality, low quality, normal quality,jpegartifacts,signature, watermark, username,blurry,bad feet,cropped,poorly drawn hands,poorly drawn face,mutation,deformed,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,extra fingers,fewer digits,extra limbs,extra arms,extra legs,malformed limbs,fused fingers,too many fingers,long neck,cross-eyed,mutated hands,polar lowres,bad body,bad proportions,gross proportions,text,error,missing fingers,missing arms,missing legs,extra digit, extra arms, extra leg, extra foot, monotone, ugly, disfigured face, diformed face, ((child)), ((childish))

美少女夢工廠完成囉~

如果你做出來的效果不好,不要灰心,網路上有很多大神教你怎麼畫出最美的臉蛋,祝大家都能找到最理想的那顆種子。

--

--

Chris Lee

隱身在金融業的資料科學家,部分文章會在個人部落格唷 https://chrisnote.com/