-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdefault.py
More file actions
319 lines (290 loc) · 10.9 KB
/
Copy pathdefault.py
File metadata and controls
319 lines (290 loc) · 10.9 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import torch
import torch.nn as nn
import torch_scatter
from pointcept.models.losses import build_criteria
from pointcept.models.utils.structure import Point
from .builder import MODELS, build_model
@MODELS.register_module()
class DefaultSegmentor(nn.Module):
def __init__(self, backbone=None, criteria=None):
super().__init__()
self.backbone = build_model(backbone)
self.criteria = build_criteria(criteria)
def forward(self, input_dict):
if "condition" in input_dict.keys():
# PPT (https://arxiv.org/abs/2308.09718)
# currently, only support one batch one condition
input_dict["condition"] = input_dict["condition"][0]
seg_logits = self.backbone(input_dict)
# train
if self.training:
loss = self.criteria(seg_logits, input_dict["segment"])
return dict(loss=loss)
# eval
elif "segment" in input_dict.keys():
loss = self.criteria(seg_logits, input_dict["segment"])
return dict(loss=loss, seg_logits=seg_logits)
# test
else:
return dict(seg_logits=seg_logits)
@MODELS.register_module()
class DefaultSegmentorV2(nn.Module):
def __init__(
self,
num_classes,
backbone_out_channels,
backbone=None,
criteria=None,
):
super().__init__()
self.seg_head = (
nn.Linear(backbone_out_channels, num_classes)
if num_classes > 0
else nn.Identity()
)
self.backbone = build_model(backbone)
self.criteria = build_criteria(criteria)
def forward(self, input_dict):
point = Point(input_dict)
point = self.backbone(point)
# Backbone added after v1.5.0 return Point instead of feat and use DefaultSegmentorV2
# TODO: remove this part after make all backbone return Point only.
if isinstance(point, Point):
feat = point.feat
else:
feat = point
seg_logits = self.seg_head(feat)
# train
if self.training:
loss = self.criteria(seg_logits, input_dict["segment"])
return dict(loss=loss)
# eval
elif "segment" in input_dict.keys():
loss = self.criteria(seg_logits, input_dict["segment"])
return dict(loss=loss, seg_logits=seg_logits)
# test
else:
return dict(seg_logits=seg_logits)
@MODELS.register_module()
class LangPretrainer(nn.Module):
def __init__(
self,
backbone=None,
criteria=None,
):
super().__init__()
self.backbone = build_model(backbone)
self.criteria = build_criteria(criteria)
def forward(self, input_dict, chunk_size=None):
if (
chunk_size is not None
and chunk_size > 0
and input_dict["coord"].shape[0] > chunk_size
):
return self._chunked_forward(input_dict, chunk_size)
point = Point(input_dict)
point_feat = self.backbone(point)
# normalize the feature
point_feat["feat"] = nn.functional.normalize(point_feat["feat"], p=2, dim=1)
# train
if self.training:
segment = input_dict["segment"] if "segment" in input_dict.keys() else None
loss = self.criteria(
point_feat["feat"],
input_dict["lang_feat"],
valid_feat_mask=input_dict["valid_feat_mask"],
segment=segment,
epoch_progress=input_dict["epoch_progress"],
)
return dict(loss=loss)
# test
else:
return dict(point_feat=point_feat)
def _chunked_forward(self, input_dict, chunk_size):
"""
Break the large point set into smaller chunks, pass each chunk through backbone,
and concat the output features.
NOTE: This only works if your model's global context isn't critical across chunks.
"""
# We'll assume "coord" (Nx3 or NxD) is the main key to figure out total #points N.
# Modify if your data structure is different.
coords = input_dict["coord"]
N = coords.shape[0]
# Prepare a list to store chunk outputs
chunk_outputs = []
# We'll do the same logic as normal forward, but inside a loop
# that processes chunk by chunk.
is_training = self.training # track if we are in training or eval
for start_idx in range(0, N, chunk_size):
end_idx = min(start_idx + chunk_size, N)
# split input_dict into chunks
chunk_input_dict = {}
for k, v in input_dict.items():
if isinstance(v, torch.Tensor) and v.shape[0] == N:
chunk_input_dict[k] = v[start_idx:end_idx]
if "condition" in input_dict.keys():
chunk_input_dict["condition"] = input_dict["condition"][0]
# need to address the 'offset' key separately, which is the same as N
chunk_input_dict["offset"] = torch.tensor(
[end_idx - start_idx], device=coords.device
)
chunk_point = Point(chunk_input_dict)
chunk_point_feat = self.backbone(chunk_point)
chunk_point_feat["feat"] = nn.functional.normalize(
chunk_point_feat["feat"], p=2, dim=1
)
if is_training:
segment = chunk_input_dict.get("segment", None)
loss = self.criteria(
chunk_point_feat["feat"],
chunk_input_dict["lang_feat"],
valid_feat_mask=chunk_input_dict["valid_feat_mask"],
segment=segment,
epoch_progress=chunk_input_dict.get("epoch_progress", None),
)
chunk_outputs.append(loss)
else:
# If eval, store chunk feats to concat
chunk_outputs.append(chunk_point_feat["feat"])
if is_training:
# sum or average the chunk losses
# e.g., total_loss = sum(chunk_outputs) / len(chunk_outputs)
total_loss = torch.stack(chunk_outputs).mean()
return dict(loss=total_loss)
else:
full_feat = torch.cat(chunk_outputs, dim=0) # shape [N, C]
return dict(point_feat={"feat": full_feat})
@MODELS.register_module()
class DefaultSegmentorSkip(nn.Module):
def __init__(
self,
num_classes,
backbone_out_channels,
backbone=None,
criteria=None,
):
super().__init__()
self.seg_head = nn.Sequential(
nn.Linear(backbone_out_channels, 256),
nn.LayerNorm(256),
nn.ReLU(inplace=True),
nn.Linear(256, 128),
nn.LayerNorm(128),
nn.ReLU(inplace=True),
nn.Linear(128, num_classes),
)
# (
# nn.Linear(backbone_out_channels, num_classes)
# if num_classes > 0
# else nn.Identity()
# )
self.backbone = build_model(backbone)
self.criteria = build_criteria(criteria)
def forward(self, input_dict):
point = Point(input_dict)
point = self.backbone(point)
# Backbone added after v1.5.0 return Point instead of feat and use DefaultSegmentorV2
# TODO: remove this part after make all backbone return Point only.
if isinstance(point, Point):
feat = point.feat
else:
feat = point
seg_logits = self.seg_head(feat)
# train
if self.training:
loss = self.criteria(seg_logits, input_dict["segment"])
return dict(loss=loss)
# eval
elif "segment" in input_dict.keys():
loss = self.criteria(seg_logits, input_dict["segment"])
return dict(loss=loss, seg_logits=seg_logits)
# test
else:
return dict(seg_logits=seg_logits)
@MODELS.register_module()
class DefaultClassifier(nn.Module):
def __init__(
self,
backbone=None,
criteria=None,
num_classes=40,
backbone_embed_dim=256,
):
super().__init__()
self.backbone = build_model(backbone)
self.criteria = build_criteria(criteria)
self.num_classes = num_classes
self.backbone_embed_dim = backbone_embed_dim
self.cls_head = nn.Sequential(
nn.Linear(backbone_embed_dim, 256),
nn.BatchNorm1d(256),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(256, 128),
nn.BatchNorm1d(128),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(128, num_classes),
)
def forward(self, input_dict):
point = Point(input_dict)
point = self.backbone(point)
# Backbone added after v1.5.0 return Point instead of feat
# And after v1.5.0 feature aggregation for classification operated in classifier
# TODO: remove this part after make all backbone return Point only.
if isinstance(point, Point):
point.feat = torch_scatter.segment_csr(
src=point.feat,
indptr=nn.functional.pad(point.offset, (1, 0)),
reduce="mean",
)
feat = point.feat
else:
feat = point
cls_logits = self.cls_head(feat)
if self.training:
loss = self.criteria(cls_logits, input_dict["category"])
return dict(loss=loss)
elif "category" in input_dict.keys():
loss = self.criteria(cls_logits, input_dict["category"])
return dict(loss=loss, cls_logits=cls_logits)
else:
return dict(cls_logits=cls_logits)
@MODELS.register_module()
class DefaultPretrainer(nn.Module):
def __init__(
self,
num_classes,
backbone_out_channels,
backbone=None,
criteria=None,
):
super().__init__()
# self.seg_head = (
# nn.Linear(backbone_out_channels, num_classes)
# if num_classes > 0
# else nn.Identity()
# )
self.backbone = build_model(backbone)
self.criteria = build_criteria(criteria)
def forward(self, input_dict):
point = Point(input_dict)
point = self.backbone(point)
# Backbone added after v1.5.0 return Point instead of feat and use DefaultSegmentorV2
# TODO: remove this part after make all backbone return Point only.
if isinstance(point, Point):
feat = point.feat
else:
feat = point
# seg_logits = self.seg_head(feat)
# train
if self.training:
loss = self.criteria(feat, input_dict["clip_feat"])
return dict(loss=loss)
# eval
elif "clip_feat" in input_dict.keys():
loss = self.criteria(feat, input_dict["clip_feat"])
return dict(loss=loss, seg_logits=feat)
# test
else:
return dict(seg_logits=feat)