Show, Attend and Tell: Neural Image Caption Generation with Visual Attention
Title
Show, Attend and Tell: Neural Image Caption Generation with Visual Attention
(零)细节
- 这篇文章是用来学习MC 方法的一篇很好的文章,其中讲到了两种降低估计方差的策略
(一)摘要
-
文章思路来源:machine translation,以及 object detection,能不能在图像中引入注意力模型,我们又该怎么引入注意力模型呢?将words/words alignment转变为words/image alignment
-
文章解决的是基于图像generating the corresponding words in the output sequence.也就是自动生成摘要for image
-
train this model in a deterministic manner(确定性方式) using standard backpropagation techniques and stochastically(随机) by maximizing a variational lower bound(变分下界)
-
实验基于:Flickr9k, Flickr30k and MS COCO
(二)介绍
-
Automatically generating captions for an image是一个挑战
-
模型的图像化说明
-
最近的工作:使用convnets和RNN结合,利用convnets去obtain vectorial(向量) representation of images,利用RNN将这些向量表示decode到natural language sentences。
-
human visual system的注意力不是将整幅图像压缩成static representation,而是将关注信息放到forefront,之前有人尝试使用图像convnet最顶层的表示,提取最显著(salient)对象的信息,是一种有效的解决方案,在以前的工作中已被广泛采用。但是缺少细节信息。可以通过lower-level的表示来保留这些细节信息,因此,需要attend at different locations从而获得一个caption(摘要)
-
文中提出了两种variants:a “hard” stochastic attention mechanism and a “soft” deterministic attention mechanism.
-
paper的贡献点
-
两个基于注意力的图像摘要模型:
a “soft” deterministic attention mechanism 通过标准反向传播方法进行训练
a “hard” stochastic attention mechanism 通过最大近似变分下界/Reinforce进行训练
-
We show how we can gain insight and interpret(解释) the results of this framework by visualizing “where” and “what” the attention focused on
-
定量验证基于注意力图像摘要生成在数据集上的表现
-
(三)相关工作
3.1 overall
- 主要介绍图像摘要生成以及注意力的一些工作
3.2 图像摘要生成
-
生成图像描述的方法很多是基于RNN的,这是由于机器翻译sequence-to-sequence训练启发的,图像摘要生成类似于机器翻译,只不过是将图像翻译成sentence。
-
The first approach to using NN for caption generation was proposed by Kiros et al.使用a multimodal log-bilinear model that was biased by features from the image.
-
Kiros et al. (2014b) explicitly allow for a natural way of doing both ranking and generation.
-
Mao et al. (2014) used a similar approach to generation but replaced a feedforward neural language model with a recurrent one.
-
Both Vinyals et al. (2014) and Donahue et al. (2014) used recurrent neural networks (RNN) based
on long short-term memory (LSTM) units (Hochreiter & Schmidhuber, 1997) for their models. -
Vinyals et al. (2014) only showed the image to the RNN at the beginning.
-
Donahue et al. (2014) and Y ao et al. (2015) also applied LSTMs to videos, allowing their model to generate video descriptions.
-
Karpathy & Li (2014)
-
Fang et al. (2014)
这些工作都看不懂啊,就不多叙述了。
作者提出的模型does not use object detectors(目标检测器) but instead learns latent alignments from scratch(从头开始学习),能够让模型脱离之前目标检测的限制(go beyond “objectness”),从而学习到抽象的表述abstract concepts,这也是这篇文章由来的思路。
又开始介绍前人工作了(使用神经网络之前生成摘要的方法):
- The first involved generating caption templates which were filled in based on the results of object detections and attribute discovery
- The second approach was based on first retrieving similar captioned images from a large database then modifying these retrieved captions to fit the query(基于首先从大型数据库中检索相似的标题图像,然后修改这些检索到的标题以适应查询)
这两种方法不行了,实在是看不下去这个前人工作,没有干货啊,就是提供点阅读素材
3.3 NN中引入注意力
Some that share the same spirit as our work include
- Larochelle & Hinton(2010);
- Denil et al. (2012);
- Tang et al. (2014) and
- more recently Gregor et al. (2015).
our work directly extends the work of
- Bahdanau et al. (2014); 机器翻译那篇
- Mnih et al. (2014); glimpse sensor那篇
- Ba et al. (2014);
- Graves (2013).
(四)带注意力机制的Image Caption Generation
4.1 模型细节
不是两个模型吗?他们的key difference 就在$ \phi{\color{Red} } $函数上,
4.1.1 Encoder:Convolutional features
-
将图像生成caption摘要,其中摘要的数学表示形式为:
y = { y 1 , … , y C } , y i ∈ R K y=\left\{\mathbf{y}_{1}, \ldots, \mathbf{y}_{C}\right\}, \mathbf{y}_{i} \in \mathbb{R}^{K} y={y1,…,yC},yi∈RK
K K K为词汇表的尺寸(the size of vocabulary), C C C是摘要的长度 -
使用convolutional neural network提取特征向量作为annotation vectors,提取 L L L个向量,每一个向量维度为 D D D,一个向量对应着a part of the image。我觉得是得到了L个部分的向量表示
a = { a 1 , … , a L } , a i ∈ R D a=\left\{\mathbf{a}_{1}, \ldots, \mathbf{a}_{L}\right\}, \mathbf{a}_{i} \in \mathbb{R}^{D} a={a1,…,aL},ai∈RD -
本文的一个突出点在于:从较低的卷积层提取特征,而不是之前的工作中使用全连接层
4.1.2 Decoder:LSTM
使用LSTM去生成摘要,每一个time step生成一个word,产生这个word是在a context vector(上下文向量),the previous hidden state and the previous generated words,这个地方的公式和LSTM的公式几乎完全一样 ,不同的是输入的地方还有上下文向量
i
t
=
σ
(
W
i
E
y
t
−
1
+
U
i
h
t
−
1
+
Z
i
z
^
t
+
b
i
)
f
t
=
σ
(
W
f
E
y
t
−
1
+
U
f
h
t
−
1
+
Z
f
z
^
t
+
b
f
)
c
t
=
f
t
c
t
−
1
+
i
t
tanh
(
W
c
E
y
t
−
1
+
U
c
h
t
−
1
+
Z
c
z
^
t
+
b
c
)
o
t
=
σ
(
W
o
E
y
t
−
1
+
U
o
h
t
−
1
+
Z
o
z
^
t
+
b
o
)
h
t
=
o
t
tanh
(
c
t
)
\begin{aligned} \mathbf{i}_{t} &=\sigma\left(W_{i} E \mathbf{y}_{t-1}+U_{i} \mathbf{h}_{t-1}+Z_{i} \hat{\mathbf{z}}_{t}+\mathbf{b}_{i}\right) \\ \mathbf{f}_{t} &=\sigma\left(W_{f} E \mathbf{y}_{t-1}+U_{f} \mathbf{h}_{t-1}+Z_{f} \hat{\mathbf{z}}_{t}+\mathbf{b}_{f}\right) \\ \mathbf{c}_{t} &=\mathbf{f}_{t} \mathbf{c}_{t-1}+\mathbf{i}_{t} \tanh \left(W_{c} E \mathbf{y}_{t-1}+U_{c} \mathbf{h}_{t-1}+Z_{c} \hat{\mathbf{z}}_{t}+\mathbf{b}_{c}\right) \\ \mathbf{o}_{t} &=\sigma\left(W_{o} E \mathbf{y}_{t-1}+U_{o} \mathbf{h}_{t-1}+Z_{o} \hat{\mathbf{z}}_{t}+\mathbf{b}_{o}\right) \\ \mathbf{h}_{t} &=\mathbf{o}_{t} \tanh \left(\mathbf{c}_{t}\right) \end{aligned}
itftctotht=σ(WiEyt−1+Uiht−1+Ziz^t+bi)=σ(WfEyt−1+Ufht−1+Zfz^t+bf)=ftct−1+ittanh(WcEyt−1+Ucht−1+Zcz^t+bc)=σ(WoEyt−1+Uoht−1+Zoz^t+bo)=ottanh(ct)
i
t
\mathbf{i}_{t}
it,
f
t
\mathbf{f}_{t}
ft,
c
t
\mathbf{c}_{t}
ct,
o
t
\mathbf{o}_{t}
ot,
h
t
\mathbf{h}_{t}
ht分别表示输入,遗忘门,memory细胞状态,输出门以及隐状态,这里的
z
t
^
\hat{\mathbf{z}_t}
zt^是上下文向量。
E
∈
R
m
×
K
E \in \mathbb{R}^{m \times K}
E∈Rm×K是词嵌入矩阵。
对应的图示表示为:
我们课程上讲到的LSTM模型为:
totally the same,allright!!仅仅是多了一个 z t ^ \hat{\mathbf{z}_t} zt^好吧,
那么接下来我们看看这个
z
t
^
\hat{\mathbf{z}_t}
zt^,很显然我们之前提到过的annotation vectors还没有用上呢,这里就到了用到的时候了,
a
=
{
a
1
,
…
,
a
L
}
,
a
i
∈
R
D
a=\left\{\mathbf{a}_{1}, \ldots, \mathbf{a}_{L}\right\}, \mathbf{a}_{i} \in \mathbb{R}^{D}
a={a1,…,aL},ai∈RD,每一个annotation vector就是有一个系数
α
i
\alpha_i
αi,这个
α
i
\alpha_i
αi的计算方式是通过一个an attention model
f
a
t
t
f_{att}
fattfor which we use a multilayer perceptron conditioned on the previous hidden state
h
t
−
1
h_{t−1}
ht−1.权重系数的计算公式为:
e
t
i
=
f
a
t
t
(
a
i
,
h
t
−
1
)
α
t
i
=
exp
(
e
t
i
)
∑
k
=
1
L
exp
(
e
t
k
)
\begin{aligned} e_{t i} &=f_{\mathrm{att}}\left(\mathbf{a}_{i}, \mathbf{h}_{t-1}\right) \\ \alpha_{t i} &=\frac{\exp \left(e_{t i}\right)}{\sum_{k=1}^{L} \exp \left(e_{t k}\right)} \end{aligned}
etiαti=fatt(ai,ht−1)=∑k=1Lexp(etk)exp(eti)
而上下文向量
z
t
^
\hat{\mathbf{z}_t}
zt^的计算表达式为:
z
^
t
=
ϕ
(
{
a
i
}
,
{
α
i
}
)
,
\hat{\mathbf{z}}_{t}=\phi\left(\left\{\mathbf{a}_{i}\right\},\left\{\alpha_{i}\right\}\right),
z^t=ϕ({ai},{αi}),
the initial memory state and hidden state of LSTM为:
c
0
=
f
init
,
c
(
1
L
∑
i
L
a
i
)
,
h
0
=
f
init,
h
(
1
L
∑
i
L
a
i
)
\mathbf{c}_{0}=f_{\text {init}, \mathrm{c}}\left(\frac{1}{L} \sum_{i}^{L} \mathbf{a}_{i}\right), \quad \mathbf{h}_{0}=f_{\text {init, } \mathrm{h}}\left(\frac{1}{L} \sum_{i}^{L} \mathbf{a}_{i}\right)
c0=finit,c(L1i∑Lai),h0=finit, h(L1i∑Lai)
其中
f
init
,
c
f_{\text {init}, \mathrm{c}}
finit,c和
f
init,
h
f_{\text {init, } \mathrm{h}}
finit, h是两个separate MLPs
在本文中将t时刻产生的词word的条件概率建模为,也就是模型t时刻的输出,这里不同于LSTM的输出门那里,那边的输出是控制信息输出到隐状态
h
t
h_t
ht的程度,这里就是纯粹的模型输出:
p
(
y
t
∣
a
,
y
1
t
−
1
)
∝
exp
(
L
o
(
E
y
t
−
1
+
L
h
h
t
+
L
z
z
^
t
)
)
p\left(\mathbf{y}_{t} \mid \mathbf{a}, \mathbf{y}_{1}^{t-1}\right) \propto \exp \left(\mathbf{L}_{o}\left(\mathbf{E} \mathbf{y}_{t-1}+\mathbf{L}_{h} \mathbf{h}_{t}+\mathbf{L}_{z} \hat{\mathbf{z}}_{t}\right)\right)
p(yt∣a,y1t−1)∝exp(Lo(Eyt−1+Lhht+Lzz^t))
这里的
L
o
∈
R
K
×
m
,
L
h
∈
R
m
×
n
,
L
z
∈
R
m
×
D
,
and
E
∈
R
m
×
K
\mathbf{L}_{o} \in \mathbb{R}^{K \times m}, \mathbf{L}_{h} \in \mathbb{R}^{m \times n}, \mathbf{L}_{z} \in \mathbb{R}^{m \times D}, \text { and } \mathbf{E} \in \mathbb{R}^{m \times K}
Lo∈RK×m,Lh∈Rm×n,Lz∈Rm×D, and E∈Rm×K都是随机初始化的可学习参数.
(五) Learning Stochastic “Hard” vs Deterministic “Soft” Attention
- 本小节给出了注意力模型的两种可选机制:stochastic attention以及deterministic attention
5.1 Stochastic “Hard” Attention
s
t
s_t
st表示where the model decides to focus attention when generating the t-th word.
s
t
=
[
s
t
,
1
,
.
.
.
,
s
t
,
L
]
s_t = [s_{t,1},...,s_{t,L}]
st=[st,1,...,st,L],
s
t
,
i
s_{t,i}
st,i是一个one-hot variable就是第i个位置为1,将Attention locations看作中间隐变量,这里的attention locations指的是啥来着呀?将
z
^
t
\hat{\mathbf{z}}_{t}
z^t看成一个随机变量
p
(
s
t
,
i
=
1
∣
s
j
<
t
,
a
)
=
α
t
,
i
z
^
t
=
∑
i
s
t
,
i
a
i
\begin{array}{l} p\left(s_{t, i}=1 \mid s_{j<t}, \mathbf{a}\right)=\alpha_{t, i} \\ \hat{\mathbf{z}}_{t}=\sum_{i} s_{t, i} \mathbf{a}_{i} \end{array}
p(st,i=1∣sj<t,a)=αt,iz^t=∑ist,iai
定义了一个新的目标函数
L
s
L_s
Ls,它是一个variational lower bound on the marginal log-likelihood
log
p
(
y
∣
a
)
\log p(\mathbf{y} \mid \mathbf{a})
logp(y∣a)(这个似然函数是在给定image features
a
\mathbf{a}
a的观察到的words 序列
y
\mathbf y
y的似然概率)
L
s
=
∑
s
p
(
s
∣
a
)
log
p
(
y
∣
s
,
a
)
≤
log
∑
s
p
(
s
∣
a
)
p
(
y
∣
s
,
a
)
=
log
p
(
y
∣
a
)
\begin{aligned} L_{s} &=\sum_{s} p(s \mid \mathbf{a}) \log p(\mathbf{y} \mid s, \mathbf{a}) \\ & \leq \log \sum_{s} p(s \mid \mathbf{a}) p(\mathbf{y} \mid s, \mathbf{a}) \\ &=\log p(\mathbf{y} \mid \mathbf{a}) \end{aligned}
Ls=s∑p(s∣a)logp(y∣s,a)≤logs∑p(s∣a)p(y∣s,a)=logp(y∣a)
相当于是
log
p
(
y
∣
a
)
\log p(\mathbf{y} \mid \mathbf{a})
logp(y∣a)的一个下界。变不变分啥的我也不清楚了,搞不明白变分是什么鬼。
然后对
L
s
L_s
Ls对
W
W
W求偏导函数:论文中给出的公式跳步了,下面是我改过的公式
$$
\begin{aligned}
\frac{\partial L_{s}}{\partial W}=\sum_{s} &\left[p(s \mid \mathbf{a}) \frac{\partial \log p(\mathbf{y} \mid s, \mathbf{a})}{\partial W}+
\log p(\mathbf{y} \mid s, \mathbf{a}) \frac{\partial p(s \mid \mathbf{a})}{\partial W}\right]\
\end{aligned}
KaTeX parse error: Can't use function '$' in math mode at position 4: 由于$̲\frac{\partial …
\begin{aligned}
\frac{\partial L_{s}}{\partial W}=\sum_{s} p(s \mid \mathbf{a}) &\left[\frac{\partial \log p(\mathbf{y} \mid s, \mathbf{a})}{\partial W}+
\log p(\mathbf{y} \mid s, \mathbf{a}) \frac{\partial \log p(s \mid \mathbf{a})}{\partial W}\right]
\end{aligned}
KaTeX parse error: Can't use function '$' in math mode at position 19: …寄出神器MC method,将$̲p(s \mid \mathb…
\begin{aligned}
\frac{\partial L_{s}}{\partial W} \approx \frac{1}{N} \sum_{n=1}^{N}\left[\frac{\partial \log p\left(\mathbf{y} \mid \tilde{s}^{n}, \mathbf{a}\right)}{\partial W}+
\log p\left(\mathbf{y} \mid \tilde{s}^{n}, \mathbf{a}\right) \frac{\partial \log p\left(\tilde{s}^{n} \mid \mathbf{a}\right)}{\partial W}\right]
\end{aligned}
KaTeX parse error: Can't use function '$' in math mode at position 4: 其中$̲\tilde{s}^{n}=\…
s_{t}^{n} \sim \text { Multinoulli }{L}\left(\left{\alpha{i}^{n}\right}\right)
$$
分布进行的。
同时为了降低这个估计的方差,我们采用了**the moving average baseline technique**
b
k
=
0.9
×
b
k
−
1
+
0.1
×
log
p
(
y
∣
s
~
k
,
a
)
b_{k}=0.9 \times b_{k-1}+0.1 \times \log p\left(\mathbf{y} \mid \tilde{s}_{k}, \mathbf{a}\right)
bk=0.9×bk−1+0.1×logp(y∣s~k,a)
为了进一步降低方差,将multinouilli 分布熵
H
[
s
]
H[s]
H[s]的梯度加到了RHS中就是 MC method采样函数中。得到了最终的模型学习规则:
∂
L
s
∂
W
≈
1
N
∑
n
=
1
N
[
∂
log
p
(
y
∣
s
~
n
,
a
)
∂
W
+
λ
r
(
log
p
(
y
∣
s
~
n
,
a
)
−
b
)
∂
log
p
(
s
~
n
∣
a
)
∂
W
+
λ
e
∂
H
[
s
~
n
]
∂
W
]
\begin{array}{l} \frac{\partial L_{s}}{\partial W} \approx \frac{1}{N} \sum_{n=1}^{N}\left[\frac{\partial \log p\left(\mathbf{y} \mid \tilde{s}^{n}, \mathbf{a}\right)}{\partial W}+\right. \\ \left.\lambda_{r}\left(\log p\left(\mathbf{y} \mid \tilde{s}^{n}, \mathbf{a}\right)-b\right) \frac{\partial \log p\left(\tilde{s}^{n} \mid \mathbf{a}\right)}{\partial W}+\lambda_{e} \frac{\partial H\left[\tilde{s}^{n}\right]}{\partial W}\right] \end{array}
∂W∂Ls≈N1∑n=1N[∂W∂logp(y∣s~n,a)+λr(logp(y∣s~n,a)−b)∂W∂logp(s~n∣a)+λe∂W∂H[s~n]]
其中
λ
r
\lambda_{r}
λr和
λ
e
\lambda_{e}
λe是需要通过交叉验证选择的超参数。上式被 Ba et al. (2014)and Mnih et al. (2014)指出是等价于强化学习规则的。
- 为了增加学习的鲁棒性。做法with probability 0.5 for a given image, we set the sampled attention location S ~ \tilde{S} S~ to its expected value α (equivalent to the deterministic attention in Sec. 4.2)这个地方没看懂啊,是怎么做的啊!!
5.2 Deterministic “Soft” Attention
对于stochastic “hard”attention需要每次采样attention
s
t
s_t
st的位置,在soft attention中我们需要取上下文向量
z
^
t
\hat{\mathbf{z}}_{t}
z^t的期望
E
p
(
s
t
∣
a
)
[
z
^
t
]
=
∑
i
=
1
L
α
t
,
i
a
i
\mathbb{E}_{p\left(s_{t} \mid a\right)}\left[\hat{\mathbf{z}}_{t}\right]=\sum_{i=1}^{L} \alpha_{t, i} \mathbf{a}_{i}
Ep(st∣a)[z^t]=i=1∑Lαt,iai
此时模型是可微的,可以通过反向传播来进行学习。同时,使用normalized weighted geometric mean(NWGM)这部分内容没看懂是怎么推导过来的,以及它和marginal likelihood之间的关系:
N
W
G
M
[
p
(
y
t
=
k
∣
a
)
]
=
∏
i
exp
(
n
t
,
k
,
i
)
p
(
s
t
,
i
=
1
∣
a
)
∑
j
∏
i
exp
(
n
t
,
j
,
i
)
p
(
s
t
,
i
=
1
∣
a
)
=
exp
(
E
p
(
s
t
∣
a
)
[
n
t
,
k
]
)
∑
j
exp
(
E
p
(
s
t
∣
a
)
[
n
t
,
j
]
)
\begin{aligned} \mathrm{NWGM}\left[p\left(y_{t}=k \mid \mathbf{a}\right)\right] &=\frac{\prod_{i} \exp \left(n_{t, k, i}\right)^{p\left(s_{t, i}=1 \mid a\right)}}{\sum_{j} \prod_{i} \exp \left(n_{t, j, i}\right)^{p\left(s_{t, i}=1 \mid a\right)}} \\ &=\frac{\exp \left(\mathbb{E}_{p\left(s_{t} \mid a\right)}\left[n_{t, k}\right]\right)}{\sum_{j} \exp \left(\mathbb{E}_{p\left(s_{t} \mid a\right)}\left[n_{t, j}\right]\right)} \end{aligned}
NWGM[p(yt=k∣a)]=∑j∏iexp(nt,j,i)p(st,i=1∣a)∏iexp(nt,k,i)p(st,i=1∣a)=∑jexp(Ep(st∣a)[nt,j])exp(Ep(st∣a)[nt,k])
5.2.1 DOUBLY STOCHASTIC ATTENTION
-
Whereas the attention at every point in time sums to 1 by construction也就是 ∑ i α t i = 1 \sum_{i} \alpha_{t i}=1 ∑iαti=1,the attention ∑ i α t i \sum_{i} \alpha_{t i} ∑iαtiis not constrained in any way,This makes it possible for the decoder to ignore some parts of the input image.为了缓轻这个玩意,作者==提出 ∑ t α t i ≈ τ \sum_{t} \alpha_{t i} \approx \tau ∑tαti≈τ,其中 τ ≥ L D \tau \geq \frac{L}{D} τ≥DL,并最终发现这种penalty提高了整体性能。
-
soft attention 采用负对数似然函数:
L d = − log ( p ( y ∣ a ) ) + λ ∑ i L ( 1 − ∑ t C α t i ) 2 L_{d}=-\log (p(\mathbf{y} \mid \mathbf{a}))+\lambda \sum_{i}^{L}\left(1-\sum_{t}^{C} \alpha_{t i}\right)^{2} Ld=−log(p(y∣a))+λi∑L(1−t∑Cαti)2
这里作者固定 τ \tau τ的值为1
好气啊,这部分内容没有看懂,还需要整理一些资料。
5.3 Training Procedure
-
两种注意力模型的变体都是使用随机梯度下降SGD采用自适应学习率来进行的。不同的问题,使用不同的优化算法,Flickr8k dataset中RMSProp好(可是怎么评价这个好呢?) Flickr30k/MS COCO dataset中Adam好
-
使用pretrained VGGnet without finetuning,作者使用了第四个卷积层的输出 14 × 14 × 512 14\times14\times512 14×14×512,也就是 196 × 512 ( L × D ) 196\times512(L\times D) 196×512(L×D),同时也可以对整个模型从头进行训练/微调
-
在具体实现过程中,由于我们的实现需要与每次更新最长句子的长度成比例的时间,我们发现对一组随机caption的训练在计算上是浪费的,因此在预处理中,我们构建了一个字典,将句子的长度映射到corresponding subset of caption,能够加速收敛
-
除了正则化之外,作者使用的其他正则化策略为early stopping
(六)实验
6.1 数据
- 每个图像都有5个参考caption
结果就是好看
6.2 Evaluation Procedures
- 相同的解码器架构,使用不同的卷积特征extractor,在性能的提升上是不同的。
- 在作者的结果中,仅报告了单一模型的性能,并没有进行模型的集成
6.3 定性的结果
(七)总结
- 我们还展示了如何利用所学的注意力来为模型生成过程提供更多的可解释性,并证明了所学的alignment非常符合人类的直觉。