torch_mimicry.modules¶
Contents
Layers¶
Script for building specific layers needed by GAN architecture.
-
class
ConditionalBatchNorm2d
(num_features, num_classes)[source]¶ Conditional Batch Norm as implemented in https://github.com/pytorch/pytorch/issues/8985
-
SNConv2d
(*args, default=True, **kwargs)[source]¶ Wrapper for applying spectral norm on conv2d layer.
-
SNEmbedding
(*args, default=True, **kwargs)[source]¶ Wrapper for applying spectral norm on embedding layer.
-
SNLinear
(*args, default=True, **kwargs)[source]¶ Wrapper for applying spectral norm on linear layer.
-
class
SelfAttention
(num_feat, spectral_norm=True)[source]¶ Self-attention layer based on version used in BigGAN code: https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py
-
forward
(x)[source]¶ Feedforward function. Implementation differs from actual SAGAN paper, see note from BigGAN: https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py#L142
See official TF Implementation: https://github.com/brain-research/self-attention-gan/blob/master/non_local.py
Parameters: x (Tensor) – Input feature map. Returns: Feature map weighed with attention map. Return type: Tensor
-
Residual Blocks¶
Implementation of residual blocks for discriminator and generator. We follow the official SNGAN Chainer implementation as closely as possible: https://github.com/pfnet-research/sngan_projection
-
class
DBlock
(in_channels, out_channels, hidden_channels=None, downsample=False, spectral_norm=True)[source]¶ Residual block for discriminator.
The channel size of intermediate feature maps.
Type: int
-
class
DBlockOptimized
(in_channels, out_channels, spectral_norm=True)[source]¶ Optimized residual block for discriminator. This is used as the first residual block, where there is a definite downsampling involved. Follows the official SNGAN reference implementation in chainer.
-
class
GBlock
(in_channels, out_channels, hidden_channels=None, upsample=False, num_classes=0, spectral_norm=False)[source]¶ Residual block for generator.
Uses bilinear (rather than nearest) interpolation, and align_corners set to False. This is as per how torchvision does upsampling, as seen in: https://github.com/pytorch/vision/blob/master/torchvision/models/segmentation/_utils.py
The channel size of intermediate feature maps.
Type: int
Losses¶
Loss functions definitions.
-
hinge_loss_dis
(output_fake, output_real)[source]¶ Hinge loss for discriminator.
Parameters: - output_fake (Tensor) – Discriminator output logits for fake images.
- output_real (Tensor) – Discriminator output logits for real images.
Returns: A scalar tensor loss output.
Return type: Tensor
-
hinge_loss_gen
(output_fake)[source]¶ Hinge loss for generator.
Parameters: output_fake (Tensor) – Discriminator output logits for fake images. Returns: A scalar tensor loss output. Return type: Tensor
-
minimax_loss_dis
(output_fake, output_real, real_label_val=1.0, fake_label_val=0.0, **kwargs)[source]¶ Standard minimax loss for GANs through the BCE Loss with logits fn.
Parameters: Returns: A scalar tensor loss output.
Return type: Tensor
-
minimax_loss_gen
(output_fake, real_label_val=1.0, **kwargs)[source]¶ Standard minimax loss for GANs through the BCE Loss with logits fn.
Parameters: - output (Tensor) – Discriminator output logits.
- labels (Tensor) – Labels for computing cross entropy.
Returns: A scalar tensor loss output.
Return type: Tensor
-
ns_loss_gen
(output_fake)[source]¶ Non-saturating loss for generator.
Parameters: output_fake (Tensor) – Discriminator output logits for fake images. Returns: A scalar tensor loss output. Return type: Tensor
-
wasserstein_loss_dis
(output_real, output_fake)[source]¶ Computes the wasserstein loss for the discriminator.
Parameters: - output_real (Tensor) – Discriminator output logits for real images.
- output_fake (Tensor) – Discriminator output logits for fake images.
Returns: A scalar tensor loss output.
Return type: Tensor