basicsr.archs.stylegan2_bilinear_arch

class basicsr.archs.stylegan2_bilinear_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_bilinear_arch.ConvLayer(in_channels, out_channels, kernel_size, downsample=False, bias=True, activate=True, interpolation_mode='bilinear')[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.

  • bias (bool) – Whether with bias. Default: True.

  • activate (bool) – Whether use activateion. Default: True.

class basicsr.archs.stylegan2_bilinear_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_bilinear_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_bilinear_arch.ModulatedConv2d(in_channels, out_channels, kernel_size, num_style_feat, demodulate=True, sample_mode=None, eps=1e-08, interpolation_mode='bilinear')[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.

  • 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_bilinear_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_bilinear_arch.ResBlock(in_channels, out_channels, interpolation_mode='bilinear')[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.

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_bilinear_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_bilinear_arch.StyleConv(in_channels, out_channels, kernel_size, num_style_feat, demodulate=True, sample_mode=None, interpolation_mode='bilinear')[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.

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_bilinear_arch.StyleGAN2GeneratorBilinear(out_size, num_style_feat=512, num_mlp=8, channel_multiplier=2, lr_mlp=0.01, narrow=1, interpolation_mode='bilinear')[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.

  • 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.

get_latent(x)[source]
make_noise()[source]

Make noise for noise injection.

mean_latent(num_latent)[source]
training: bool
class basicsr.archs.stylegan2_bilinear_arch.ToRGB(in_channels, num_style_feat, upsample=True, interpolation_mode='bilinear')[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.

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