機械狗
城堡大海夕陽宮崎駿動畫
花落知多少
半雞半人,強壯
雞你太美
請訪問天工巧繪SkyPaint 
你也可以微信掃描二維碼,在小程序上體驗: 
SkyPaint文本生成圖片模型主要由兩大部分組成,即提示詞文本編碼器模型和擴散模型兩大部分。因此我們的優化也分為兩步: 首先,基於OpenAI-CLIP優化了提示詞文本編碼器模型使得SkyPaint具有中英文識別能力, 然後,優化了擴散模型,使得SkyPaint具有現代藝術能力可以產生高質量圖片。
模型下載地址SkyPaint-v1.0
from diffusers import StableDiffusionPipeline
device = 'cuda'
pipe = StableDiffusionPipeline . from_pretrained ( "path_to_our_model" ). to ( device )
prompts = [
'机械狗' ,
'城堡 大海 夕阳 宫崎骏动画' ,
'花落知多少' ,
'鸡你太美' ,
]
for prompt in prompts :
prompt = 'sai-v1 art, ' + prompt
image = pipe ( prompt ). images [ 0 ]
image . save ( "%s.jpg" % prompt )————————————————————————————————————————————————
SkyCLIP是我們採用一種高效的訓練中英雙語CLIP模型的方法得到的CLIP模型,該方法僅需要使用文本數據即可實現對OpenAI-CLIP模型的高效蒸餾,大幅降低了數據門檻,同時訓練所需算力要求相較於原始CLIP模型減少90%以上,方便開源社區可以進行複現/微調。該方法僅改變了OpenAI-CLIP的文本編碼器,可搭配使用OpenAI-CLIP的圖像編碼器實現圖文檢索功能。
將OpenAI-CLIP的text_encoder作為教師模型並且凍結參數,學生模型採用和教師模型同樣大小的多語言BERT模型,訓練時英文輸入通過教師模型獲取相應的t_en_hiddent_state,英文和中文分別通過學生模型獲取相應s_en_hiddent_state,s_zh_hidden_state,採用l1、l2、cos距離等構造損失函數使得學生模型的中英文hiddent_state逐漸靠近教師模型的hiddent_state。由於平行語料的中文和英文存在天然的不等長性質,為了使得平行的中文和英文盡量接近,訓練過程中我們還添加了中文解碼器,使用學生模型的中英文hiddent_state作為解碼器的hidden_state輸入,通過翻譯任務來輔助實現中文和英文的對齊目的。
目前我們主要評估了SkyCLIP在Flickr30K-CN的zero-shot表現,主要對比了若干具備中文能力的相關開源模型,為確保對比的公平性,具有多個模型尺寸的我們均選取基於OpenAI-CLIP ViT-L/14尺寸的模型,我們評估的流程參考了Chinese-CLIP所提供的評估腳本。
Flickr30K-CN Retrieval :
| Task | Text-to-Image | Image-to-Text | MR | ||||
|---|---|---|---|---|---|---|---|
| Setup | Zero-shot | Zero-shot | |||||
| Metric | R@1 | R@5 | R@10 | R@1 | R@5 | R@10 | |
| Taiyi-326M | 53.8 | 79.9 | 86.6 | 64.0 | 90.4 | 96.1 | 78.47 |
| AltCLIP | 50.7 | 75.4 | 83.1 | 73.4 | 92.8 | 96.9 | 78.72 |
| Wukong | 51.9 | 78.6 | 85.9 | 75 | 94.4 | 97.7 | 80.57 |
| R2D2 | 42.6 | 69.5 | 78.6 | 63.0 | 90.1 | 96.4 | 73.37 |
| CN-CLIP | 68.1 | 89.7 | 94.5 | 80.2 | 96.6 | 98.2 | 87.87 |
| SkyCLIP | 58.8 | 82.6 | 89.6 | 78.8 | 96.1 | 98.3 | 84.04 |
from PIL import Image
import requests
import clip
import torch
from transformers import BertTokenizer
from transformers import CLIPProcessor , CLIPModel , CLIPTextModel
import numpy as np
query_texts = [ '一个人' , '一辆汽车' , '两个男人' , '两个女人' ] # 这里是输入提示词,可以随意替换。
# 加载SkyCLIP 中英文双语 text_encoder
text_tokenizer = BertTokenizer . from_pretrained ( "./tokenizer" )
text_encoder = CLIPTextModel . from_pretrained ( "./text_encoder" ). eval ()
text = text_tokenizer ( query_texts , return_tensors = 'pt' , padding = True )[ 'input_ids' ]
url = "http://images.cocodataset.org/val2017/000000040083.jpg" #这里可以换成任意图片的url
# 加载CLIP的image encoder
clip_model = CLIPModel . from_pretrained ( "openai/clip-vit-large-patch14" )
clip_text_proj = clip_model . text_projection
processor = CLIPProcessor . from_pretrained ( "openai/clip-vit-large-patch14" )
image = processor ( images = Image . open ( requests . get ( url , stream = True ). raw ), return_tensors = "pt" )
with torch . no_grad ():
image_features = clip_model . get_image_features ( ** image )
text_features = text_encoder ( text )[ 0 ]
# sep_token对应于openai-clip的eot_token
sep_index = torch . nonzero ( text == student_tokenizer . sep_token_id )
text_features = text_features [ torch . arange ( text . shape [ 0 ]), sep_index [:, 1 ]]
# 乘text投影矩阵
text_features = clip_text_proj ( text_features )
image_features = image_features / image_features . norm ( dim = 1 , keepdim = True )
text_features = text_features / text_features . norm ( dim = 1 , keepdim = True )
# 计算余弦相似度 logit_scale是尺度系数
logit_scale = clip_model . logit_scale . exp ()
logits_per_image = logit_scale * image_features @ text_features . t ()
logits_per_text = logits_per_image . t ()
probs = logits_per_image . softmax ( dim = - 1 ). cpu (). numpy ()
print ( np . around ( probs , 3 ))我們的數據採用了篩選過的Laion數據集作為訓練數據,同時在文本前面加上了'sai-v1 art' 作為tag使模型能夠更快速的學習到我們想要的風格及質量。 預訓練模型採用了stable-diffusion-v1-5 作為預訓練,使用了16塊A100訓練了50個小時。 目前模型還在持續優化中,後續會有更穩定的模型更新。

