You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
5.7 KiB
134 lines
5.7 KiB
4 years ago
|
""" Convolution with Weight Standardization (StdConv and ScaledStdConv)
|
||
|
|
||
|
StdConv:
|
||
|
@article{weightstandardization,
|
||
|
author = {Siyuan Qiao and Huiyu Wang and Chenxi Liu and Wei Shen and Alan Yuille},
|
||
|
title = {Weight Standardization},
|
||
|
journal = {arXiv preprint arXiv:1903.10520},
|
||
|
year = {2019},
|
||
|
}
|
||
|
Code: https://github.com/joe-siyuan-qiao/WeightStandardization
|
||
|
|
||
|
ScaledStdConv:
|
||
|
Paper: `Characterizing signal propagation to close the performance gap in unnormalized ResNets`
|
||
|
- https://arxiv.org/abs/2101.08692
|
||
|
Official Deepmind JAX code: https://github.com/deepmind/deepmind-research/tree/master/nfnets
|
||
|
|
||
|
Hacked together by / copyright Ross Wightman, 2021.
|
||
|
"""
|
||
4 years ago
|
import torch
|
||
|
import torch.nn as nn
|
||
|
import torch.nn.functional as F
|
||
|
|
||
4 years ago
|
from .padding import get_padding, get_padding_value, pad_same
|
||
4 years ago
|
|
||
|
|
||
|
class StdConv2d(nn.Conv2d):
|
||
|
"""Conv2d with Weight Standardization. Used for BiT ResNet-V2 models.
|
||
|
|
||
|
Paper: `Micro-Batch Training with Batch-Channel Normalization and Weight Standardization` -
|
||
|
https://arxiv.org/abs/1903.10520v2
|
||
|
"""
|
||
|
def __init__(
|
||
4 years ago
|
self, in_channel, out_channels, kernel_size, stride=1, padding=None,
|
||
|
dilation=1, groups=1, bias=False, eps=1e-6):
|
||
4 years ago
|
if padding is None:
|
||
|
padding = get_padding(kernel_size, stride, dilation)
|
||
|
super().__init__(
|
||
|
in_channel, out_channels, kernel_size, stride=stride,
|
||
|
padding=padding, dilation=dilation, groups=groups, bias=bias)
|
||
|
self.eps = eps
|
||
|
|
||
|
def forward(self, x):
|
||
4 years ago
|
weight = F.batch_norm(
|
||
3 years ago
|
self.weight.reshape(1, self.out_channels, -1), None, None,
|
||
4 years ago
|
training=True, momentum=0., eps=self.eps).reshape_as(self.weight)
|
||
4 years ago
|
x = F.conv2d(x, weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
|
||
4 years ago
|
return x
|
||
|
|
||
|
|
||
|
class StdConv2dSame(nn.Conv2d):
|
||
|
"""Conv2d with Weight Standardization. TF compatible SAME padding. Used for ViT Hybrid model.
|
||
|
|
||
|
Paper: `Micro-Batch Training with Batch-Channel Normalization and Weight Standardization` -
|
||
|
https://arxiv.org/abs/1903.10520v2
|
||
|
"""
|
||
|
def __init__(
|
||
4 years ago
|
self, in_channel, out_channels, kernel_size, stride=1, padding='SAME',
|
||
|
dilation=1, groups=1, bias=False, eps=1e-6):
|
||
4 years ago
|
padding, is_dynamic = get_padding_value(padding, kernel_size, stride=stride, dilation=dilation)
|
||
4 years ago
|
super().__init__(
|
||
4 years ago
|
in_channel, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation,
|
||
|
groups=groups, bias=bias)
|
||
|
self.same_pad = is_dynamic
|
||
4 years ago
|
self.eps = eps
|
||
|
|
||
|
def forward(self, x):
|
||
4 years ago
|
if self.same_pad:
|
||
|
x = pad_same(x, self.kernel_size, self.stride, self.dilation)
|
||
4 years ago
|
weight = F.batch_norm(
|
||
3 years ago
|
self.weight.reshape(1, self.out_channels, -1), None, None,
|
||
4 years ago
|
training=True, momentum=0., eps=self.eps).reshape_as(self.weight)
|
||
4 years ago
|
x = F.conv2d(x, weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
|
||
4 years ago
|
return x
|
||
|
|
||
|
|
||
|
class ScaledStdConv2d(nn.Conv2d):
|
||
|
"""Conv2d layer with Scaled Weight Standardization.
|
||
|
|
||
|
Paper: `Characterizing signal propagation to close the performance gap in unnormalized ResNets` -
|
||
|
https://arxiv.org/abs/2101.08692
|
||
4 years ago
|
|
||
|
NOTE: the operations used in this impl differ slightly from the DeepMind Haiku impl. The impact is minor.
|
||
4 years ago
|
"""
|
||
|
|
||
4 years ago
|
def __init__(
|
||
4 years ago
|
self, in_channels, out_channels, kernel_size, stride=1, padding=None,
|
||
|
dilation=1, groups=1, bias=True, gamma=1.0, eps=1e-6, gain_init=1.0):
|
||
4 years ago
|
if padding is None:
|
||
|
padding = get_padding(kernel_size, stride, dilation)
|
||
|
super().__init__(
|
||
4 years ago
|
in_channels, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation,
|
||
|
groups=groups, bias=bias)
|
||
4 years ago
|
self.gain = nn.Parameter(torch.full((self.out_channels, 1, 1, 1), gain_init))
|
||
4 years ago
|
self.scale = gamma * self.weight[0].numel() ** -0.5 # gamma * 1 / sqrt(fan-in)
|
||
4 years ago
|
self.eps = eps
|
||
4 years ago
|
|
||
|
def forward(self, x):
|
||
4 years ago
|
weight = F.batch_norm(
|
||
3 years ago
|
self.weight.reshape(1, self.out_channels, -1), None, None,
|
||
4 years ago
|
weight=(self.gain * self.scale).view(-1),
|
||
4 years ago
|
training=True, momentum=0., eps=self.eps).reshape_as(self.weight)
|
||
4 years ago
|
return F.conv2d(x, weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
|
||
4 years ago
|
|
||
|
|
||
|
class ScaledStdConv2dSame(nn.Conv2d):
|
||
|
"""Conv2d layer with Scaled Weight Standardization and Tensorflow-like SAME padding support
|
||
|
|
||
|
Paper: `Characterizing signal propagation to close the performance gap in unnormalized ResNets` -
|
||
|
https://arxiv.org/abs/2101.08692
|
||
4 years ago
|
|
||
|
NOTE: the operations used in this impl differ slightly from the DeepMind Haiku impl. The impact is minor.
|
||
4 years ago
|
"""
|
||
|
|
||
|
def __init__(
|
||
4 years ago
|
self, in_channels, out_channels, kernel_size, stride=1, padding='SAME',
|
||
|
dilation=1, groups=1, bias=True, gamma=1.0, eps=1e-6, gain_init=1.0):
|
||
4 years ago
|
padding, is_dynamic = get_padding_value(padding, kernel_size, stride=stride, dilation=dilation)
|
||
|
super().__init__(
|
||
|
in_channels, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation,
|
||
|
groups=groups, bias=bias)
|
||
4 years ago
|
self.gain = nn.Parameter(torch.full((self.out_channels, 1, 1, 1), gain_init))
|
||
4 years ago
|
self.scale = gamma * self.weight[0].numel() ** -0.5
|
||
|
self.same_pad = is_dynamic
|
||
4 years ago
|
self.eps = eps
|
||
4 years ago
|
|
||
|
def forward(self, x):
|
||
4 years ago
|
if self.same_pad:
|
||
|
x = pad_same(x, self.kernel_size, self.stride, self.dilation)
|
||
4 years ago
|
weight = F.batch_norm(
|
||
3 years ago
|
self.weight.reshape(1, self.out_channels, -1), None, None,
|
||
4 years ago
|
weight=(self.gain * self.scale).view(-1),
|
||
4 years ago
|
training=True, momentum=0., eps=self.eps).reshape_as(self.weight)
|
||
4 years ago
|
return F.conv2d(x, weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
|