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