BLIP 小结

2023-10-08 06:52
文章标签 小结 blip

本文主要是介绍BLIP 小结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

论文:Bootstrapping Language-Image Pre-training (BLIP)

代码:https://github.com/salesforce/BLIP

1 motivation

目前多模态模型在图片理解类任务、生成类任务表现卓越主要源于Scale up model and scale up dataset(更大的模型,更多的数据)。但是VLP(vision-language pre-training)数据集大多是网络爬取而来(称之为web datasets),里面的caption包含很多噪声,并不是一个理想的监督来源。BLIP这篇文章提出一种boostrapping caption的方案来“提纯”带噪声web datasets,从而进一步提升多模态模型的能力。

概括来说:这篇文章设计了一种去噪方案,来提纯web datasets,以此带来精度提升。

2 method

2.1 模型架构

BLIP多模态架构为双塔架构。论文中用3个vision language pretraining(VLP) task来激发模型的多模态能力。

在这里插入图片描述

2.2 多模态预训练任务

2.2.1 Image-Text Contrastive Loss (ITC)

和CLIP训练任务一致。核心思想是:给定图片-文本向量对 { ( f e a i m g ( 1 ) , f e a t e x t ( 1 ) ) , ( f e a i m g ( 2 ) , f e a t e x t ( 2 ) ) , ⋯ , ( f e a i m g ( N ) , f e a t e x t ( N ) ) } \{(\mathrm{fea}_{\mathrm{img}}^{(1)}, \mathrm{fea}_{\mathrm{text}}^{(1)}), (\mathrm{fea}_{\mathrm{img}}^{(2)}, \mathrm{fea}_{\mathrm{text}}^{(2)}), \cdots, (\mathrm{fea}_{\mathrm{img}}^{(N)}, \mathrm{fea}_{\mathrm{text}}^{(N)}) \} {(feaimg(1),featext(1)),(feaimg(2),featext(2)),,(feaimg(N),featext(N))}.其训练目标为,同pair的 ( f e a i m g ( i ) , f e a t e x t ( i ) ) (\mathrm{fea}_{\mathrm{img}}^{(i)}, \mathrm{fea}_{\mathrm{text}}^{(i)}) (feaimg(i),featext(i))相似度越接近越好,非同pair的 ( f e a i m g ( i ) , f e a t e x t ( j ) , i ≠ j ) (\mathrm{fea}_{\mathrm{img}}^{(i)}, \mathrm{fea}_{\mathrm{text}}^{(j)}, i \neq j) (feaimg(i),featext(j),i=j)相似度越远越好,形如
L o s s = 1 2 ( ∑ i = 0 N C E ( exp ⁡ ( f e a i m g ( i ) ⋅ f e a t e x t ( i ) ) ∑ j = 0 N exp ⁡ ( f e a i m g ( i ) ⋅ f e a t e x t ( j ) ) ) + ∑ j = 0 N C E ( exp ⁡ ( f e a i m g ( j ) ⋅ f e a t e x t ( j ) ) ∑ i = 0 N exp ⁡ ( f e a i m g ( j ) ⋅ f e a t e x t ( i ) ) ) ) Loss = \frac{1}{2} \left(\sum_{i=0}^{N} CE(\frac { \exp ( \mathrm{fea}^{(i)}_{\mathrm{img}} \cdot \mathrm{fea}^{(i)}_{\mathrm{text}})} {\sum_{j=0}^{N}{\exp( \mathrm{fea}^{(i)}_{\mathrm{img}} \cdot \mathrm{fea}^{(j)}_{\mathrm{text}})} } ) + \sum_{j=0}^{N} CE(\frac { \exp ( \mathrm{fea}^{(j)}_{\mathrm{img}} \cdot \mathrm{fea}^{(j)}_{\mathrm{text}})} {\sum_{i=0}^{N}{\exp( \mathrm{fea}^{(j)}_{\mathrm{img}} \cdot \mathrm{fea}^{(i)}_{\mathrm{text}})} } ) \right) Loss=21(i=0NCE(j=0Nexp(feaimg(i)featext(j))exp(feaimg(i)featext(i)))+j=0NCE(i=0Nexp(feaimg(j)featext(i))exp(feaimg(j)featext(j))))
伪代码如下(from CLIP paper)

在这里插入图片描述

有了上面的背景知识,ITC的步骤就很好理解了:

STEP1: 图片经过image encoder得到image embedding R B × 3 × H × W ⟶ E n c o d e r i m g R B × L i m g × d \mathbb{R}^{B\times 3 \times H \times W} \stackrel{\mathrm{Encoder}_{\mathrm{img}}} \longrightarrow \mathbb{R}^ {B\times L_{\mathrm{img}} \times d} RB×3×H×WEncoderimgRB×Limg×d

STEP2: 文本经过text encoder得到text embedding R B × L × d ′ ⟶ E n c o d e r t e x t R B × L t e x t × d \mathbb{R}^{B\times L \times d'} \stackrel{\mathrm{Encoder}_{\mathrm{text}}} \longrightarrow \mathbb{R}^ {B\times L_{\mathrm{text}} \times d} RB×L×dEncodertextRB×Ltext×d

STEP3: 分别拿到image embedding中[CLS]token对应的embedding f e a i m g ∈ R B × d \mathrm{fea}_{\mathrm{img}} \in \mathbb{R} ^{B \times d} feaimgRB×d, 与文本text embedding中[CLS]token对应的embedding f e a t e x t ∈ R B × d \mathrm{fea}_{\mathrm{text}} \in \mathbb{R} ^{B \times d} featextRB×d.

STEP4: f e a i m g \mathrm{fea}_{\mathrm{img}} feaimg f e a t e x t \mathrm{fea}_{\mathrm{text}} featext投影到同一维度

STEP5: 同pair的 ( f e a i m g ( i ) , f e a t e x t ( i ) ) (\mathrm{fea}_{\mathrm{img}}^{(i)}, \mathrm{fea}_{\mathrm{text}}^{(i)}) (feaimg(i),featext(i))相似度约接近越好,非同pair的 ( f e a i m g ( i ) , f e a t e x t ( j ) , i ≠ j ) (\mathrm{fea}_{\mathrm{img}}^{(i)}, \mathrm{fea}_{\mathrm{text}}^{(j)}, i \neq j) (feaimg(i),featext(j),i=j)相似度越远越好。
在这里插入图片描述

2.2.2 Image-text matching (ITM)

ITM也是VLP的常用任务,它的实现形式有很多,核心思想是:给定图片-文本向量对 { ( f e a i m g ( 1 ) , f e a t e x t ( 1 ) ) , ( f e a i m g ( 2 ) , f e a t e x t ( 2 ) ) , ⋯ , ( f e a i m g ( N ) , f e a t e x t ( N ) ) } \{(\mathrm{fea}_{\mathrm{img}}^{(1)}, \mathrm{fea}_{\mathrm{text}}^{(1)}), (\mathrm{fea}_{\mathrm{img}}^{(2)}, \mathrm{fea}_{\mathrm{text}}^{(2)}), \cdots, (\mathrm{fea}_{\mathrm{img}}^{(N)}, \mathrm{fea}_{\mathrm{text}}^{(N)}) \} {(feaimg(1),featext(1)),(feaimg(2),featext(2)),,(feaimg(N),featext(N))}.其训练目标为预测 ( f e a i m g ( i ) , f e a t e x t ( j ) ) (\mathrm{fea}_{\mathrm{img}}^{(i)}, \mathrm{fea}_{\mathrm{text}}^{(j)}) (feaimg(i),featext(j))是否来自同一个pair。是为1,否则为0。形如:
L o s s = ∑ i ∑ j { C E ( L o g i t ( f e a i m g ( j ) , f e a t e x t ( i ) ) , 1 ) i f i = j C E ( L o g i t ( f e a i m g ( j ) , f e a t e x t ( i ) ) , 0 ) i f i ≠ j \mathrm{Loss} = \sum_{i} \sum_{j} \begin{cases} \mathrm{CE}(\mathrm{Logit}_{} (\mathrm{fea}^{(j)}_{\mathrm{img}}, \mathrm{fea}^{(i)}_{\mathrm{text}}), 1) \quad \mathrm{if \, i = j} \\ \mathrm{CE}(\mathrm{Logit}_{} (\mathrm{fea}^{(j)}_{\mathrm{img}}, \mathrm{fea}^{(i)}_{\mathrm{text}}), 0) \quad \mathrm{if \, i \neq j} \end{cases} Loss=ij{CE(Logit(feaimg(j),featext(i)),1)ifi=jCE(Logit(feaimg(j),featext(i)),0)ifi=j
下面来看具体是如何实现的。

STEP1: 图片经过image encoder得到 image embedding R B × 3 × H × W ⟶ E n c o d e r i m g R B × L i m g × d \mathbb{R}^{B\times 3 \times H \times W} \stackrel{\mathrm{Encoder}_{\mathrm{img}}} \longrightarrow \mathbb{R}^ {B\times L_{\mathrm{img}} \times d} RB×3×H×WEncoderimgRB×Limg×d

STEP2: 文本经过text encoder得到text embedding R B × L × d ′ ⟶ E n c o d e r t e x t R B × L t e x t × d \mathbb{R}^{B\times L \times d'} \stackrel{\mathrm{Encoder}_{\mathrm{text}}} \longrightarrow \mathbb{R}^ {B\times L_{\mathrm{text}} \times d} RB×L×dEncodertextRB×Ltext×d,和ITC有所区别的是,此处将image embedding 作为encoder_hidden_states也送入到text encoder(image embedding与text embedding在cross-attent层进行特征交互,image se quence embedding作为key,value。text embedding作为query)。因此最后输出的text embedding也同时蕴含了image sequence embedding的信息。作者将此时的text encoder称为 image-ground text encoder

image embedding与text embedding在cross-attention的特征交互实现可见transformers库BertSelfAttention函数

STEP3: 取文本text embedding中[CLS]token对应的embedding f e a t e x t ∈ R B × d \mathrm{fea}_{\mathrm{text}} \in \mathbb{R} ^{B \times d} featextRB×d

  • 当送入image sequence embedding与text是pair时, f e a t e x t \mathrm{fea}_{\mathrm{text}} featext的类别标签为1
  • 当送入image sequence embedding与text非pair时, f e a t e x t \mathrm{fea}_{\mathrm{text}} featext的类别标签为0

随后用cross-entropy计算损失。

训练完成后:得到 image-ground text encoder

在这里插入图片描述

2.2.3 Language modeling loss(LM)

LM时GPT系列的预训练任务。简单来说就是根据前面的词来预测下一个词。与NLP的LM有所不同的是VLP同时将image-embedding引入到上下文信息。

STEP1: 图片输入到image encoder中的得到 image embedding( f e a i m g \mathrm{fea}_{\mathrm{img}} feaimg)。 R B × 3 × H × W ⟶ E n c o d e r i m g R B × L i m g × d \mathbb{R}^{B\times 3 \times H \times W} \stackrel{\mathrm{Encoder}_{\mathrm{img}}} \longrightarrow \mathbb{R}^ {B\times L_{\mathrm{img}} \times d} RB×3×H×WEncoderimgRB×Limg×d

STEP2: 将 image embedding( f e a i m g \mathrm{fea}_{\mathrm{img}} feaimg)作为key,value送入到text-decoder的cross-attention中与text embedding进行特征交互。作者将此时的text-decoder称为image-ground text decoder

STEP3:最大化自回归序列的似然概率进行训练。训练完成后:得到 image-ground text decoder
∑ i = 1 L log ⁡ ( p ( y i ∣ y < i , f e a i m g ; Θ ) ) \sum_{i=1} ^ {L} \log (p(y_i|y_{<i}, \mathrm{fea}_{img}; \Theta)) i=1Llog(p(yiy<i,feaimg;Θ))
在这里插入图片描述

通过以上预训练任务:得到

  • image encoder
  • image-ground text encoder
  • image-ground text decoder

2.3 boostrapping caption

通过2.2节的预训练任务我们得到3个模型:1)image encoder;2)image-ground text encoder ;3) image-ground text decoder

下面来看如何结合上述上个模型来对web dataset进行“提纯”,主要步骤如下:

STEP1: 用人工标注的数据集 { ( I h , T h ) ∣ h = 1 , 2 , . . . } \{(I_h, T_h)|h=1,2, ...\} {(Ih,Th)h=1,2,...}对预训练模型image-ground text encoder与 image-ground text decoder 进行微调。 ( I h , T h ) (I_h, T_h) (Ih,Th)为图文对。

STEP2: 遍历web datasets { ( I w , T w ) ∣ w = 1 , 2... } \{(I_w, T_w)|w=1,2... \} {(Iw,Tw)w=1,2...}.进行下面操作

  • STEP2.1 通过 image-ground text decoder (论文称之为Captioner)预测 I w I_w Iw的caption T s T_s Ts。此时对于图片 I w I_w Iw有两个图文对 ( I w , T w ) (I_w, T_w) (Iw,Tw) ( I w , T s ) (I_w, T_s) (Iw,Ts)
  • STEP2.2 通过image-ground text encoder(论文称之为Filter)来判别图文对 ( I w , T w ) (I_w, T_w) (Iw,Tw) ( I w , T s ) (I_w, T_s) (Iw,Ts)是否matching。过滤不matching的图文对 ( I w , T w N o t m a t c h i n g ) , ( I w , T s N o t m a t c h i n g ) (I_w, T^{\mathrm{Not \, matching}}_w), (I_w, T^{\mathrm{Not \, matching}}_s) (Iw,TwNotmatching),(Iw,TsNotmatching)

STEP3: 汇总所有图文对 D = { ( I w , T w m a t c h ) + ( I w , T s m a t c h ) + ( I h , T h ) } D = \{(I_w, T_w^{\mathrm{match}})+(I_w, T_s^{\mathrm{match}}) + (I_h, T_h) \} D={(Iw,Twmatch)+(Iw,Tsmatch)+(Ih,Th)}

用提纯后的数据集用2.2的预训练任务重新训练。

在这里插入图片描述

3 result

从作者给出的实验来看,boostrapping caption不论在retrieval还是在caption任务上都能带来一定的提升。但当scale up dataset and scale up model后,boostrapping caption的提升就很有限了(在caption任务上尤为明显),如下表的最后一行。

在这里插入图片描述

BLIP只评估了在flickr30K上的zero-shot retrieval的能力,相比之前的SOTA在image-to-text retrieval的R@1上有2个点的提升,其它指标提升不大。其它对比结果见原论文。

在这里插入图片描述

这篇关于BLIP 小结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/163497

相关文章

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

Java中的Lambda表达式及其应用小结

《Java中的Lambda表达式及其应用小结》Java中的Lambda表达式是一项极具创新性的特性,它使得Java代码更加简洁和高效,尤其是在集合操作和并行处理方面,:本文主要介绍Java中的La... 目录前言1. 什么是Lambda表达式?2. Lambda表达式的基本语法例子1:最简单的Lambda表

Java中Scanner的用法示例小结

《Java中Scanner的用法示例小结》有时候我们在编写代码的时候可能会使用输入和输出,那Java也有自己的输入和输出,今天我们来探究一下,对JavaScanner用法相关知识感兴趣的朋友一起看看吧... 目录前言一 输出二 输入Scanner的使用多组输入三 综合练习:猜数字游戏猜数字前言有时候我们在

SQL BETWEEN 的常见用法小结

《SQLBETWEEN的常见用法小结》BETWEEN操作符是SQL中非常有用的工具,它允许你快速选取某个范围内的值,本文给大家介绍SQLBETWEEN的常见用法,感兴趣的朋友一起看看吧... 在SQL中,BETWEEN是一个操作符,用于选取介于两个值之间的数据。它包含这两个边界值。BETWEEN操作符常用

go 指针接收者和值接收者的区别小结

《go指针接收者和值接收者的区别小结》在Go语言中,值接收者和指针接收者是方法定义中的两种接收者类型,本文主要介绍了go指针接收者和值接收者的区别小结,文中通过示例代码介绍的非常详细,需要的朋友们下... 目录go 指针接收者和值接收者的区别易错点辨析go 指针接收者和值接收者的区别指针接收者和值接收者的

python uv包管理小结

《pythonuv包管理小结》uv是一个高性能的Python包管理工具,它不仅能够高效地处理包管理和依赖解析,还提供了对Python版本管理的支持,本文主要介绍了pythonuv包管理小结,具有一... 目录安装 uv使用 uv 管理 python 版本安装指定版本的 Python查看已安装的 Python

C#中DrawCurve的用法小结

《C#中DrawCurve的用法小结》本文主要介绍了C#中DrawCurve的用法小结,通常用于绘制一条平滑的曲线通过一系列给定的点,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 如何使用 DrawCurve 方法(不带弯曲程度)2. 如何使用 DrawCurve 方法(带弯曲程度)3.使用Dr

MySQL 分区与分库分表策略应用小结

《MySQL分区与分库分表策略应用小结》在大数据量、复杂查询和高并发的应用场景下,单一数据库往往难以满足性能和扩展性的要求,本文将详细介绍这两种策略的基本概念、实现方法及优缺点,并通过实际案例展示如... 目录mysql 分区与分库分表策略1. 数据库水平拆分的背景2. MySQL 分区策略2.1 分区概念