Beispiel #1
0
class TestDetectorBase(unittest.TestCase):
    """Main test class for detector base class in frontend."""

    def setUp(self):
        super().setUp()
        self.detector = DummyDetector()
        self.loader = OlssonLoader(TEST_DATA_PATH, image_extension="JPG")

    def test_number_of_detections(self):
        """Tests that the number of detections is less than the maximum number configured."""
        test_image = self.loader.get_image(0)
        keypoints = self.detector.detect(test_image)

        self.assertLessEqual(len(keypoints), self.detector.max_keypoints)

    def test_coordinates_range(self):
        """Tests that each coordinate is within the image bounds."""
        test_image = self.loader.get_image(0)
        keypoints = self.detector.detect(test_image)

        np.testing.assert_array_equal(keypoints.coordinates[:, 0] >= 0, True)
        np.testing.assert_array_equal(keypoints.coordinates[:, 0] <= test_image.width, True)
        np.testing.assert_array_equal(keypoints.coordinates[:, 1] >= 0, True)
        np.testing.assert_array_equal(keypoints.coordinates[:, 1] <= test_image.height, True)

    def test_scale(self):
        """Tests that the scales are positive."""
        keypoints = self.detector.detect(self.loader.get_image(0))

        if keypoints.scales is not None:
            np.testing.assert_array_equal(keypoints.scales >= 0, True)

    def test_computation_graph(self):
        """Test the dask's computation graph formation using a single image."""

        idx_under_test = 0

        image_graph = self.loader.create_computation_graph_for_images()[idx_under_test]
        keypoints_graph = self.detector.create_computation_graph(image_graph)

        with dask.config.set(scheduler="single-threaded"):
            keypoints = dask.compute(keypoints_graph)[0]

        # check the results via normal workflow and dask workflow for an image
        expected_keypoints = self.detector.detect(self.loader.get_image(0))

        self.assertEqual(keypoints, expected_keypoints)

    def test_pickleable(self):
        """Tests that the detector object is pickleable (required for dask)."""
        try:
            pickle.dumps(self.detector)
        except TypeError:
            self.fail("Cannot dump detector using pickle")
class TestDescriptorBase(unittest.TestCase):
    """Unit tests for the DescriptorBase class.

    Should be inherited by all descriptor unit tests.
    """
    def setUp(self):
        self.descriptor = DummyDescriptor()
        self.loader = OlssonLoader(str(TEST_DATA_PATH), image_extension="JPG")

    def test_result_size(self):
        """Check if the number of descriptors are same as number of features."""

        input_image = self.loader.get_image(0)
        input_keypoints = Keypoints(coordinates=np.random.randint(
            low=[0, 0],
            high=[input_image.width, input_image.height],
            size=(5, 2),
        ))

        result = self.descriptor.describe(input_image, input_keypoints)

        self.assertEqual(len(input_keypoints), result.shape[0])

    def test_with_no_features(self):
        """Checks that empty feature inputs works well."""
        input_image = self.loader.get_image(0)
        input_keypoints = Keypoints(coordinates=np.array([]))

        result = self.descriptor.describe(input_image, input_keypoints)

        self.assertEqual(0, result.size)

    def test_create_computation_graph(self):
        """Checks the dask computation graph."""

        # testing some indices
        idxs_under_test = [0, 5]

        for idx in idxs_under_test:

            test_image = self.loader.get_image(idx)
            test_keypoints = Keypoints(coordinates=np.random.randint(
                low=[0, 0],
                high=[test_image.width, test_image.height],
                size=(np.random.randint(5, 10), 2),
            ))

            descriptor_graph = self.descriptor.create_computation_graph(
                dask.delayed(test_image),
                dask.delayed(test_keypoints),
            )

            with dask.config.set(scheduler="single-threaded"):
                descriptors = dask.compute(descriptor_graph)[0]

            expected_descriptors = self.descriptor.describe(
                test_image, test_keypoints)

            np.testing.assert_allclose(descriptors, expected_descriptors)

    def test_pickleable(self):
        """Tests that the descriptor is pickleable (required for dask)."""
        try:
            pickle.dumps(self.descriptor)
        except TypeError:
            self.fail("Cannot dump descriptor using pickle")
class TestFolderLoader(unittest.TestCase):
    """Unit tests for folder loader, which loads image from a folder on disk."""
    def setUp(self):
        """Set up the loader for the test."""
        super().setUp()

        self.loader = OlssonLoader(str(DEFAULT_FOLDER), image_extension="JPG")

    def test_len(self):
        """Test the number of entries in the loader."""

        self.assertEqual(12, len(self.loader))

    def test_get_image_valid_index(self):
        """Tests that get_image works for all valid indices."""

        for idx in range(len(self.loader)):
            self.assertIsNotNone(self.loader.get_image(idx))

    def test_get_image_invalid_index(self):
        """Test that get_image raises an exception on an invalid index."""

        # negative index
        with self.assertRaises(IndexError):
            self.loader.get_image(-1)
        # len() as index
        with self.assertRaises(IndexError):
            self.loader.get_image(12)
        # index > len()
        with self.assertRaises(IndexError):
            self.loader.get_image(15)

    def test_image_contents(self):
        """Test the actual image which is being fetched by the loader at an index.

        This test's primary purpose is to check if the ordering of filename is being respected by the loader
        """
        index_to_test = 5
        file_path = DEFAULT_FOLDER / "images" / "DSC_0006.JPG"
        loader_image = self.loader.get_image(index_to_test)
        expected_image = io_utils.load_image(file_path)
        np.testing.assert_allclose(expected_image.value_array,
                                   loader_image.value_array)

    def test_get_camera_pose_exists(self):
        """Tests that the correct pose is fetched (present on disk)."""
        fetched_pose = self.loader.get_camera_pose(1)

        wRi_expected = np.array([
            [0.998079, 0.015881, 0.0598844],
            [-0.0161175, 0.999864, 0.00346851],
            [-0.0598212, -0.00442703, 0.998199],
        ])
        wti_expected = np.array([-0.826311, -0.00409053, 0.111315])

        expected_pose = Pose3(Rot3(wRi_expected), wti_expected)
        self.assertTrue(expected_pose.equals(fetched_pose, 1e-2))

    def test_get_camera_pose_missing(self):
        """Tests that the camera pose is None, because it is missing on disk."""
        loader = OlssonLoader(str(NO_EXTRINSICS_FOLDER), image_extension="JPG")
        fetched_pose = loader.get_camera_pose(5)
        self.assertIsNone(fetched_pose)

    def test_get_camera_intrinsics_explicit(self):
        """Tests getter for intrinsics when explicit data.mat file with intrinsics are present on disk."""
        expected_fx = 2398.119
        expected_fy = 2393.952
        expected_fx = min(expected_fx, expected_fy)

        expected_px = 628.265
        expected_py = 932.382

        computed = self.loader.get_camera_intrinsics(5)
        expected = Cal3Bundler(fx=expected_fx,
                               k1=0,
                               k2=0,
                               u0=expected_px,
                               v0=expected_py)

        self.assertTrue(expected.equals(computed, 1e-3))

    def test_get_camera_intrinsics_exif(self):
        """Tests getter for intrinsics when explicit numpy arrays are absent and we fall back on exif."""
        loader = OlssonLoader(EXIF_FOLDER,
                              image_extension="JPG",
                              use_gt_intrinsics=False)
        computed = loader.get_camera_intrinsics(5)
        expected = Cal3Bundler(fx=2378.983, k1=0, k2=0, u0=648.0, v0=968.0)
        self.assertTrue(expected.equals(computed, 1e-3))

    def test_get_camera_intrinsics_missing(self):
        """Tests getter for intrinsics when explicit numpy arrays are absent and we fall back on exif."""
        loader = OlssonLoader(NO_EXIF_FOLDER, image_extension="JPG")
        computed = loader.get_camera_intrinsics(5)
        self.assertIsNone(computed)

    def test_create_computation_graph_for_images(self):
        """Tests the graph for loading all the images."""
        image_graph = self.loader.create_computation_graph_for_images()

        # check the length of the graph
        self.assertEqual(12, len(image_graph))
        results = dask.compute(image_graph)[0]

        # randomly check image loads from a few indices
        np.testing.assert_allclose(results[5].value_array,
                                   self.loader.get_image(5).value_array)
        np.testing.assert_allclose(results[7].value_array,
                                   self.loader.get_image(7).value_array)

    def test_create_computation_graph_for_intrinsics(self):
        """Tests the graph for all intrinsics."""

        intrinsics_graph = self.loader.create_computation_graph_for_intrinsics(
        )

        # check the length of the graph
        self.assertEqual(12, len(intrinsics_graph))

        results = dask.compute(intrinsics_graph)[0]

        # randomly check intrinsics from a few indices
        self.assertTrue(
            self.loader.get_camera_intrinsics(5).equals(results[5], 1e-5))
        self.assertTrue(
            self.loader.get_camera_intrinsics(7).equals(results[7], 1e-5))