TransFG Architecture
- Overlapping patch split:ViT 是把图片分成一系列不重叠的 patches,作者认为这可能会破坏 discriminative regions. 为了解决上述问题,作者提出使用 Overlapping patch split,划分的 patch 数
N
N
N 为
其中, P P P 为 patch 大小, S S S 为步长 - Part Selection Module:FGVC 需要模型精准定位出图像中用来区分相似类别的 discriminative regions. 如下图所示,模型需要关注到鸟的眼睛以及喉咙才能对它们进行正确分类。CNN 架构的网络常用 RPN 或是弱监督分割算法来解决这一问题
而作者认为,ViT 的多头自注意力机制就已经可以很好地帮助模型区分出显著特征区域了。假设有 L L L 个 Transformer 层,注意力头数为 K K K,batch size 为 b s bs bs,图像的分块数为 p p p,作者将前 L − 1 L-1 L−1 层的注意力矩阵 a l ∈ R b s × K × ( p + 1 ) × ( p + 1 ) a_l\in\R^{bs\times K\times (p+1)\times(p+1)} al∈Rbs×K×(p+1)×(p+1) ( ( p + 1 ) × ( p + 1 ) (p+1)\times(p+1) (p+1)×(p+1) 是 [CLS] 和各个 patch 之间的注意力矩阵) 用矩阵乘积相乘来融合前 L − 1 L-1 L−1 层的的注意力权重信息
其中 a f i n a l ∈ R b s × K × ( p + 1 ) × ( p + 1 ) a_{final}\in\R^{bs\times K\times (p+1)\times(p+1)} afinal∈Rbs×K×(p+1)×(p+1). 然后作者根据每个 head 内 [CLS] 和各个 patch 的注意力权重来选择 K K K 个显著特征区域 (疑问:这个选择的过程是不可导的,因此是不会被优化的,但是直接把所有注意力矩阵乘起来有很高的可解释性吗?这一点作者没有做出详细的解释),例如对于样本 i i i 的 head j j j,选取出的显著特征区域索引为 torch.max ( a f i n a l [ i , j , 0 , 1 : ] , dim = − 1 ) [ 1 ] \text{torch.max} (a_{final}[i,j,0,1:], \text{dim}=-1)[1] torch.max(afinal[i,j,0,1:],dim=−1)[1]. 对于每个样本,最终可以选出 K K K 个显著特征区域。作者最终只将选出的 K K K 个 patch 和 [CLS] 输入最后一个 Transformer 层得到最终的预测结果 - Contrastive Feature Learning:
其中,为了防止对比损失项被简单负样本 (相似度较小的负样本对) 淹没,作者加入了阈值 α = 0.4 \alpha=0.4 α=0.4,只有当负样本对的相似度大于 α \alpha α 才会计算其对比损失 - Total loss
Experiments
Ablation Study
- Influence of image patch split method.
- Influence of Part Selection Module.
- Influence of contrastive loss.
在可视化 selected top-4 token positions 时,作者将 patch 位置不变,大小放大了一倍
References
- paper: He, Ju, et al. “Transfg: A transformer architecture for fine-grained recognition.” Proceedings of the AAAI Conference on Artificial Intelligence. Vol. 36. No. 1. 2022.
- code: https://github.com/TACJu/TransFG
- Warning: several open issues on Github are about reproducible problems and the authors did not respond actively… (About CUB-200-2011’s accuracy, About CUB ACC, About the training details)