1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| import torch import torch.nn as nn
class SwitchNorm1d(nn.Module): def __init__(self, num_features, eps=1e-5, momentum=0.997, using_moving_average=True): super(SwitchNorm1d, self).__init__() self.eps = eps self.momentum = momentum self.using_moving_average = using_moving_average self.weight = nn.Parameter(torch.ones(1, num_features)) self.bias = nn.Parameter(torch.zeros(1, num_features)) self.mean_weight = nn.Parameter(torch.ones(2)) self.var_weight = nn.Parameter(torch.ones(2)) self.register_buffer('running_mean', torch.zeros(1, num_features)) self.register_buffer('running_var', torch.zeros(1, num_features)) self.reset_parameters()
def reset_parameters(self): self.running_mean.zero_() self.running_var.zero_() self.weight.data.fill_(1) self.bias.data.zero_()
def _check_input_dim(self, input): if input.dim() != 2: raise ValueError('expected 2D input (got {}D input)' .format(input.dim()))
def forward(self, x): self._check_input_dim(x) mean_ln = x.mean(1, keepdim=True) var_ln = x.var(1, keepdim=True)
if self.training: mean_bn = x.mean(0, keepdim=True) var_bn = x.var(0, keepdim=True) if self.using_moving_average: self.running_mean.mul_(self.momentum) self.running_mean.add_((1 - self.momentum) * mean_bn.data) self.running_var.mul_(self.momentum) self.running_var.add_((1 - self.momentum) * var_bn.data) else: self.running_mean.add_(mean_bn.data) self.running_var.add_(mean_bn.data ** 2 + var_bn.data) else: mean_bn = torch.autograd.Variable(self.running_mean) var_bn = torch.autograd.Variable(self.running_var)
softmax = nn.Softmax(0) mean_weight = softmax(self.mean_weight) var_weight = softmax(self.var_weight)
mean = mean_weight[0] * mean_ln + mean_weight[1] * mean_bn var = var_weight[0] * var_ln + var_weight[1] * var_bn
x = (x - mean) / (var + self.eps).sqrt() return x * self.weight + self.bias
class SwitchNorm2d(nn.Module): def __init__(self, num_features, eps=1e-5, momentum=0.9, using_moving_average=True, using_bn=True, last_gamma=False): super(SwitchNorm2d, self).__init__() self.eps = eps self.momentum = momentum self.using_moving_average = using_moving_average self.using_bn = using_bn self.last_gamma = last_gamma self.weight = nn.Parameter(torch.ones(1, num_features, 1, 1)) self.bias = nn.Parameter(torch.zeros(1, num_features, 1, 1)) if self.using_bn: self.mean_weight = nn.Parameter(torch.ones(3)) self.var_weight = nn.Parameter(torch.ones(3)) else: self.mean_weight = nn.Parameter(torch.ones(2)) self.var_weight = nn.Parameter(torch.ones(2)) if self.using_bn: self.register_buffer('running_mean', torch.zeros(1, num_features, 1)) self.register_buffer('running_var', torch.zeros(1, num_features, 1))
self.reset_parameters()
def reset_parameters(self): if self.using_bn: self.running_mean.zero_() self.running_var.zero_() if self.last_gamma: self.weight.data.fill_(0) else: self.weight.data.fill_(1) self.bias.data.zero_()
def _check_input_dim(self, input): if input.dim() != 4: raise ValueError('expected 4D input (got {}D input)' .format(input.dim()))
def forward(self, x): self._check_input_dim(x) N, C, H, W = x.size() x = x.view(N, C, -1) mean_in = x.mean(-1, keepdim=True) var_in = x.var(-1, keepdim=True)
mean_ln = mean_in.mean(1, keepdim=True) temp = var_in + mean_in ** 2 var_ln = temp.mean(1, keepdim=True) - mean_ln ** 2
if self.using_bn: if self.training: mean_bn = mean_in.mean(0, keepdim=True) var_bn = temp.mean(0, keepdim=True) - mean_bn ** 2 if self.using_moving_average: self.running_mean.mul_(self.momentum) self.running_mean.add_((1 - self.momentum) * mean_bn.data) self.running_var.mul_(self.momentum) self.running_var.add_((1 - self.momentum) * var_bn.data) else: self.running_mean.add_(mean_bn.data) self.running_var.add_(mean_bn.data ** 2 + var_bn.data) else: mean_bn = torch.autograd.Variable(self.running_mean) var_bn = torch.autograd.Variable(self.running_var)
softmax = nn.Softmax(0) mean_weight = softmax(self.mean_weight) var_weight = softmax(self.var_weight)
if self.using_bn: mean = mean_weight[0] * mean_in + mean_weight[1] * mean_ln + mean_weight[2] * mean_bn var = var_weight[0] * var_in + var_weight[1] * var_ln + var_weight[2] * var_bn else: mean = mean_weight[0] * mean_in + mean_weight[1] * mean_ln var = var_weight[0] * var_in + var_weight[1] * var_ln
x = (x-mean) / (var+self.eps).sqrt() x = x.view(N, C, H, W) return x * self.weight + self.bias
class SwitchNorm3d(nn.Module): def __init__(self, num_features, eps=1e-5, momentum=0.997, using_moving_average=True, using_bn=True, last_gamma=False): super(SwitchNorm3d, self).__init__() self.eps = eps self.momentum = momentum self.using_moving_average = using_moving_average self.using_bn = using_bn self.last_gamma = last_gamma self.weight = nn.Parameter(torch.ones(1, num_features, 1, 1, 1)) self.bias = nn.Parameter(torch.zeros(1, num_features, 1, 1, 1)) if self.using_bn: self.mean_weight = nn.Parameter(torch.ones(3)) self.var_weight = nn.Parameter(torch.ones(3)) else: self.mean_weight = nn.Parameter(torch.ones(2)) self.var_weight = nn.Parameter(torch.ones(2)) if self.using_bn: self.register_buffer('running_mean', torch.zeros(1, num_features, 1)) self.register_buffer('running_var', torch.zeros(1, num_features, 1))
self.reset_parameters()
def reset_parameters(self): if self.using_bn: self.running_mean.zero_() self.running_var.zero_() if self.last_gamma: self.weight.data.fill_(0) else: self.weight.data.fill_(1) self.bias.data.zero_()
def _check_input_dim(self, input): if input.dim() != 5: raise ValueError('expected 5D input (got {}D input)' .format(input.dim()))
def forward(self, x): self._check_input_dim(x) N, C, D, H, W = x.size() x = x.view(N, C, -1) mean_in = x.mean(-1, keepdim=True) var_in = x.var(-1, keepdim=True)
mean_ln = mean_in.mean(1, keepdim=True) temp = var_in + mean_in ** 2 var_ln = temp.mean(1, keepdim=True) - mean_ln ** 2
if self.using_bn: if self.training: mean_bn = mean_in.mean(0, keepdim=True) var_bn = temp.mean(0, keepdim=True) - mean_bn ** 2 if self.using_moving_average: self.running_mean.mul_(self.momentum) self.running_mean.add_((1 - self.momentum) * mean_bn.data) self.running_var.mul_(self.momentum) self.running_var.add_((1 - self.momentum) * var_bn.data) else: self.running_mean.add_(mean_bn.data) self.running_var.add_(mean_bn.data ** 2 + var_bn.data) else: mean_bn = torch.autograd.Variable(self.running_mean) var_bn = torch.autograd.Variable(self.running_var)
softmax = nn.Softmax(0) mean_weight = softmax(self.mean_weight) var_weight = softmax(self.var_weight)
if self.using_bn: mean = mean_weight[0] * mean_in + mean_weight[1] * mean_ln + mean_weight[2] * mean_bn var = var_weight[0] * var_in + var_weight[1] * var_ln + var_weight[2] * var_bn else: mean = mean_weight[0] * mean_in + mean_weight[1] * mean_ln var = var_weight[0] * var_in + var_weight[1] * var_ln
x = (x - mean) / (var + self.eps).sqrt() x = x.view(N, C, D, H, W) return x * self.weight + self.bias
|