-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdn.py
95 lines (76 loc) · 2.87 KB
/
gdn.py
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
import torch
import torch.utils.data
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Function
class LowerBound(Function):
@staticmethod
def forward(ctx, inputs, bound):
b = torch.ones(inputs.size(), device=inputs.device)*bound
b = b.to(inputs.device)
ctx.save_for_backward(inputs, b)
return torch.max(inputs, b)
@staticmethod
def backward(ctx, grad_output):
inputs, b = ctx.saved_tensors
pass_through_1 = inputs >= b
pass_through_2 = grad_output < 0
pass_through = pass_through_1 | pass_through_2
return pass_through.type(grad_output.dtype) * grad_output, None
class GDN(nn.Module):
"""Generalized divisive normalization layer.
y[i] = x[i] / sqrt(beta[i] + sum_j(gamma[j, i] * x[j]^2))
"""
def __init__(self,
ch,
device,
inverse=False,
beta_min=1e-6,
gamma_init=.1,
reparam_offset=2**-18):
super(GDN, self).__init__()
self.inverse = inverse
self.beta_min = beta_min
self.gamma_init = gamma_init
self.reparam_offset = torch.tensor([reparam_offset], device=device)
self.build(ch, torch.device(device))
def build(self, ch, device):
self.pedestal = self.reparam_offset**2
self.beta_bound = (self.beta_min + self.reparam_offset**2)**.5
self.gamma_bound = self.reparam_offset
# Create beta param
beta = torch.sqrt(torch.ones(ch, device=device)+self.pedestal)
self.beta = nn.Parameter(beta)
# Create gamma param
eye = torch.eye(ch, device=device)
g = self.gamma_init*eye
g = g + self.pedestal
gamma = torch.sqrt(g)
self.gamma = nn.Parameter(gamma)
def forward(self, inputs):
unfold = False
if inputs.dim() == 5:
unfold = True
bs, ch, d, w, h = inputs.size()
inputs = inputs.view(bs, ch, d*w, h)
_, ch, _, _ = inputs.size()
# Beta bound and reparam
beta = LowerBound.apply(self.beta, self.beta_bound)
beta = beta**2 - self.pedestal
# Gamma bound and reparam
gamma = LowerBound.apply(self.gamma, self.gamma_bound)
gamma = gamma**2 - self.pedestal
gamma = gamma.view(ch, ch, 1, 1)
# Norm pool calc
norm_ = nn.functional.conv2d(inputs**2, gamma, beta)
norm_ = torch.sqrt(norm_)
# Apply norm
if self.inverse:
outputs = inputs * norm_
else:
outputs = inputs / norm_
if unfold:
outputs = outputs.view(bs, ch, d, w, h)
return outputs