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。