def test_get_top_k_without_responses(self): """Tests the selection of top entries in a keypoints w/o responses.""" input_keypoints = Keypoints( coordinates=np.array( [ [10.0, 23.2], [37.1, 50.2], [90.1, 10.7], [150.0, 122.0], [250.0, 49.0], ] ) ) # test with requested length > current length requested_length = len(input_keypoints) * 2 computed = input_keypoints.get_top_k(requested_length) self.assertEqual(computed, input_keypoints) # test with requested length < current length requested_length = 2 computed = input_keypoints.get_top_k(requested_length) expected = Keypoints(coordinates=input_keypoints.coordinates[:requested_length]) self.assertEqual(computed, expected)
def detect_and_describe(self, image: Image) -> Tuple[Keypoints, np.ndarray]: """Jointly generate keypoint detections and their associated descriptors from a single image.""" # TODO(ayushbaid): fix inference issue #110 device = torch.device("cuda" if self._use_cuda else "cpu") model = SuperPoint(self._config).to(device) model.eval() # Compute features. image_tensor = torch.from_numpy( np.expand_dims( image_utils.rgb_to_gray_cv(image).value_array.astype( np.float32) / 255.0, (0, 1))).to(device) with torch.no_grad(): model_results = model({"image": image_tensor}) torch.cuda.empty_cache() # Unpack results. coordinates = model_results["keypoints"][0].detach().cpu().numpy() scores = model_results["scores"][0].detach().cpu().numpy() keypoints = Keypoints(coordinates, scales=None, responses=scores) descriptors = model_results["descriptors"][0].detach().cpu().numpy().T # Filter features. if image.mask is not None: keypoints, valid_idxs = keypoints.filter_by_mask(image.mask) descriptors = descriptors[valid_idxs] keypoints, selection_idxs = keypoints.get_top_k(self.max_keypoints) descriptors = descriptors[selection_idxs] return keypoints, descriptors
def test_get_top_k_with_responses(self): """Tests the selection of top entries in a keypoints with responses.""" input_keypoints = Keypoints( coordinates=np.array( [ [10.0, 23.2], [37.1, 50.2], [90.1, 10.7], [150.0, 122.0], [250.0, 49.0], ] ), scales=np.array([1, 3, 2, 3.2, 1.8]), responses=np.array([0.3, 0.7, 0.9, 0.1, 0.2]), ) # test with requested length > current length requested_length = len(input_keypoints) * 2 computed = input_keypoints.get_top_k(requested_length) self.assertEqual(computed, input_keypoints) # test with requested length < current length requested_length = 2 computed = input_keypoints.get_top_k(requested_length) expected = Keypoints( coordinates=np.array( [ [37.1, 50.2], [90.1, 10.7], ] ), scales=np.array([3, 2]), responses=np.array([0.7, 0.9]), ) # compare in an order-insensitive fashion self.compare_without_ordering(computed, expected)