Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Fix unittest with GPU #1301

Merged
merged 3 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions mmdet3d/models/dense_heads/point_rpn_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from mmcv.runner import BaseModule, force_fp32
from torch import nn as nn

from mmdet3d.core import xywhr2xyxyr
from mmdet3d.core.bbox.structures import (DepthInstance3DBoxes,
LiDARInstance3DBoxes)
from mmdet3d.ops.iou3d.iou3d_utils import nms_gpu, nms_normal_gpu
Expand Down Expand Up @@ -321,26 +322,31 @@ def class_agnostic_nms(self, obj_scores, sem_scores, bbox, points,
else:
raise NotImplementedError('Unsupported bbox type!')

bbox = bbox.tensor[nonempty_box_mask]
obj_scores = obj_scores[nonempty_box_mask]
sem_scores = sem_scores[nonempty_box_mask]
bbox_selected = bbox.tensor[nonempty_box_mask]
bbox_for_nms = xywhr2xyxyr(bbox.bev)[nonempty_box_mask]

if self.test_cfg.score_thr is not None:
score_thr = self.test_cfg.score_thr
keep = (obj_scores >= score_thr)
obj_scores = obj_scores[keep]
sem_scores = sem_scores[keep]
bbox = bbox[keep]
bbox_selected = bbox_selected[keep]
bbox_for_nms = bbox_for_nms[keep]

if obj_scores.shape[0] > 0:
topk = min(nms_cfg.nms_pre, obj_scores.shape[0])
obj_scores_nms, indices = torch.topk(obj_scores, k=topk)
bbox_for_nms = bbox[indices]
bbox_selected = bbox_selected[indices]
bbox_for_nms = bbox_for_nms[indices]
sem_scores_nms = sem_scores[indices]

keep = nms_func(bbox_for_nms[:, 0:7], obj_scores_nms,
nms_cfg.iou_thr)
keep = keep[:nms_cfg.nms_post]

bbox_selected = bbox_for_nms[keep]
bbox_selected = bbox_selected[keep]
score_selected = obj_scores_nms[keep]
cls_preds = sem_scores_nms[keep]
labels = torch.argmax(cls_preds, -1)
Expand Down
9 changes: 6 additions & 3 deletions tests/test_data/test_datasets/test_kitti_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,12 @@ def test_evaluate():
metric = ['mAP']
result = dict(boxes_3d=boxes_3d, labels_3d=labels_3d, scores_3d=scores_3d)
ap_dict = kitti_dataset.evaluate([result], metric)
assert np.isclose(ap_dict['KITTI/Overall_3D_easy'], 3.0303030303030307)
assert np.isclose(ap_dict['KITTI/Overall_3D_moderate'], 3.0303030303030307)
assert np.isclose(ap_dict['KITTI/Overall_3D_hard'], 3.0303030303030307)
assert np.isclose(ap_dict['KITTI/Overall_3D_AP11_easy'],
3.0303030303030307)
assert np.isclose(ap_dict['KITTI/Overall_3D_AP11_moderate'],
3.0303030303030307)
assert np.isclose(ap_dict['KITTI/Overall_3D_AP11_hard'],
3.0303030303030307)


def test_show():
Expand Down
14 changes: 8 additions & 6 deletions tests/test_data/test_datasets/test_kitti_mono_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ def test_evaluate():

metric = ['mAP']
ap_dict = kitti_dataset.evaluate(results, metric)
assert np.isclose(ap_dict['img_bbox/KITTI/Overall_3D_easy'], 3.0303)
assert np.isclose(ap_dict['img_bbox/KITTI/Overall_3D_moderate'], 6.0606)
assert np.isclose(ap_dict['img_bbox/KITTI/Overall_3D_hard'], 6.0606)
assert np.isclose(ap_dict['img_bbox2d/KITTI/Overall_2D_easy'], 3.0303)
assert np.isclose(ap_dict['img_bbox2d/KITTI/Overall_2D_moderate'], 6.0606)
assert np.isclose(ap_dict['img_bbox2d/KITTI/Overall_2D_hard'], 6.0606)
assert np.isclose(ap_dict['img_bbox/KITTI/Overall_3D_AP11_easy'], 3.0303)
assert np.isclose(ap_dict['img_bbox/KITTI/Overall_3D_AP11_moderate'],
6.0606)
assert np.isclose(ap_dict['img_bbox/KITTI/Overall_3D_AP11_hard'], 6.0606)
assert np.isclose(ap_dict['img_bbox2d/KITTI/Overall_2D_AP11_easy'], 3.0303)
assert np.isclose(ap_dict['img_bbox2d/KITTI/Overall_2D_AP11_moderate'],
6.0606)
assert np.isclose(ap_dict['img_bbox2d/KITTI/Overall_2D_AP11_hard'], 6.0606)
9 changes: 6 additions & 3 deletions tests/test_data/test_datasets/test_waymo_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@ def test_evaluate():
# kitti protocol
metric = ['kitti']
ap_dict = waymo_dataset.evaluate([result], metric=metric)
assert np.isclose(ap_dict['KITTI/Overall_3D_easy'], 3.0303030303030307)
assert np.isclose(ap_dict['KITTI/Overall_3D_moderate'], 3.0303030303030307)
assert np.isclose(ap_dict['KITTI/Overall_3D_hard'], 3.0303030303030307)
assert np.isclose(ap_dict['KITTI/Overall_3D_AP11_easy'],
3.0303030303030307)
assert np.isclose(ap_dict['KITTI/Overall_3D_AP11_moderate'],
3.0303030303030307)
assert np.isclose(ap_dict['KITTI/Overall_3D_AP11_hard'],
3.0303030303030307)

# waymo protocol
metric = ['waymo']
Expand Down
12 changes: 6 additions & 6 deletions tests/test_models/test_common_modules/test_dgcnn_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def test_dgcnn_gf_module():
self = DGCNNGFModule(
mlp_channels=[18, 64, 64],
num_sample=20,
knn_mod='D-KNN',
knn_mode='D-KNN',
radius=None,
norm_cfg=dict(type='BN2d'),
act_cfg=dict(type='ReLU'),
pool_mod='max').cuda()
pool_mode='max').cuda()

assert self.mlps[0].layer0.conv.in_channels == 18
assert self.mlps[0].layer0.conv.out_channels == 64
Expand All @@ -36,11 +36,11 @@ def test_dgcnn_gf_module():
self = DGCNNGFModule(
mlp_channels=[6, 64, 64],
num_sample=20,
knn_mod='F-KNN',
knn_mode='F-KNN',
radius=None,
norm_cfg=dict(type='BN2d'),
act_cfg=dict(type='ReLU'),
pool_mod='max').cuda()
pool_mode='max').cuda()

# test forward
new_points = self(xyz)
Expand All @@ -50,11 +50,11 @@ def test_dgcnn_gf_module():
self = DGCNNGFModule(
mlp_channels=[6, 64, 64],
num_sample=20,
knn_mod='F-KNN',
knn_mode='F-KNN',
radius=0.2,
norm_cfg=dict(type='BN2d'),
act_cfg=dict(type='ReLU'),
pool_mod='max').cuda()
pool_mode='max').cuda()


def test_dgcnn_fa_module():
Expand Down
9 changes: 4 additions & 5 deletions tests/test_models/test_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,11 @@ def test_imvoxelnet():
assert labels_3d.shape[0] >= 0


def test_pointrcnn():
def test_point_rcnn():
if not torch.cuda.is_available():
pytest.skip('test requires GPU and torch+cuda')
pointrcnn_cfg = _get_detector_cfg(
'pointrcnn/pointrcnn_2x8_kitti-3d-3classes.py')
'point_rcnn/point_rcnn_2x8_kitti-3d-3classes.py')
self = build_detector(pointrcnn_cfg).cuda()
points_0 = torch.rand([1000, 4], device='cuda')
points_1 = torch.rand([1000, 4], device='cuda')
Expand Down Expand Up @@ -539,9 +539,8 @@ def test_smoke():
attr_labels = None
img_metas = [
dict(
cam_intrinsic=[[721.5377, 0., 609.5593, 0.],
[0., 721.5377, 172.854, 0.], [0., 0., 1., 0.],
[0., 0., 0., 1.]],
cam2img=[[721.5377, 0., 609.5593, 0.], [0., 721.5377, 172.854, 0.],
[0., 0., 1., 0.], [0., 0., 0., 1.]],
scale_factor=np.array([1., 1., 1., 1.], dtype=np.float32),
pad_shape=[384, 1280],
trans_mat=np.array([[0.25, 0., 0.], [0., 0.25, 0], [0., 0., 1.]],
Expand Down
29 changes: 13 additions & 16 deletions tests/test_models/test_heads/test_heads.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def _get_head_cfg(fname):
These are deep copied to allow for safe modification of parameters without
influencing other tests.
"""
import mmcv
config = _get_config_module(fname)
model = copy.deepcopy(config.model)
train_cfg = mmcv.Config(copy.deepcopy(config.model.train_cfg))
Expand All @@ -70,7 +69,6 @@ def _get_rpn_head_cfg(fname):
These are deep copied to allow for safe modification of parameters without
influencing other tests.
"""
import mmcv
config = _get_config_module(fname)
model = copy.deepcopy(config.model)
train_cfg = mmcv.Config(copy.deepcopy(config.model.train_cfg))
Expand All @@ -88,7 +86,6 @@ def _get_roi_head_cfg(fname):
These are deep copied to allow for safe modification of parameters without
influencing other tests.
"""
import mmcv
config = _get_config_module(fname)
model = copy.deepcopy(config.model)
train_cfg = mmcv.Config(copy.deepcopy(config.model.train_cfg))
Expand All @@ -106,7 +103,6 @@ def _get_pts_bbox_head_cfg(fname):
These are deep copied to allow for safe modification of parameters without
influencing other tests.
"""
import mmcv
config = _get_config_module(fname)
model = copy.deepcopy(config.model)
train_cfg = mmcv.Config(copy.deepcopy(config.model.train_cfg.pts))
Expand All @@ -132,7 +128,7 @@ def _get_pointrcnn_rpn_head_cfg(fname):
rpn_head = model.rpn_head
rpn_head.update(train_cfg=train_cfg.rpn)
rpn_head.update(test_cfg=test_cfg.rpn)
return rpn_head, train_cfg.rpn.rpn_proposal
return rpn_head, train_cfg.rpn


def _get_vote_head_cfg(fname):
Expand All @@ -141,7 +137,6 @@ def _get_vote_head_cfg(fname):
These are deep copied to allow for safe modification of parameters without
influencing other tests.
"""
import mmcv
config = _get_config_module(fname)
model = copy.deepcopy(config.model)
train_cfg = mmcv.Config(copy.deepcopy(config.model.train_cfg))
Expand Down Expand Up @@ -290,11 +285,11 @@ def test_parta2_rpnhead_getboxes():
assert result_list[0]['boxes_3d'].tensor.shape == torch.Size([512, 7])


def test_pointrcnn_rpnhead_getboxes():
def test_point_rcnn_rpnhead_getboxes():
if not torch.cuda.is_available():
pytest.skip('test requires GPU and torch+cuda')
rpn_head_cfg, proposal_cfg = _get_pointrcnn_rpn_head_cfg(
'./pointrcnn/pointrcnn_2x8_kitti-3d-3classes.py')
'./point_rcnn/point_rcnn_2x8_kitti-3d-3classes.py')
self = build_head(rpn_head_cfg)
self.cuda()

Expand All @@ -315,7 +310,7 @@ def test_pointrcnn_rpnhead_getboxes():
assert cls_preds.shape == (2, 1024, 3)
points = torch.rand([2, 1024, 3], dtype=torch.float32).cuda()
result_list = self.get_bboxes(points, bbox_preds, cls_preds, input_metas)
max_num = proposal_cfg.max_num
max_num = proposal_cfg.nms_cfg.nms_post
bbox, score_selected, labels, cls_preds_selected = result_list[0]
assert bbox.tensor.shape == (max_num, 7)
assert score_selected.shape == (max_num, )
Expand Down Expand Up @@ -515,22 +510,24 @@ def test_smoke_mono3d_head():


def test_parta2_bbox_head():
if not torch.cuda.is_available():
pytest.skip('test requires GPU and torch+cuda')
parta2_bbox_head_cfg = _get_parta2_bbox_head_cfg(
'./parta2/hv_PartA2_secfpn_2x8_cyclic_80e_kitti-3d-3class.py')
self = build_head(parta2_bbox_head_cfg)
seg_feats = torch.rand([256, 14, 14, 14, 16])
part_feats = torch.rand([256, 14, 14, 14, 4])
self = build_head(parta2_bbox_head_cfg).cuda()
seg_feats = torch.rand([256, 14, 14, 14, 16]).cuda()
part_feats = torch.rand([256, 14, 14, 14, 4]).cuda()

cls_score, bbox_pred = self.forward(seg_feats, part_feats)
assert cls_score.shape == (256, 1)
assert bbox_pred.shape == (256, 7)


def test_pointrcnn_bbox_head():
def test_point_rcnn_bbox_head():
if not torch.cuda.is_available():
pytest.skip('test requires GPU and torch+cuda')
pointrcnn_bbox_head_cfg = _get_pointrcnn_bbox_head_cfg(
'./pointrcnn/pointrcnn_2x8_kitti-3d-3classes.py')
'./point_rcnn/point_rcnn_2x8_kitti-3d-3classes.py')
self = build_head(pointrcnn_bbox_head_cfg).cuda()
feats = torch.rand([100, 512, 133]).cuda()
rcnn_cls, rcnn_reg = self.forward(feats)
Expand Down Expand Up @@ -612,12 +609,12 @@ def test_part_aggregation_ROI_head():
assert labels_3d.shape == (12, )


def test_pointrcnn_roi_head():
def test_point_rcnn_roi_head():
if not torch.cuda.is_available():
pytest.skip('test requires GPU and torch+cuda')

roi_head_cfg = _get_roi_head_cfg(
'./pointrcnn/pointrcnn_2x8_kitti-3d-3classes.py')
'./point_rcnn/point_rcnn_2x8_kitti-3d-3classes.py')

self = build_head(roi_head_cfg).cuda()

Expand Down
3 changes: 1 addition & 2 deletions tests/test_models/test_heads/test_roi_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def test_single_roipoint_extractor():
if not torch.cuda.is_available():
pytest.skip('test requires GPU and torch+cuda')

roi_layer_cfg = dict(
type='RoIPointPool3d', num_sampled_points=512, pool_extra_width=0)
roi_layer_cfg = dict(type='RoIPointPool3d', num_sampled_points=512)

self = Single3DRoIPointExtractor(roi_layer=roi_layer_cfg)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_models/test_necks/test_necks.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_dla_neck():
for i in range(len(in_channels))
]
outputs = neck(feats)
assert outputs.shape == (4, 64, 8, 8)
assert outputs[0].shape == (4, 64, 8, 8)
else:
# Test DLA Neck without DCNv2 on CPU
neck_cfg = dict(
Expand Down