典型常见的知识蒸馏方法总结二

来源:https://github.com/open-mmlab/MMRazor收录的方法

ICCV 2021:Channel-Wise Knowledge Distillation for Dense Prediction

在这里插入图片描述

首先,使用1x1卷积将教师模型与学生模型的中间层特征的通道维度进行对齐。

A 1x1 convolution layer is employed to upsample the number of channels for the student network if the number of channels mismatches between the teacher and the student.

然后,对每个通道的WxH的特征进行softmax归一化。

在这里插入图片描述

最后,使用KL散度损失来最小化学生特征与教师特征之间的距离

在这里插入图片描述

ICLR 2018 Workshop:Training Shallow and Thin Networks for Acceleration via Knowledge Distillation with Conditional Adversarial Networks

Adversarial learning for knowledge distillation can be first found in (Ref. ICLR 2018 Training Shallow and Thin Networks for Acceleration via Knowledge Distillation with Conditional Adversarial Networks).
Liu et al. (Ref. CVPR 2019:Structured Knowledge Distillation for Semantic Segmentation) shares the similar idea for semantic segmentation, named holistic distillation. We also leverage the adversarial learning performed in the output space.

Main idea: We propose to use conditional adversarial networks to learn the loss function to transfer knowledge from teacher to student.

在这里插入图片描述

CVPR 2019:Structured Knowledge Distillation for Semantic Segmentation

提出从大网络中蒸馏结构化知识到小网络中,structure knowledge
调研了两种结构化知识 (1)pair-wise similarity (2) GAN-based holistic distillation

在这里插入图片描述

Pixel-wise distillation:使用语义分割预测的每个类别的类别
### 实现多种注意力机制的Non-Local Block 为了支持不同类型的注意力机制,如Gaussian和Embedded Gaussian,在Non-Local Block中的实现主要依赖于调整`f(x)`函数的形式以及可能涉及到的不同维度变换操作。下面具体描述这两种注意力机制下的Non-Local模块是如何工作的。 #### Gaussian Attention Mechanism 在这种情况下,`f(x)`被定义为两个位置特征向量之间的高斯核函数[^1]: \[ f(\theta_i, \phi_j) = e^{-(\|\theta_i-\phi_j\|^2)/c} \] 其中\( c \)是一个常数项用于控制距离衰减的速度;而\(\theta\)和\(\phi\)分别是从输入特征映射中抽取的位置编码。这种形式下,计算成本较高因为涉及到了所有像素点间的两两比较运算。 ```python import torch.nn.functional as F def gaussian_attention(theta, phi): # Compute pairwise distances between theta and phi vectors. dists = ((theta.unsqueeze(2) - phi.unsqueeze(1)) ** 2).sum(dim=3) return (-dists / C).exp() # Apply exponential to get similarity scores. ``` #### Embedded Gaussian Attention Mechanism 相比之下,嵌入式的高斯注意力建立在一个更高效的框架之上——通过线性投影减少原始特征空间的维度后再应用相似度测量[^4]: \[ y_i=\frac{\sum_{j}(e^{{W_\theta}\cdot{x}_i)^T(W_\phi\cdot{x}_j)}h_j}{\sum_k{e^{{W_\theta}\cdot{x}_i)^T(W_\phi\cdot{x}_k)}}}, W_g\in R^{C\times d}, h_j=W_gx_j \] 这里引入了三个权重矩阵\( W_\theta \), \( W_\phi \), 和 \( W_g \),它们负责将原特征转换成较低维表示以便更快捷地完成匹配过程。此方法不仅降低了时间复杂度还提高了模型性能。 ```python class EmbeddedGaussianBlock(nn.Module): def __init__(self, in_channels, inter_channels=None): super().__init__() self.theta_conv = nn.Conv2d(in_channels, inter_channels, kernel_size=1) self.phi_conv = nn.Conv2d(in_channels, inter_channels, kernel_size=1) self.g_conv = nn.Conv2d(in_channels, inter_channels, kernel_size=1) def forward(self, x): batch_size = x.size(0) g_x = self.g_conv(x).view(batch_size, self.inter_channels, -1) g_x = g_x.permute(0, 2, 1) theta_x = self.theta_conv(x).view(batch_size, self.inter_channels, -1) theta_x = theta_x.permute(0, 2, 1) phi_x = self.phi_conv(x).view(batch_size, self.inter_channels, -1) f = torch.matmul(theta_x, phi_x) f_div_C = F.softmax(f, dim=-1) y = torch.matmul(f_div_C, g_x) y = y.permute(0, 2, 1).contiguous() y = y.view(batch_size, self.inter_channels, *x.size()[2:]) return y ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值