BeautifulPrompt:PAI 推出自研 Prompt 美化器,賦能 AIGC一鍵出美圖

背景

Stable Diffusion(SD)是一種流行的AI生成內容(AI Generated Content,AIGC)模型,能在文字輸入的基礎上生成各種風格多樣的圖像。在目前的AIGC方向,SD是開源社區最熱門的模型。然而,SD能夠生成高顏值的圖像,非常依賴於用戶提供的Prompt。如果沒有好的Prompt,SD往往無法生成用戶預期的圖像,極大的影響用戶的使用體驗。在先前的工作中,阿里雲機器學習PAI團隊在AIGC方向做了很多探索,包括PAI-Diffusion中文模型的開源、基於Blade的推理優化等,並且推出一系列行業解決方案。爲了提升SD系列模型的易用性、降低使用門檻、釋放AI模型的創造潛力,我們提出並訓練完成面向SD自動Prompt美化器,使得用戶只要輸入一個極其簡單的Prompt,就可以得到一系列經過語言模型優化過的、細節滿滿的Prompt,幫助您更簡單地生成高顏值圖像。在下文中,我們詳細介紹PAI自動Prompt生成模型的功能和背後的技術乾貨。

一鍵體驗Prompt自動生成

在詳細介紹PAI自動Prompt生成模型前,我們首先給出體驗Prompt生成效果。在以下的示例中,我們分別對比了原始Prompt和我們生成Prompt在Stable Diffusion v1.5底座模型上生成圖像的效果。對於每個Prompt,我們隨機生成兩張圖片進行對比。

 

我們也在ModelScope上構建了一個Demo以供用戶體驗(鏈接)。只要輸入一個簡單的Prompt,我們的模型就可以擴寫成一個細節描述詳細的Prompt,然後使用Stable Diffusion進行文圖生成。

Prompt美化器背後的技術

在本節中,我們詳細介紹如何訓練一個優秀的Prompt美化器。整體架構如下圖所示:

底座模型

PAI-Prompt生成模型底座基於BLOOM(BigScience Language Open-science Open-access Multilingual),由BigScience訓練並且開源。BLOOM具有Decoder-only模型架構,和GPT-3非常相似,最大具有1760億參數。在我們的模型訓練時,我們選擇了具有11億參數的BLOOM模型進行繼續訓練,其原因在於它的參數量不大,當它用於線上部署時,推理速度較快,而且訓練和推理成本都相當可控,具有很高的實用價值。

無需數據標註的SFT

由於模型的訓練需要高質量和低質量的Prompt對,這些數據一般很難直接去做標註。因此我們使用如下方法自動話地蒐集訓練數據。

1.摘要生成:首先,我們蒐集開源的高質量Prompt數據集,作爲語言模型生成的Target。在這種場景下,由於缺乏低質量的Prompt,我們可以使用ChatGPT等大模型生成Summary作爲低質量的Prompt。以下是一個Summary的例子:

Instruction: Summarize this image description in 10 words or less and ignore words like archdaily, wallpaper, highly detailed, 8k, [r/earthporn]. Check English. Ignore modifiers 'by xxx', 'with xxx' or 'in xxx'. Ignore adjective.
Input: a beautiful very detailed illustration of abandoned urbex unfinished building city nature industrial architecture architecture building spaceport by caspar david friedrich, scumm bar meadow nature synthwave, archdaily, wallpaper, highly detailed, trending on artstation.
Output: of abandoned urban building in nature.

Input: portrait painting of a lost boy by cedric peyravernay and greg ruthkowski, in the style of dishonored concept art, concept design, trending on artstation \n
Output:

2.Prompt擴展:利用低質量的Prompt,使用ChatGPT生成更高質量的Prompt。以下是一個Prompt生成的例子:

Instruction: create a detailed and creative description of the 'input'. Your response should include specific details about the colors, textures, and overall composition of the painting, as well as any unique features or elements that make it stand out.
Please provide a clear and concise response that captures the essence of the painting while also encouraging creativity and originality in your description. You may consider describing the setting or environment depicted in the painting.
Input: Digital painting of a girl with candy hat.

3.圖像標題生成:我們蒐集了高質量的圖文對,對圖像進行image captioning,生成更多可供訓練模型的Prompt。

最終,得到的數據會進行美觀值和一致性篩選,我們保留質量較高的數據用於SFT。

面向SD的強化學習優化

RLHF(Reinforcement Learning from Human Feedback)對ChatGPT等大模型的效果提升有重要的作用。在我們的應用中,我們設計了面向Stable Diffusion的強化學習算法,優化Prompt生成模型。

對於Reward Model,我們在得到圖文對數據基礎上,使用美學值評分模型來給圖片打分,並使用一個語言模型來擬合對應Pprompt->美學值評分,將此作爲我們的打分模型。此外,我們還採用最先進的強化學習算法PPO來進一步優化模型,獎勵函數使用打分模型和一致性得分加權:

reward = a * score_model(prompt) + b * consistency_model(raw_prompt, prompt)

這樣可以進一步加強我們生成Prompt的美觀性和圖文一致性。在完成了上述三階段訓練以後,我們的模型在小參數規模下(1.1B)的效果不亞於ChatGPT生成Prompt的效果,示例如下:

 

模型調用

如果想快速體驗模型效果,可以訪問我們在ModelScope社區的創空間頁面鏈接。同時,我們也在huggingface等開源社區上架了這一模型,使用接口如下:

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained('alibaba-pai/pai-bloom-1b1-text2prompt-sd')
model = AutoModelForCausalLM.from_pretrained('alibaba-pai/pai-bloom-1b1-text2prompt-sd').eval().cuda()

raw_prompt = '1 girl'
input = f'Instruction: Give a simple description of the image to generate a drawing prompt.\nInput: {raw_prompt}\nOutput:'
input_ids = tokenizer.encode(input, return_tensors='pt').cuda()

outputs = model.generate(
    input_ids,
    max_length=384,
    do_sample=True,
    temperature=1.0,
    top_k=50,
    top_p=0.95,
    repetition_penalty=1.2,
    num_return_sequences=5)

prompts = tokenizer.batch_decode(outputs[:, input_ids.size(1):], skip_special_tokens=True)
prompts = [p.strip() for p in prompts]
print(prompts)

未來展望

在這一期的工作中,我們提出並訓練完成面向SD自動Prompt美化器,使得用戶只要輸入一個極其簡單的Prompt,就可以得到一系列經過語言模型優化過的Prompt,幫助您更簡單地生成高顏值圖像。在未來,我們計劃增加這一類模型對各種類SD模型的適配,豐富PAI-AIGC的算法和產品能力。

點擊立即免費試用雲產品 開啓雲上實踐之旅!

作者:曹庭鋒、汪誠愚、吳梓恆、黃俊

原文鏈接

本文爲阿里雲原創內容,未經允許不得轉載。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章