-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLConv.py
248 lines (199 loc) · 8.95 KB
/
LConv.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
from lib.lorentz.manifold import CustomLorentz
from lib.lorentz.layers import LorentzFullyConnected
class LorentzConv1d(nn.Module):
""" Implements a fully hyperbolic 1D convolutional layer using the Lorentz model.
Args:
manifold: Instance of Lorentz manifold
in_channels, out_channels, kernel_size, stride, padding, bias: Same as nn.Conv1d
LFC_normalize: If Chen et al.'s internal normalization should be used in LFC
"""
def __init__(
self,
manifold: CustomLorentz,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
bias=True,
LFC_normalize=False
):
super(LorentzConv1d, self).__init__()
self.manifold = manifold
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
lin_features = (self.in_channels - 1) * self.kernel_size + 1
self.linearized_kernel = LorentzFullyConnected(
manifold,
lin_features,
self.out_channels,
bias=bias,
normalize=LFC_normalize
)
def forward(self, x):
""" x has to be in channel-last representation -> Shape = bs x len x C """
bsz = x.shape[0]
# origin padding
x = F.pad(x, (0, 0, self.padding, self.padding))
x[..., 0].clamp_(min=self.manifold.k.sqrt())
patches = x.unfold(1, self.kernel_size, self.stride)
# Lorentz direct concatenation of features within patches
patches_time = patches.narrow(2, 0, 1)
patches_time_rescaled = torch.sqrt(torch.sum(patches_time ** 2, dim=(-2,-1), keepdim=True) - ((self.kernel_size - 1) * self.manifold.k))
patches_time_rescaled = patches_time_rescaled.view(bsz, patches.shape[1], -1)
patches_space = patches.narrow(2, 1, patches.shape[2]-1).reshape(bsz, patches.shape[1], -1)
patches_pre_kernel = torch.concat((patches_time_rescaled, patches_space), dim=-1)
out = self.linearized_kernel(patches_pre_kernel)
return out
class LorentzConv2d(nn.Module):
""" Implements a fully hyperbolic 2D convolutional layer using the Lorentz model.
Args:
manifold: Instance of Lorentz manifold
in_channels, out_channels, kernel_size, stride, padding, dilation, bias: Same as nn.Conv2d (dilation not tested)
LFC_normalize: If Chen et al.'s internal normalization should be used in LFC
"""
def __init__(
self,
manifold: CustomLorentz,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
dilation=1,
bias=True,
LFC_normalize=False
):
super(LorentzConv2d, self).__init__()
self.manifold = manifold
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.bias = bias
if isinstance(stride, int):
self.stride = (stride, stride)
else:
self.stride = stride
if isinstance(kernel_size, int):
self.kernel_size = (kernel_size, kernel_size)
else:
self.kernel_size = kernel_size
if isinstance(padding, int):
self.padding = (padding, padding)
else:
self.padding = padding
if isinstance(dilation, int):
self.dilation = (dilation, dilation)
else:
self.dilation = dilation
self.kernel_len = self.kernel_size[0] * self.kernel_size[1]
lin_features = ((self.in_channels - 1) * self.kernel_size[0] * self.kernel_size[1]) + 1
self.linearized_kernel = LorentzFullyConnected(
manifold,
lin_features,
self.out_channels,
bias=bias,
normalize=LFC_normalize
)
self.unfold = torch.nn.Unfold(kernel_size=(self.kernel_size[0], self.kernel_size[1]), dilation=dilation, padding=padding, stride=stride)
self.reset_parameters()
def reset_parameters(self):
stdv = math.sqrt(2.0 / ((self.in_channels-1) * self.kernel_size[0] * self.kernel_size[1]))
self.linearized_kernel.weight.weight.data.uniform_(-stdv, stdv)
if self.bias:
self.linearized_kernel.weight.bias.data.uniform_(-stdv, stdv)
def forward(self, x):
""" x has to be in channel-last representation -> Shape = bs x H x W x C """
bsz = x.shape[0]
h, w = x.shape[1:3]
h_out = math.floor(
(h + 2 * self.padding[0] - self.dilation[0] * (self.kernel_size[0] - 1) - 1) / self.stride[0] + 1)
w_out = math.floor(
(w + 2 * self.padding[1] - self.dilation[1] * (self.kernel_size[1] - 1) - 1) / self.stride[1] + 1)
x = x.permute(0, 3, 1, 2)
patches = self.unfold(x) # batch_size, channels * elements/window, windows
patches = patches.permute(0, 2, 1)
# Now we have flattened patches with multiple time elements -> fix the concatenation to perform Lorentz direct concatenation by Qu et al. (2022)
patches_time = torch.clamp(patches.narrow(-1, 0, self.kernel_len), min=self.manifold.k.sqrt()) # Fix zero (origin) padding
patches_time_rescaled = torch.sqrt(torch.sum(patches_time ** 2, dim=-1, keepdim=True) - ((self.kernel_len - 1) * self.manifold.k))
patches_space = patches.narrow(-1, self.kernel_len, patches.shape[-1] - self.kernel_len)
patches_space = patches_space.reshape(patches_space.shape[0], patches_space.shape[1], self.in_channels - 1, -1).transpose(-1, -2).reshape(patches_space.shape) # No need, but seems to improve runtime??
patches_pre_kernel = torch.concat((patches_time_rescaled, patches_space), dim=-1)
out = self.linearized_kernel(patches_pre_kernel)
out = out.view(bsz, h_out, w_out, self.out_channels)
return out
class LorentzConvTranspose2d(nn.Module):
""" Implements a fully hyperbolic 2D transposed convolutional layer using the Lorentz model.
Args:
manifold: Instance of Lorentz manifold
in_channels, out_channels, kernel_size, stride, padding, output_padding, bias: Same as nn.ConvTranspose2d
LFC_normalize: If Chen et al.'s internal normalization should be used in LFC
"""
def __init__(
self,
manifold: CustomLorentz,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
output_padding=0,
bias=True,
LFC_normalize=False
):
super(LorentzConvTranspose2d, self).__init__()
self.manifold = manifold
self.in_channels = in_channels
self.out_channels = out_channels
if isinstance(stride, int):
self.stride = (stride, stride)
else:
self.stride = stride
if isinstance(kernel_size, int):
self.kernel_size = (kernel_size, kernel_size)
else:
self.kernel_size = kernel_size
if isinstance(padding, int):
self.padding = (padding, padding)
else:
self.padding = padding
if isinstance(output_padding, int):
self.output_padding = (output_padding, output_padding)
else:
self.output_padding = output_padding
padding_implicit = [0,0]
padding_implicit[0] = kernel_size - self.padding[0] - 1 # Ensure padding > kernel_size
padding_implicit[1] = kernel_size - self.padding[1] - 1 # Ensure padding > kernel_size
self.pad_weight = nn.Parameter(F.pad(torch.ones((self.in_channels,1,1,1)),(1,1,1,1)), requires_grad=False)
self.conv = LorentzConv2d(
manifold=manifold,
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=1,
padding=padding_implicit,
bias=bias,
LFC_normalize=LFC_normalize
)
def forward(self, x):
""" x has to be in channel last representation -> Shape = bs x H x W x C """
if self.stride[0] > 1 or self.stride[1] > 1:
# Insert hyperbolic origin vectors between features
x = x.permute(0,3,1,2)
# -> Insert zero vectors
x = F.conv_transpose2d(x, self.pad_weight,stride=self.stride,padding=1, groups=self.in_channels)
x = x.permute(0,2,3,1)
x[..., 0].clamp_(min=self.manifold.k.sqrt())
x = self.conv(x)
if self.output_padding[0] > 0 or self.output_padding[1] > 0:
x = F.pad(x, pad=(0, self.output_padding[1], 0, self.output_padding[0])) # Pad one side of each dimension (bottom+right) (see PyTorch documentation)
x[..., 0].clamp_(min=self.manifold.k.sqrt()) # Fix origin padding
return x