-
Notifications
You must be signed in to change notification settings - Fork 17
Feature/dino gem agg descriptors #37
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c7415fd
first-pass at dino aggregated frame descriptors, tested on kmd demo
liqyn 6513434
add stacked descriptors and maximum piecewise similarity
liqyn 1d66f39
merge with main
liqyn 1dd8978
tested demo params
liqyn fc45e46
pr changes
liqyn a3c005a
refactor submap descriptor extraction for roman_ros
liqyn 8c35f84
update setup.py version/info
mbpeterson70 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,6 +116,7 @@ def from_params(cls, params: FastSAMParams, depth_cam_params: CameraParams): | |
| allow_tblr_edges=[True, True, True, True], | ||
| area_bounds=[img_area / (params.min_mask_len_div**2), img_area / (params.max_mask_len_div**2)], | ||
| semantics=params.semantics, | ||
| frame_descriptor=params.frame_descriptor, | ||
| triangle_ignore_masks=params.triangle_ignore_masks | ||
| ) | ||
|
|
||
|
|
@@ -132,6 +133,7 @@ def setup_filtering(self, | |
| allow_tblr_edges = [True, True, True, True], | ||
| keep_mask_minimal_intersection=0.3, | ||
| semantics: str = None, | ||
| frame_descriptor: str = None, | ||
| triangle_ignore_masks=None | ||
| ): | ||
| """ | ||
|
|
@@ -176,6 +178,9 @@ def setup_filtering(self, | |
| else: | ||
| raise ValueError(f"Invalid semantics option: {semantics}. Choose from 'clip', 'dino', or 'none'.") | ||
| self.semantic_patches_shape = None | ||
| self.frame_descriptor_type = frame_descriptor | ||
| if frame_descriptor is not None: | ||
| assert self.semantics.lower() == 'dino', "Frame descriptor only supported with DINO semantics." | ||
|
|
||
| if triangle_ignore_masks is not None: | ||
| self.constant_ignore_mask = np.zeros((self.depth_cam_params.height, self.depth_cam_params.width), dtype=np.uint8) | ||
|
|
@@ -235,16 +240,16 @@ def setup_rgbd_params( | |
| self.erosion_element = None | ||
| self.plane_filter_params = plane_filter_params | ||
|
|
||
| def run(self, t, pose, img, depth_data=None, plot=False): | ||
| def run(self, t, pose, img, depth_data=None): | ||
| """ | ||
| Takes and image and returns filtered FastSAM masks as Observations. | ||
|
|
||
| Args: | ||
| img (cv image): camera image | ||
| plot (bool, optional): Returns plots if true. Defaults to False. | ||
|
|
||
| Returns: | ||
| self.observations (list): list of Observations | ||
| frame_descriptor (np.ndarray): semantic descriptor of the frame if frame_descriptor is not None, else None | ||
| """ | ||
| self.observations = [] | ||
|
|
||
|
|
@@ -274,14 +279,20 @@ def run(self, t, pose, img, depth_data=None, plot=False): | |
| img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB) | ||
| preprocessed = self.semantics_preprocess(images=img_rgb, return_tensors="pt").to(self.device) | ||
| dino_output = self.semantics_model(**preprocessed) | ||
| dino_features = self.get_per_pixel_features( | ||
| dino_output_patches = self.get_output_patches( | ||
| model_output=dino_output.last_hidden_state, | ||
| img_shape=img.shape, | ||
| feature_dim=dino_shape | ||
| ) | ||
| dino_features = self.get_per_pixel_features( | ||
| model_output_patches=dino_output_patches, | ||
| img_shape=img.shape | ||
| ) | ||
mbpeterson70 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dino_features = self.unapply_rotation(dino_features) | ||
|
|
||
|
|
||
|
|
||
| frame_descriptor = None | ||
| if self.frame_descriptor_type is not None: | ||
| frame_descriptor = self.get_frame_descriptor(dino_output_patches) | ||
|
|
||
| for mask in masks: | ||
|
|
||
|
|
@@ -393,7 +404,7 @@ def run(self, t, pose, img, depth_data=None, plot=False): | |
| else: | ||
| self.observations.append(Observation(t, pose, mask, mask_downsampled, ptcld)) | ||
|
|
||
| return self.observations | ||
| return self.observations, frame_descriptor | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will break things downstream in |
||
|
|
||
| def apply_rotation(self, img, unrotate=False): | ||
| if self.rotate_img is None: | ||
|
|
@@ -577,10 +588,9 @@ def mask_bounding_box(self, mask): | |
|
|
||
| return (min_col, min_row, max_col, max_row,) | ||
|
|
||
| def get_per_pixel_features(self, model_output: ArrayLike, img_shape: ArrayLike, | ||
| feature_dim: int) -> ArrayLike: | ||
| def get_output_patches(self, model_output: ArrayLike, img_shape: ArrayLike, feature_dim: int) -> ArrayLike: | ||
| """ | ||
| Extract (Dino) per-pixel features | ||
| Extract (Dino) output patches | ||
|
|
||
| Args: | ||
| model_output (ArrayLike): Last hidden state of (Dino) model | ||
|
|
@@ -601,6 +611,19 @@ def get_per_pixel_features(self, model_output: ArrayLike, img_shape: ArrayLike, | |
|
|
||
| model_output_patches = model_output_flat_patches.reshape(self.semantic_patches_shape) | ||
|
|
||
| return model_output_patches # 1 x h x w x feature_dim | ||
|
|
||
| def get_per_pixel_features(self, model_output_patches: ArrayLike, img_shape: ArrayLike) -> ArrayLike: | ||
| """ | ||
| Extract (Dino) per-pixel features | ||
|
|
||
| Args: | ||
| model_output_patches (ArrayLike): Reshaped (Dino) output patches | ||
| img_shape (ArrayLike): Original image shape | ||
|
|
||
| Returns: | ||
| ArrayLike: Reshaped (Dino) output | ||
| """ | ||
| # interpolate the feature map to match the size of the original image | ||
| per_pixel_features = torch.nn.functional.interpolate( | ||
| model_output_patches.permute(0, 3, 1, 2), # permute to be batch, channels, height, width | ||
|
|
@@ -613,3 +636,21 @@ def get_per_pixel_features(self, model_output: ArrayLike, img_shape: ArrayLike, | |
|
|
||
| return per_pixel_features # h x w x feature_dim | ||
|
|
||
| def get_frame_descriptor(self, dino_features: torch.Tensor) -> np.ndarray: | ||
| with torch.no_grad(): # prevent memory leak | ||
| dino_features_flat = dino_features.view(-1, dino_features.shape[-1]) | ||
| if self.frame_descriptor_type == 'dino-gap': | ||
| frame_descriptor = torch.sum(dino_features_flat, dim=0) | ||
| elif self.frame_descriptor_type == 'dino-gmp': | ||
| frame_descriptor = torch.max(dino_features_flat, dim=0).values | ||
| elif self.frame_descriptor_type == 'dino-gem': | ||
| cubed_descriptor = torch.mean(dino_features_flat ** 3, dim=0) | ||
| frame_descriptor = torch.sign(cubed_descriptor) * \ | ||
| (torch.abs(cubed_descriptor).clamp(min=1e-12) ** (1.0 / 3)) # avoid NaN from negative or zero root | ||
| else: | ||
| raise ValueError(f"frame descriptor must be one of 'dino-gap', 'dino-gmp', or 'dino-gem'.") | ||
|
|
||
| frame_descriptor /= torch.norm(frame_descriptor) | ||
|
|
||
| return frame_descriptor.cpu().detach().numpy() | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.