-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from juglab/ms/feat/model_adapter
added model adapter as a way to use different models for feature extraction Former-commit-id: 83afea1
- Loading branch information
Showing
16 changed files
with
245 additions
and
132 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
src/featureforest/SAM/models/weights/mobile_sam.pt.REMOVED.git-id
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
__version__ = "0.0.2" | ||
|
||
from ._embedding_extractor_widget import EmbeddingExtractorWidget | ||
from ._sam_predictor_widget import SAMPredictorWidget | ||
# from ._sam_predictor_widget import SAMPredictorWidget | ||
from ._sam_rf_segmentation_widget import SAMRFSegmentationWidget | ||
from ._sam_prompt_segmentation_widget import SAMPromptSegmentationWidget | ||
# from ._sam_prompt_segmentation_widget import SAMPromptSegmentationWidget | ||
|
||
__all__ = ( | ||
"EmbeddingExtractorWidget", | ||
"SAMPredictorWidget", | ||
# "SAMPredictorWidget", | ||
"SAMRFSegmentationWidget", | ||
"SAMPromptSegmentationWidget" | ||
# "SAMPromptSegmentationWidget" | ||
) |
This file contains 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 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 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,2 @@ | ||
from .model import get_model | ||
from .adapter import MobileSAMAdapter |
This file contains 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,52 @@ | ||
from typing import Tuple | ||
|
||
import torch | ||
import torch.nn as nn | ||
from torch import Tensor | ||
from torchvision.transforms import v2 as tv_transforms2 | ||
|
||
from featureforest.models.base import BaseModelAdapter | ||
from featureforest.utils.data import ( | ||
get_nonoverlapped_patches, | ||
) | ||
|
||
|
||
class MobileSAMAdapter(BaseModelAdapter): | ||
"""MobileSAM model adapter | ||
""" | ||
def __init__( | ||
self, | ||
model: nn.Module, | ||
input_transforms: tv_transforms2.Compose, | ||
patch_size: int, | ||
overlap: int, | ||
) -> None: | ||
super().__init__(model, input_transforms, patch_size, overlap) | ||
# we need sam image encoder part | ||
self.encoder = self.model.image_encoder | ||
self.encoder_num_channels = 256 | ||
self.embed_layer_num_channels = 64 | ||
|
||
def get_features_patches( | ||
self, in_patches: Tensor | ||
) -> Tuple[Tensor, Tensor]: | ||
# get the mobile-sam encoder and embedding layer outputs | ||
with torch.no_grad(): | ||
output, embed_output, _ = self.encoder( | ||
self.input_transforms(in_patches) | ||
) | ||
|
||
# get non-overlapped feature patches | ||
out_feature_patches = get_nonoverlapped_patches( | ||
self.embedding_transform(output.cpu()), | ||
self.patch_size, self.overlap | ||
) | ||
embed_feature_patches = get_nonoverlapped_patches( | ||
self.embedding_transform(embed_output.cpu()), | ||
self.patch_size, self.overlap | ||
) | ||
|
||
return out_feature_patches, embed_feature_patches | ||
|
||
def get_total_output_channels(self) -> int: | ||
return self.encoder_num_channels + self.embed_layer_num_channels |
This file contains 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
File renamed without changes.
This file contains 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,73 @@ | ||
import torch | ||
import torch.nn as nn | ||
from torch import Tensor | ||
from torchvision.transforms import v2 as tv_transforms2 | ||
|
||
from ..utils.data import ( | ||
get_nonoverlapped_patches, | ||
) | ||
|
||
|
||
class BaseModelAdapter: | ||
"""Base class for adapting any models in featureforest. | ||
""" | ||
def __init__( | ||
self, | ||
model: nn.Module, | ||
input_transforms: tv_transforms2.Compose, | ||
patch_size: int, | ||
overlap: int, | ||
) -> None: | ||
"""Initialization function | ||
Args: | ||
model (nn.Module): the pytorch model (e.g. a ViT encoder) | ||
input_transforms (tv_transforms2.Compose): input transformations for the specific model | ||
patch_size (int): input patch size | ||
overlap (int): input patch overlap | ||
""" | ||
self.model = model | ||
self.input_transforms = input_transforms | ||
self.patch_size = patch_size | ||
self.overlap = overlap | ||
# to transform feature patches to the original patch size | ||
self.embedding_transform = tv_transforms2.Compose([ | ||
tv_transforms2.Resize( | ||
(self.patch_size, self.patch_size), | ||
interpolation=tv_transforms2.InterpolationMode.BICUBIC, | ||
antialias=True | ||
), | ||
]) | ||
|
||
def get_features_patches( | ||
self, in_patches: Tensor | ||
) -> Tensor: | ||
"""Returns a tensor of model's extracted features. | ||
This function is more like an abstract function, and should be overridden. | ||
Args: | ||
in_patches (Tensor): input patches | ||
Returns: | ||
Tensor: model's extracted features | ||
""" | ||
# get the model output | ||
with torch.no_grad(): | ||
out_features = self.model(self.input_transforms(in_patches)) | ||
# assert self.patch_size == out_features.shape[-1] | ||
|
||
# get non-overlapped feature patches | ||
feature_patches = get_nonoverlapped_patches( | ||
self.embedding_transform(out_features.cpu()), | ||
self.patch_size, self.overlap | ||
) | ||
|
||
return feature_patches | ||
|
||
def get_total_output_channels(self) -> int: | ||
"""Returns total number of model output channels (a.k.a. number of feature maps). | ||
Returns: | ||
int: total number of output channels | ||
""" | ||
return 256 |
Oops, something went wrong.