-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgdn_v2.py
103 lines (85 loc) · 3.12 KB
/
gdn_v2.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
96
97
98
99
100
101
102
103
# The implementation of GDN is inherited from
# https://github.com/jorge-pessoa/pytorch-gdn,
# under the MIT License.
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())*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
lower_bound = LowerBound.apply
class GDN(nn.Module):
"""Generalized divisive normalization layer.
y[i] = x[i] / sqrt(beta[i] + sum_j(gamma[j, i] * x[j]))
"""
def __init__(self,
ch,
device=None,
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_tensor = torch.FloatTensor([reparam_offset])
self.register_buffer('reparam_offset', self.reparam_offset_tensor)
self.build(ch)
def build(self, ch):
self.pedestal_tensor = self.reparam_offset**2
self.register_buffer('pedestal', self.pedestal_tensor)
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)+self.pedestal)
self.beta = nn.Parameter(beta)
self.register_parameter('beta', self.beta)
# Create gamma param
eye = torch.eye(ch)
g = self.gamma_init*eye
g = g + self.pedestal
gamma = torch.sqrt(g)
self.gamma = nn.Parameter(gamma)
self.register_parameter('gamma', self.gamma)
# self.pedestal = self.pedestal
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 = lower_bound(self.beta, self.beta_bound)
beta = beta**2 - self.pedestal
# Gamma bound and reparam
gamma = lower_bound(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