-
Notifications
You must be signed in to change notification settings - Fork 5
/
resnet_blocks.py
197 lines (160 loc) · 5.77 KB
/
resnet_blocks.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
import torch.nn as nn
import torch.nn.functional as F
from lib.lorentz.manifold import CustomLorentz
from lib.lorentz.layers import (
LorentzConv2d,
LorentzBatchNorm2d,
LorentzReLU,
)
def get_Conv2d(manifold, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=True, LFC_normalize=False):
return LorentzConv2d(
manifold=manifold,
in_channels=in_channels+1,
out_channels=out_channels+1,
kernel_size=kernel_size,
stride=stride,
padding=padding,
bias=bias,
LFC_normalize=LFC_normalize
)
def get_BatchNorm2d(manifold, num_channels):
return LorentzBatchNorm2d(manifold=manifold, num_channels=num_channels+1)
def get_Activation(manifold):
return LorentzReLU(manifold)
class LorentzInputBlock(nn.Module):
""" Input Block of ResNet model """
def __init__(self, manifold: CustomLorentz, img_dim, in_channels, bias=True):
super(LorentzInputBlock, self).__init__()
self.manifold = manifold
self.conv = nn.Sequential(
get_Conv2d(
self.manifold,
img_dim,
in_channels,
kernel_size=3,
padding=1,
bias=bias
),
get_BatchNorm2d(self.manifold, in_channels),
get_Activation(self.manifold),
)
def forward(self, x):
x = x.permute(0, 2, 3, 1) # Make channel last (bs x H x W x C)
x = self.manifold.projx(F.pad(x, pad=(1, 0)))
return self.conv(x)
class LorentzBasicBlock(nn.Module):
""" Basic Block for Lorentz ResNet-10, -18 and -34 """
expansion = 1
def __init__(self, manifold: CustomLorentz, in_channels, out_channels, stride=1, bias=True):
super(LorentzBasicBlock, self).__init__()
self.manifold = manifold
self.activation = get_Activation(self.manifold)
self.conv = nn.Sequential(
get_Conv2d(
self.manifold,
in_channels,
out_channels,
kernel_size=3,
stride=stride,
padding=1,
bias=bias
),
get_BatchNorm2d(self.manifold, out_channels),
get_Activation(self.manifold),
get_Conv2d(
self.manifold,
out_channels,
out_channels * LorentzBasicBlock.expansion,
kernel_size=3,
padding=1,
bias=bias
),
get_BatchNorm2d(self.manifold, out_channels * LorentzBasicBlock.expansion),
)
self.shortcut = nn.Sequential()
if stride != 1 or in_channels != out_channels * LorentzBasicBlock.expansion:
self.shortcut = nn.Sequential(
get_Conv2d(
self.manifold,
in_channels,
out_channels * LorentzBasicBlock.expansion,
kernel_size=1,
stride=stride,
padding=0,
bias=bias
),
get_BatchNorm2d(
self.manifold, out_channels * LorentzBasicBlock.expansion
),
)
def forward(self, x):
res = self.shortcut(x)
out = self.conv(x)
# Residual = Add space components
out = out.narrow(-1, 1, res.shape[-1]-1) + res.narrow(-1, 1, res.shape[-1]-1)
out = self.manifold.add_time(out)
out = self.activation(out)
return out
class LorentzBottleneck(nn.Module):
""" Residual block for Lorentz ResNet with > 50 layers """
expansion = 4
def __init__(self, manifold: CustomLorentz, in_channels, out_channels, stride=1, bias=False):
super(LorentzBottleneck, self).__init__()
self.manifold = manifold
self.activation = get_Activation(self.manifold)
self.conv = nn.Sequential(
get_Conv2d(
self.manifold,
in_channels,
out_channels,
kernel_size=1,
padding=0,
bias=bias
),
get_BatchNorm2d(self.manifold, out_channels),
get_Activation(self.manifold),
get_Conv2d(
self.manifold,
out_channels,
out_channels,
kernel_size=3,
stride=stride,
padding=1,
bias=bias
),
get_BatchNorm2d(self.manifold, out_channels),
get_Activation(self.manifold),
get_Conv2d(
self.manifold,
out_channels,
out_channels * LorentzBottleneck.expansion,
kernel_size=1,
padding=0,
bias=bias
),
get_BatchNorm2d(self.manifold, out_channels * LorentzBottleneck.expansion),
)
self.shortcut = nn.Sequential()
if stride != 1 or in_channels != out_channels * LorentzBottleneck.expansion:
self.shortcut = nn.Sequential(
get_Conv2d(
self.manifold,
in_channels,
out_channels * LorentzBottleneck.expansion,
kernel_size=1,
stride=stride,
padding=0,
bias=bias
),
get_BatchNorm2d(
self.manifold, out_channels * LorentzBottleneck.expansion
),
)
def forward(self, x):
res = self.shortcut(x)
out = self.conv(x)
# Residual = Add space components
out = out.narrow(-1, 1, res.shape[-1]-1) + res.narrow(-1, 1, res.shape[-1]-1)
out = self.manifold.add_time(out)
out = self.activation(out)
return out