-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
112 lines (97 loc) · 4.08 KB
/
model.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import config
# from model_blocks import TransformerBlock, TransitionDown, TransitionUp
from blocks import TransformerBlock, TransitionDown, TransitionUp
class Encoder(nn.Module):
def __init__(self, cfg):
super().__init__()
npoints, nblocks, nneighbor, n_c, d_points = cfg.num_point, cfg.model.nblocks, cfg.model.nneighbor, cfg.num_class, cfg.input_dim
self.fc1 = nn.Sequential(
nn.Linear(d_points, 32),
nn.ReLU(),
nn.Linear(32, 32)
)
self.transformer1 = TransformerBlock(32, cfg.model.transformer_dim, nneighbor)
self.transition_downs = nn.ModuleList()
self.transformers = nn.ModuleList()
for i in range(nblocks):
channel = 32 * 2 ** (i + 1)
self.transition_downs.append(TransitionDown(npoints // 4 ** (i + 1), nneighbor, [channel // 2 + 3, channel, channel]))
self.transformers.append(TransformerBlock(channel, cfg.model.transformer_dim, nneighbor))
self.nblocks = nblocks
def forward(self, x):
xyz = x[..., :3]
# print(x.shape)
points = self.transformer1(xyz, self.fc1(x))[0]
xyz_and_feats = [(xyz, points)]
for i in range(self.nblocks):
xyz, points = self.transition_downs[i](xyz, points)
points = self.transformers[i](xyz, points)[0]
xyz_and_feats.append((xyz, points))
print(xyz)
return points, xyz_and_feats
class PointTransformerCls(nn.Module):
def __init__(self, cfg):
super().__init__()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
self.device = torch.device(device)
self.encoder = Encoder(cfg)
npoints, nblocks, nneighbor, n_c, d_points = cfg.num_point, cfg.model.nblocks, cfg.model.nneighbor, cfg.num_class, cfg.input_dim
self.fc2 = nn.Sequential(
nn.Linear(32 * 2 ** nblocks, 256),
nn.ReLU(),
nn.Linear(256, 64),
nn.ReLU(),
nn.Linear(64, n_c)
)
self.nblocks = nblocks
def forward(self, x):
points, _ = self.encoder(x)
res = self.fc2(points.mean(1))
return res
class Decoder(nn.Module):
def __init__(self):
super(Decoder, self).__init__()
class PTAE(nn.Module):
def __init__(self):
super(PTAE, self).__init__()
class PointTransformerSeg(nn.Module):
def __init__(self, cfg):
super().__init__()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
self.device = torch.device(device)
self.encoder = Encoder(cfg)
npoints, nblocks, nneighbor, n_c, d_points = cfg.num_point, cfg.model.nblocks, cfg.model.nneighbor, cfg.num_class, cfg.input_dim
self.fc2 = nn.Sequential(
nn.Linear(32 * 2 ** nblocks, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, 32 * 2 ** nblocks)
)
self.transformer2 = TransformerBlock(32 * 2 ** nblocks, cfg.model.transformer_dim, nneighbor)
self.nblocks = nblocks
self.transition_ups = nn.ModuleList()
self.transformers = nn.ModuleList()
for i in reversed(range(nblocks)):
channel = 32 * 2 ** i
self.transition_ups.append(TransitionUp(channel * 2, channel, channel))
self.transformers.append(TransformerBlock(channel, cfg.model.transformer_dim, nneighbor))
self.fc3 = nn.Sequential(
nn.Linear(32, 64),
nn.ReLU(),
nn.Linear(64, 64),
nn.ReLU(),
nn.Linear(64, n_c)
)
def forward(self, x):
points, xyz_and_feats = self.encoder(x)
xyz = xyz_and_feats[-1][0]
points = self.transformer2(xyz, self.fc2(points))[0]
for i in range(self.nblocks):
points = self.transition_ups[i](xyz, points, xyz_and_feats[- i - 2][0], xyz_and_feats[- i - 2][1])
xyz = xyz_and_feats[- i - 2][0]
points = self.transformers[i](xyz, points)[0]
return self.fc3(points)