mmagic.models.editors.stylegan3.stylegan3_modules
¶
Module Contents¶
Classes¶
Fully connected layer used in StyleGANv3. |
|
Style mapping network used in StyleGAN3. The main difference between it |
|
Module which generate input for synthesis layer. |
|
Layer of Synthesis network for stylegan3. |
|
Synthesis network for stylegan3. |
Functions¶
|
Modulated Conv2d in StyleGANv3. |
Attributes¶
- mmagic.models.editors.stylegan3.stylegan3_modules.modulated_conv2d(x, w, s, demodulate=True, padding=0, input_gain=None)[source]¶
Modulated Conv2d in StyleGANv3.
- Parameters
x (torch.Tensor) – Input tensor with shape (batch_size, in_channels, height, width).
w (torch.Tensor) – Weight of modulated convolution with shape (out_channels, in_channels, kernel_height, kernel_width).
s (torch.Tensor) – Style tensor with shape (batch_size, in_channels).
demodulate (bool) – Whether apply weight demodulation. Defaults to True.
padding (int or list[int]) – Convolution padding. Defaults to 0.
input_gain (list[int]) – Scaling factors for input. Defaults to None.
- Returns
Convolution Output.
- Return type
torch.Tensor
- class mmagic.models.editors.stylegan3.stylegan3_modules.FullyConnectedLayer(in_features, out_features, activation='linear', bias=True, lr_multiplier=1.0, weight_init=1.0, bias_init=0.0)[source]¶
Bases:
mmengine.model.BaseModule
Fully connected layer used in StyleGANv3.
- Parameters
in_features (int) – Number of channels in the input feature.
out_features (int) – Number of channels in the out feature.
activation (str, optional) – Activation function with choices ‘relu’, ‘lrelu’, ‘linear’. ‘linear’ means no extra activation. Defaults to ‘linear’.
bias (bool, optional) – Whether to use additive bias. Defaults to True.
lr_multiplier (float, optional) – Equalized learning rate multiplier. Defaults to 1..
weight_init (float, optional) – Weight multiplier for initialization. Defaults to 1..
bias_init (float, optional) – Initial bias. Defaults to 0..
- class mmagic.models.editors.stylegan3.stylegan3_modules.MappingNetwork(noise_size, style_channels, num_ws, cond_size=0, num_layers=2, lr_multiplier=0.01, w_avg_beta=0.998)[source]¶
Bases:
mmengine.model.BaseModule
Style mapping network used in StyleGAN3. The main difference between it and styleganv1,v2 is that mean latent is registered as a buffer and dynamic updated during training.
- Parameters
noise_size (int, optional) – Size of the input noise vector.
style_channels (int) – The number of channels for style code.
num_ws (int | None) – The repeat times of w latent. If None is passed, the output will shape like (batch_size, 1), otherwise the output will shape like (bz, num_ws, 1).
cond_size (int, optional) – Size of the conditional input. Defaults to None.
num_layers (int, optional) – The number of layers of mapping network. Defaults to 2.
lr_multiplier (float, optional) – Equalized learning rate multiplier. Defaults to 0.01.
w_avg_beta (float, optional) – The value used for update w_avg. Defaults to 0.998.
- forward(z, label=None, truncation=1, num_truncation_layer=None, update_emas=False)[source]¶
Style mapping function.
- Parameters
z (torch.Tensor) – Input noise tensor.
label (torch.Tensor, optional) – The conditional input. Defaults to None.
truncation (float, optional) – Truncation factor. Give value less than 1., the truncation trick will be adopted. Defaults to 1.
num_truncation_layer (int, optional) – Number of layers use truncated latent. Defaults to None.
update_emas (bool, optional) – Whether update moving average of mean latent. Defaults to False.
- Returns
W-plus latent.
- Return type
torch.Tensor
- class mmagic.models.editors.stylegan3.stylegan3_modules.SynthesisInput(style_channels, channels, size, sampling_rate, bandwidth)[source]¶
Bases:
mmengine.model.BaseModule
Module which generate input for synthesis layer.
- Parameters
style_channels (int) – The number of channels for style code.
channels (int) – The number of output channel.
size (int) – The size of sampling grid.
sampling_rate (int) – Sampling rate for construct sampling grid.
bandwidth (float) – Bandwidth of random frequencies.
- class mmagic.models.editors.stylegan3.stylegan3_modules.SynthesisLayer(style_channels, is_torgb, is_critically_sampled, use_fp16, in_channels, out_channels, in_size, out_size, in_sampling_rate, out_sampling_rate, in_cutoff, out_cutoff, in_half_width, out_half_width, conv_kernel=3, filter_size=6, lrelu_upsampling=2, use_radial_filters=False, conv_clamp=256, magnitude_ema_beta=0.999)[source]¶
Bases:
mmengine.model.BaseModule
Layer of Synthesis network for stylegan3.
- Parameters
style_channels (int) – The number of channels for style code.
is_torgb (bool) – Whether output of this layer is transformed to rgb image.
is_critically_sampled (bool) – Whether filter cutoff is set exactly at the bandlimit.
use_fp16 (bool, optional) – Whether to use fp16 training in this module. If this flag is True, the whole module will be wrapped with
auto_fp16
.in_channels (int) – The channel number of the input feature map.
out_channels (int) – The channel number of the output feature map.
in_size (int) – The input size of feature map.
out_size (int) – The output size of feature map.
in_sampling_rate (int) – Sampling rate for upsampling filter.
out_sampling_rate (int) – Sampling rate for downsampling filter.
in_cutoff (float) – Cutoff frequency for upsampling filter.
out_cutoff (float) – Cutoff frequency for downsampling filter.
in_half_width (float) – The approximate width of the transition region for upsampling filter.
out_half_width (float) – The approximate width of the transition region for downsampling filter.
conv_kernel (int, optional) – The kernel of modulated convolution. Defaults to 3.
filter_size (int, optional) – Base filter size. Defaults to 6.
lrelu_upsampling (int, optional) – Upsamling rate for filtered_lrelu. Defaults to 2.
use_radial_filters (bool, optional) – Whether use radially symmetric jinc-based filter in downsamping filter. Defaults to False.
conv_clamp (int, optional) – Clamp bound for convolution. Defaults to 256.
magnitude_ema_beta (float, optional) – Beta coefficient for calculating input magnitude ema. Defaults to 0.999.
- forward(x, w, force_fp32=False, update_emas=False)[source]¶
Forward function for synthesis layer.
- Parameters
x (torch.Tensor) – Input feature map tensor.
w (torch.Tensor) – Input style tensor.
force_fp32 (bool, optional) – Force fp32 ignore the weights. Defaults to True.
update_emas (bool, optional) – Whether update moving average of input magnitude. Defaults to False.
- Returns
Output feature map tensor.
- Return type
torch.Tensor
- static design_lowpass_filter(numtaps, cutoff, width, fs, radial=False)[source]¶
Design lowpass filter giving related arguments.
- Parameters
numtaps (int) – Length of the filter. numtaps must be odd if a passband includes the Nyquist frequency.
cutoff (float) – Cutoff frequency of filter
width (float) – The approximate width of the transition region.
fs (float) – The sampling frequency of the signal.
radial (bool, optional) – Whether use radially symmetric jinc-based filter. Defaults to False.
- Returns
Kernel of lowpass filter.
- Return type
torch.Tensor
- class mmagic.models.editors.stylegan3.stylegan3_modules.SynthesisNetwork(style_channels, out_size, img_channels, channel_base=32768, channel_max=512, num_layers=14, num_critical=2, first_cutoff=2, first_stopband=2 ** 2.1, last_stopband_rel=2 ** 0.3, margin_size=10, output_scale=0.25, num_fp16_res=4, **layer_kwargs)[source]¶
Bases:
mmengine.model.BaseModule
Synthesis network for stylegan3.
- Parameters
style_channels (int) – The number of channels for style code.
out_size (int) – The resolution of output image.
img_channels (int) – The number of channels for output image.
channel_base (int, optional) – Overall multiplier for the number of channels. Defaults to 32768.
channel_max (int, optional) – Maximum number of channels in any layer. Defaults to 512.
num_layers (int, optional) – Total number of layers, excluding Fourier features and ToRGB. Defaults to 14.
num_critical (int, optional) – Number of critically sampled layers at the end. Defaults to 2.
first_cutoff (int, optional) – Cutoff frequency of the first layer. Defaults to 2.
first_stopband (int, optional) – Minimum stopband of the first layer. Defaults to 2**2.1.
last_stopband_rel (float, optional) – Minimum stopband of the last layer, expressed relative to the cutoff. Defaults to 2**0.3.
margin_size (int, optional) – Number of additional pixels outside the image. Defaults to 10.
output_scale (float, optional) – Scale factor for output value. Defaults to 0.25.
num_fp16_res (int, optional) – Number of first few layers use fp16. Defaults to 4.