基於 ChatGPT-Stable Diffusion 實現 AI 繪畫
近年來,AI 生成內容(AIGC)領域的飛速發展催生了諸多創新技術與應用,尤其是 AI 繪畫。爲降低構建 Prompt 的複雜性,本文基於 ChatGPT 實現了一個 Stable Diffusion 提示詞生成器,支持多語言提示詞和智能優化與擴展提示詞,助力初學者輕鬆探索 AI 繪畫世界~
01 背景
近年來,AI 生成內容(AIGC)領域發展迅猛,湧現出衆多創新技術和應用。其中,AI 繪畫作爲一個重要的分支,自 2022 年起取得了顯著的進展,吸引了廣泛關注。從 2022 年 2 月初備受矚目的 Disco Diffusion,到 4 月份 OpenAI 發佈的強大文本到圖像生成模型 DALL·E 2,緊接着 5 月份 Google 推出了 Imagen 模型。同年 8 月份,初創公司 StabilityAI 發佈並開源了卓越的 AI 繪畫模型 Stable Diffusion。而最近,令人驚歎且廣受好評的 Midjourney 也成爲了熱門話題。這些技術和應用的快速發展充分展現了 AI 繪畫領域的巨大潛力。
目前,AI 繪畫模型中最受歡迎的無疑是 Stable Diffusion 和 Midjourney。在 AI 繪畫領域,Prompt(提示詞)起着至關重要的作用。通常情況下, Prompt 質量越高,生成的圖片效果越令人滿意。然而,無論是 Stable Diffusion 還是 Midjourney,都面臨着構建 Prompt 的複雜性較高,對於初學者來說具有一定的門檻。此外,這些模型都是基於英文語料數據集進行訓練的,因此並不支持原生中文提示詞。爲了解決這一問題,我們藉助 ChatGPT 實現了一個 Stable Diffusion 的提示詞生成器(Prompt Generator),並搭建了一個將文本生成圖片的服務,從而降低了 AI 繪畫的門檻。具體特點包括:支持包括中文在內的多種語言提示詞、支持智能優化與擴展提示詞功能、只需輸入少量關鍵詞,即可生成效果不錯的圖片。
02 實現原理
2.1 Stable Diffusion
2.1.1 簡介
Stable Diffusion 是一個 2022 年發佈的深度學習文本到圖像的潛在擴散模型(LDM / Latent Diffusion Model),由 CompVis、Stability AI 和 LAION 的研究人員和工程師創建。它使用來自 LAION-5B 開源數據庫子集的 512x512 圖像進行訓練,通過引入隱向量空間來解決 Diffusion 速度瓶頸,除了可用於文生圖任務,還可以用於圖生圖、特定角色刻畫,甚至是超分或者上色任務。本文不對其原理進行贅述,感興趣的同學可以閱讀原論文或相關文章。
2.1.2 如何部署
Stable Diffusion WebUI 有多種部署方式,這裏介紹最簡單的方式:Docker 部署。我們在一個 8GB GPU 上就可以運行和體驗該服務。只需要下面介紹具體的步驟:
- 拉取 Github 代碼
git clone https://github.com/AbdBarho/stable-diffusion-webui-docker.git
- 運行
# 進入項目目錄
cd stable-diffusion-webui-docker
# 下載相關模型
docker compose --profile download up --build
# 等待下載完成後,編譯運行鏡像
docker compose --profile auto up --build
- 打開 WebUI
默認運行端口爲 7860,獲取服務器 IP 後,在瀏覽器打開鏈接:http://{ip}:7860/. 頁面如下所示:
- 下載其他模型
默認下載的 sd 模型是 v1.5 版本的,包括 v1-5-pruned-emaonly.ckpt 和 sd-v1-5-inpainting.ckpt。如果需要體驗其他模型,可以先訪問這個網站:https://civitai.com/ 。上面可以找到各種基於 sd 的開源模型和效果圖,以及模型對應的點贊數、下載量和評價。可以根據業務需求或者點贊數 / 下載量來篩選模型,然後下載到部署的服務器上,sd 模型的存放路徑爲:
stable-diffusion-webui-docker/data/StableDiffusion/
我們 AI 繪畫服務默認部署的是 DreamShaper 模型。
2.2 Prompt Generator
Stable Diffusion 的提示詞包括兩種:正面和負面提示詞。簡單來說,正面提示詞是你希望模型生成的圖片內容,負面提示詞是不希望模型生成的內容。例如,sd 不太擅長生成手,我們可以在負面提示詞中加入 “bad hands”、“missing fingers”、“extra fingers” 等詞。
本文中提到的提示詞生成器,指的是正面提示詞的自動生成。負面提示詞相對來說比較簡單和固定,一般沒必要進行生成。我們使用的默認負面提示詞如下:
facelowres,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,bad feet,ugly,duplicate,trannsexual,hermaphrodite,out of frame,extra fingers,mutated hands,poorly drawn hands,poorly drawn face,mutation,deformed,blurry,bad anatomy,bad proportions,extra limbs,cloned face,disfigured,more than 2 nipples,out of frame,ugly,extra limbs,bad anatomy,gross,worst quality,low quality,normal quality,signature,watermark,username,blurry,proportions,malformed limbs,missing arms,missing legs,extra arms,extra legs,mutated hands,fused fingers,too many fingers,long neck
如何基於 ChatGPT 實現 AI 繪畫的提示詞生成器?核心是 Prompt Engineering。提到 Prompt 工程,最近吳恩達教授與 OpenAI 合作推出的 《ChatGPT Prompt Engineering for Developers》教程面向入門 LLM 的開發者,深入淺出地介紹了對於開發者,如何構造 Prompt 並基於 OpenAI 提供的 API 實現包括總結、推斷、轉換等多種常用功能,是入門 LLM 開發的經典教程。本文會結合該教程,介紹 AI 繪畫提示詞生成器的最佳實踐。
2.2.1 背景知識
由於 ChatGPT 的訓練數據只截止到 2021 年,有一些新的知識它是不知道的。因此我們對於一些特定的任務,我們需要提供一些背景知識給 ChatGPT。例如,一句話介紹什麼是 Stable Diffusion。
Stable Diffusion is an AI art generation model similar to DALLE-2.
2.2.2 任務要求
吳恩達教授在教程的 Guidelines 中提到,Prompt 工程有兩個原則:Write clear and specific instructions、Give the model time to “think”。
我們需要保證任務要求清晰和明確。對於複雜的任務,還需要將任務進行拆解,把一個綜合任務拆解成若干個小的任務。例如,我們可以將該任務拆分成兩個子任務。如下所示:
Please perform the following actions:
1. Translate the IDEA into English if it is not in English.
2. Write a detailed prompt exactly about the English IDEA follow the rules delimited by triple backticks.
Rules:
- Follow the structure of the example prompts. This means Write a description of the scene, followed by modifiers divided by commas to alter the mood, style, lighting, and more, excluding the artist name, separated by commas.
- Use at most 50 words.
大家可能會疑惑,這個任務不是挺簡單的嗎?爲什麼要拆解成兩個子任務?不這樣做會有什麼影響嗎?答案是:拆解任務會增加 ChatGPT 輸出結果的可靠性和魯棒性。因爲我們在實驗的過程中發現,對於中文提示詞,如果不拆解任務,就算在 Prompt 中要求 ChatGPT 輸出英文內容,有時候它還是會輸出中文的結果。所以,如果要求 ChatGPT 先將用戶輸入的非英文提示詞翻譯成英文,再對英文提示詞進行優化,輸出的結果纔會保證是英文的。
此外,ChatGPT 有時候會生成一大段文本,而內容太長反而會影響 AI 繪畫的效果。因此,我們需要在 Prompt 中限制其返回結果的字數,對應上面的第二條規則:
Use at most 50 words.
2.2.3 合規性檢查
作爲一個 AI 繪畫服務,需要過濾 / 拒絕不合規的繪畫提示詞,避免 AI 生成不健康的內容。因此我們需要對提示詞進行合規性檢查,包括檢查提示詞是否包含色情、暴力、政治敏感、誤導性、仇恨言論或歧視性語言等。ChatGPT 本身就有內容合規性判別的能力,我們可以藉助它的能力來實現合規性檢查,對於不滿足要求的提示詞直接返回某個錯誤代碼(1004)。我們只需要在 prompt 中加入以下內容:
If the IDEA is inappropriate or misleading or containing hate speech or discriminatory language, then simply write \"1004\"
2.2.4 少量樣本提示("Few-shot" prompting)
爲了讓 ChatGPT 生成高質量的繪畫提示詞,我們可以提供少量樣本 / 例子讓它 “學習”,即教程中所說的 "Few-shot" prompting。該方法本質上是大語言模型(LLM)Fine-tune 的一種方式。樣本不在多而在精,樣本太多會增加 ChatGPT 的調用成本(按照 tokens 數量收費)。在樣本數量有限的情況下,我們要儘量選擇多樣化的樣本,使其覆蓋儘可能多的場景。例如,我們可以提供以下幾個樣本(User 表示輸入文本,Assistant 表示預期輸出的內容):
- 樣本 1
User: 一個弓箭手向森林怪獸射箭。
Assistant: portait of a homer simpson archer shooting arrow at forest monster, front game card, drark, marvel comics, dark, intricate, highly detailed, smooth, artstation, digital illustration.
繪圖效果:
- 樣本 2
User: 畫一個海盜。
Assistant: pirate, concept art, deep focus, fantasy, intricate, highly detailed, digital painting, artstation, matte, sharp focus, illustration.
繪圖效果:
- 樣本 3
User: painting of rachel lane / alison brie, hybrid.
Assistant: a fantasy style portrait painting of rachel lane / alison brie hybrid in the style of francois boucher oil painting unreal 5 daz. rpg portrait, extremely detailed artgerm.
繪圖效果:
- 樣本 4
User: 希臘女神雅典娜。
Assistant: athena, greek goddess, claudia black, art by artgerm and greg rutkowski and magali villeneuve, bronze greek armor, owl crown, d & d, fantasy, intricate, portrait, highly detailed, headshot, digital painting, trending on artstation, concept art, sharp focus, illustration.
繪圖效果:
- 樣本 5
User: steve urkle as the hulk.
Assistant: ultra realistic illustration of steve urkle as the hulk, intricate, elegant, highly detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration.
繪圖效果:
- 樣本 6
User: 落霞與孤鶩齊飛,秋水共長天一色。
Assistant: sunset with lone wild duck flying together, autumn water and sky merging into one color, traditional chinese landscape painting, ink wash, liu kuo-sung, guo xi, fan kuan, qi baishi, serene atmosphere, misty mountains, tranquil lake, vibrant colors, harmony with nature, poetic, calligraphy, trending on artstation, concept art, sharp focus, illustration.
繪圖效果:
通過提供少量樣本讓 ChatGPT“學習”,可以提高模型輸出的可靠性,提升 Prompt 的生成效果。
2.2.5 小結
首先,我們提供任務相關的背景知識,讓 ChatGPT 對任務有初步的瞭解。然後,對任務要求進行清晰和明確地描述,並且把相對複雜的任務拆分成兩個子任務。此外,基於 ChatGPT 實現了提示詞內容合規性檢查,對不滿足條件的請求進行拒絕。最後,爲了提升效果,我們選取了幾個不同風格的高質量樣本讓它 “學習”,從而實現舉一反三,提高輸出結果的可靠性。
Prompt 工程不是一蹴而就的,需要根據需求不斷迭代調試和實驗,從而不斷優化我們的 Prompt,提升最終效果。這也是教程中提到的 Iterative Prompt Develelopment。
03 效果展示
這裏展示幾個例子,可以看出 Prompt 優化前後的效果對比:
3.1 中國古代,賽博朋克,機械仙鶴
優化前:cyberpunk mechanical crane in ancient China.
優化後:cyberpunk mechanical crane in ancient China, concept art, art by Johnson Ting, highly detailed, intricate, sharp focus, vivid colors, neon lights, futuristic, digital painting, trending on ArtStation, Unreal Engine 5, 4K UHD image, fantasy, sci-fi, mechanical, robotic, steampunk, industrial, cinematic, atmospheric, epic, breathtaking.
3.2 梵高風格,塞爾達曠野之息
優化前:The Legend of Zelda: Breath of the Wild in the style of Vincent van Gogh.
優化後:The Legend of Zelda: Breath of the Wild in the style of Vincent van Gogh, digital painting, art by Lois van Baarle, highly detailed, intricate, vibrant colors, bold brushstrokes, post-impressionist, art nouveau, inspired by Starry Night, landscape, trending on ArtStation, concept art, sharp focus, illustration.
3.3 春眠不覺曉,處處聞啼鳥
優化前:Sleeping in Spring, Unaware of the Dawn, Birds Singing Everywhere.
優化後:Sleeping in Spring, Unaware of the Dawn, Birds Singing Everywhere, traditional Chinese landscape painting, ink wash, Li Bai's poem, serene atmosphere, blooming flowers, lush trees, tranquil pond, poetic, calligraphy, trending on artstation, concept art, sharp focus, illustration.
3.4 天空之城
優化前:castle in the sky.
優化後:castle in the sky, ghibli studio, hayao miyazaki, anime, floating island, steampunk, intricate, highly detailed, digital painting, artstation, matte, sharp focus, illustration.
04 總結與展望
本文介紹了基於 ChatGPT 的 AI 繪圖提示詞生成器的實現原理,總結了大語言模型 Prompt 工程的一些實踐經驗。
我們知道,以 ChatGPT 爲代表的大語言模型在很多領域都可以發揮巨大作用。但很多時候由於沒有做好 Prompt 工程,任務需求描述得不夠準確,或者沒有提供有效的樣本,導致模型的效果不夠理想。作爲 LLM 的使用者,我們需要利用 Prompt 來充分發揮出模型的潛力,從而提高業務的落地效果。
未來隨着 LLM 和 AIGC 相關技術的發展,Prompt 工程可能會越來越簡單,甚至在不久的將來可能都不需要 Prompt 工程,屆時大語言模型已經足夠聰明,我們只需要簡單描述幾句話它就能完成複雜的任務。但至少對於目前來說,Prompt 工程還是有必要的。歡迎在評論區中交流和探討。
參考文獻
Stable Diffusion
https://github.com/CompVis/stable-diffusion
High-Resolution Image Synthesis with Latent Diffusion Models
https://arxiv.org/abs/2112.10752
Stable Diffusion WebUI Docker
https://github.com/AbdBarho/stable-diffusion-webui-docker
ChatGPT Prompt Engineering for Developers
https://learn.deeplearning.ai/chatgpt-prompt-eng
原創作者|李派銳
技術責編|鄭海兵
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/MvxS3PUG8pYobxDCAZtdcg