/
model.py
141 lines (117 loc) · 5.04 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
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
import torch
import torch.nn as nn
from pointnet_util import PointNetFeaturePropagation, PointNetSetAbstraction
from .transformer import TransformerBlock
class TransitionDown(nn.Module):
def __init__(self, k, nneighbor, channels):
super().__init__()
self.sa = PointNetSetAbstraction(k, 0, nneighbor, channels[0], channels[1:], group_all=False, knn=True)
def forward(self, xyz, points):
return self.sa(xyz, points)
class TransitionUp(nn.Module):
def __init__(self, dim1, dim2, dim_out):
class SwapAxes(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x.transpose(1, 2)
super().__init__()
self.fc1 = nn.Sequential(
nn.Linear(dim1, dim_out),
SwapAxes(),
nn.BatchNorm1d(dim_out), # TODO
SwapAxes(),
nn.ReLU(),
)
self.fc2 = nn.Sequential(
nn.Linear(dim2, dim_out),
SwapAxes(),
nn.BatchNorm1d(dim_out), # TODO
SwapAxes(),
nn.ReLU(),
)
self.fp = PointNetFeaturePropagation(-1, [])
def forward(self, xyz1, points1, xyz2, points2):
feats1 = self.fc1(points1)
feats2 = self.fc2(points2)
feats1 = self.fp(xyz2.transpose(1, 2), xyz1.transpose(1, 2), None, feats1.transpose(1, 2)).transpose(1, 2)
return feats1 + feats2
class Backbone(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]
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))
return points, xyz_and_feats
class PointTransformerCls(nn.Module):
def __init__(self, cfg):
super().__init__()
self.backbone = Backbone(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.backbone(x)
res = self.fc2(points.mean(1))
return res
class PointTransformerSeg(nn.Module):
def __init__(self, cfg):
super().__init__()
self.backbone = Backbone(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.backbone(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)