Kakao Brain 發佈 ViT、ALIGN 模型及包含 700 million 對圖文的 COYO 資料集
New ViT and ALIGN Models From Kakao Brain
Kakao Brain 與 Hugging Face 公開 ViT、ALIGN 視覺語言模型,以及由 700 million 對圖文構成的開源 COYO 資料集。
ViT 與 ALIGN 連同 COYO 訓練資料一併開放,並提供與 Google 版本的任務比較,呈現資料可取得性對視覺語言研究復現的價值。
Kakao Brain 和 Hugging Face 很高興推出全新的開源圖文資料集 COYO,當中包含 700 million 對資料;並推出兩款以此訓練的全新視覺語言模型:ViT 和 ALIGN。這是 ALIGN 模型首次免費公開作開源用途,亦是 ViT 和 ALIGN 模型首次連同訓練資料集一併發佈。
Kakao Brain 的 ViT 和 ALIGN 模型沿用 Google 對應模型的架構和超參數,但使用開源 COYO 資料集訓練。Google 的 ViT 和 ALIGN 模型雖然使用大型資料集訓練(ViT 使用 300 million 張圖片,ALIGN 則使用 1.8 billion 對圖文),但由於資料集並未公開,因此無法複製其訓練流程。這項成果對希望取得資料、重現視覺語言模型研究的研究人員尤其有價值。關於 Kakao ViT 和 ALIGN 模型的詳情,請按此查看。
本文會介紹全新的 COYO 資料集、Kakao Brain 的 ViT 和 ALIGN 模型,以及如何使用它們!主要重點如下:
- 首款開源 ALIGN 模型!
- 首批以開源資料集 COYO 訓練的開放式 ViT 和 ALIGN 模型
- Kakao Brain 的 ViT 和 ALIGN 模型表現媲美 Google 版本
- ViT 和 ALIGN 示範已在 HF 上線!你可以在線上使用自己選擇的圖片樣本試用 ViT 和 ALIGN 示範!
效能比較
Kakao Brain 發佈的 ViT 和 ALIGN 模型表現媲美、部分情況甚至勝過 Google 公佈的實作結果。Kakao Brain 的 ALIGN-B7-Base 模型使用較少配對訓練(700 million 對,相比之下為 1.8 billion 對),但在 Image KNN 分類任務上表現媲美 Google 的 ALIGN-B7-Base,而在 MS-COCO 圖片至文字和文字至圖片檢索任務上表現更佳。Kakao Brain 的 ViT-L/16 在 ImageNet 和 ImageNet-ReaL 上,以 384 和 512 的模型解析度評估時,表現亦與 Google 的 ViT-L/16 相若。這表示社羣可使用 Kakao Brain 的 ViT 和 ALIGN 模型重現 Google 發佈的模型,尤其適合需要取得訓練資料的使用者。我們期待看到這些模型以開源和透明方式發佈,並達到媲美最先進模型的效能!

COYO 資料集

這些模型發佈的一項特別之處,是模型使用免費且容易取得的 COYO 資料集訓練。COYO 是一個包含 700 million 對圖文的資料集,與 Google 的 ALIGN 1.8B 圖文資料集相似,兩者均收集網頁上的「雜訊」替代文字與圖片配對,但 COYO 以開源方式發佈。COYO-700M 和 ALIGN 1.8B 之所以稱為「雜訊」資料,是因為只進行了最低限度的篩選。COYO 與另一個開源圖文資料集 LAION 相似,但有以下差異。LAION 2B 是規模大得多的資料集,包含 2 billion 對英文資料,相較之下,COYO 有 700 million 對;不過,COYO 配對包含更多 metadata,讓使用者更靈活並可更精細地控制使用方式。下表列出兩者差異:COYO 為所有配對提供美學分數、更完善的浮水印分數,以及人臉數量資料。
| COYO | LAION 2B | ALIGN 1.8B |
|---|---|---|
| 使用 CLIP ViT-B/32 和 ViT-L/14 模型計算的圖文相似度分數,作為 metadata 提供;為避免可能出現的篩選偏差,不會據此剔除任何資料 | 以 CLIP(ViT-B/32)提供圖文相似度分數,只保留高於 0.28 閾值的範例 | 最低限度、按出現頻率篩選 |
| 篩除圖片和文字中的 NSFW 內容 | 篩除圖片中的 NSFW 內容 | Google Cloud API |
| 提供作為 metadata 的人臉識別(人臉數量)資料 | 沒有提供人臉識別資料 | NA |
| 700 million 對,全為英文 | 2 billion 對英文資料 | 1.8 billion |
| 來自 2020 年 10 月至 2021 年 8 月的 CC | 來自 2014 至 2020 年的 CC | NA |
| 美學分數 | 部分美學分數 | NA |
| 更完善的浮水印分數 | 浮水印分數 | NA |
| Hugging Face Hub | Hugging Face Hub | 未公開 |
| 英文 | 英文 | 英文? |
ViT 和 ALIGN 的運作方式
這些模型有甚麼用途?以下簡單介紹 ViT 和 ALIGN 模型的運作方式。
ViT(Vision Transformer,視覺 Transformer)是 Google 於 2020 年提出的視覺模型,架構與文字 Transformer 相似。 這是有別於視覺卷積神經網絡(CNN)的全新視覺方法;自 2012 年 AlexNet 出現以來,CNN 一直主導視覺任務。ViT 的運算效率比效能相若的 CNN 高最多四倍,而且不受領域限制。ViT 會將圖片切分成一連串圖片區塊作為輸入,就像文字 Transformer 接收一連串文字一樣,並為每個區塊加入位置嵌入,以學習圖片結構。ViT 尤其以出色的效能與運算量平衡見稱。Google 的部分 ViT 模型雖然已開源,但它們使用的 JFT-300 million 圖片標籤配對資料集並未公開。Kakao Brain 使用已公開發佈的 COYO-Labeled-300M 訓練模型;所發佈的 ViT 模型在多項任務上的表現相若,而且其程式碼、模型和訓練資料(COYO-Labeled-300M)全部公開,方便重現研究並推動開放科學。
ViT 運作方式示意圖,取自 Google Blog
Google 隨後在 2021 年推出 ALIGN,即大型圖像與雜訊文字嵌入模型。這是一款以「雜訊」文字圖像資料訓練的視覺語言模型,可用於圖片與文字檢索等多種視覺和跨模態任務。ALIGN 採用簡單的雙編碼器架構,以圖文配對訓練,並透過對比損失函數學習。ALIGN 的「雜訊」訓練語料值得留意之處,是兼顧規模和穩健性。以往,視覺語言表徵學習使用大規模人工標註資料集訓練,需要大量預處理。ALIGN 的語料使用圖片的替代文字(即圖片無法載入時顯示的文字)作為圖片説明,因此資料必然帶有雜訊,但規模更大(1.8 billion 對),令 ALIGN 在多項任務中達到最先進水平。Kakao Brain 的 ALIGN 是此模型首個開源版本,使用 COYO 資料集訓練,表現勝過 Google 公佈的結果。
ALIGN 模型,取自 Google Blog
如何使用 COYO 資料集
使用 🤗 Datasets 函式庫,只需一行程式碼便可輕鬆下載 COYO 資料集。若要預覽 COYO 資料集,並瞭解更多資料整理流程和包含的 metadata 屬性,請前往 Hub 資料集頁面或原始 Git 程式碼庫。開始前,先安裝 🤗 Datasets 函式庫:pip install datasets,然後下載資料集。
>>> from datasets import load_dataset
>>> dataset = load_dataset('kakaobrain/coyo-700m')
>>> dataset
雖然規模遠小於 LAION,COYO 資料集仍然十分龐大,包含 747M 對圖文,可能無法將整個資料集下載至本機。若只想下載部分資料集,只需傳入 streaming=True 參數至 load_dataset() 方法,建立可迭代資料集,並在處理過程中逐筆下載資料即可。
>>> from datasets import load_dataset
>>> dataset = load_dataset('kakaobrain/coyo-700m', streaming=True)
>>> print(next(iter(dataset['train'])))
{'id': 2680060225205, 'url': 'https://cdn.shopify.com/s/files/1/0286/3900/2698/products/TVN_Huile-olive-infuse-et-s-227x300_e9a90ffd-b6d2-4118-95a1-29a5c7a05a49_800x.jpg?v=1616684087', 'text': 'Olive oil infused with Tuscany herbs', 'width': 227, 'height': 300, 'image_phash': '9f91e133b1924e4e', 'text_length': 36, 'word_count': 6, 'num_tokens_bert': 6, 'num_tokens_gpt': 9, 'num_faces': 0, 'clip_similarity_vitb32': 0.19921875, 'clip_similarity_vitl14': 0.147216796875, 'nsfw_score_opennsfw2': 0.0058441162109375, 'nsfw_score_gantman': 0.018961310386657715, 'watermark_score': 0.11015450954437256, 'aesthetic_score_laion_v2': 4.871710777282715}
如何從 Hub 使用 ViT 和 ALIGN
現在來試用全新的 ViT 和 ALIGN 模型。由於 ALIGN 剛加入 🤗 Transformers,我們會安裝最新版本函式庫:pip install -q git+https://github.com/huggingface/transformers.git,然後匯入所需模組和函式庫,開始使用 ViT 進行圖片分類。請注意,新加入的 ALIGN 模型會在函式庫下一個版本納入 PyPI 套件。
import requests
from PIL import Image
import torch
from transformers import ViTImageProcessor, ViTForImageClassification
接着,我們會從 COCO 資料集下載一張沙發上有兩隻貓和遙控器的隨機圖片,並預處理圖片,轉換成模型預期的輸入格式。為此,可使用相應的預處理器類別(ViTProcessor)。初始化模型和預處理器時,我們會使用 Hub 上其中一個 Kakao Brain ViT 儲存庫。請注意,從儲存庫初始化預處理器,可確保預處理後的圖片符合該特定預訓練模型所需格式。
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
processor = ViTImageProcessor.from_pretrained('kakaobrain/vit-large-patch16-384')
model = ViTForImageClassification.from_pretrained('kakaobrain/vit-large-patch16-384')
其餘步驟很簡單:我們會先預處理圖片,再將它作為輸入傳入模型,以取得類別 logits。Kakao Brain ViT 圖片分類模型使用 ImageNet 標籤訓練,輸出的 logits 形狀為 (batch_size, 1000)。
# preprocess image or list of images
inputs = processor(images=image, return_tensors="pt")
# inference
with torch.no_grad():
outputs = model(**inputs)
# apply SoftMax to logits to compute the probability of each class
preds = torch.nn.functional.softmax(outputs.logits, dim=-1)
# print the top 5 class predictions and their probabilities
top_class_preds = torch.argsort(preds, descending=True)[0, :5]
for c in top_class_preds:
print(f"{model.config.id2label[c.item()]} with probability {round(preds[0, c.item()].item(), 4)}")
完成了!若想更方便快捷,亦可使用圖片分類pipeline,並將 Kakao Brain ViT 儲存庫名稱作為目標模型,以初始化 pipeline。之後可傳入圖片 URL、本機路徑或 Pillow 圖片,並可選擇使用 top_k 參數,傳回排名最高的 k 項預測。現在就為貓和遙控器的圖片取得排名最高的 5 項預測吧。
>>> from transformers import pipeline
>>> classifier = pipeline(task='image-classification', model='kakaobrain/vit-large-patch16-384')
>>> classifier('http://images.cocodataset.org/val2017/000000039769.jpg', top_k=5)
[{'score': 0.8223727941513062, 'label': 'remote control, remote'}, {'score': 0.06580372154712677, 'label': 'tabby, tabby cat'}, {'score': 0.0655883178114891, 'label': 'tiger cat'}, {'score': 0.0388941615819931, 'label': 'Egyptian cat'}, {'score': 0.0011215205304324627, 'label': 'lynx, catamount'}]
如想進一步試用 Kakao Brain ViT 模型,請前往 🤗 Hub 上的Space。

接下來試用 ALIGN。它可用來取得文字或圖片的多模態嵌入,亦可進行 zero-shot 圖片分類。ALIGN 在 transformers 中的實作和用法與 CLIP 相似。首先下載預訓練模型及其處理器;處理器可預處理圖片和文字,確保它們符合 ALIGN 視覺與文字編碼器所需的格式。現在再次匯入所需模組,並初始化預處理器和模型。
import requests
from PIL import Image
import torch
from transformers import AlignProcessor, AlignModel
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
processor = AlignProcessor.from_pretrained('kakaobrain/align-base')
model = AlignModel.from_pretrained('kakaobrain/align-base')
我們先進行 zero-shot 圖片分類。方法是提供候選標籤(自由格式文字),再使用 AlignModel 判斷哪段描述更符合圖片。首先預處理圖片和文字輸入,然後將預處理後的輸入傳入 AlignModel。
candidate_labels = ['an image of a cat', 'an image of a dog']
inputs = processor(images=image, text=candidate_labels, return_tensors='pt')
with torch.no_grad():
outputs = model(**inputs)
# this is the image-text similarity score
logits_per_image = outputs.logits_per_image
# we can take the softmax to get the label probabilities
probs = logits_per_image.softmax(dim=1)
print(probs)
就是這麼簡單!若想進一步試用 Kakao Brain ALIGN 模型進行 zero-shot 圖片分類,只需前往 🤗 Hub 上的示範。請注意,AlignModel 的輸出包括 text_embeds 和 image_embeds(請參閲 ALIGN 的文件)。如果 zero-shot 分類不需要計算每張圖片和每段文字的 logits,可使用方便的 get_image_features() 和 get_text_features() 方法,從 AlignModel 類別取得視覺和文字嵌入。
text_embeds = model.get_text_features(
input_ids=inputs['input_ids'],
attention_mask=inputs['attention_mask'],
token_type_ids=inputs['token_type_ids'],
)
image_embeds = model.get_image_features(
pixel_values=inputs['pixel_values'],
)
另一種方法是使用 ALIGN 獨立的視覺和文字編碼器取得多模態嵌入。這些嵌入可用於訓練多種下游任務的模型,例如物件偵測、圖片分割和圖片説明生成。以下示範如何使用 AlignTextModel 和 AlignVisionModel 取得嵌入。請注意,可使用方便的 AlignProcessor 類別,分別預處理文字和圖片。
from transformers import AlignTextModel
processor = AlignProcessor.from_pretrained('kakaobrain/align-base')
model = AlignTextModel.from_pretrained('kakaobrain/align-base')
# get embeddings of two text queries
inputs = processor(['an image of a cat', 'an image of a dog'], return_tensors='pt')
with torch.no_grad():
outputs = model(**inputs)
# get the last hidden state and the final pooled output
last_hidden_state = outputs.last_hidden_state
pooled_output = outputs.pooler_output
推理時將 output_hidden_states 和 output_attentions 參數設為 True,亦可選擇傳回所有隱藏狀態和注意力值。
with torch.no_grad():
outputs = model(**inputs, output_hidden_states=True, output_attentions=True)
# print what information is returned
for key, value in outputs.items():
print(key)
我們亦會使用 AlignVisionModel 執行相同步驟,取得圖片的多模態嵌入。
from transformers import AlignVisionModel
processor = AlignProcessor.from_pretrained('kakaobrain/align-base')
model = AlignVisionModel.from_pretrained('kakaobrain/align-base')
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(images=image, return_tensors='pt')
with torch.no_grad():
outputs = model(**inputs)
# print the last hidden state and the final pooled output
last_hidden_state = outputs.last_hidden_state
pooled_output = outputs.pooler_output
與 ViT 相似,我們可使用 zero-shot 圖片分類pipeline,讓操作更簡單。以下示範如何使用此 pipeline,以自由格式文字作候選標籤,在真實環境中進行圖片分類。
>>> from transformers import pipeline
>>> classifier = pipeline(task='zero-shot-image-classification', model='kakaobrain/align-base')
>>> classifier(
... 'https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png',
... candidate_labels=['animals', 'humans', 'landscape'],
... )
[{'score': 0.9263709783554077, 'label': 'animals'}, {'score': 0.07163811475038528, 'label': 'humans'}, {'score': 0.0019908479880541563, 'label': 'landscape'}]
>>> classifier(
... 'https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png',
... candidate_labels=['black and white', 'photorealist', 'painting'],
... )
[{'score': 0.9735308885574341, 'label': 'black and white'}, {'score': 0.025493400171399117, 'label': 'photorealist'}, {'score': 0.0009757201769389212, 'label': 'painting'}]
總結
近年多模態模型取得重大進展,CLIP 和 ALIGN 等模型解鎖了圖片説明生成、zero-shot 圖片分類和開放詞彙物件偵測等多種下游任務。本文介紹 Kakao Brain 貢獻至 Hub 的最新開源 ViT 和 ALIGN 模型,以及全新的 COYO 圖文資料集。我們亦示範如何透過幾行程式碼,獨立使用這些模型或配合 🤗 Transformers pipelines 執行多種任務。
以上就是全部內容!我們會繼續整合最具影響力的電腦視覺和多模態模型,期待收到你的意見。如想掌握電腦視覺和多模態研究最新消息,可在 Twitter 關注我們:@adirik、@a_e_roberts、@NielsRogge、@RisingSayak 和 @huggingface。
來源:Hugging Face Blog · huggingface.co