Skip to content

Commit e90e176

Browse files
[Fix] Fix UT of Seg TTA & p2v_map bug when gt is none (open-mmlab#2466)
* fix p2v_map when no gt & unit name * fix gt bug * fix max_voxels
1 parent f8e3ce8 commit e90e176

File tree

7 files changed

+25
-21
lines changed

7 files changed

+25
-21
lines changed

configs/_base_/models/minkunet.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
point_cloud_range=[-100, -100, -20, 100, 100, 20],
1010
voxel_size=[0.05, 0.05, 0.05],
1111
max_voxels=(-1, -1)),
12-
),
12+
max_voxels=80000),
1313
backbone=dict(
1414
type='MinkUNetBackbone',
1515
in_channels=4,
1616
base_channels=32,
17-
encoder_channels=[32, 64, 128, 256],
18-
decoder_channels=[256, 128, 96, 96],
1917
num_stages=4,
20-
init_cfg=None),
18+
encoder_channels=[32, 64, 128, 256],
19+
decoder_channels=[256, 128, 96, 96]),
2120
decode_head=dict(
2221
type='MinkUNetHead',
2322
channels=96,

configs/_base_/models/spvcnn.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
point_cloud_range=[-100, -100, -20, 100, 100, 20],
1010
voxel_size=[0.05, 0.05, 0.05],
1111
max_voxels=(-1, -1)),
12-
),
12+
max_voxels=80000),
1313
backbone=dict(
1414
type='SPVCNNBackbone',
1515
in_channels=4,
1616
base_channels=32,
17+
num_stages=4,
1718
encoder_channels=[32, 64, 128, 256],
1819
decoder_channels=[256, 128, 96, 96],
19-
num_stages=4,
2020
drop_ratio=0.3),
2121
decode_head=dict(
2222
type='MinkUNetHead',

mmdet3d/models/data_preprocessors/data_preprocessor.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class Det3DDataPreprocessor(DetDataPreprocessor):
4949
voxelization and dynamic voxelization. Defaults to 'hard'.
5050
voxel_layer (dict or :obj:`ConfigDict`, optional): Voxelization layer
5151
config. Defaults to None.
52+
max_voxels (int): Maximum number of voxels in each voxel grid. Defaults
53+
to None.
5254
mean (Sequence[Number], optional): The pixel mean of R, G, B channels.
5355
Defaults to None.
5456
std (Sequence[Number], optional): The pixel standard deviation of
@@ -77,6 +79,7 @@ def __init__(self,
7779
voxel: bool = False,
7880
voxel_type: str = 'hard',
7981
voxel_layer: OptConfigType = None,
82+
max_voxels: Optional[int] = None,
8083
mean: Sequence[Number] = None,
8184
std: Sequence[Number] = None,
8285
pad_size_divisor: int = 1,
@@ -103,6 +106,7 @@ def __init__(self,
103106
batch_augments=batch_augments)
104107
self.voxel = voxel
105108
self.voxel_type = voxel_type
109+
self.max_voxels = max_voxels
106110
if voxel:
107111
self.voxel_layer = VoxelizationByGridShape(**voxel_layer)
108112

@@ -423,20 +427,22 @@ def voxelize(self, points: List[torch.Tensor],
423427
res_coors -= res_coors.min(0)[0]
424428

425429
res_coors_numpy = res_coors.cpu().numpy()
426-
inds, voxel2point_map = self.sparse_quantize(
430+
inds, point2voxel_map = self.sparse_quantize(
427431
res_coors_numpy, return_index=True, return_inverse=True)
428-
voxel2point_map = torch.from_numpy(voxel2point_map).cuda()
429-
if self.training:
430-
if len(inds) > 80000:
431-
inds = np.random.choice(inds, 80000, replace=False)
432+
point2voxel_map = torch.from_numpy(point2voxel_map).cuda()
433+
if self.training and self.max_voxels is not None:
434+
if len(inds) > self.max_voxels:
435+
inds = np.random.choice(
436+
inds, self.max_voxels, replace=False)
432437
inds = torch.from_numpy(inds).cuda()
433-
data_sample.gt_pts_seg.voxel_semantic_mask \
434-
= data_sample.gt_pts_seg.pts_semantic_mask[inds]
438+
if hasattr(data_sample.gt_pts_seg, 'pts_semantic_mask'):
439+
data_sample.gt_pts_seg.voxel_semantic_mask \
440+
= data_sample.gt_pts_seg.pts_semantic_mask[inds]
435441
res_voxel_coors = res_coors[inds]
436442
res_voxels = res[inds]
437443
res_voxel_coors = F.pad(
438444
res_voxel_coors, (0, 1), mode='constant', value=i)
439-
data_sample.voxel2point_map = voxel2point_map.long()
445+
data_sample.point2voxel_map = point2voxel_map.long()
440446
voxels.append(res_voxels)
441447
coors.append(res_voxel_coors)
442448
voxels = torch.cat(voxels, dim=0)
@@ -466,12 +472,12 @@ def get_voxel_seg(self, res_coors: torch.Tensor, data_sample: SampleList):
466472
True)
467473
voxel_semantic_mask = torch.argmax(voxel_semantic_mask, dim=-1)
468474
data_sample.gt_pts_seg.voxel_semantic_mask = voxel_semantic_mask
469-
data_sample.gt_pts_seg.point2voxel_map = point2voxel_map
475+
data_sample.point2voxel_map = point2voxel_map
470476
else:
471477
pseudo_tensor = res_coors.new_ones([res_coors.shape[0], 1]).float()
472478
_, _, point2voxel_map = dynamic_scatter_3d(pseudo_tensor,
473479
res_coors, 'mean', True)
474-
data_sample.gt_pts_seg.point2voxel_map = point2voxel_map
480+
data_sample.point2voxel_map = point2voxel_map
475481

476482
def ravel_hash(self, x: np.ndarray) -> np.ndarray:
477483
"""Get voxel coordinates hash for np.unique().

mmdet3d/models/decode_heads/cylinder3d_head.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def predict(
151151
for batch_idx in range(len(batch_data_samples)):
152152
seg_logits_sample = seg_logits[coors[:, 0] == batch_idx]
153153
point2voxel_map = batch_data_samples[
154-
batch_idx].gt_pts_seg.point2voxel_map.long()
154+
batch_idx].point2voxel_map.long()
155155
point_seg_predicts = seg_logits_sample[point2voxel_map]
156156
seg_pred_list.append(point_seg_predicts)
157157

mmdet3d/models/decode_heads/minkunet_head.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def predict(self, inputs: SparseTensor,
6161
seg_logit_list = []
6262
for i, data_sample in enumerate(batch_data_samples):
6363
seg_logit = seg_logits[batch_idx == i]
64-
seg_logit = seg_logit[data_sample.voxel2point_map]
64+
seg_logit = seg_logit[data_sample.point2voxel_map]
6565
seg_logit_list.append(seg_logit)
6666

6767
return seg_logit_list

tests/test_models/test_decode_heads/test_cylinder3d_head.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def test_cylinder3d_head_loss(self):
6060
self.assertGreater(loss_lovasz, 0, 'lovasz loss should be positive')
6161

6262
batch_inputs_dict = dict(voxels=dict(voxel_coors=coors))
63-
datasample.gt_pts_seg.point2voxel_map = torch.randint(
64-
0, 50, (100, )).int().cuda()
63+
datasample.point2voxel_map = torch.randint(0, 50, (100, )).int().cuda()
6564
point_logits = cylinder3d_head.predict(sparse_voxels,
6665
batch_inputs_dict, [datasample])
6766
assert point_logits[0].shape == torch.Size([100, 20])

tests/test_models/test_segmentors/test_seg3d_tta_model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ def test_seg3d_tta_model(self):
3636
pcd_vertical_flip=pcd_vertical_flip_list[i]))
3737
])
3838
if torch.cuda.is_available():
39-
model.eval()
39+
model.eval().cuda()
4040
model.test_step(dict(inputs=points, data_samples=data_samples))

0 commit comments

Comments
 (0)