Kakao Brain 開源的 ViT、ALIGN 和新的 COYO 圖文資料集
Kakao Brain 和 Hugging Face 激動地宣佈,共同釋出一個新的開源圖文資料集 COYO (包含 7 億個圖文對),以及兩個基於此資料集訓練的新視覺語言模型:ViT 和 ALIGN。這是 ALIGN 模型首次向公眾免費開源,也是 ViT 和 ALIGN 模型首次連同其訓練資料集一同釋出。
Kakao Brain 的 ViT 和 ALIGN 模型遵循了 Google 原始模型中提供的相同架構和超引數,但它們是在開源的 COYO 資料集上訓練的。Google 的 ViT 和 ALIGN 模型雖然在龐大的資料集上訓練 (ViT 在 3 億張圖片上訓練,ALIGN 在 18 億個圖文對上訓練),但由於這些資料集未公開,因此無法復現。這項貢獻對於那些希望在可以訪問資料的情況下復現視覺語言模型的研究人員來說尤其有價值。有關 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
模型,雖然訓練資料對少得多 (7 億對 vs 18 億對),但在 Image KNN 分類任務上與 Google 的 ALIGN-B7-Base
表現相當,在 MS-COCO 檢索的圖到文和文到圖任務上表現更優。當在模型解析度為 384 和 512 的 ImageNet 和 ImageNet-ReaL 上進行評估時,Kakao Brain 的 ViT-L/16
表現與 Google 的 ViT-L/16
相似。這意味著社群可以使用 Kakao Brain 的 ViT 和 ALIGN 模型來複現 Google 的 ViT 和 ALIGN 版本,特別是當用戶需要訪問訓練資料時。我們很高興看到這些效能與最先進水平相當的模型能夠以開源和透明的方式釋出!

COYO 資料集

這些模型釋出的特別之處在於,它們是在免費且可訪問的 COYO 資料集上訓練的。COYO 是一個包含 7 億個圖文對的資料集,類似於 Google 的 ALIGN 1.8B
圖文資料集,後者是來自網頁的“嘈雜”的 alt-text 和圖片對的集合,但 COYO 是開源的。COYO-700M
和 ALIGN 1.8B
之所以“嘈雜”,是因為只應用了最少的過濾。COYO
類似於另一個開源圖文資料集 LAION
,但有以下區別。雖然 LAION-2B
是一個更大的資料集,包含 20 億個英文圖文對,而 COYO
只有 7 億個,但 COYO
的圖文對附帶更多元資料,為使用者提供了更大的靈活性和更精細的使用控制。下表顯示了這些差異:COYO
為所有圖文對配備了美學評分、更穩健的水印評分和人臉計數資料。
COYO | LAION 2B | ALIGN 1.8B |
---|---|---|
使用 CLIP ViT-B/32 和 ViT-L/14 模型計算的圖文相似度評分,作為元資料提供,但未進行任何過濾,以避免可能的消除偏見 | 使用 CLIP (ViT-B/32) 提供的圖文相似度評分 - 僅保留閾值高於 0.28 的樣本 | 最少的、基於頻率的過濾 |
對影像和文字進行 NSFW 過濾 | 對影像進行 NSFW 過濾 | Google Cloud API |
提供人臉識別 (人臉計數) 資料作為元資料 | 無人臉識別資料 | 不適用 |
7 億個圖文對,全為英文 | 20 億個英文圖文對 | 18 億 |
來自 2020 年 10 月 - 2021 年 8 月的 CC | 來自 2014-2020 年的 CC | 不適用 |
美學評分 | 部分美學評分 | 不適用 |
更穩健的水印評分 | 水印評分 | 不適用 |
Hugging Face Hub | Hugging Face Hub | 未公開 |
英語 | 英語 | 英文? |
ViT 和 ALIGN 的工作原理
那麼這些模型是做什麼的呢?讓我們簡要討論一下 ViT 和 ALIGN 模型的工作原理。
ViT——視覺 Transformer (Vision Transformer)——是 Google 於 2020 年提出的一種視覺模型,其結構類似於文字 Transformer 架構。這是視覺領域的一種新方法,不同於自 2012 年 AlexNet 以來主導視覺任務的卷積神經網路 (CNN)。它的計算效率比效能相似的 CNN 高達四倍,並且是領域無關的。ViT 的輸入是一張影像,它被分解成一系列影像塊 (patches)——就像文字 Transformer 的輸入是一系列文字一樣——併為每個影像塊提供位置嵌入以學習影像結構。ViT 的效能尤其以其出色的效能與計算成本的權衡而著稱。雖然 Google 的一些 ViT 模型是開源的,但它們訓練所用的 JFT-300M 圖文對資料集並未公開發布。而 Kakao Brain 的 ViT 模型在公開的 COYO-Labeled-300M 上訓練,且在各種任務上表現相似,其程式碼、模型和訓練資料 (COYO-Labeled-300M) 都完全公開,以實現可復現性和開放科學。

Google 隨後在 2021 年推出了 ALIGN——一個大規模影像和噪聲文字嵌入模型——這是一個在“嘈雜”的圖文資料上訓練的視覺語言模型,用於各種視覺和跨模態任務,如圖文檢索。ALIGN 採用簡單的雙編碼器架構,在圖文對上進行訓練,透過對比損失函式進行學習。ALIGN 的“嘈雜”訓練語料庫以其規模和魯棒性的平衡而著稱。此前,視覺語言表示學習通常在帶有手動標籤的大規模資料集上進行訓練,這需要大量的預處理。ALIGN 的語料庫使用影像的 alt-text (當影像載入失敗時顯示的文字) 作為影像的標題——這導致了一個不可避免的嘈雜但規模大得多 (18 億對) 的資料集,使 ALIGN 在各種任務上都能達到最先進 (SoTA) 的水平。Kakao Brain 的 ALIGN 是該模型的第一個開源版本,在 COYO
資料集上訓練,並且效能優於 Google 報告的結果。

如何使用 COYO 資料集
我們可以使用 🤗 Datasets 庫,透過一行程式碼方便地下載 COYO
資料集。要預覽 COYO
資料集並瞭解更多關於資料整理過程和所包含的元屬性的資訊,請訪問 Hub 上的資料集頁面或原始的 Git 倉庫。要開始使用,讓我們先安裝 🤗 Datasets 庫:pip install datasets
並下載它。
>>> from datasets import load_dataset
>>> dataset = load_dataset('kakaobrain/coyo-700m')
>>> dataset
雖然 COYO
資料集比 LAION
資料集小得多,但它仍然非常龐大,有 7.47 億個圖文對,將整個資料集下載到本地可能不可行。為了只下載資料集的一個子集,我們可以簡單地在 load_dataset()
方法中傳入 streaming=True
引數來建立一個可迭代的資料集,並邊迭代邊下載資料例項。
>>> 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,它可用於檢索文字或影像的多模態嵌入,或執行零樣本影像分類。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')
我們將從零樣本影像分類開始。為此,我們將提供候選標籤 (自由形式的文字),並使用 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 模型的零樣本影像分類,請直接訪問它在 🤗 Hub 上的演示。請注意,AlignModel
的輸出包括 text_embeds
和 image_embeds
(參見 ALIGN 的文件)。如果我們不需要為零樣本分類計算每個影像和每個文字的 logits,我們可以使用 AlignModel
類的便捷方法 get_image_features()
和 get_text_features()
來檢索視覺和文字嵌入。
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 類似,我們可以使用零樣本影像分類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 這樣的模型解鎖了各種下游任務,如影像字幕、零樣本影像分類和開放詞彙目標檢測。在這篇部落格中,我們討論了由 Kakao Brain 貢獻到 Hub 的最新開源 ViT 和 ALIGN 模型,以及新的 COYO 圖文資料集。我們還展示瞭如何用幾行程式碼使用這些模型來執行各種任務,既可以單獨使用,也可以作為 🤗 Transformers pipelines 的一部分。
就是這些!我們將繼續整合最具影響力的計算機視覺和多模態模型,並期待您的反饋。要了解計算機視覺和多模態研究的最新訊息,您可以在 Twitter 上關注我們:@adirik, @a_e_roberts, @NielsRogge, @RisingSayak, 和 @huggingface。