-
Notifications
You must be signed in to change notification settings - Fork 2
Fix cnn and add SatCLIP #107
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
38fbbd4
Fix cnn and add SSL4EO encoder configurations
gabrieletijunaityte 769a401
Fix cnn and add SSL4EO encoder configurations
gabrieletijunaityte cbce9f1
Add satclip requirement
gabrieletijunaityte 8b6a48d
Add satclip encoder
gabrieletijunaityte b68f12c
Satclip requirement as non-optional
gabrieletijunaityte b3b276e
Merge branch 'develop' into feature/fix_cnn
vdplasthijs bab8ed6
Remove freezing config from CNN encoder
gabrieletijunaityte 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from typing import Dict, List, override | ||
|
|
||
| import torch | ||
| from huggingface_hub import hf_hub_download | ||
| from satclip.load import get_satclip | ||
|
|
||
| from src.models.components.geo_encoders.base_geo_encoder import BaseGeoEncoder | ||
|
|
||
|
|
||
| class SatClipCoordinateEncoder(BaseGeoEncoder): | ||
| def __init__( | ||
| self, | ||
| geo_data_name="coords", | ||
| hf_cache_dir: str = "../.cache", | ||
| accelerator: torch.device = torch.device("cpu"), | ||
| ) -> None: | ||
| """SatClip coordinate encoder :param geo_data_name: type of geo data used for this encoder | ||
| (supports only coordinates) :param hf_cache_dir: hugging face cache directory to store data | ||
| :param accelerator: where to load model (as it is float64, mps is not supported)""" | ||
| super().__init__() | ||
|
|
||
| self.allowed_geo_data_names = ["coords"] | ||
| assert ( | ||
| geo_data_name in self.allowed_geo_data_names | ||
| ), f"geo_data_name must be one of {self.allowed_geo_data_names}, got {geo_data_name}" | ||
| self.geo_data_name = geo_data_name | ||
|
|
||
| self.cache_dir = hf_cache_dir | ||
| assert accelerator != torch.device("mps"), f"accelerator {accelerator} is not supported" | ||
| self.accelerator = accelerator | ||
|
|
||
| @override | ||
| def _setup(self) -> List[str]: | ||
| """Setup satclip encoder from hugging face hub and set output dimension.""" | ||
| self.geo_encoder = get_satclip( | ||
| hf_hub_download( | ||
| "microsoft/SatCLIP-ViT16-L40", "satclip-vit16-l40.ckpt", cache_dir=self.cache_dir | ||
| ), | ||
| device=self.accelerator, | ||
| ) | ||
|
|
||
| self.output_dim = self.geo_encoder.nnet.last_layer.dim_out | ||
| return [] | ||
|
|
||
| @override | ||
| def forward( | ||
| self, | ||
| batch: Dict[str, torch.Tensor], | ||
| ) -> torch.Tensor: | ||
| """Forward pass of satclip encoder.""" | ||
|
|
||
| coords = batch.get("eo", {}).get("coords") | ||
|
|
||
| # Swap coordinates | ||
| coords = coords[:, [1, 0]] | ||
|
|
||
| # SatClip needs float64 | ||
| dtype = self.dtype | ||
| if coords.dtype != dtype: | ||
| coords = coords.to(dtype) | ||
|
|
||
| feats = self.geo_encoder(coords) | ||
| if self.extra_projector: | ||
| feats = self.extra_projector(feats) | ||
|
|
||
| return feats |
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.