論文:Chinese CLIP: Contrastive Vision-Language Pretraining in Chinese
代碼:https://github.com/OFA-Sys/Chinese-CLIP
出處:阿里達摩院
時間:2022.11
貢獻:
- 提出了 Chinese CLIP,是經(jīng)過在大尺度中文圖像-文本對兒的兩階段預訓練
一、背景
CLIP 的成功極大地促進了對比學習在視覺-語言模型預訓練上的研究和應用
不同于傳統(tǒng)生成式預訓練,CLIP 是一種基于對比學習的模型,在從網(wǎng)絡上收集的約 4 億個 image-text pair 上進行預訓練,而且有很好的 zero-shot 遷移能力
由于 CLIP 能夠在視覺和文本之間建立聯(lián)系,所有頁改變了多模態(tài)表達學習和計算機視覺的相關研究
在 CLIP 之后有很多的工作,如 DALL-E,StyleCLIP,SLIP,CLIP4Clip 等
盡管如此,但在中國還沒有一個專為中文多模態(tài)學習設計的模型
所以,本文中提出了 Chinese CLIP (CN-CLIP),模型在大量的中文文本-圖像對兒上進行了預訓練
數(shù)據(jù):
- 約 2 億 image-text pairs
模型大小:
- 建立了 5 個不同大小的模型,從 77M 到 958M 參數(shù)量
訓練方式:
- 使用兩階段訓練的方式
- 第一階段:凍結 image encoder
- 第二階段:image encoder 和 text encoder 都參與訓練(如圖 1)
- 消融實驗證明了兩階段訓練的好處
測試數(shù)據(jù)集:
- 在 3 個 Chinese cross-modal 檢索數(shù)據(jù)集上進行的測試
- 包括 MUGE2, Flickr30K-CN, COCO-CN
二、方法
2.1 基礎內(nèi)容
首先介紹一下 CLIP 的訓練方法:
- CLIP 是由 image encoder 和 text encoder 構成的
- image encoder:是 vision backbone,如 ResNet、ViT 等
- text encoder:是 transformer model,如 BERT、GPT 等
- CLIP 有多種不同大小的模型尺度
- vision backbone:5 個不同尺度的 resnet(ResNet-50, ResNet101, RN50x4, RN50x16, RN50x64),3 個餅圖的 ViT (ViT-B/32, ViT-B/16, and ViT-L/14)
- 模型的訓練使用的是 large-batch 的對比學習,給定一個 batch 中有 N 個圖像,則共有 N × N N \times N N×N 個可能的圖文對兒,有 N 對兒 positive samples,有 N 2 ? N N^2-N N2?N 對兒 negative samples。則共有 N 2 N^2 N2 個 similarity score,來優(yōu)化 cross-entropy loss
CLIP 的擴展:
- 可以遷移到跨模態(tài)的檢索
- image encoder 可以作為 vision backbone 來代替在 ImageNet 上預訓練的 ResNet 或 ViT
- 通過計算給定的 image 和固定形式 Candidate label (如 a photo of [label]) 的相似性,也可以進行開集的 zero-shot 分類
2.2 數(shù)據(jù)集
CLIP 成功的另外一個原因在于預訓練使用的超大尺度的訓練集
由在 CLIP 上進行的實驗可知,通過增大數(shù)據(jù)量、增長訓練周期都可以提供模型在 zero-shot 學習任務上的效果
訓練數(shù)據(jù)集:
- 從 LAION-5B 中抽取包含 ‘zh’ 的中文數(shù)據(jù),共抽取 1.08 億數(shù)據(jù)
- 從 Wukong 中獲得 0.72 億數(shù)據(jù)
- 將英文多模態(tài)數(shù)據(jù)翻譯為中文的,包括 Visual Genome 和 MSCOCO
- 最后共得到約 2 億的訓練數(shù)據(jù)
- 一般將圖像 resize 為 224x224 來使用,在 ViT-L/14 中使用 336x336 大小
2.3 預訓練方法
最簡單的訓練方法是從頭開始訓練,就是會受到訓練數(shù)據(jù)的數(shù)量和質(zhì)量的限制
為了利用現(xiàn)有的預訓練模型優(yōu)勢:
- 對于 image encoder ,使用了官方 CLIP 開源預訓練模型來初始化
- 對于 text encoder,使用 RoBERTa-wwm-ext 和 RBT3
如何基于開源模型來預訓練:
- constrastive tuning, 對比微調(diào),類似于將 CLIP 遷移到下游任務上去
- Locked-image Tuning(LiT),效果更好
Chinese CLIP 如何進行預訓練:兩階段預訓練方法,如圖 1 所示
- 主要思想是凍結 image encoder(凍結所有的參數(shù))使用 LiT 讓 text encoder 能夠從 OpenAI 的 CLIP 的基礎視覺模型中讀出高質(zhì)量的表示,然后將這些表示遷移到需要的數(shù)據(jù)域中。
- 第一階段:凍結 image encoder 的所有參數(shù),只訓練 text encoder,這一動作是基于一個假設:訓練好的 vision backbone 已經(jīng)有很強的能力來抽取視覺特征了。第一階段的訓練直到對下游任務沒有明顯的提升而結束
- 第二階段,讓 image encoder 的參數(shù)參與訓練,這樣一來 image encoder 的參數(shù)就可以學習從中文網(wǎng)站來的圖了
- 實驗結果表明兩階段的預訓練能夠比直接從訓練好的模型來 finetune 效果更好
2.4 模型尺寸
有 5 種不同的模型尺寸,從 77 ~ 958 million 參數(shù)量,無特殊說明情況下預訓練圖像大小為 224x224
- ResNet 50 model: C N ? C L I P R N 50 CN-CLIP_{RN50} CN?CLIPRN50?
- C N ? C L I P V i T ? B / 16 CN-CLIP_{ViT-B/16} CN?CLIPViT?B/16?
- C N ? C L I P V i T ? L / 14 CN-CLIP_{ViT-L/14} CN?CLIPViT?L/14?
- C N ? C L I P V i T ? L / 14 @ 336 p x CN-CLIP_{ViT-L/14@336px} CN?CLIPViT?L/14@336px?
- C N ? C L I P V i T ? H / 14 CN-CLIP_{ViT-H/14} CN?CLIPViT?H/14?
三、效果
數(shù)據(jù)集:
- MUGE-Retrieval:image-text 檢索數(shù)據(jù)集
- Flickr30K-CN:Flickr30K 數(shù)據(jù)集且將標簽翻譯為中文
- COCOCN:COCO 數(shù)據(jù)集且將標簽翻譯為中文
評價指標:
- zero-shot learning:使用 image 和 text 之間的 smilarity score 來衡量,使用 top-k similar candidates
- finetuning:使用 Recall@K 來評價,K={1, 5, 10},也會使用 Mean Recall (MR)來評價,即 Recall@K 的均值
四、代碼
4.1 推理
官方 github 中給了一段推理代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-547592.html
import torch
from PIL import Image
import cn_clip.clip as clip
from cn_clip.clip import load_from_name, available_models
print("Available models:", available_models())
# Available models: ['ViT-B-16', 'ViT-L-14', 'ViT-L-14-336', 'ViT-H-14', 'RN50']
device = "cuda" if torch.cuda.is_available() else "cpu"
# model 選擇和 預處理
# 預處理:resize to (224,224), convert to rgb, normalize (mean, std)
model, preprocess = load_from_name("ViT-B-16", device=device, download_root='./')
model.eval()
image = preprocess(Image.open("examples/pokemon.jpeg")).unsqueeze(0).to(device)
# image 是經(jīng)過預處理后的圖,大小為 [1, 3, 224, 224]
text = clip.tokenize(["杰尼龜", "妙蛙種子", "小火龍", "皮卡丘"]).to(device)
# clip.tokenize 是對類別 list 進行 token 處理,這里有 4 個類別,得到的就是 [4, 52] 的向量表示
with torch.no_grad():
logits_per_image, logits_per_text = model.get_similarity(image, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
print("Label probs:", probs) # [[1.268734e-03 5.436878e-02 6.795761e-04 9.436829e-01]]
這里面涉及到了相似度提?。?code>model.get_similarity,位于 model.py
中文章來源地址http://www.zghlxwxcb.cn/news/detail-547592.html
def get_similarity(self, image, text):
image_features = self.encode_image(image) # Vision Transformer, image_features.shape=[1, 512]
text_features = self.encode_text(text) # BERT, image_features.shape=[4, 512]
# normalized features
image_features = image_features / image_features.norm(dim=1, keepdim=True)
text_features = text_features / text_features.norm(dim=1, keepdim=True)
# cosine similarity as logits
logit_scale = self.logit_scale.exp()
logits_per_image = logit_scale * image_features @ text_features.t() # [1, 4] tensor
logits_per_text = logits_per_image.t() # [4, 1] tensor
# shape = [global_batch_size, global_batch_size]
# logits_per_image: 每個類別對應的概率
return logits_per_image, logits_per_text
到了這里,關于【AIGC】10、Chinese CLIP | 專為中文圖文匹配設計的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!