Action Recognition Based on Two-Stage Distillation
In the field of computer vision, CNN and Transformer have advantages in local information extraction and global feature modeling, respectively, and how to fuse CNN and Transformer has become one of the research hotspots. Some works directly introduce convolutional operations in the Transformer encoder, however, this will change the original structure of the Transformer and limit the global modeling ability of self-attention. Some other work performs knowledge distillation in the logit output layer of CNN and Transformer, however, it fails to utilize the feature layer information of CNN. Aiming at the above problems, this paper proposes the feature alignment distillation module, which realizes the feature layer distillation between Transformer and CNN by dimensionally aligning Transformer’s feature layer with CNN’s feature layer, so that Transformer learns the CNN’s local modeling ability. Aiming at the problem that the feature alignment operation will introduce the convolution operation to increase the model computation, this paper also proposes the feature mapping logit distillation module, which realizes a general distillation method for the feature layer of Transformer and CNN by mapping the feature layer of Transformer to logit. In order to enable student models to learn both local modeling ability and long-distance dependent modeling ability, this paper proposes a two-stage distillation framework, which realizes the collaborative guidance of CNN teachers and Transformer teachers to student models. The experimental results show that the method in this paper achieves feature layer distillation of CNN and Transformer, and enables the student model to learn both local modeling capability and long-distance dependency modeling capability under the collaborative guidance of CNN instructor and Transformer instructor, which improves the accuracy of the baseline model on the downstream task of action recognition.
Feature Distillation
在计算机视觉领域,卷积神经网络(CNN)和Transformer网络是两种常见模型,他们具有各自的优势。CNN由于卷积运算的局部性和权值共享机制,具有很强的归纳偏置(归纳偏置是指:在不依赖数据的情况下,将学习算法推向特解,从而高度影响学习算法的泛化能力
使用特征对齐蒸馏模块,实现了在不改变原有模型网络结构的情况下,在CNN与Transformer的特征层面进行知识蒸馏,将CNN的局部建模能力转移到Transformer中。但是,上述模块还存在一些问题。首先,为了对齐CNN与Transformer的特征层维度,本文构造了如
在
本文的主要贡献总结如下:
• 本文提出了特征对齐蒸馏模块,实现了CNN与Transformer间的特征层对齐,并将CNN的局部建模能力转移到了Transformer中。
• 本文提出了特征映射logit蒸馏模块,实现了CNN与Transformer特征层的通用蒸馏方法。
• 本文提出了两阶段蒸馏框架,通过CNN教师与Transformer教师的协同指导,使学生模型同时学习到了局部建模能力和长距离依赖建模能力。
(1)
在公式(1)中,可学习参数
和
分别确定了每个头的注意力中心和宽度,而固定参数
则表示query和key像素之间的相对位移。为了实现CNN与Transformer特征层的对齐,在
(2)
而本文提出的多头卷积注意力层,通过使用相对位置自注意
(3)
在公式(3),
和
均参照公式(1)。由于
包含可学习参数
和
,而
和
分别确定了每个头的注意力中心和宽度,所以MHCA可以自适应学习相对位置嵌入的合适范围
(4)
在公式(4)中, 和 分别为CNN教师和Transformer学生的特征层输出,aligner为特征对齐模块,MSE表示均方差损失。
基于特征的蒸馏和基于logit的蒸馏是两种常见的蒸馏方法,它们各有优势。基于特征的蒸馏的目标是使得学生模型的中间层特征尽可能接近教师模型的中间层特征,以帮助学生模型学习教师模型各层级的深层次特征。而基于logit的蒸馏的目标则是尽可能地匹配学生模型与教师模型的输出分布。相比基于特征的蒸馏,其减少了计算的复杂度。在之前的工作中,若进行蒸馏的教师模型和学生模型架构相似(如教师模型与学生模型都是CNN架构或都是Transformer架构),它们的学习表征在特征空间中表现出相似性
与2.1节中的特征对齐蒸馏模块不同,特征映射logit蒸馏模块不是直接对教师模型和学生模型的特征层输出进行蒸馏,而是通过在学生模型的特征层加入额外的logit输出分支,将学生模型特征层与教师模型特征层不匹配的表示转移到对齐的logit空间中。通过对学生模型额外的logit输出分支与教师模型的logit输出进行损失计算,使学生模型的特征层学习教师模型的预测分布,实现了特征层的通用异构网络蒸馏。
本文提出的特征映射logit蒸馏模块如
尽管不同架构的模型在logit空间中学习相同的目标,但这些模型拥有不同的归纳偏置
(5)
在公式(5)中, 是样本和类标签的联合分布, 是样本输入, 为真值标签, 和 分别为学生模型和教师模型对样本输入的预测, 和 分别为交叉熵损失函数和KL散度, 则是控制硬标签和软标签蒸馏的系数。通过将目标类和非目标类的logits信息分开引导,蒸馏损失可以表示如下:
(6)
在公式(6)中, 和 分别表示预测类和目标类,由于KL散度的分母项对梯度没有贡献,因此相比于原始蒸馏损失将分母项省去,且为了得到通用的蒸馏损失,将超参数 也省去。最后,为了增强来自目标类的信息,在 项上增加一个调制参数 ,得到如下蒸馏损失:
(7)
在3.2节和3.3节的实验中,均取
为1.1。本文将Transformer学生模型的平均分为四个部分,并在每个部分的末端进行如
(8)
在公式(8)中, 。
如
如
(9)
在公式(9)中, 为学生模型的硬标签损失,其中 为交叉熵损失,y为真值标签, 为学生模型的预测结果。 为logit蒸馏损失,其中 为KL散度, 为教师模型的预测结果。 为特征对齐蒸馏模块损失,参照公式(4)。
如
(10)
需要说明的是,第二阶段的相对位置编码与第一阶段相同。
关于2.2节提到的特征映射logit蒸馏模块,其作为2.1节的特征对齐蒸馏模块的替换。因此,若在第一阶段使用特征映射logit蒸馏模块,则第一阶段的总损失函数如下所示:
(11)
在公式(11)中, 为特征映射logit蒸馏模块损失,参照公式(8)。
按照2.3节的设置,本文在训练模型时使用两阶段框架:第一阶段,在Transformer学生模型和CNN教师模型间进行蒸馏;第二阶段,在Transformer学生模型和Transformer教师模型间进行蒸馏。
若在第一阶段使用如3.1节所表述的特征对齐蒸馏模块,则总体模型如
若在第一阶段使用如3.2节所表述的特征映射logit蒸馏模块,则第一阶段的模型如
在本章中,模型在Something-Something V2和Kinetics-400数据集上进行实验。
预训练策略。本章使用Kinetics-400数据集进行预训练,输入视频长度为16帧,每帧分辨率大为224 × 224,使用密集采样
微调和推理策略。微调阶段,将预训练完成的模型应用于下游视频分类任务,包括Kinetics-400数据集和Something-Something V2数据集。在Kinetics-400数据集上微调时,共进行150个轮次,使用密集采样
Model |
Pretrain |
Crops × Clips |
Top-1 (%) |
Top-5 (%) |
TSM
|
K400 |
3 × 2 |
63.4 |
88.5 |
TEINet
|
IN-1K |
1 × 1 |
62.1 |
- |
TDN
|
IN-1K |
1 × 1 |
65.3 |
89.5 |
ACTION-Net
|
IN-1K |
1 × 1 |
64.0 |
89.3 |
SlowFast R101, 8 × 8
|
K400 |
3 × 1 |
63.1 |
87.6 |
MSNet
|
IN-1K |
1 × 1 |
64.7 |
89.4 |
blvNet
|
IN-1K |
1 × 1 |
65.2 |
90.3 |
Timesformer-HR
|
IN-1K |
3 × 1 |
62.5 |
- |
ViVit-L/16 × 2
|
IN-1K |
3 × 1 |
65.9 |
89.9 |
MViT-B, 64 × 3
|
K400 |
3 × 1 |
67.7 |
90.9 |
Mformer-L
|
K400 |
3 × 1 |
68.1 |
91.2 |
X-ViT
|
IN-1K |
3 × 1 |
66.2 |
90.6 |
SIFAR-L
|
K400 |
3 × 1 |
64.2 |
88.4 |
Video-Swin
|
K400 |
3 × 1 |
69.6 |
92.7 |
TPS-T
|
IN-1K |
3 × 1 |
66.4 |
90.2 |
TPS-T
|
K400 |
3 × 1 |
67.9 |
90.8 |
TSD-fpld |
K400 |
3 × 2 |
70.1 |
92.3 |
Model |
Pretrain |
Crops × Clips |
Top-1 (%) |
Top-5 (%) |
I3D
|
IN-1K |
1 × 1 |
72.1 |
90.3 |
NL-I3D
|
IN-1K |
6 × 10 |
77.7 |
93.3 |
CoST
|
IN-1K |
3 × 10 |
77.5 |
93.2 |
SolwFast-R50
|
IN-1K |
3 × 10 |
75.6 |
92.1 |
续表
X3D-XL
|
- |
3 × 10 |
79.1 |
93.9 |
TSM
|
IN-1K |
3 × 10 |
74.7 |
91.4 |
TEINet
|
IN-1K |
3 × 10 |
76.2 |
92.5 |
TEA
|
IN-1K |
3 × 10 |
76.1 |
92.5 |
TDN
|
IN-1K |
3 × 10 |
77.5 |
93.2 |
Timersformer-L
|
IN-21K |
3 × 1 |
80.7 |
94.7 |
X-ViT
|
IN-21K |
3 × 1 |
80.2 |
94.7 |
MViT-B, 32 × 3
|
IN-21K |
1 × 5 |
80.2 |
94.4 |
MViT-B, 64 × 3
|
IN-21K |
3 × 3 |
81.2 |
95.1 |
Mformer-HR
|
K400 |
3 × 1 |
81.1 |
95.2 |
TokenShift-HR
|
IN-21K |
3 × 10 |
80.4 |
94.5 |
TPS-T
|
IN-1K |
3 × 4 |
78.2 |
92.2 |
TSD-fad |
K400 |
3 × 5 |
81.3 |
94.7 |
在
在本节中,通过设置多组消融实验以验证各个模块的有效性,消融实验结果见
(1) 本文研究了不同的特征蒸馏模块对模型的影响。结果如
(2)
Something-Something V2 |
Kinetics-400 |
|
TSD-fpld (w/o) |
69.5 |
80.7 |
TSD-fpld |
70.1 |
81.0 |
TSD-fad (w/o) |
69.1 |
80.9 |
TSD-fad |
69.6 |
81.3 |
Layer4 |
Layer3 |
Layer2 |
Layer1 |
Top-1 (%) |
69.5 |
||||
√ |
69.9 |
|||
√ |
√ |
70.0 |
||
√ |
√ |
√ |
70.0 |
|
√ |
√ |
√ |
√ |
70.1 |
Stage1 epochs |
0 |
50 |
75 |
100 |
125 |
150 |
Top-1 (%) |
69.3 |
69.8 |
69.9 |
70.1 |
69.7 |
69.5 |
Lfeature |
Llabel (stage1) |
Llogit (stage1) |
Llabe l (stage2) |
Llogit (stage2) |
Top-1 (%) |
√ |
√ |
80.5 |
|||
√ |
√ |
√ |
80.8 |
||
√ |
√ |
√ |
√ |
80.9 |
|
√ |
√ |
√ |
√ |
√ |
81.3 |
模型进行蒸馏,四个分支分别记为Layer1、Layer2、Layer3和Layer4,如
(3)
(4)
本文针对Transformer网络缺少一定的归纳偏置的问题,提出了特征对齐蒸馏模块。该模块通过将Transformer的特征层与CNN的特征层进行维度对齐,实现了Transformer与CNN的特征层蒸馏,使Transformer学习到了CNN的局部建模能力。针对特征对齐操作会引入卷积操作增加模型计算量的问题,本文又提出了特征映射logit蒸馏模块,通过将Transformer的特征层映射为logit,实现了Transformer与CNN特征层的通用蒸馏方法。为了使学生模型同时学习局部建模能力和长距离依赖建模能力,本文提出了两阶段蒸馏框架,实现了CNN教师和Transformer教师对学生模型的协同指导。本文提出的方法在Something-Something V2和Kinetics-400数据集上分别取得了70.1%和81.3%的Top-1准确率,具有良好的性能。
安徽省自然科学基金项目(2408085MF157)。