conyzhang commited on
Commit
b0eef17
1 Parent(s): 791b5b3

Upload New_ViT_and_ALIGN_Models_From_Kakao_Brain_cn.md

Browse files

![align-architecture.png](https://s3.amazonaws.com/moonup/production/uploads/1678324149262-640816c18dca6cec91cacc42.png)
![coyo-samples.png](https://s3.amazonaws.com/moonup/production/uploads/1678324151809-640816c18dca6cec91cacc42.png)
![vit_demo.png](https://s3.amazonaws.com/moonup/production/uploads/1678324149619-640816c18dca6cec91cacc42.png)
![vit-align-performance.png](https://s3.amazonaws.com/moonup/production/uploads/1678324148590-640816c18dca6cec91cacc42.png)
![vit-architecture.gif](https://s3.amazonaws.com/moonup/production/uploads/1678324155638-640816c18dca6cec91cacc42.gif)

New_ViT_and_ALIGN_Models_From_Kakao_Brain_cn.md ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # New ViT and ALIGN Models From Kakao Brain
2
+
3
+ Created by: cony zhang
4
+ Created time: March 7, 2023 7:08 AM
5
+ Last edited by: cony zhang
6
+ Last edited time: March 9, 2023 9:01 AM
7
+ URL: https://huggingface.co/blog/vit-align
8
+
9
+ # **Kakao Brain 发布开源 ViT and ALIGN模型和数据**
10
+
11
+ 最近 Kakao Brain 在 Hugging Face 发布了一个全新的开源图像文本数据集 COYO,包含7亿对图像和文本,并训练了两个新的视觉语言模型 ViT 和 ALIGN [ViT](https://github.com/kakaobrain/coyo-vit)和[ALIGN](https://github.com/kakaobrain/coyo-align)。
12
+
13
+ 这是 ALIGN 模型首次公开发布供开源使用,同时 ViT 和 ALIGN 模型的发布都附带有训练数据集。
14
+
15
+ Google 的 ViT 和 ALIGN 模型都使用了巨大的数据集(ViT 训练于 3 亿张图像,ALIGN 训练于 18 亿个图像-文本对)进行训练,因为数据集不公开导致无法复现。Kakao Brain 的 ViT 和 ALIGN 模型采用与 Google 原始模型相同的架构和超参数,不同的是其在开源 [COYO](https://github.com/kakaobrain/coyo-dataset) 数据集上进行训练。对于想要拥有数据并复现视觉语言模型的研究人员有很大的价值。详细的 Kakao ViT 和 ALIGN 模型信息可以参照:
16
+
17
+ [`https://huggingface.co/kakaobrain`](https://huggingface.co/kakaobrain)
18
+
19
+ 这篇博客将介绍新的 [COYO](https://github.com/kakaobrain/coyo-dataset) 数据集、Kakao Brain 的 ViT 和 ALIGN 模型,以及如何使用它们!以下是主要要点:
20
+
21
+ - 第一个开源的 ALIGN 模型!
22
+ - 第一个在开源数据集 [COYO](https://github.com/kakaobrain/coyo-dataset) 上训练的开源 ViT 和 ALIGN 模型。
23
+ - Kakao Brain 的 ViT和 ALIGN 模型表现与 Google 版本相当。
24
+ - ViT模型在HF上可演示!您可以使用自己的图像样本在线体验ViT!
25
+
26
+ ## 性能比较
27
+
28
+ Kakao Brain 发布的 ViT 和 ALIGN 模型与 Google 的模型表现相当,某些方面甚至更好。Kakao Brain 的 ALIGN-B7-Base 模型虽然训练的数据对少得多( 7 亿VS 1.8 亿),但在图像 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 ,尤其是当用户需要训练数据时。所以我们很高兴开源这些与现有技术相当的模型!
29
+
30
+ ![New%20ViT%20and%20ALIGN%20Models%20From%20Kakao%20Brain%2096dc66c155824d38814f76b7e882ad2f/vit-align-performance.png](New%20ViT%20and%20ALIGN%20Models%20From%20Kakao%20Brain%2096dc66c155824d38814f76b7e882ad2f/vit-align-performance.png)
31
+
32
+ ## COYO 数据集
33
+
34
+ ![New%20ViT%20and%20ALIGN%20Models%20From%20Kakao%20Brain%2096dc66c155824d38814f76b7e882ad2f/coyo-samples.png](New%20ViT%20and%20ALIGN%20Models%20From%20Kakao%20Brain%2096dc66c155824d38814f76b7e882ad2f/coyo-samples.png)
35
+
36
+ 本次发布的模型特别之处在于都是基于开源的 COYO 数据集训练的。[COYO](https://github.com/kakaobrain/coyo-dataset#dataset-preview) 数据集包含7亿图像-文本对,类似于 Google 的`ALIGN 1.8B`图像-文本数据集,是从网页上收集的“嘈杂”的html文本(alt-text)和图像对。`COYO-700M`和`ALIGN 1.8B`都是“嘈杂”的,只使用了适当的清洗处理。`COYO`类似于另一个开源的图像–文本数据集`LAION`,但有一些区别。尽管`LAION 2B`是一个更大的数据集,包含20亿个英语配对,但`COYO`的附带有更多元数据,为用户提供更多灵活性和更细粒度的使用。以下表格显示了它们之间的区别:`COYO`所有数据对都提供了美感评分,更健壮的水印评分和面部计数信息(face count data)。
37
+
38
+ | COYO | LAION 2B | ALIGN 1.8B |
39
+ | --- | --- | --- |
40
+ | Image-text similarity score calculated with CLIP ViT-B/32 and ViT-L/14 models, they are provided as metadata but nothing is filtered out so as to avoid possible elimination bias | Image-text similarity score provided with CLIP (ViT-B/32) - only examples above threshold 0.28 | Minimal, Frequency based filtering |
41
+ | NSFW filtering on images and text | NSFW filtering on images | Google Cloud API |
42
+ | Face recognition (face count) data provided as meta-data | No face recognition data | NA |
43
+ | 700 million pairs all English | 2 billion English | 1.8 billion |
44
+ | From CC 2020 Oct - 2021 Aug | From CC 2014-2020 | NA |
45
+ | Aesthetic Score | Aesthetic Score Partial | NA |
46
+ | More robust Watermark score | Watermark Score | NA |
47
+ | Hugging Face Hub | Hugging Face Hub | Not made public |
48
+ | English | English | English? |
49
+
50
+ ## ViT和ALIGN是如何工作的
51
+
52
+ 这些模型是干什么的?让我们简要讨论一下 ViT和ALIGN模型的工作原理。
53
+
54
+ ViT—Vision Transformer 是谷歌于 2020 年提出的一种视觉模型,类似于文本Transformer架构。这是一种与卷积神经网络不同的视觉方法(AlexNet自2012年以来一直主导视觉任务)。同样表��下,它的计算效率比CNN高达四倍,且具有域不可知性(domain agnostic)。ViT将输入的图像分解成一系列图像块(patch),就像文本 Transformer 输入文本序列一样,然后为每个块提供位置嵌入以学习图像结构。ViT 的性能尤其在于具有出色的性能-计算权衡。谷歌的一些ViT模型是开源的,但其训练使用的JFT-300百万图像-标签对数据集尚未公开发布。Kakao Brain的训练模型是基于公开发布的COYO-Labeled-300M进行训练,对应的ViT模型在各种任务上具有相似表现,其代码、模型和训练数据(COYO-Labeled-300M)完全公开,以便能够进行复现和科学研究。
55
+
56
+ ![New%20ViT%20and%20ALIGN%20Models%20From%20Kakao%20Brain%2096dc66c155824d38814f76b7e882ad2f/vit-architecture.gif](New%20ViT%20and%20ALIGN%20Models%20From%20Kakao%20Brain%2096dc66c155824d38814f76b7e882ad2f/vit-architecture.gif)
57
+
58
+ 谷歌在2021年推出了 ALIGN,它是一种基于“嘈杂”文本–图像数据训练的视觉语言模型,可用于各种视觉和跨模态任务,如文本-图像检索。ALIGN 采用简单的双编码器架构,通过对比损失函数学习图像和文本对,ALIGN 的“嘈杂”训练语料特点包括用语料规模弥补其噪音以及强大的鲁棒性。之前的视觉语言表示学习都是在手动标注的大规模数据集上进行训练,这就需要大量的预先处理和成本。ALIGN 的语料库使用HTML文本(alt-text)数据作为图像的描述,导致数据集不可避免地嘈杂,但更大的数据量(18亿对)使ALIGN能够在各种任务上表现出SoTA水平。Kakao Brain的模型是第一个ALIGN开源版本,它在 COYO 数据集上训练,表现比谷歌的结果更好。
59
+
60
+ ![New%20ViT%20and%20ALIGN%20Models%20From%20Kakao%20Brain%2096dc66c155824d38814f76b7e882ad2f/align-architecture.png](New%20ViT%20and%20ALIGN%20Models%20From%20Kakao%20Brain%2096dc66c155824d38814f76b7e882ad2f/align-architecture.png)
61
+
62
+ ## 如何使用COYO数据集
63
+
64
+ 我们可以使用 HuggingFace🤗数据集库的一行代码方便地下载 COYO 数据集。要预览COYO数据集并了解数据处理过程和包含的元属性,请前往hub数据集页面。
65
+
66
+ [`https://huggingface.co/datasets/kakaobrain/coyo-700m`](https://huggingface.co/datasets/kakaobrain/coyo-700m)
67
+
68
+ 开始前,请安装HuggingFace🤗数据集库:`pip install datasets`,然后下载数据集。
69
+
70
+ ```python
71
+ from datasets import load_dataset
72
+
73
+ dataset = load_dataset('kakaobrain/coyo-700m')
74
+ dataset
75
+ ```
76
+
77
+ 由于 COYO 数据集非常庞大,包含747M个图像-文本对,您可能无法在本地下载整个数据集。或者可能只需要下载和使用数据集的子集。为此,可以简单地将`streaming=True`参数传递给`load_dataset()`方法,以创建可迭代数据集,并在需要时下载数据实例。
78
+
79
+ ```python
80
+ from datasets import load_dataset
81
+
82
+ dataset = load_dataset('kakaobrain/coyo-700m', streaming=True)
83
+ print(next(iter(dataset['train'])))
84
+ {'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}
85
+ ```
86
+
87
+ ## 如何使用 Hub 中的 ViT 和 ALIGN
88
+
89
+ 让我们尝试一下新的 ViT 和 ALIGN 模型。由于 ALIGN 是新加入 HuggingFace🤗 Transformers 的,我们先安装最新版本的库:`pip install -q git+https://github.com/huggingface/transformers.git`然后导入我们将要使用的模块和库,开始使用 ViT 进行图像分类。请注意,新添加的 ALIGN 模型将会包含到下一版 PyPI 包。
90
+
91
+ ```python
92
+ import requests
93
+ from PIL import Image
94
+ import torch
95
+ from transformers import ViTImageProcessor, ViTForImageClassification
96
+ ```
97
+
98
+ 接下来,我们将从COCO数据集中随机下载一张有沙发图像,上边有两只猫和一个遥控器,并对图像进行预处理为模型所期望的输入格式,我们可以方便地使用相应的预处理器类(`ViTProcessor`)实现这一步。初始化模型和预处理器,可以使用hub中[Kakao Brain Vi·T repos](https://huggingface.co/models?search=kakaobrain/vit)之一。请注意使用hub 中的库预处理器,确保预处理后的图像符合特定预训练模型所需的格式。
99
+
100
+ ```python
101
+ url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
102
+ image = Image.open(requests.get(url, stream=True).raw)
103
+
104
+ processor = ViTImageProcessor.from_pretrained('kakaobrain/vit-large-patch16-384')
105
+ model = ViTForImageClassification.from_pretrained('kakaobrain/vit-large-patch16-384')
106
+ ```
107
+
108
+ 接下来将图��预处理并将其输入到模型,实现检索类别标签。Kakao Brain ViT图像分类模型是在ImageNet标签上训练的,输出形状为 batch_size×1000维度的类别(logits)。
109
+
110
+ ```python
111
+ # preprocess image or list of images
112
+ inputs = processor(images=image, return_tensors="pt")
113
+
114
+ # inference
115
+ with torch.no_grad():
116
+ outputs = model(**inputs)
117
+
118
+ # apply SoftMax to logits to compute the probability of each class
119
+ preds = torch.nn.functional.softmax(outputs.logits, dim=-1)
120
+
121
+ # print the top 5 class predictions and their probabilities
122
+ top_class_preds = torch.argsort(preds, descending=True)[0, :5]
123
+
124
+ for c in top_class_preds:
125
+ print(f"{model.config.id2label[c.item()]} with probability {round(preds[0, c.item()].item(), 4)}")
126
+ ```
127
+
128
+ 到这里就完成了!为了更加简单和简洁,还可以使用图像分类管道(pipeline)并将Kakao Brain ViT仓库名称作为目标模型传递给初始化管道。然后,我们可以传入图像的URL或本地路径,或Pillow图像,可选“top_k”参数表述返回前k个预测。让我们继续对猫和遥控器图片获取前5个预测结果。
129
+
130
+ ```python
131
+ from transformers import pipeline
132
+
133
+ classifier = pipeline(task='image-classification', model='kakaobrain/vit-large-patch16-384')
134
+ classifier('http://images.cocodataset.org/val2017/000000039769.jpg', top_k=5)
135
+ ```
136
+
137
+ 如果您想更多地尝试 Kakao Brain ViT模型,请前往🤗 中心的项目空间。
138
+
139
+ [`https://huggingface.co/spaces/adirik/kakao-brain-vit`](https://huggingface.co/spaces/adirik/kakao-brain-vit)
140
+
141
+ ![New%20ViT%20and%20ALIGN%20Models%20From%20Kakao%20Brain%2096dc66c155824d38814f76b7e882ad2f/vit_demo.png](New%20ViT%20and%20ALIGN%20Models%20From%20Kakao%20Brain%2096dc66c155824d38814f76b7e882ad2f/vit_demo.png)
142
+
143
+ 我们开始实验 ALIGN,它可用于检索文本或图像的多模态嵌入或执行零样本图像分类。ALIGN 的 Transformer 实现和用法类似于 [CLIP](https://huggingface.co/docs/transformers/main/en/model_doc/clip)。首先,下载预训练模型和其处理器(processor),处理器预处理图像和文本,使它们符合 ALIGN 的预期格式,以便将其输入到视觉和文本编码器中。这步导入了我们将要使用的模块并初始化预处理器和模型。
144
+
145
+ ```python
146
+ import requests
147
+ from PIL import Image
148
+ import torch
149
+ from transformers import AlignProcessor, AlignModel
150
+
151
+ url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
152
+ image = Image.open(requests.get(url, stream=True).raw)
153
+
154
+ processor = AlignProcessor.from_pretrained('kakaobrain/align-base')
155
+ model = AlignModel.from_pretrained('kakaobrain/align-base')
156
+ ```
157
+
158
+ 先从零样本图像分类开始。为此,我们将提供候选标签(自由格式文本),并使用 AlignModel 找出更好地描述图像的表述。我们将首先预处理图像和文本输入,并将预处理后的输入送到 AlignModel 中。
159
+
160
+ ```python
161
+ candidate_labels = ['an image of a cat', 'an image of a dog']
162
+
163
+ inputs = processor(images=image, text=candidate_labels, return_tensors='pt')
164
+
165
+ with torch.no_grad():
166
+ outputs = model(**inputs)
167
+
168
+ # this is the image-text similarity score
169
+ logits_per_image = outputs.logits_per_image
170
+
171
+ # we can take the softmax to get the label probabilities
172
+ probs = logits_per_image.softmax(dim=1)
173
+ print(probs)
174
+ ```
175
+
176
+ 完成了,就这么简单。要进一步尝试 Kakao Brain ALIGN 模型进行零样本图像分类,只需前往 HuggingFace🤗 Hub 上的 demo演示。请注意,`AlignModel` 的输出包括 `text_embeds` 和 `image_embeds`(参阅 ALIGN 的 [文档](https://huggingface.co/docs/transformers/main/en/model_doc/align))。如果不需要计算用于零样本分类的每个图像和每个文本的逻辑(logits),可以使用 `AlignModel` 类中的 `get_image_features()` 和 `get_text_features()` 方法便捷地检索视觉和文本嵌入。
177
+
178
+ ```python
179
+ text_embeds = model.get_text_features(
180
+ input_ids=inputs['input_ids'],
181
+ attention_mask=inputs['attention_mask'],
182
+ token_type_ids=inputs['token_type_ids'],
183
+ )
184
+ image_embeds = model.get_image_features(
185
+ pixel_values=inputs['pixel_values'],
186
+ )
187
+ ```
188
+
189
+ 或者,我们可以使用 ALIGN 的独立视觉和文本编码器获取多模态嵌入。然后可以使用这些嵌入用于各种下游任务的模型训练,例如目标检测、图像分割和图像字幕生成。让我们看看如何使用 `AlignTextModel` 和 `AlignVisionModel` 获取这些嵌入。请注意,我们可以使用便捷的AlignProcessor 类分别对文本和图像进行预处理。
190
+
191
+ ```python
192
+ from transformers import AlignTextModel
193
+
194
+ processor = AlignProcessor.from_pretrained('kakaobrain/align-base')
195
+ model = AlignTextModel.from_pretrained('kakaobrain/align-base')
196
+
197
+ # get embeddings of two text queries
198
+ inputs = processor(['an image of a cat', 'an image of a dog'], return_tensors='pt')
199
+
200
+ with torch.no_grad():
201
+ outputs = model(**inputs)
202
+
203
+ # get the last hidden state and the final pooled output
204
+ last_hidden_state = outputs.last_hidden_state
205
+ pooled_output = outputs.pooler_output
206
+ ```
207
+
208
+ 我们也可以在推理过程中设置output_hidden_states和output_attentions参数为True,以返回所有隐藏状态和注意力值。
209
+
210
+ ```python
211
+ with torch.no_grad():
212
+ outputs = model(**inputs, output_hidden_states=True, output_attentions=True)
213
+ # print what information is returned
214
+ for key, value in outputs.items():
215
+ print(key)
216
+ ```
217
+
218
+ 在 AlignVisionModel 中执行相同的操作,获取图像的多模态嵌入。
219
+
220
+ ```python
221
+ from transformers import AlignVisionModel
222
+
223
+ processor = AlignProcessor.from_pretrained('kakaobrain/align-base')
224
+ model = AlignVisionModel.from_pretrained('kakaobrain/align-base')
225
+
226
+ url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
227
+ image = Image.open(requests.get(url, stream=True).raw)
228
+
229
+ inputs = processor(images=image, return_tensors='pt')
230
+
231
+ with torch.no_grad():
232
+ outputs = model(**inputs)
233
+
234
+ # print the last hidden state and the final pooled output
235
+ last_hidden_state = outputs.last_hidden_state
236
+ pooled_output = outputs.pooler_output
237
+ ```
238
+
239
+ 与ViT类似,使用零样本图像分类管道(pipeline)可以让过程更加轻松。以下实现了如何使用此流程使用自由文本候选标签在野外执行图像分类。
240
+
241
+ ```python
242
+ from transformers import pipeline
243
+
244
+ classifier = pipeline(task='zero-shot-image-classification', model='kakaobrain/align-base')
245
+ classifier(
246
+ 'https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png',
247
+ candidate_labels=['animals', 'humans', 'landscape'],
248
+ )
249
+
250
+ classifier(
251
+ 'https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png',
252
+ candidate_labels=['black and white', 'photorealist', 'painting'],
253
+ )
254
+ ```
255
+
256
+ ## 结论
257
+
258
+ 近年来,多模态取得了令人难以置信的进展,例如 CLIP 和 ALIGN 等模型赋能了各种下游任务,例如图像描述、零样本图像分类和开放世界目标检测。本博客,我们介绍了由 Kakao Brain 贡献的最新开源代码 ViT 和 ALIGN 模型,以及新的 COYO 文本-图像数据集。展示了如何使用这些模型执行各种任务,只需几行代码即可单独使用或作为 🤗Transformers pipeline 的一部分使用。
259
+
260
+ 我们正在继续整合最有影响力的计算机视觉和多模型模型,并乐于听取您的反馈。要了解计算机视觉和多模态研究的最新消息,作者及 Twitter :@adirik、@a_e_roberts、@NielsRogge、@RisingSayak 和 @huggingface。
261
+
262
+ > 英文原文: [https://huggingface.co/blog/vit-align](https://huggingface.co/blog/vit-align) 译者: Cony Zhang (张聪聪)
263
+ >