-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
6676 port generative networks spade (#7320)
Towards #6676 . ### Description This adds SPADE-enabled autoencoder and diffusion_model_unet architectures. They are new implementations for each network, rather than options in the existing network, because @virginiafdez and I felt that adding additional options to the existing networks to enable spade compatibility significantly reduced the readability of them for users who were not interested in SPADE functionality. These are the last networks to be ported over. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [x] In-line docstrings updated. - [x] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Mark Graham <markgraham539@gmail.com> Signed-off-by: Mark Graham <mark@Marks-MacBook-Pro.local> Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b85a534
commit aa4a4db
Showing
8 changed files
with
2,312 additions
and
0 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
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,96 @@ | ||
# 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): | ||
""" | ||
Spatially Adaptive Normalization (SPADE) block, allowing for normalization of activations conditioned on a | ||
semantic map. This block is used in SPADE-based image-to-image translation models, as described in | ||
Semantic Image Synthesis with Spatially-Adaptive Normalization (https://arxiv.org/abs/1903.07291). | ||
Args: | ||
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 = None, | ||
) -> None: | ||
super().__init__() | ||
|
||
if norm_params is None: | ||
norm_params = {} | ||
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, | ||
act="LEAKYRELU", | ||
) | ||
self.mlp_gamma = Convolution( | ||
spatial_dims=spatial_dims, | ||
in_channels=hidden_channels, | ||
out_channels=norm_nc, | ||
kernel_size=kernel_size, | ||
act=None, | ||
) | ||
self.mlp_beta = Convolution( | ||
spatial_dims=spatial_dims, | ||
in_channels=hidden_channels, | ||
out_channels=norm_nc, | ||
kernel_size=kernel_size, | ||
act=None, | ||
) | ||
|
||
def forward(self, x: torch.Tensor, segmap: torch.Tensor) -> torch.Tensor: | ||
""" | ||
Args: | ||
x: input tensor with shape (B, C, [spatial-dimensions]) where C is the number of semantic channels. | ||
segmap: input segmentation map (B, C, [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: torch.Tensor = normalized * (1 + gamma) + beta | ||
return out |
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
Oops, something went wrong.