-
Notifications
You must be signed in to change notification settings - Fork 34
/
detector.py
240 lines (209 loc) · 10.7 KB
/
detector.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import torch
import torch.nn as nn
import sys
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(BASE_DIR)
sys.path.append(BASE_DIR)
from .backbone_module import Pointnet2Backbone
from .transformer import TransformerDecoderLayer
from .modules import PointsObjClsModule, FPSModule, GeneralSamplingModule, PositionEmbeddingLearned, PredictHead, \
ClsAgnosticPredictHead
class GroupFreeDetector(nn.Module):
r"""
A Group-Free detector for 3D object detection via Transformer.
Parameters
----------
num_class: int
Number of semantics classes to predict over -- size of softmax classifier
num_heading_bin: int
num_size_cluster: int
input_feature_dim: (default: 0)
Input dim in the feature descriptor for each point. If the point cloud is Nx9, this
value should be 6 as in an Nx9 point cloud, 3 of the channels are xyz, and 6 are feature descriptors
width: (default: 1)
PointNet backbone width ratio
num_proposal: int (default: 128)
Number of proposals/detections generated from the network. Each proposal is a 3D OBB with a semantic class.
sampling: (default: kps)
Initial object candidate sampling method
"""
def __init__(self, num_class, num_heading_bin, num_size_cluster, mean_size_arr,
input_feature_dim=0, width=1, bn_momentum=0.1, sync_bn=False, num_proposal=128, sampling='kps',
dropout=0.1, activation="relu", nhead=8, num_decoder_layers=6, dim_feedforward=2048,
self_position_embedding='xyz_learned', cross_position_embedding='xyz_learned',
size_cls_agnostic=False):
super().__init__()
self.num_class = num_class
self.num_heading_bin = num_heading_bin
self.num_size_cluster = num_size_cluster
self.mean_size_arr = mean_size_arr
assert (mean_size_arr.shape[0] == self.num_size_cluster)
self.input_feature_dim = input_feature_dim
self.num_proposal = num_proposal
self.bn_momentum = bn_momentum
self.sync_bn = sync_bn
self.width = width
self.nhead = nhead
self.sampling = sampling
self.num_decoder_layers = num_decoder_layers
self.dim_feedforward = dim_feedforward
self.self_position_embedding = self_position_embedding
self.cross_position_embedding = cross_position_embedding
self.size_cls_agnostic = size_cls_agnostic
# Backbone point feature learning
self.backbone_net = Pointnet2Backbone(input_feature_dim=self.input_feature_dim, width=self.width)
if self.sampling == 'fps':
self.fps_module = FPSModule(num_proposal)
elif self.sampling == 'kps':
self.points_obj_cls = PointsObjClsModule(288)
self.gsample_module = GeneralSamplingModule()
else:
raise NotImplementedError
# Proposal
if self.size_cls_agnostic:
self.proposal_head = ClsAgnosticPredictHead(num_class, num_heading_bin, num_proposal, 288)
else:
self.proposal_head = PredictHead(num_class, num_heading_bin, num_size_cluster,
mean_size_arr, num_proposal, 288)
if self.num_decoder_layers <= 0:
# stop building if has no decoder layer
return
# Transformer Decoder Projection
self.decoder_key_proj = nn.Conv1d(288, 288, kernel_size=1)
self.decoder_query_proj = nn.Conv1d(288, 288, kernel_size=1)
# Position Embedding for Self-Attention
if self.self_position_embedding == 'none':
self.decoder_self_posembeds = [None for i in range(num_decoder_layers)]
elif self.self_position_embedding == 'xyz_learned':
self.decoder_self_posembeds = nn.ModuleList()
for i in range(self.num_decoder_layers):
self.decoder_self_posembeds.append(PositionEmbeddingLearned(3, 288))
elif self.self_position_embedding == 'loc_learned':
self.decoder_self_posembeds = nn.ModuleList()
for i in range(self.num_decoder_layers):
self.decoder_self_posembeds.append(PositionEmbeddingLearned(6, 288))
else:
raise NotImplementedError(f"self_position_embedding not supported {self.self_position_embedding}")
# Position Embedding for Cross-Attention
if self.cross_position_embedding == 'none':
self.decoder_cross_posembeds = [None for i in range(num_decoder_layers)]
elif self.cross_position_embedding == 'xyz_learned':
self.decoder_cross_posembeds = nn.ModuleList()
for i in range(self.num_decoder_layers):
self.decoder_cross_posembeds.append(PositionEmbeddingLearned(3, 288))
else:
raise NotImplementedError(f"cross_position_embedding not supported {self.cross_position_embedding}")
# Transformer decoder layers
self.decoder = nn.ModuleList()
for i in range(self.num_decoder_layers):
self.decoder.append(
TransformerDecoderLayer(
288, nhead, dim_feedforward, dropout, activation,
self_posembed=self.decoder_self_posembeds[i],
cross_posembed=self.decoder_cross_posembeds[i],
))
# Prediction Head
self.prediction_heads = nn.ModuleList()
for i in range(self.num_decoder_layers):
if self.size_cls_agnostic:
self.prediction_heads.append(ClsAgnosticPredictHead(num_class, num_heading_bin, num_proposal, 288))
else:
self.prediction_heads.append(PredictHead(num_class, num_heading_bin, num_size_cluster,
mean_size_arr, num_proposal, 288))
# Init
self.init_weights()
self.init_bn_momentum()
if self.sync_bn:
nn.SyncBatchNorm.convert_sync_batchnorm(self)
def forward(self, inputs):
""" Forward pass of the network
Args:
inputs: dict
{point_clouds}
point_clouds: Variable(torch.cuda.FloatTensor)
(B, N, 3 + input_channels) tensor
Point cloud to run predicts on
Each point in the point-cloud MUST
be formated as (x, y, z, features...)
Returns:
end_points: dict
"""
end_points = {}
end_points = self.backbone_net(inputs['point_clouds'], end_points)
# Query Points Generation
points_xyz = end_points['fp2_xyz']
points_features = end_points['fp2_features']
xyz = end_points['fp2_xyz']
features = end_points['fp2_features']
end_points['seed_inds'] = end_points['fp2_inds']
end_points['seed_xyz'] = xyz
end_points['seed_features'] = features
if self.sampling == 'fps':
xyz, features, sample_inds = self.fps_module(xyz, features)
cluster_feature = features
cluster_xyz = xyz
end_points['query_points_xyz'] = xyz # (batch_size, num_proposal, 3)
end_points['query_points_feature'] = features # (batch_size, C, num_proposal)
end_points['query_points_sample_inds'] = sample_inds # (bsz, num_proposal) # should be 0,1,...,num_proposal
elif self.sampling == 'kps':
points_obj_cls_logits = self.points_obj_cls(features) # (batch_size, 1, num_seed)
end_points['seeds_obj_cls_logits'] = points_obj_cls_logits
points_obj_cls_scores = torch.sigmoid(points_obj_cls_logits).squeeze(1)
sample_inds = torch.topk(points_obj_cls_scores, self.num_proposal)[1].int()
xyz, features, sample_inds = self.gsample_module(xyz, features, sample_inds)
cluster_feature = features
cluster_xyz = xyz
end_points['query_points_xyz'] = xyz # (batch_size, num_proposal, 3)
end_points['query_points_feature'] = features # (batch_size, C, num_proposal)
end_points['query_points_sample_inds'] = sample_inds # (bsz, num_proposal) # should be 0,1,...,num_proposal
else:
raise NotImplementedError
# Proposal
proposal_center, proposal_size = self.proposal_head(cluster_feature,
base_xyz=cluster_xyz,
end_points=end_points,
prefix='proposal_') # N num_proposal 3
base_xyz = proposal_center.detach().clone()
base_size = proposal_size.detach().clone()
# Transformer Decoder and Prediction
if self.num_decoder_layers > 0:
query = self.decoder_query_proj(cluster_feature)
key = self.decoder_key_proj(points_features) if self.decoder_key_proj is not None else None
# Position Embedding for Cross-Attention
if self.cross_position_embedding == 'none':
key_pos = None
elif self.cross_position_embedding in ['xyz_learned']:
key_pos = points_xyz
else:
raise NotImplementedError(f"cross_position_embedding not supported {self.cross_position_embedding}")
for i in range(self.num_decoder_layers):
prefix = 'last_' if (i == self.num_decoder_layers - 1) else f'{i}head_'
# Position Embedding for Self-Attention
if self.self_position_embedding == 'none':
query_pos = None
elif self.self_position_embedding == 'xyz_learned':
query_pos = base_xyz
elif self.self_position_embedding == 'loc_learned':
query_pos = torch.cat([base_xyz, base_size], -1)
else:
raise NotImplementedError(f"self_position_embedding not supported {self.self_position_embedding}")
# Transformer Decoder Layer
query = self.decoder[i](query, key, query_pos, key_pos)
# Prediction
base_xyz, base_size = self.prediction_heads[i](query,
base_xyz=cluster_xyz,
end_points=end_points,
prefix=prefix)
base_xyz = base_xyz.detach().clone()
base_size = base_size.detach().clone()
return end_points
def init_weights(self):
# initialize transformer
for m in self.decoder.parameters():
if m.dim() > 1:
nn.init.xavier_uniform_(m)
def init_bn_momentum(self):
for m in self.modules():
if isinstance(m, (nn.BatchNorm2d, nn.BatchNorm1d)):
m.momentum = self.bn_momentum