This repository was archived by the owner on Feb 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
373 add code for spade vae gan #405
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
18e47e3
Code for SPADE VAE-GAN added, trimmed. Tests added (although at the m…
390eb58
Added SPADE network code, tests and jupyter notebook for 2D
97737b1
Added SPADE network code, tests and jupyter notebook for 2D
e77599e
Add tutorial outputs
Warvito 067998b
Implemented changes as per PR correction: adding docstrings, removing…
39ed0f1
Addresses hidden comments and runtests with autofix
marksgraham 1b42195
Fix tutorial formatting
marksgraham 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
|
|
||
| class KLDLoss(nn.Module): | ||
| def forward(self, mu, logvar): | ||
| return -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp()) |
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,95 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import torch | ||
| import torch.nn as nn | ||
| import torch.nn.functional as F | ||
| from monai.networks.blocks import ADN, Convolution | ||
|
|
||
|
|
||
| class SPADE(nn.Module): | ||
| """ | ||
| SPADE normalisation block based on the 2019 paper by Park et al. (doi: https://doi.org/10.48550/arXiv.1903.07291) | ||
|
|
||
| Args: | ||
marksgraham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| label_nc: number of semantic labels | ||
| norm_nc: number of output channels | ||
| kernel_size: kernel size | ||
| spatial_dims: number of spatial dimensions | ||
| hidden_channels: number of channels in the intermediate gamma and beta layers | ||
| norm: type of base normalisation used before applying the SPADE normalisation | ||
| norm_params: parameters for the base normalisation | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| label_nc: int, | ||
| norm_nc: int, | ||
| kernel_size: int = 3, | ||
| spatial_dims: int = 2, | ||
| hidden_channels: int = 64, | ||
| norm: str | tuple = "INSTANCE", | ||
| norm_params: dict = {}, | ||
| ) -> None: | ||
|
|
||
| super().__init__() | ||
|
|
||
| if len(norm_params) != 0: | ||
| norm = (norm, norm_params) | ||
| self.param_free_norm = ADN( | ||
| act=None, dropout=0.0, norm=norm, norm_dim=spatial_dims, ordering="N", in_channels=norm_nc | ||
| ) | ||
| self.mlp_shared = Convolution( | ||
| spatial_dims=spatial_dims, | ||
| in_channels=label_nc, | ||
| out_channels=hidden_channels, | ||
| kernel_size=kernel_size, | ||
| norm=None, | ||
| padding=kernel_size // 2, | ||
| act="LEAKYRELU", | ||
| ) | ||
| self.mlp_gamma = Convolution( | ||
| spatial_dims=spatial_dims, | ||
| in_channels=hidden_channels, | ||
| out_channels=norm_nc, | ||
| kernel_size=kernel_size, | ||
| padding=kernel_size // 2, | ||
| act=None, | ||
| ) | ||
| self.mlp_beta = Convolution( | ||
| spatial_dims=spatial_dims, | ||
| in_channels=hidden_channels, | ||
| out_channels=norm_nc, | ||
| kernel_size=kernel_size, | ||
| padding=kernel_size // 2, | ||
| act=None, | ||
| ) | ||
|
|
||
| def forward(self, x: torch.Tensor, segmap: torch.Tensor) -> torch.Tensor: | ||
| """ | ||
| Args: | ||
| x: input tensor | ||
| segmap: input segmentation map (bxcx[spatial-dimensions]) where c is the number of semantic channels. | ||
| The map will be interpolated to the dimension of x internally. | ||
| """ | ||
|
|
||
| # Part 1. generate parameter-free normalized activations | ||
| normalized = self.param_free_norm(x) | ||
|
|
||
| # Part 2. produce scaling and bias conditioned on semantic map | ||
| segmap = F.interpolate(segmap, size=x.size()[2:], mode="nearest") | ||
| actv = self.mlp_shared(segmap) | ||
| gamma = self.mlp_gamma(actv) | ||
| beta = self.mlp_beta(actv) | ||
| out = normalized * (1 + gamma) + beta | ||
marksgraham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return out | ||
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.