全局響應歸一化(Global Response Normalization,GRN)是ConvNeXtV2中提出的一種歸一化方法,其實也就是一種注意力機制,跟視覺中常用的SE、ECA、CBAM的作用一樣,就是對特征進行重標定。
GRN的pytorch代碼如下:
import torch
from torch import nn as nn
class GlobalResponseNorm(nn.Module):
""" Global Response Normalization layer
"""
def __init__(self, dim, eps=1e-6, channels_last=True):
super().__init__()
self.eps = eps
if channels_last:
self.spatial_dim = (1, 2)
self.channel_dim = -1
self.wb_shape = (1, 1, 1, -1)
else:
self.spatial_dim = (2, 3)
self.channel_dim = 1
self.wb_shape = (1, -1, 1, 1)
self.weight = nn.Parameter(torch.zeros(dim))
self.bias = nn.Parameter(torch.zeros(dim))
def forward(self, x):
x_g = x.norm(p=2, dim=self.spatial_dim, keepdim=True)
x_n = x_g / (x_g.mean(dim=self.channel_dim, keepdim=True) + self.eps)
out=x + torch.addcmul(self.bias.view(self.wb_shape), self.weight.view(self.wb_shape), x * x_n)
return out
if __name__ == "__main__":
net = GlobalResponseNorm(dim=96,channels_last=False)
x = torch.randn(5, 96, 112, 112)
out = net(x)
GRN主要由全局特征聚合、特征歸一化和特征校準三部分組成。
其中全局特征聚合的代碼是:
x_g = x.norm(p=2, dim=self.spatial_dim, keepdim=True)
通過在H和W維度上使用L2范數,把空間特征聚合成為一個向量,其實也可以使用類似SE里的全局平均池化層,主要用于獲取全局性的通道信息。
特征歸一化的代碼是:
x_n = x_g / (x_g.mean(dim=self.channel_dim, keepdim=True) + self.eps)
用于計算當前通道相對于其他通道的相對重要性,其值在0~1之間,該方法類似于SE里的sigmoid輸出。
特征校準的代碼是:
out=x + torch.addcmul(self.bias.view(self.wb_shape), self.weight.view(self.wb_shape), x * x_n)
這就是一個特征重標定的過程,特征歸一化輸出的其實是一個權重值,這個值載荷輸入x相乘就能獲得每個通道的重要程度,GRN中還加入了兩個可學習參數weight和bias用于優(yōu)化。
同時GRN里還使用了跳躍連接,論文說是為了更好的地用于訓練優(yōu)化。