예제 #1
0
    def test_errors_contain_correct_image_numbers(self):
        detections = [
            [{
                'label_probs': [0.1, 0.2, 0.3, 0.4],
                'bbox': [12, 14, 55, 46],
                'covars': [[[1, 0], [0, 1]], [[1, 0], [0, 1]]]
            }],
            [],
            [],
            [{
                'label_probs': [0.1, 0.2, 0.3, 0.4],
                'bbox': [12, 14, 55, 46],
                'covars': [[[1, 0], [0, 1]], [[1, 0], [0, 1]]]
            }, {
                'label_probs': [0.1, 0.2, 0.3, 0.4],
                'bbox': [12, 14, 55, 46],
                'covars': [[[1, 2], [2, 1]], [[1, 0], [0, 1]]]
            }],
        ]
        sequence_json = self.make_sequence(detections)
        seq_gen = submission_loader.read_sequence(sequence_json)

        # the first 3 images are fine
        for _ in range(3):
            next(seq_gen)

        img_dets = next(seq_gen)
        next(img_dets)  # First detection is fine

        with self.assertRaises(ValueError) as cm:
            next(img_dets)  # Second detection, fourth image has a problem
        msg = str(cm.exception)
        self.assertIn(os.path.basename(sequence_json), msg)
        self.assertIn('3', msg)
        self.assertIn('1', msg)
예제 #2
0
    def test_errors_if_no_valid_classes(self):
        detections = [
            [{
                'label_probs': [0.1, 0.2, 0.3, 0.4],
                'bbox': [12, 14, 55, 46],
                'covars': [[[1, 0], [0, 1]], [[1, 0], [0, 1]]]
            }],
            [],
            [],
            [{
                'label_probs': [0.1, 0.2, 0.3, 0.4],
                'bbox': [12, 14, 55, 46],
                'covars': [[[1, 0], [0, 1]], [[1, 0], [0, 1]]]
            }, {
                'label_probs': [0.1, 0.2, 0.3, 0.4],
                'bbox': [12, 14, 55, 46],
                'covars': [[[1, 2], [2, 1]], [[1, 0], [0, 1]]]
            }],
        ]
        patch_classes(detections)

        os.makedirs(self.temp_dir, exist_ok=True)
        json_file = os.path.join(self.temp_dir, 'test.json')
        with open(json_file, 'w') as fp:
            json.dump({
                'classes': [str(idx) for idx in range(len(class_list.CLASSES))],
                'detections': detections
            }, fp)

        with self.assertRaises(ValueError) as cm:
            next(submission_loader.read_sequence(json_file))
        msg = str(cm.exception)
        self.assertIn('test.json', msg)
예제 #3
0
 def test_inner_generator_returns_detection_instances(self):
     detections = [
         [{
             'label_probs': [0.1, 0.2, 0.3, 0.4],
             'bbox': [12, 14, 55, 46],
             'covars': [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
         }],
         [],
         [],
         [{
             'label_probs': [0.1, 0.2, 0.3, 0.4],
             'bbox': [12, 14, 55, 46],
             'covars': [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
         }, {
             'label_probs': [0.1, 0.2, 0.3, 0.4],
             'bbox': [12, 14, 55, 46],
             'covars': [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
         }],
     ]
     sequence_json = self.make_sequence(detections)
     gen = submission_loader.read_sequence(sequence_json)
     seq_dets = list(gen)
     self.assertEqual(len(detections), len(seq_dets))
     for img_idx in range(len(seq_dets)):
         img_dets = list(seq_dets[img_idx])
         self.assertEqual(len(detections[img_idx]), len(img_dets))
         for det_idx in range(len(img_dets)):
             self.assertIsInstance(img_dets[det_idx], data_holders.DetectionInstance)
             expected_probs = detections[img_idx][det_idx]['label_probs']
             expected_list = np.zeros(len(class_list.CLASSES), dtype=np.float32)
             expected_list[0:len(expected_probs)] = expected_probs
             self.assertNPEqual(expected_list, img_dets[det_idx].class_list)
예제 #4
0
 def test_returns_generator_of_generators(self):
     detections = [
         [{
             'label_probs': [0.1, 0.2, 0.3, 0.4],
             'bbox': [12, 14, 55, 46],
             'covars': [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
         }],
         [],
         [],
         [{
             'label_probs': [0.1, 0.2, 0.3, 0.4],
             'bbox': [12, 14, 55, 46],
             'covars': [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
         }, {
             'label_probs': [0.1, 0.2, 0.3, 0.4],
             'bbox': [12, 14, 55, 46],
             'covars': [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
         }],
     ]
     sequence_json = self.make_sequence(detections)
     gen = submission_loader.read_sequence(sequence_json)
     self.assertIsInstance(gen, types.GeneratorType)
     seq_dets = list(gen)
     self.assertEqual(len(detections), len(seq_dets))
     for img_idx in range(len(seq_dets)):
         self.assertIsInstance(seq_dets[img_idx], types.GeneratorType)
         img_dets = list(seq_dets[img_idx])
         self.assertEqual(len(detections[img_idx]), len(img_dets))
예제 #5
0
    def test_errors_if_detections_missing(self):
        os.makedirs(self.temp_dir, exist_ok=True)
        json_file = os.path.join(self.temp_dir, 'test.json')
        with open(json_file, 'w') as fp:
            json.dump({
                'classes': class_list.CLASSES,
                # 'detections': detections
            }, fp)

        with self.assertRaises(KeyError) as cm:
            next(submission_loader.read_sequence(json_file))
        msg = str(cm.exception)
        self.assertIn('test.json', msg)
예제 #6
0
 def test_slice_both(self):
     det = {
         'label_probs': [0.1, 0.2, 0.3, 0.4],
         'bbox': [12, 14, 55, 46],
         'covars': [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
     }
     detections = [
         [det for _ in range(1)],
         [det for _ in range(3)],
         [det for _ in range(5)],
         [det for _ in range(7)],
     ]
     sequence_json = self.make_sequence(detections)
     gen = submission_loader.read_sequence(sequence_json, start_index=1, end_index=3)
     self.assertIsInstance(gen, types.GeneratorType)
     seq_dets = list(gen)
     self.assertEqual(2, len(seq_dets))
     for img_idx in range(2):
         self.assertIsInstance(seq_dets[img_idx], types.GeneratorType)
         img_dets = list(seq_dets[img_idx])
         self.assertEqual(len(detections[img_idx + 1]), len(img_dets))
예제 #7
0
 def test_returns_empty_generator_for_no_images(self):
     sequence_json = self.make_sequence([])
     gen = submission_loader.read_sequence(sequence_json)
     self.assertIsInstance(gen, types.GeneratorType)
     dets = list(gen)
     self.assertEqual(dets, [])