basicsr.archs.stylegan2_arch
- class basicsr.archs.stylegan2_arch.ConstantInput(num_channel, size)[source]
Bases:
Module
Constant input.
- Parameters
num_channel (int) – Channel number of constant input.
size (int) – Spatial size of constant input.
- forward(batch)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class basicsr.archs.stylegan2_arch.ConvLayer(in_channels, out_channels, kernel_size, downsample=False, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)[source]
Bases:
Sequential
Conv Layer used in StyleGAN2 Discriminator.
- Parameters
in_channels (int) – Channel number of the input.
out_channels (int) – Channel number of the output.
kernel_size (int) – Kernel size.
downsample (bool) – Whether downsample by a factor of 2. Default: False.
resample_kernel (list[int]) – A list indicating the 1D resample kernel magnitude. A cross production will be applied to extent 1D resample kernel to 2D resample kernel. Default: (1, 3, 3, 1).
bias (bool) – Whether with bias. Default: True.
activate (bool) – Whether use activateion. Default: True.
- class basicsr.archs.stylegan2_arch.EqualConv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, bias=True, bias_init_val=0)[source]
Bases:
Module
Equalized Linear as StyleGAN2.
- Parameters
in_channels (int) – Channel number of the input.
out_channels (int) – Channel number of the output.
kernel_size (int) – Size of the convolving kernel.
stride (int) – Stride of the convolution. Default: 1
padding (int) – Zero-padding added to both sides of the input. Default: 0.
bias (bool) – If
True
, adds a learnable bias to the output. Default:True
.bias_init_val (float) – Bias initialized value. Default: 0.
- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class basicsr.archs.stylegan2_arch.EqualLinear(in_channels, out_channels, bias=True, bias_init_val=0, lr_mul=1, activation=None)[source]
Bases:
Module
Equalized Linear as StyleGAN2.
- Parameters
in_channels (int) – Size of each sample.
out_channels (int) – Size of each output sample.
bias (bool) – If set to
False
, the layer will not learn an additive bias. Default:True
.bias_init_val (float) – Bias initialized value. Default: 0.
lr_mul (float) – Learning rate multiplier. Default: 1.
activation (None | str) – The activation after
linear
operation. Supported: ‘fused_lrelu’, None. Default: None.
- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class basicsr.archs.stylegan2_arch.ModulatedConv2d(in_channels, out_channels, kernel_size, num_style_feat, demodulate=True, sample_mode=None, resample_kernel=(1, 3, 3, 1), eps=1e-08)[source]
Bases:
Module
Modulated Conv2d used in StyleGAN2.
There is no bias in ModulatedConv2d.
- Parameters
in_channels (int) – Channel number of the input.
out_channels (int) – Channel number of the output.
kernel_size (int) – Size of the convolving kernel.
num_style_feat (int) – Channel number of style features.
demodulate (bool) – Whether to demodulate in the conv layer. Default: True.
sample_mode (str | None) – Indicating ‘upsample’, ‘downsample’ or None. Default: None.
resample_kernel (list[int]) – A list indicating the 1D resample kernel magnitude. Default: (1, 3, 3, 1).
eps (float) – A value added to the denominator for numerical stability. Default: 1e-8.
- forward(x, style)[source]
Forward function.
- Parameters
x (Tensor) – Tensor with shape (b, c, h, w).
style (Tensor) – Tensor with shape (b, num_style_feat).
- Returns
Modulated tensor after convolution.
- Return type
Tensor
- training: bool
- class basicsr.archs.stylegan2_arch.NormStyleCode[source]
Bases:
Module
- forward(x)[source]
Normalize the style codes.
- Parameters
x (Tensor) – Style codes with shape (b, c).
- Returns
Normalized tensor.
- Return type
Tensor
- training: bool
- class basicsr.archs.stylegan2_arch.ResBlock(in_channels, out_channels, resample_kernel=(1, 3, 3, 1))[source]
Bases:
Module
Residual block used in StyleGAN2 Discriminator.
- Parameters
in_channels (int) – Channel number of the input.
out_channels (int) – Channel number of the output.
resample_kernel (list[int]) – A list indicating the 1D resample kernel magnitude. A cross production will be applied to extent 1D resample kernel to 2D resample kernel. Default: (1, 3, 3, 1).
- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class basicsr.archs.stylegan2_arch.ScaledLeakyReLU(negative_slope=0.2)[source]
Bases:
Module
Scaled LeakyReLU.
- Parameters
negative_slope (float) – Negative slope. Default: 0.2.
- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class basicsr.archs.stylegan2_arch.StyleConv(in_channels, out_channels, kernel_size, num_style_feat, demodulate=True, sample_mode=None, resample_kernel=(1, 3, 3, 1))[source]
Bases:
Module
Style conv.
- Parameters
in_channels (int) – Channel number of the input.
out_channels (int) – Channel number of the output.
kernel_size (int) – Size of the convolving kernel.
num_style_feat (int) – Channel number of style features.
demodulate (bool) – Whether demodulate in the conv layer. Default: True.
sample_mode (str | None) – Indicating ‘upsample’, ‘downsample’ or None. Default: None.
resample_kernel (list[int]) – A list indicating the 1D resample kernel magnitude. Default: (1, 3, 3, 1).
- forward(x, style, noise=None)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class basicsr.archs.stylegan2_arch.StyleGAN2Discriminator(out_size, channel_multiplier=2, resample_kernel=(1, 3, 3, 1), stddev_group=4, narrow=1)[source]
Bases:
Module
StyleGAN2 Discriminator.
- Parameters
out_size (int) – The spatial size of outputs.
channel_multiplier (int) – Channel multiplier for large networks of StyleGAN2. Default: 2.
resample_kernel (list[int]) – A list indicating the 1D resample kernel magnitude. A cross production will be applied to extent 1D resample kernel to 2D resample kernel. Default: (1, 3, 3, 1).
stddev_group (int) – For group stddev statistics. Default: 4.
narrow (float) – Narrow ratio for channels. Default: 1.0.
- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class basicsr.archs.stylegan2_arch.StyleGAN2Generator(out_size, num_style_feat=512, num_mlp=8, channel_multiplier=2, resample_kernel=(1, 3, 3, 1), lr_mlp=0.01, narrow=1)[source]
Bases:
Module
StyleGAN2 Generator.
- Parameters
out_size (int) – The spatial size of outputs.
num_style_feat (int) – Channel number of style features. Default: 512.
num_mlp (int) – Layer number of MLP style layers. Default: 8.
channel_multiplier (int) – Channel multiplier for large networks of StyleGAN2. Default: 2.
resample_kernel (list[int]) – A list indicating the 1D resample kernel magnitude. A cross production will be applied to extent 1D resample kernel to 2D resample kernel. Default: (1, 3, 3, 1).
lr_mlp (float) – Learning rate multiplier for mlp layers. Default: 0.01.
narrow (float) – Narrow ratio for channels. Default: 1.0.
- forward(styles, input_is_latent=False, noise=None, randomize_noise=True, truncation=1, truncation_latent=None, inject_index=None, return_latents=False)[source]
Forward function for StyleGAN2Generator.
- Parameters
styles (list[Tensor]) – Sample codes of styles.
input_is_latent (bool) – Whether input is latent style. Default: False.
noise (Tensor | None) – Input noise or None. Default: None.
randomize_noise (bool) – Randomize noise, used when ‘noise’ is False. Default: True.
truncation (float) – TODO. Default: 1.
truncation_latent (Tensor | None) – TODO. Default: None.
inject_index (int | None) – The injection index for mixing noise. Default: None.
return_latents (bool) – Whether to return style latents. Default: False.
- training: bool
- class basicsr.archs.stylegan2_arch.ToRGB(in_channels, num_style_feat, upsample=True, resample_kernel=(1, 3, 3, 1))[source]
Bases:
Module
To RGB from features.
- Parameters
in_channels (int) – Channel number of input.
num_style_feat (int) – Channel number of style features.
upsample (bool) – Whether to upsample. Default: True.
resample_kernel (list[int]) – A list indicating the 1D resample kernel magnitude. Default: (1, 3, 3, 1).
- forward(x, style, skip=None)[source]
Forward function.
- Parameters
x (Tensor) – Feature tensor with shape (b, c, h, w).
style (Tensor) – Tensor with shape (b, num_style_feat).
skip (Tensor) – Base/skip tensor. Default: None.
- Returns
RGB images.
- Return type
Tensor
- training: bool
- class basicsr.archs.stylegan2_arch.UpFirDnDownsample(resample_kernel, factor=2)[source]
Bases:
Module
Upsample, FIR filter, and downsample (downsampole version).
- Parameters
resample_kernel (list[int]) – A list indicating the 1D resample kernel magnitude.
factor (int) – Downsampling scale factor. Default: 2.
- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class basicsr.archs.stylegan2_arch.UpFirDnSmooth(resample_kernel, upsample_factor=1, downsample_factor=1, kernel_size=1)[source]
Bases:
Module
Upsample, FIR filter, and downsample (smooth version).
- Parameters
resample_kernel (list[int]) – A list indicating the 1D resample kernel magnitude.
upsample_factor (int) – Upsampling scale factor. Default: 1.
downsample_factor (int) – Downsampling scale factor. Default: 1.
kernel_size (int) – Kernel size: Default: 1.
- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class basicsr.archs.stylegan2_arch.UpFirDnUpsample(resample_kernel, factor=2)[source]
Bases:
Module
Upsample, FIR filter, and downsample (upsampole version).
References: 1. https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.upfirdn.html # noqa: E501 2. http://www.ece.northwestern.edu/local-apps/matlabhelp/toolbox/signal/upfirdn.html # noqa: E501
- Parameters
resample_kernel (list[int]) – A list indicating the 1D resample kernel magnitude.
factor (int) – Upsampling scale factor. Default: 2.
- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool