forked from qinglew/PointCloudTransformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
202 lines (156 loc) · 6.72 KB
/
util.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
import torch
import torch.nn.functional as F
from pointnet2_ops import pointnet2_utils
def cal_loss(pred, ground_truth, smoothing=True):
''' Calculate cross entropy loss, apply label smoothing if needed. '''
ground_truth = ground_truth.contiguous().view(-1)
if smoothing:
eps = 0.2
n_class = pred.size(1)
one_hot = torch.zeros_like(pred).scatter(1, ground_truth.view(-1, 1), 1)
one_hot = one_hot * (1 - eps) + (1 - one_hot) * eps / (n_class - 1)
log_prb = F.log_softmax(pred, dim=1)
loss = -(one_hot * log_prb).sum(dim=1).mean()
else:
loss = F.cross_entropy(pred, ground_truth, reduction='mean')
return loss
def square_distance(src, dst):
"""
Calculate Euclid distance between each two points.
src^T * dst = xn * xm + yn * ym + zn * zm;
sum(src^2, dim=-1) = xn*xn + yn*yn + zn*zn;
sum(dst^2, dim=-1) = xm*xm + ym*ym + zm*zm;
dist = (xn - xm)^2 + (yn - ym)^2 + (zn - zm)^2
= sum(src**2,dim=-1)+sum(dst**2,dim=-1)-2*src^T*dst
Input:
src: source points, [B, N, C]
dst: target points, [B, M, C]
Output:
dist: per-point square distance, [B, N, M]
"""
B, N, _ = src.shape
_, M, _ = dst.shape
dist = -2 * torch.matmul(src, dst.permute(0, 2, 1))
dist += torch.sum(src ** 2, -1).view(B, N, 1)
dist += torch.sum(dst ** 2, -1).view(B, 1, M)
return dist
def query_ball_point(radius, nsample, xyz, new_xyz):
"""
Ball query.
Input:
radius: local region radius
nsample: max sample number in local region
xyz: all points, [B, N, 3]
new_xyz: query points, [B, S, 3]
Output:
group_idx: grouped points index, [B, S, nsample]
"""
device = xyz.device
B, N, C = xyz.shape
_, S, _ = new_xyz.shape
group_idx = torch.arange(N, dtype=torch.long).to(device).view(1, 1, N).repeat([B, S, 1])
sqrdists = square_distance(new_xyz, xyz)
group_idx[sqrdists > radius ** 2] = N
group_idx = group_idx.sort(dim=-1)[0][:, :, :nsample]
group_first = group_idx[:, :, 0].view(B, S, 1).repeat([1, 1, nsample])
mask = group_idx == N
group_idx[mask] = group_first[mask]
return group_idx
def knn_point(k, xyz, new_xyz):
"""
K nearest neighborhood.
Input:
k: max sample number in local region
xyz: all points, [B, N, C]
new_xyz: query points, [B, S, C]
Output:
group_idx: grouped points index, [B, S, k]
"""
sqrdists = square_distance(new_xyz, xyz)
_, group_idx = torch.topk(sqrdists, k, dim=-1, largest=False, sorted=False)
return group_idx
def index_points(points, idx):
"""
Input:
points: input points data, [B, N, C]
idx: sample index data, [B, S]
Output:
new_points:, indexed points data, [B, S, C]
"""
device = points.device
B = points.shape[0]
view_shape = list(idx.shape)
view_shape[1:] = [1] * (len(view_shape) - 1)
repeat_shape = list(idx.shape)
repeat_shape[0] = 1
batch_indices = torch.arange(B, dtype=torch.long).to(device).view(view_shape).repeat(repeat_shape)
new_points = points[batch_indices, idx, :]
return new_points
def sample_and_ball_group(s, radius, n, coords, features):
"""
Sampling by FPS and grouping by ball query.
Input:
s[int]: number of points to be sampled by FPS
k[int]: number of points to be grouped into a neighbor by ball query
n[int]: fix number of points in ball neighbor
coords[tensor]: input points coordinates data with size of [B, N, 3]
features[tensor]: input points features data with size of [B, N, D]
Returns:
new_coords[tensor]: sampled and grouped points coordinates by FPS with size of [B, s, k, 3]
new_features[tensor]: sampled and grouped points features by FPS with size of [B, s, k, 2D]
"""
batch_size = coords.shape[0]
coords = coords.contiguous()
# FPS sampling
fps_idx = pointnet2_utils.furthest_point_sample(coords, s).long() # [B, s]
new_coords = index_points(coords, fps_idx) # [B, s, 3]
new_features = index_points(features, fps_idx) # [B, s, D]
# ball_query grouping
idx = query_ball_point(radius, n, coords, new_coords) # [B, s, n]
grouped_features = index_points(features, idx) # [B, s, n, D]
# Matrix sub
grouped_features_norm = grouped_features - new_features.view(batch_size, s, 1, -1) # [B, s, n, D]
# Concat, my be different in many networks
aggregated_features = torch.cat([grouped_features_norm, new_features.view(batch_size, s, 1, -1).repeat(1, 1, n, 1)], dim=-1) # [B, s, n, 2D]
return new_coords, aggregated_features # [B, s, 3], [B, s, n, 2D]
def sample_and_knn_group(s, k, coords, features):
"""
Sampling by FPS and grouping by KNN.
Input:
s[int]: number of points to be sampled by FPS
k[int]: number of points to be grouped into a neighbor by KNN
coords[tensor]: input points coordinates data with size of [B, N, 3]
features[tensor]: input points features data with size of [B, N, D]
Returns:
new_coords[tensor]: sampled and grouped points coordinates by FPS with size of [B, s, k, 3]
new_features[tensor]: sampled and grouped points features by FPS with size of [B, s, k, 2D]
"""
batch_size = coords.shape[0]
coords = coords.contiguous()
# FPS sampling
fps_idx = pointnet2_utils.furthest_point_sample(coords, s).long() # [B, s]
new_coords = index_points(coords, fps_idx) # [B, s, 3]
new_features = index_points(features, fps_idx) # [B, s, D]
# K-nn grouping
idx = knn_point(k, coords, new_coords) # [B, s, k]
grouped_features = index_points(features, idx) # [B, s, k, D]
# Matrix sub
grouped_features_norm = grouped_features - new_features.view(batch_size, s, 1, -1) # [B, s, k, D]
# Concat
aggregated_features = torch.cat([grouped_features_norm, new_features.view(batch_size, s, 1, -1).repeat(1, 1, k, 1)], dim=-1) # [B, s, k, 2D]
return new_coords, aggregated_features # [B, s, 3], [B, s, k, 2D]
class Logger():
def __init__(self, path):
self.f = open(path, 'a')
def cprint(self, text):
print(text)
self.f.write(text+'\n')
self.f.flush()
def close(self):
self.f.close()
if __name__ == '__main__':
points = torch.rand(32, 1024, 3).to('cuda')
features = torch.rand(32, 1024, 128).to('cuda')
new_points, new_features = sample_and_knn_group(512, 32, points, features)
print(new_points.size())
print(new_features.size())