-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdn_3d.py
159 lines (123 loc) · 4.57 KB
/
gdn_3d.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
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
# 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
import numpy as np
class LowerBound(Function):
"""
Low_bound make the numerical calculation close to the bound
"""
@staticmethod
def forward(ctx, x, y):
ctx.save_for_backward(x, y*torch.ones_like(x))
x = torch.clamp(x, min=y)
return x
@staticmethod
def backward(ctx, g):
x, y = ctx.saved_tensors
grad1 = g.clone()
pass_through_if = torch.logical_or(x >= y, g < 0)
t = pass_through_if
return grad1*t, None
lower_bound = LowerBound.apply
class GDN3d(nn.Module):
"""Generalized divisive normalization layer.
y[i] = x[i] / sqrt(beta[i] + sum_j(gamma[j, i] * x[j]))
"""
def __init__(self,
ch,
inverse=False,
beta_min=1e-6,
gamma_init=.1,
reparam_offset=2**-18):
super(GDN3d, self).__init__()
self.inverse = inverse
self.beta_min = beta_min
self.gamma_init = gamma_init
self.reparam_offset = reparam_offset
self.build(ch)
def build(self, ch):
self.pedestal_data = 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)+self.pedestal_data)
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_data
gamma = torch.sqrt(g)
self.gamma = nn.Parameter(gamma)
self.register_parameter('gamma', self.gamma)
self.pedestal_tensor = torch.FloatTensor([self.pedestal_data])
self.register_buffer('pedestal', self.pedestal_tensor)
def forward(self, inputs):
_, 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, 1)
# Norm pool calc
norm_ = nn.functional.conv3d(inputs**2, gamma, beta)
norm_ = torch.sqrt(norm_)
# Apply norm
outputs = inputs / norm_
return outputs
class IGDN3d(nn.Module):
"""Generalized divisive normalization layer.
y[i] = x[i] / sqrt(beta[i] + sum_j(gamma[j, i] * x[j]))
"""
def __init__(self,
ch,
inverse=False,
beta_min=1e-6,
gamma_init=.1,
reparam_offset=2**-18):
super(IGDN3d, self).__init__()
self.inverse = inverse
self.beta_min = beta_min
self.gamma_init = gamma_init
self.reparam_offset = reparam_offset
self.build(ch)
def build(self, ch):
self.pedestal_data = 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)+self.pedestal_data)
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_data
gamma = torch.sqrt(g)
self.gamma = nn.Parameter(gamma)
self.register_parameter('gamma', self.gamma)
self.pedestal_tensor = torch.FloatTensor([self.pedestal_data])
self.register_buffer('pedestal', self.pedestal_tensor)
def forward(self, inputs):
_, 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, 1)
# Norm pool calc
norm_ = nn.functional.conv3d(inputs**2, gamma, beta)
norm_ = torch.sqrt(norm_)
outputs = inputs * norm_
return outputs