-
Notifications
You must be signed in to change notification settings - Fork 36
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 #357 from fmartiescofet/unet_decoder
Feat: Implement Terratorch UNet decoder
- Loading branch information
Showing
5 changed files
with
125 additions
and
21 deletions.
There are no files selected for viewing
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,39 @@ | ||
import torch | ||
from segmentation_models_pytorch.base.initialization import initialize_decoder | ||
from segmentation_models_pytorch.decoders.unet.decoder import UnetDecoder | ||
from torch import nn | ||
|
||
from terratorch.registry import TERRATORCH_DECODER_REGISTRY | ||
|
||
|
||
@TERRATORCH_DECODER_REGISTRY.register | ||
class UNetDecoder(nn.Module): | ||
"""UNetDecoder. Wrapper around UNetDecoder from segmentation_models_pytorch to avoid ignoring the first layer.""" | ||
|
||
def __init__( | ||
self, embed_dim: list[int], channels: list[int], use_batchnorm: bool = True, attention_type: str | None = None | ||
): | ||
"""Constructor | ||
Args: | ||
embed_dim (list[int]): Input embedding dimension for each input. | ||
channels (list[int]): Channels used in the decoder. | ||
use_batchnorm (bool, optional): Whether to use batchnorm. Defaults to True. | ||
attention_type (str | None, optional): Attention type to use. Defaults to None | ||
""" | ||
super().__init__() | ||
self.decoder = UnetDecoder( | ||
encoder_channels=[embed_dim[0], *embed_dim], | ||
decoder_channels=channels, | ||
n_blocks=len(channels), | ||
use_batchnorm=use_batchnorm, | ||
center=False, | ||
attention_type=attention_type, | ||
) | ||
initialize_decoder(self.decoder) | ||
self.out_channels = channels[-1] | ||
|
||
def forward(self, x: list[torch.Tensor]) -> torch.Tensor: | ||
# The first layer is ignored in the original UnetDecoder, so we need to duplicate the first layer | ||
x = [x[0].clone(), *x] | ||
return self.decoder(*x) |
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