Contents
Introduction
- 作者将自监督对比学习扩展为了有监督对比学习 (SupCon) 来充分利用数据集提供的标签信息。有监督对比学习使得同类的样本特征彼此接近,不同类的样本特征彼此远离,并且对每个 anchor 而言,有监督对比学习使用多个正样本和多个负样本,相比之下,triplet loss 只使用一个正样本和一个负样本,N-pair loss 只使用一个正样本和多个负样本,这使得有监督对比学习不需要负样本挖掘就能达到不错的性能
Method
Representation Learning Framework
- Stage 1. 给定一个 batch 的数据,首先给每个样本进行两次数据增强,然后将数据增强后的样本输入 encoder 得到 2048 维的 normalized embed,经过 projection network 得到最终输出,计算 supervised contrastive loss
- Stage 2. 丢掉 projection network,冻结 backbone 参数,新增全连接层,使用 CE loss 训练 (The linear classifier can also be trained jointly with the encoder, as long as it doesn’t propagate gradients back to the encoder.)
Contrastive Loss Functions
Self-Supervised Contrastive Loss
- 其中, i ∈ I = { 1 , . . . , 2 N } i\in I=\{1,...,2N\} i∈I={1,...,2N} 为 multiviewed batch (i.e., 数据增强后 batch) 内样本的索引, j ( i ) j(i) j(i) 为与 i i i 从同一图像得到的增强图像索引, A ( i ) = I \ { i } A(i)=I\backslash\{i\} A(i)=I\{i}, z i = P r o j ( E n c ( x ~ i ) ) z_i=Proj(Enc(\tilde x_i)) zi=Proj(Enc(x~i)), x ~ i \tilde x_i x~i 为数据增强后的图像
Supervised Contrastive Losses
- 引入标签信息后,一个 anchor 可以找到 multiviewed batch 内属于同一类别的多个样本作为正样本,下面两式均可将自监督对比学习推广到有监督对比学习:
其中 P ( i ) = { p ∈ A ( i ) : y p = y i } P(i)=\{p\in A(i): y_p= y_i\} P(i)={p∈A(i):yp=yi}. - 在
L
o
u
t
s
u
p
\mathcal L_{out}^{sup}
Loutsup 中,summation over positives 在 log 外,而在
L
i
n
s
u
p
\mathcal L_{in}^{sup}
Linsup 中,summation over positives 在 log 内。虽然它们从形式上看差不多,但实验证明
L
o
u
t
s
u
p
\mathcal L_{out}^{sup}
Loutsup 的性能更好
作者认为这是 normalization factor 1 / ∣ P ( i ) ∣ 1/|P(i)| 1/∣P(i)∣ 带来的影响。对于 L o u t s u p \mathcal L_{out}^{sup} Loutsup,normalization factor 可以 “remove bias present in the positives in a multiviewed batch contributing to the loss”,而对于 L i n s u p \mathcal L_{in}^{sup} Linsup,由于 normalization factor 在 log 内,因此它只是一个常数项,并不会影响参数梯度。下面作者利用梯度推导进行了说明,最终作者选择使用 L o u t s u p {\mathcal L_{out}^{sup}} Loutsup 的形式作为有监督对比损失
Gradient Derivation
- (1)
L
i
n
s
u
p
\boldsymbol {\mathcal L_{in}^{sup}}
Linsup
其中,
- (2)
L
o
u
t
s
u
p
\boldsymbol {\mathcal L_{out}^{sup}}
Loutsup
其中,
- 现在两种形式的损失函数梯度可以统一地写为
其中,
If each z p z_p zp is set to the (less biased) mean positive representation vector, z ˉ \bar z zˉ, X i p i n X_{ip}^{in} Xipin 就等价于 X i p o u t X_{ip}^{out} Xipout
作者认为 using the mean of positives benefits training,因此 L o u t s u p {\mathcal L_{out}^{sup}} Loutsup 性能更好
Intrinsic Hard Positive and Negative Mining Properties
- Intrinsic ability to perform hard positive/negative mining. 当在 projection head 后加上 normalization 时,有监督对比学习产生的梯度信息就能隐式地进行 hard positive/negative mining,hard positives/negatives 的梯度更大,easy positives/negatives 的梯度更小,并且对于难正样本而言,负样本数量越多这一效果越明显。因此,作者认为有监督对比学习无需进行显式的难样本挖掘。下面进行具体分析
- 设
w
i
w_i
wi 为 projection head 的 unnormalized output,i.e.,
z
i
=
w
i
/
∥
w
i
∥
z_i=w_i/\|w_i\|
zi=wi/∥wi∥,有
其中,
代入之前推导的梯度公式,有
其中,
- 下面以正样本为例,考虑
∂
L
i
sup
∂
w
i
∣
P
(
i
)
\left.\frac{\partial \mathcal{L}_i^{\sup }}{\partial w_i}\right|_{\mathrm{P}(\mathrm{i})}
∂wi∂Lisup
P(i). 对简单正样本
z
i
⋅
z
p
≈
1
z_i\cdot z_p\approx1
zi⋅zp≈1,有
对难正样本 z i ⋅ z p ≈ 0 z_i\cdot z_p\approx0 zi⋅zp≈0,有
此时考虑 L o u t s u p \mathcal L_{out}^{sup} Loutsup 的梯度模长,有
其中, ∑ n ∈ N ( i ) exp ( z i ⋅ z n / τ ) ≥ 0 \sum_{n \in N(i)} \exp \left(\boldsymbol{z}_i \cdot \boldsymbol{z}_n / \tau\right) \geq 0 ∑n∈N(i)exp(zi⋅zn/τ)≥0 (assuming z i ⋅ z n ≤ 0 \boldsymbol z_i \cdot \boldsymbol z_n \leq 0 zi⋅zn≤0), ∑ p ′ ∈ P ( i ) exp ( z i ⋅ z p ′ / τ ) − ∣ P ( i ) ∣ ≥ 0 \sum_{p^{\prime} \in P(i)} \exp \left(\boldsymbol{z}_i \cdot \boldsymbol{z}_{p^{\prime}} / \tau\right)-|P(i)| \geq 0 ∑p′∈P(i)exp(zi⋅zp′/τ)−∣P(i)∣≥0 (assuming z i ⋅ z p ′ ≥ 0 \boldsymbol z_i \cdot \boldsymbol z_{p'} \geq 0 zi⋅zp′≥0). 因此, ∑ n ∈ N ( i ) exp ( z i ⋅ z n / τ ) \sum_{n \in N(i)} \exp \left(\boldsymbol{z}_i \cdot \boldsymbol{z}_n / \tau\right) ∑n∈N(i)exp(zi⋅zn/τ) 和 ∑ p ′ ∈ P ( i ) exp ( z i ⋅ z p ′ / τ ) − ∣ P ( i ) ∣ \sum_{p^{\prime} \in P(i)} \exp \left(\boldsymbol{z}_i \cdot \boldsymbol{z}_{p^{\prime}} / \tau\right)-|P(i)| ∑p′∈P(i)exp(zi⋅zp′/τ)−∣P(i)∣ 越大,即正负样本的数量越多,难正样本的梯度模长也就越大,难正样本挖掘的效果也就越明显 - 对于负样本也可以推出类似结论
- 总的来说,增加 batch size 可以增加正负样本数量,进而增强难正负样本挖掘的效果,同时也增加了选取到更合适的难正负样本的可能,因此大 batch size 可以有效增加有监督对比学习的性能;此外,上述推导过程也说明 projection head 后的 normalization layer 是十分必要的,它有助于进行隐式的难正负样本挖掘
Connection to Triplet Loss and N-pairs Loss
- Triplet Loss 和 N-pairs Loss 是有监督对比学习的特例。当 batch 内只有一个正样本和一个负样本时,有监督对比学习等价于 Triplet Loss,而当 batch 内只有一个正样本和多个负样本时,有监督对比学习等价于 N-pairs Loss.
- Triplet Loss.
- N-pairs Loss.
Experiments
Classification Accuracy