-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnetworks.py
More file actions
69 lines (55 loc) · 2.08 KB
/
networks.py
File metadata and controls
69 lines (55 loc) · 2.08 KB
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
import torch
class Block(torch.nn.Module):
def __init__(self, input_dim, output_dim, skip=False):
super().__init__()
self.skip = skip
self.fc1 = torch.nn.Linear(input_dim, input_dim)
self.fc2 = torch.nn.Linear(input_dim, output_dim)
self.silu1 = torch.nn.SiLU()
self.silu2 = torch.nn.SiLU()
self.norm1 = torch.nn.LayerNorm(input_dim)
self.norm2 = torch.nn.LayerNorm(output_dim)
self.block1 = torch.nn.Sequential(*[
self.fc1, self.silu1,
])
self.block2 = torch.nn.Sequential(*[
self.fc2, self.silu2, self.norm2
])
def forward(self, x):
out = self.block1(x)
if self.skip:
x = x + out
else:
x = out
x = self.norm1(x)
return self.block2(x)
class GenericNetwork(torch.nn.Module):
def __init__(self, layer_nums):
super().__init__()
self.layer_nums = list(layer_nums)
self.input_dim = layer_nums[0]
self.output_dim = self.layer_nums[-1]
layers = []
for i in range(len(self.layer_nums)-1):
layers.append(Block(self.layer_nums[i], self.layer_nums[i+1]))
self.net = torch.nn.Sequential(*layers)
def forward(self, x):
assert x.ndim == 3, "Input must be batch x points x features"
return self.net(x)
class FeatureNetwork(torch.nn.Module):
def __init__(self, feature_dim=2048, num_layers=4, **kwargs) -> None:
super().__init__()
self.feat_dim = feature_dim
self.layers = [feature_dim // (2**n) for n in range(num_layers + 1)]
self.encoder = GenericNetwork(self.layers)
self.decoder = GenericNetwork(self.layers[::-1])
def encode(self, F, norm=True):
f = self.encoder(F[:,None,:])
if norm:
f = f / (f.norm(dim=-1, keepdim=True) + 1e-8)
return f
def forward(self, F, norm=True):
f = self.encode(F, norm=norm)
F_hat = self.decoder(f)
F_hat = F_hat / F_hat.norm(dim=-1, keepdim=True)
return f[:,0], F_hat[:,0]