Skip to content

Commit

Permalink
Detection eval - Fix bug if first key has no boxes (nutonomy#408)
Browse files Browse the repository at this point in the history
* Fix bug if first key has no boxes

* Outsource and test _get_box_class_field

* Revert accidentally committed notebook update
  • Loading branch information
holger-motional authored Jun 9, 2020
1 parent ef1f1bd commit da3630a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 81 deletions.
36 changes: 26 additions & 10 deletions python-sdk/nuscenes/eval/common/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,8 @@ def filter_eval_boxes(nusc: NuScenes,
:param max_dist: Maps the detection name to the eval distance threshold for that class.
:param verbose: Whether to print to stdout.
"""
# Retrieve box type.
assert len(eval_boxes.boxes) > 0
first_key = list(eval_boxes.boxes.keys())[0]
box = eval_boxes.boxes[first_key][0]
if isinstance(box, DetectionBox):
class_field = 'detection_name'
elif isinstance(box, TrackingBox):
class_field = 'tracking_name'
else:
raise Exception('Error: Invalid box type: %s' % box)
# Retrieve box type for detectipn/tracking boxes.
class_field = _get_box_class_field(eval_boxes)

# Accumulators for number of filtered boxes.
total, dist_filter, point_filter, bike_rack_filter = 0, 0, 0, 0
Expand Down Expand Up @@ -266,3 +258,27 @@ def filter_eval_boxes(nusc: NuScenes,
print("=> After bike rack filtering: %d" % bike_rack_filter)

return eval_boxes


def _get_box_class_field(eval_boxes: EvalBoxes) -> str:
"""
Retrieve the name of the class field in the boxes.
This parses through all boxes until it finds a valid box.
If there are no valid boxes, this function throws an exception.
:param eval_boxes: The EvalBoxes used for evaluation.
:return: The name of the class field in the boxes, e.g. detection_name or tracking_name.
"""
assert len(eval_boxes.boxes) > 0
box = None
for val in eval_boxes.boxes.values():
if len(val) > 0:
box = val[0]
break
if isinstance(box, DetectionBox):
class_field = 'detection_name'
elif isinstance(box, TrackingBox):
class_field = 'tracking_name'
else:
raise Exception('Error: Invalid box type: %s' % box)

return class_field
20 changes: 20 additions & 0 deletions python-sdk/nuscenes/eval/detection/tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from nuscenes.eval.common.data_classes import EvalBoxes
from nuscenes.eval.common.loaders import filter_eval_boxes
from nuscenes.eval.detection.data_classes import DetectionBox
from nuscenes.eval.common.loaders import _get_box_class_field


class TestLoader(unittest.TestCase):
Expand Down Expand Up @@ -169,6 +170,25 @@ def test_filter_eval_boxes(self):
self.assertEqual(filtered_boxes.boxes[sample_token][1].ego_dist, 45.0)
self.assertEqual(filtered_boxes.boxes[sample_token][2].num_pts, 1)

def test_get_box_class_field(self):
eval_boxes = EvalBoxes()
box1 = DetectionBox(sample_token='box1',
translation=(683.681, 1592.002, 0.809),
size=(1, 1, 1),
detection_name='bicycle',
ego_translation=(25.0, 0.0, 0.0))

box2 = DetectionBox(sample_token='box2',
translation=(683.681, 1592.002, 0.809),
size=(1, 1, 1),
detection_name='motorcycle',
ego_translation=(45.0, 0.0, 0.0))
eval_boxes.add_boxes('sample1', [])
eval_boxes.add_boxes('sample2', [box1, box2])

class_field = _get_box_class_field(eval_boxes)
self.assertEqual(class_field, 'detection_name')


if __name__ == '__main__':
unittest.main()
88 changes: 17 additions & 71 deletions python-sdk/tutorials/map_expansion_tutorial.ipynb

Large diffs are not rendered by default.

0 comments on commit da3630a

Please sign in to comment.