|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +import torch.nn.functional as F |
| 4 | + |
| 5 | +from .padding import get_padding |
| 6 | +from .conv2d_same import conv2d_same |
| 7 | + |
| 8 | + |
| 9 | +def get_weight(module): |
| 10 | + std, mean = torch.std_mean(module.weight, dim=[1, 2, 3], keepdim=True, unbiased=False) |
| 11 | + weight = (module.weight - mean) / (std + module.eps) |
| 12 | + return weight |
| 13 | + |
| 14 | + |
| 15 | +class StdConv2d(nn.Conv2d): |
| 16 | + """Conv2d with Weight Standardization. Used for BiT ResNet-V2 models. |
| 17 | +
|
| 18 | + Paper: `Micro-Batch Training with Batch-Channel Normalization and Weight Standardization` - |
| 19 | + https://arxiv.org/abs/1903.10520v2 |
| 20 | + """ |
| 21 | + def __init__( |
| 22 | + self, in_channel, out_channels, kernel_size, stride=1, |
| 23 | + padding=None, dilation=1, groups=1, bias=False, eps=1e-5): |
| 24 | + if padding is None: |
| 25 | + padding = get_padding(kernel_size, stride, dilation) |
| 26 | + super().__init__( |
| 27 | + in_channel, out_channels, kernel_size, stride=stride, |
| 28 | + padding=padding, dilation=dilation, groups=groups, bias=bias) |
| 29 | + self.eps = eps |
| 30 | + |
| 31 | + def get_weight(self): |
| 32 | + std, mean = torch.std_mean(self.weight, dim=[1, 2, 3], keepdim=True, unbiased=False) |
| 33 | + weight = (self.weight - mean) / (std + self.eps) |
| 34 | + return weight |
| 35 | + |
| 36 | + def forward(self, x): |
| 37 | + x = F.conv2d(x, self.get_weight(), self.bias, self.stride, self.padding, self.dilation, self.groups) |
| 38 | + return x |
| 39 | + |
| 40 | + |
| 41 | +class StdConv2dSame(nn.Conv2d): |
| 42 | + """Conv2d with Weight Standardization. TF compatible SAME padding. Used for ViT Hybrid model. |
| 43 | +
|
| 44 | + Paper: `Micro-Batch Training with Batch-Channel Normalization and Weight Standardization` - |
| 45 | + https://arxiv.org/abs/1903.10520v2 |
| 46 | + """ |
| 47 | + def __init__( |
| 48 | + self, in_channel, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=False, eps=1e-5): |
| 49 | + super().__init__( |
| 50 | + in_channel, out_channels, kernel_size, stride=stride, |
| 51 | + padding=0, dilation=dilation, groups=groups, bias=bias) |
| 52 | + self.eps = eps |
| 53 | + |
| 54 | + def get_weight(self): |
| 55 | + std, mean = torch.std_mean(self.weight, dim=[1, 2, 3], keepdim=True, unbiased=False) |
| 56 | + weight = (self.weight - mean) / (std + self.eps) |
| 57 | + return weight |
| 58 | + |
| 59 | + def forward(self, x): |
| 60 | + x = conv2d_same(x, self.get_weight(), self.bias, self.stride, self.padding, self.dilation, self.groups) |
| 61 | + return x |
| 62 | + |
| 63 | + |
| 64 | +class ScaledStdConv2d(nn.Conv2d): |
| 65 | + """Conv2d layer with Scaled Weight Standardization. |
| 66 | +
|
| 67 | + Paper: `Characterizing signal propagation to close the performance gap in unnormalized ResNets` - |
| 68 | + https://arxiv.org/abs/2101.08692 |
| 69 | + """ |
| 70 | + |
| 71 | + def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=None, dilation=1, groups=1, |
| 72 | + bias=True, gain=True, gamma=1.0, eps=1e-5, use_layernorm=False): |
| 73 | + if padding is None: |
| 74 | + padding = get_padding(kernel_size, stride, dilation) |
| 75 | + super().__init__( |
| 76 | + in_channels, out_channels, kernel_size, stride=stride, |
| 77 | + padding=padding, dilation=dilation, groups=groups, bias=bias) |
| 78 | + self.gain = nn.Parameter(torch.ones(self.out_channels, 1, 1, 1)) if gain else None |
| 79 | + self.scale = gamma * self.weight[0].numel() ** -0.5 # gamma * 1 / sqrt(fan-in) |
| 80 | + self.eps = eps ** 2 if use_layernorm else eps |
| 81 | + self.use_layernorm = use_layernorm # experimental, slightly faster/less GPU memory use |
| 82 | + |
| 83 | + def get_weight(self): |
| 84 | + if self.use_layernorm: |
| 85 | + weight = self.scale * F.layer_norm(self.weight, self.weight.shape[1:], eps=self.eps) |
| 86 | + else: |
| 87 | + std, mean = torch.std_mean(self.weight, dim=[1, 2, 3], keepdim=True, unbiased=False) |
| 88 | + weight = self.scale * (self.weight - mean) / (std + self.eps) |
| 89 | + if self.gain is not None: |
| 90 | + weight = weight * self.gain |
| 91 | + return weight |
| 92 | + |
| 93 | + def forward(self, x): |
| 94 | + return F.conv2d(x, self.get_weight(), self.bias, self.stride, self.padding, self.dilation, self.groups) |
0 commit comments