-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodule.py
221 lines (167 loc) · 6.79 KB
/
module.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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from util import sample_and_knn_group
class Embedding(nn.Module):
"""
Input Embedding layer which consist of 2 stacked LBR layer.
"""
def __init__(self, in_channels=3, out_channels=128):
super(Embedding, self).__init__()
self.conv1 = nn.Conv1d(in_channels, out_channels, kernel_size=1, bias=False)
self.conv2 = nn.Conv1d(out_channels, out_channels, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm1d(out_channels)
self.bn2 = nn.BatchNorm1d(out_channels)
def forward(self, x):
"""
Input
x: [B, in_channels, N]
Output
x: [B, out_channels, N]
"""
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
return x
class SA(nn.Module):
"""
Self Attention module.
"""
def __init__(self, channels):
super(SA, self).__init__()
self.da = channels // 4
self.q_conv = nn.Conv1d(channels, channels // 4, 1, bias=False)
self.k_conv = nn.Conv1d(channels, channels // 4, 1, bias=False)
self.q_conv.weight = self.k_conv.weight
self.v_conv = nn.Conv1d(channels, channels, 1)
self.trans_conv = nn.Conv1d(channels, channels, 1)
self.after_norm = nn.BatchNorm1d(channels)
self.act = nn.ReLU()
self.softmax = nn.Softmax(dim=-1)
def forward(self, x):
"""
Input
x: [B, de, N]
Output
x: [B, de, N]
"""
# compute query, key and value matrix
x_q = self.q_conv(x).permute(0, 2, 1) # [B, N, da]
x_k = self.k_conv(x) # [B, da, N]
x_v = self.v_conv(x) # [B, de, N]
# compute attention map and scale, the sorfmax
energy = torch.bmm(x_q, x_k) / (math.sqrt(self.da)) # [B, N, N]
attention = self.softmax(energy) # [B, N, N]
# weighted sum
x_s = torch.bmm(x_v, attention) # [B, de, N]
x_s = self.act(self.after_norm(self.trans_conv(x_s)))
# residual
x = x + x_s
return x
class SG(nn.Module):
"""
SG(sampling and grouping) module.
"""
def __init__(self, s, in_channels, out_channels):
super(SG, self).__init__()
self.s = s
self.conv1 = nn.Conv1d(in_channels, out_channels, kernel_size=1, bias=False)
self.conv2 = nn.Conv1d(out_channels, out_channels, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm1d(out_channels)
self.bn2 = nn.BatchNorm1d(out_channels)
def forward(self, x, coords):
"""
Input:
x: features with size of [B, in_channels//2, N]
coords: coordinates data with size of [B, N, 3]
"""
x = x.permute(0, 2, 1) # (B, N, in_channels//2)
new_xyz, new_feature = sample_and_knn_group(s=self.s, k=32, coords=coords, features=x) # [B, s, 3], [B, s, 32, in_channels]
b, s, k, d = new_feature.size()
new_feature = new_feature.permute(0, 1, 3, 2)
new_feature = new_feature.reshape(-1, d, k) # [Bxs, in_channels, 32]
batch_size = new_feature.size(0)
new_feature = F.relu(self.bn1(self.conv1(new_feature))) # [Bxs, in_channels, 32]
new_feature = F.relu(self.bn2(self.conv2(new_feature))) # [Bxs, in_channels, 32]
new_feature = F.adaptive_max_pool1d(new_feature, 1).view(batch_size, -1) # [Bxs, in_channels]
new_feature = new_feature.reshape(b, s, -1).permute(0, 2, 1) # [B, in_channels, s]
return new_xyz, new_feature
class NeighborEmbedding(nn.Module):
def __init__(self, samples=[512, 256]):
super(NeighborEmbedding, self).__init__()
self.conv1 = nn.Conv1d(3, 64, kernel_size=1, bias=False)
self.conv2 = nn.Conv1d(64, 64, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm1d(64)
self.bn2 = nn.BatchNorm1d(64)
self.sg1 = SG(s=samples[0], in_channels=128, out_channels=128)
self.sg2 = SG(s=samples[1], in_channels=256, out_channels=256)
def forward(self, x):
"""
Input:
x: [B, 3, N]
"""
xyz = x.permute(0, 2, 1) # [B, N ,3]
features = F.relu(self.bn1(self.conv1(x))) # [B, 64, N]
features = F.relu(self.bn2(self.conv2(features))) # [B, 64, N]
xyz1, features1 = self.sg1(features, xyz) # [B, 128, 512]
_, features2 = self.sg2(features1, xyz1) # [B, 256, 256]
return features2
class OA(nn.Module):
"""
Offset-Attention Module.
"""
def __init__(self, channels):
super(OA, self).__init__()
self.q_conv = nn.Conv1d(channels, channels // 4, 1, bias=False)
self.k_conv = nn.Conv1d(channels, channels // 4, 1, bias=False)
self.q_conv.weight = self.k_conv.weight
self.v_conv = nn.Conv1d(channels, channels, 1)
self.trans_conv = nn.Conv1d(channels, channels, 1)
self.after_norm = nn.BatchNorm1d(channels)
self.act = nn.ReLU()
self.softmax = nn.Softmax(dim=-1) # change dim to -2 and change the sum(dim=1, keepdims=True) to dim=2
def forward(self, x):
"""
Input:
x: [B, de, N]
Output:
x: [B, de, N]
"""
x_q = self.q_conv(x).permute(0, 2, 1)
x_k = self.k_conv(x)
x_v = self.v_conv(x)
energy = torch.bmm(x_q, x_k)
attention = self.softmax(energy)
attention = attention / (1e-9 + attention.sum(dim=1, keepdims=True)) # here
x_r = torch.bmm(x_v, attention)
x_r = self.act(self.after_norm(self.trans_conv(x - x_r)))
x = x + x_r
return x
if __name__ == '__main__':
"""
Please be careful to excute the testing code, because
it may cause the GPU out of memory.
"""
pc = torch.rand(32, 3, 1024).to('cuda')
# testing for Embedding
embedding = Embedding().to('cuda')
out = embedding(pc)
print("Embedding output size:", out.size())
# testing for SA
sa = SA(channels=out.size(1)).to('cuda')
out = sa(out)
print("SA output size:", out.size())
# testing for SG
coords = torch.rand(32, 1024, 3).to('cuda')
features = torch.rand(32, 64, 1024).to('cuda')
sg = SG(512, 128, 128).to('cuda')
new_coords, out = sg(features, coords)
print("SG output size:", new_coords.size(), out.size())
# testing for NeighborEmbedding
ne = NeighborEmbedding().to('cuda')
out = ne(pc)
print("NeighborEmbedding output size:", out.size())
# testing for OA
oa = OA(256).to('cuda')
out = oa(out)
print("OA output size:", out.size())