跳到正文
原文
Hugging Face Blog·· 2023-03-06精选AI 评分65

Kakao Brain 发布 ViT 与 ALIGN 模型及 7 亿对图文数据集 COYO

New ViT and ALIGN Models From Kakao Brain

AI 导读

Kakao Brain 与 Hugging Face 发布开源图文数据集 COYO(7 亿对)以及基于该数据集训练的 ViT 和 ALIGN 两个视觉语言模型,这是 ALIGN 首次免费开源,也是 ViT、ALIGN 首次附带公开训练数据发布。

推荐理由

首个开源 ALIGN 模型连同 700M 训练数据集一并放出,读者可据此判断复现视觉语言建模的可行性。

正文 · AI 翻译

Kakao Brain 和 HF Mirror 激动地发布了一个新的开源图文数据集 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 检索的图像到文本、文本到图像任务上表现更优。Kakao Brain 的 ViT-L/16 在模型分辨率 384 和 512 下于 ImageNet 和 ImageNet-ReaL 上评估时,与 Google 的 ViT-L/16 表现相似。这意味着社区可以使用 Kakao Brain 的 ViT 和 ALIGN 模型来复现 Google 的 ViT 和 ALIGN 发布,尤其当用户需要访问训练数据时。我们很高兴看到这些性能与最先进水平相当的开源且透明的模型发布!

ViT and ALIGN performance

COYO DATASET

COYO samples

这些模型发布的特别之处在于,它们是在免费且可访问的 COYO 数据集上训练的。COYO 是一个包含 7 亿对数据的图文数据集,类似于 Google 的 ALIGN 1.8B 图文数据集,后者是从网页收集的“嘈杂”替代文本和图像对的集合,但 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 亿
来自 CC 2020 年 10 月 - 2021 年 8 月 来自 CC 2014-2020 不适用
美学分数 部分美学分数 不适用
更稳健的水印分数 水印分数 不适用
HF Mirror Hub HF Mirror Hub 未公开
英文 英文 英文?

ViT 和 ALIGN 如何工作

那么这些模型究竟做了什么?让我们简要讨论一下 ViT 和 ALIGN 模型的工作原理。

ViT——即视觉 Transformer——是 Google 于 2020 年提出的一种视觉模型,其结构与文本 Transformer 架构相似。 这是一种全新的视觉方法,与自 2012 年 AlexNet 以来主导视觉任务的卷积神经网络(CNN)截然不同。它的计算效率比性能相近的 CNN 高出多达四倍,并且与领域无关。ViT 以一张图像作为输入,将其拆分为一系列图像块——正如文本 Transformer 以一系列文本作为输入一样——并为每个图像块赋予位置嵌入,以学习图像结构。ViT 的性能尤为突出之处在于其出色的性能与计算量之间的权衡。虽然 Google 的一些 ViT 模型是开源的,但其训练所用的 JFT-3 亿图像-标签对数据集并未公开发布。而 Kakao Brain 的模型在已公开发布的 COYO-Labeled-300M 上训练,其发布的 ViT 模型在各种任务上表现相近,其代码、模型和训练数据(COYO-Labeled-300M)完全公开,以保证可复现性和开放科学。

ViT architecture ViT 工作原理的可视化,来自 Google 博客

Google 随后推出了 ALIGN——即 2021 年提出的大规模图像与噪声文本嵌入模型——一种在“噪声”文本-图像数据上训练的视觉-语言模型,适用于图文检索等各种视觉和跨模态任务。ALIGN 采用简单的双编码器架构,在图像和文本对上训练,通过对比损失函数学习。ALIGN 的“噪声”训练语料库以平衡规模与稳健性而著称。此前,视觉语言表示学习一直是在带有手动标签的大规模数据集上训练的,这需要大量的预处理工作。ALIGN 的语料库使用图像 alt 文本(即图像加载失败时显示的文本)作为图像的说明文字——这不可避免地带来了噪声,但数据集规模大得多(18 亿对),使 ALIGN 能够在各种任务上达到 SoTA 水平。Kakao Brain 的 ALIGN 是该模型的首个开源版本,在 COYO 数据集上训练,表现优于 Google 报告的结果。

ALIGN 模型,来自 Google 博客

如何使用 COYO 数据集

我们可以使用 🤗 Datasets 库通过一行代码便捷地下载 COYO 数据集。要预览 COYO 数据集并了解更多关于数据整理过程以及所包含的元属性,请前往 hub 上的数据集页面或原始 Git 仓库。首先,让我们安装 🤗 Datasets 库:pip install datasets 并下载它。

>>> from datasets import load_dataset

>>> dataset = load_dataset('kakaobrain/coyo-700m')
>>> dataset

虽然它比 LAION 数据集小得多,但 COYO 数据集仍然非常庞大,拥有 7.47 亿对图像-文本对,将整个数据集下载到本地可能是不可行的。为了仅下载数据集的一个子集,我们可以简单地将 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 标签上训练的,并输出形状为 (batch_size, 1000) 的 logits。

# 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。

vit performance

我们继续来试验 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 上的 demo。请注意,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://hf-mirror.com/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://hf-mirror.com/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。

来源:Hugging Face Blog · hf-mirror.com