Shortcuts

mmagic.models.editors.biggan

Package Contents

Classes

BigGAN

Implementation of `Large Scale GAN Training for High Fidelity Natural

BigGANDeepDiscriminator

BigGAN-Deep Discriminator. The implementation refers to

BigGANDeepGenerator

BigGAN-Deep Generator. The implementation refers to

BigGANDiscriminator

BigGAN Discriminator. The implementation refers to

BigGANGenerator

BigGAN Generator. The implementation refers to

BigGANConditionBN

Conditional Batch Normalization used in BigGAN.

BigGANDeepDiscResBlock

Residual block used in BigGAN-Deep's discriminator.

BigGANDeepGenResBlock

Residual block used in BigGAN-Deep's generator.

BigGANDiscResBlock

Residual block used in BigGAN's discriminator.

BigGANGenResBlock

Residual block used in BigGAN's generator.

SelfAttentionBlock

Self-Attention block used in BigGAN.

SNConvModule

Spectral Normalization ConvModule.

class mmagic.models.editors.biggan.BigGAN(generator: ModelType, discriminator: Optional[ModelType] = None, data_preprocessor: Optional[Union[dict, mmengine.Config]] = None, generator_steps: int = 1, discriminator_steps: int = 1, noise_size: Optional[int] = None, num_classes: Optional[int] = None, ema_config: Optional[Dict] = None)[source]

Bases: mmagic.models.base_models.BaseConditionalGAN

Implementation of Large Scale GAN Training for High Fidelity Natural Image Synthesis (BigGAN).

Detailed architecture can be found in BigGANGenerator and BigGANDiscriminator

Parameters
  • generator (ModelType) – The config or model of the generator.

  • discriminator (Optional[ModelType]) – The config or model of the discriminator. Defaults to None.

  • data_preprocessor (Optional[Union[dict, Config]]) – The pre-process config or DataPreprocessor.

  • generator_steps (int) – Number of times the generator was completely updated before the discriminator is updated. Defaults to 1.

  • discriminator_steps (int) – Number of times the discriminator was completely updated before the generator is updated. Defaults to 1.

  • noise_size (Optional[int]) – Size of the input noise vector. Default to 128.

  • num_classes (Optional[int]) – The number classes you would like to generate. Defaults to None.

  • ema_config (Optional[Dict]) – The config for generator’s exponential moving average setting. Defaults to None.

disc_loss(disc_pred_fake: torch.Tensor, disc_pred_real: torch.Tensor) Tuple[source]

Get disc loss. BigGAN use hinge loss to train the discriminator.

Parameters
  • disc_pred_fake (Tensor) – Discriminator’s prediction of the fake images.

  • disc_pred_real (Tensor) – Discriminator’s prediction of the real images.

Returns

Loss value and a dict of log variables.

Return type

tuple[Tensor, dict]

gen_loss(disc_pred_fake)[source]

Get disc loss. BigGAN use hinge loss to train the generator.

Parameters

disc_pred_fake (Tensor) – Discriminator’s prediction of the fake images.

Returns

Loss value and a dict of log variables.

Return type

tuple[Tensor, dict]

train_discriminator(inputs: dict, data_samples: mmagic.structures.DataSample, optimizer_wrapper: mmengine.optim.OptimWrapper) Dict[str, torch.Tensor][source]

Train discriminator.

Parameters
  • inputs (dict) – Inputs from dataloader.

  • data_samples (DataSample) – Data samples from dataloader.

  • optim_wrapper (OptimWrapper) – OptimWrapper instance used to update model parameters.

Returns

A dict of tensor for logging.

Return type

Dict[str, Tensor]

train_generator(inputs: dict, data_samples: mmagic.structures.DataSample, optimizer_wrapper: mmengine.optim.OptimWrapper) Dict[str, torch.Tensor][source]

Train generator.

Parameters
  • inputs (dict) – Inputs from dataloader.

  • data_samples (DataSample) – Data samples from dataloader. Do not used in generator’s training.

  • optim_wrapper (OptimWrapper) – OptimWrapper instance used to update model parameters.

Returns

A dict of tensor for logging.

Return type

Dict[str, Tensor]

class mmagic.models.editors.biggan.BigGANDeepDiscriminator(input_scale, num_classes=0, in_channels=3, out_channels=1, base_channels=96, block_depth=2, sn_eps=1e-06, sn_style='ajbrock', init_type='ortho', act_cfg=dict(type='ReLU', inplace=False), with_spectral_norm=True, blocks_cfg=dict(type='BigGANDeepDiscResBlock'), arch_cfg=None, pretrained=None)[source]

Bases: torch.nn.Module

BigGAN-Deep Discriminator. The implementation refers to https://github.com/ajbrock/BigGAN-PyTorch/blob/master/BigGANdeep.py # noqa.

The overall structure of BigGAN’s discriminator is the same with the projection discriminator.

The main difference between BigGAN and BigGAN-deep is that BigGAN-deep use more deeper residual blocks to construct the whole model.

More details can be found in: Large Scale GAN Training for High Fidelity Natural Image Synthesis (ICLR2019).

The design of the model structure is highly corresponding to the output resolution. For origin BigGAN-Deep’s generator, you can set output_scale as you need and use the default value of arch_cfg and blocks_cfg. If you want to customize the model, you can set the arguments in this way:

arch_cfg: Config for the architecture of this generator. You can refer the _default_arch_cfgs in the _get_default_arch_cfg function to see the format of the arch_cfg. Basically, you need to provide information of each block such as the numbers of input and output channels, whether to perform upsampling etc.

blocks_cfg: Config for the convolution block. You can adjust block params like channel_ratio here. You can also replace the block type to your registered customized block. However, you should notice that some params are shared between these blocks like act_cfg, with_spectral_norm, sn_eps etc.

Parameters
  • input_scale (int) – The scale of the input image.

  • num_classes (int, optional) – The number of conditional classes. Defaults to 0.

  • in_channels (int, optional) – The channel number of the input image. Defaults to 3.

  • out_channels (int, optional) – The channel number of the final output. Defaults to 1.

  • base_channels (int, optional) – The basic channel number of the discriminator. The other layers contains channels based on this number. Defaults to 96.

  • block_depth (int, optional) – The repeat times of Residual Blocks in each level of architecture. Defaults to 2.

  • sn_eps (float, optional) – Epsilon value for spectral normalization. Defaults to 1e-6.

  • sn_style (str, optional) – The style of spectral normalization. If set to ajbrock, implementation by ajbrock(https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py) will be adopted. If set to torch, implementation by PyTorch will be adopted. Defaults to ajbrock.

  • init_type (str, optional) – The name of an initialization method: ortho | N02 | xavier. Defaults to ‘ortho’.

  • act_cfg (dict, optional) – Config for the activation layer. Defaults to dict(type=’ReLU’).

  • with_spectral_norm (bool, optional) – Whether to use spectral normalization. Defaults to True.

  • blocks_cfg (dict, optional) – Config for the convolution block. Defaults to dict(type=’BigGANDiscResBlock’).

  • arch_cfg (dict, optional) – Config for the architecture of this discriminator. Defaults to None.

  • pretrained (str | dict, optional) – Path for the pretrained model or dict containing information for pretrained models whose necessary key is ‘ckpt_path’. Besides, you can also provide ‘prefix’ to load the generator part from the whole state dict. Defaults to None.

_get_default_arch_cfg(input_scale, base_channels)[source]
forward(x, label=None)[source]

Forward function.

Parameters
  • x (torch.Tensor) – Fake or real image tensor.

  • label (torch.Tensor | None) – Label Tensor. Defaults to None.

Returns

Prediction for the reality of the input image with

given label.

Return type

torch.Tensor

init_weights(pretrained=None, init_type='ortho')[source]

Init weights for models.

Parameters
  • pretrained (str | dict, optional) – Path for the pretrained model or dict containing information for pretrained models whose necessary key is ‘ckpt_path’. Besides, you can also provide ‘prefix’ to load the generator part from the whole state dict. Defaults to None.

  • init_type (str, optional) – The name of an initialization method: ortho | N02 | xavier. Defaults to ‘ortho’.

class mmagic.models.editors.biggan.BigGANDeepGenerator(output_scale, noise_size=120, num_classes=0, out_channels=3, base_channels=96, block_depth=2, input_scale=4, with_shared_embedding=True, shared_dim=128, sn_eps=1e-06, sn_style='ajbrock', init_type='ortho', concat_noise=True, act_cfg=dict(type='ReLU', inplace=False), upsample_cfg=dict(type='nearest', scale_factor=2), with_spectral_norm=True, auto_sync_bn=True, blocks_cfg=dict(type='BigGANDeepGenResBlock'), arch_cfg=None, out_norm_cfg=dict(type='BN'), pretrained=None, rgb2bgr=False)[source]

Bases: torch.nn.Module

BigGAN-Deep Generator. The implementation refers to https://github.com/ajbrock/BigGAN-PyTorch/blob/master/BigGANdeep.py # noqa.

In BigGAN, we use a SAGAN-based architecture composing of an self-attention block and number of convolutional residual blocks with spectral normalization. BigGAN-deep follow the same architecture.

The main difference between BigGAN and BigGAN-deep is that BigGAN-deep uses deeper residual blocks to construct the whole model.

More details can be found in: Large Scale GAN Training for High Fidelity Natural Image Synthesis (ICLR2019).

The design of the model structure is highly corresponding to the output resolution. For the original BigGAN-Deep’s generator, you can set output_scale as you need and use the default value of arch_cfg and blocks_cfg. If you want to customize the model, you can set the arguments in this way:

arch_cfg: Config for the architecture of this generator. You can refer the _default_arch_cfgs in the _get_default_arch_cfg function to see the format of the arch_cfg. Basically, you need to provide information of each block such as the numbers of input and output channels, whether to perform upsampling, etc.

blocks_cfg: Config for the convolution block. You can adjust block params like channel_ratio here. You can also replace the block type to your registered customized block. However, you should notice that some params are shared among these blocks like act_cfg, with_spectral_norm, sn_eps, etc.

Parameters
  • output_scale (int) – Output scale for the generated image.

  • noise_size (int, optional) – Size of the input noise vector. Defaults to 120.

  • num_classes (int, optional) – The number of conditional classes. If set to 0, this model will be degraded to an unconditional model. Defaults to 0.

  • out_channels (int, optional) – Number of channels in output images. Defaults to 3.

  • base_channels (int, optional) – The basic channel number of the generator. The other layers contains channels based on this number. Defaults to 96.

  • block_depth (int, optional) – The repeat times of Residual Blocks in each level of architecture. Defaults to 2.

  • input_scale (int, optional) – The scale of the input 2D feature map. Defaults to 4.

  • with_shared_embedding (bool, optional) – Whether to use shared embedding. Defaults to True.

  • shared_dim (int, optional) – The output channels of shared embedding. Defaults to 128.

  • sn_eps (float, optional) – Epsilon value for spectral normalization. Defaults to 1e-6.

  • sn_style (str, optional) – The style of spectral normalization. If set to ajbrock, implementation by ajbrock(https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py) will be adopted. If set to torch, implementation by PyTorch will be adopted. Defaults to ajbrock.

  • init_type (str, optional) – The name of an initialization method: ortho | N02 | xavier. Defaults to ‘ortho’.

  • concat_noise (bool, optional) – Whether to concat input noise vector with class vector. Defaults to True.

  • act_cfg (dict, optional) – Config for the activation layer. Defaults to dict(type=’ReLU’).

  • upsample_cfg (dict, optional) – Config for the upsampling operation. Defaults to dict(type=’nearest’, scale_factor=2).

  • with_spectral_norm (bool, optional) – Whether to use spectral normalization. Defaults to True.

  • auto_sync_bn (bool, optional) – Whether to use synchronized batch normalization. Defaults to True.

  • blocks_cfg (dict, optional) – Config for the convolution block. Defaults to dict(type=’BigGANGenResBlock’).

  • arch_cfg (dict, optional) – Config for the architecture of this generator. Defaults to None.

  • out_norm_cfg (dict, optional) – Config for the norm of output layer. Defaults to dict(type=’BN’).

  • pretrained (str | dict, optional) – Path for the pretrained model or dict containing information for pretrained models whose necessary key is ‘ckpt_path’. Besides, you can also provide ‘prefix’ to load the generator part from the whole state dict. Defaults to None.

  • rgb2bgr (bool, optional) – Whether to reformat the output channels with order bgr. We provide several pre-trained BigGAN-Deep weights whose output channels order is rgb. You can set this argument to True to use the weights.

_get_default_arch_cfg(output_scale, base_channels)[source]
forward(noise, label=None, num_batches=0, return_noise=False, truncation=- 1.0, use_outside_embedding=False)[source]

Forward function.

Parameters
  • noise (torch.Tensor | callable | None) – You can directly give a batch of noise through a torch.Tensor or offer a callable function to sample a batch of noise data. Otherwise, the None indicates to use the default noise sampler.

  • label (torch.Tensor | callable | None) – You can directly give a batch of label through a torch.Tensor or offer a callable function to sample a batch of label data. Otherwise, the None indicates to use the default label sampler. Defaults to None.

  • num_batches (int, optional) – The number of batch size. Defaults to 0.

  • return_noise (bool, optional) – If True, noise_batch and label will be returned in a dict with fake_img. Defaults to False.

  • truncation (float, optional) – Truncation factor. Give value not less than 0., the truncation trick will be adopted. Otherwise, the truncation trick will not be adopted. Defaults to -1..

  • use_outside_embedding (bool, optional) – Whether to use outside embedding or use shared_embedding. Set to True if embedding has already be performed outside this function. Default to False.

Returns

If not return_noise, only the output image

will be returned. Otherwise, a dict contains fake_img, noise_batch and label will be returned.

Return type

torch.Tensor | dict

init_weights(pretrained=None, init_type='ortho')[source]

Init weights for models.

Parameters
  • pretrained (str | dict, optional) – Path for the pretrained model or dict containing information for pretrained models whose necessary key is ‘ckpt_path’. Besides, you can also provide ‘prefix’ to load the generator part from the whole state dict. Defaults to None.

  • init_type (str, optional) – The name of an initialization method: ortho | N02 | xavier. Defaults to ‘ortho’.

class mmagic.models.editors.biggan.BigGANDiscriminator(input_scale, num_classes=0, in_channels=3, out_channels=1, base_channels=96, sn_eps=1e-06, sn_style='ajbrock', act_cfg=dict(type='ReLU'), with_spectral_norm=True, blocks_cfg=dict(type='BigGANDiscResBlock'), arch_cfg=None, init_cfg=dict(type='ortho'))[source]

Bases: mmengine.model.BaseModule

BigGAN Discriminator. The implementation refers to https://github.com/ajbrock/BigGAN-PyTorch/blob/master/BigGAN.py # noqa.

In BigGAN, we use a SAGAN-based architecture composing of an self-attention block and number of convolutional residual blocks with spectral normalization.

More details can be found in: Large Scale GAN Training for High Fidelity Natural Image Synthesis (ICLR2019).

The design of the model structure is highly corresponding to the output resolution. For the original BigGAN’s generator, you can set output_scale as you need and use the default value of arch_cfg and blocks_cfg. If you want to customize the model, you can set the arguments in this way:

arch_cfg: Config for the architecture of this generator. You can refer the _default_arch_cfgs in the _get_default_arch_cfg function to see the format of the arch_cfg. Basically, you need to provide information of each block such as the numbers of input and output channels, whether to perform upsampling, etc.

blocks_cfg: Config for the convolution block. You can replace the block type to your registered customized block and adjust block params here. However, you should notice that some params are shared among these blocks like act_cfg, with_spectral_norm, sn_eps, etc.

Parameters
  • input_scale (int) – The scale of the input image.

  • num_classes (int, optional) – The number of conditional classes. Defaults to 0.

  • in_channels (int, optional) – The channel number of the input image. Defaults to 3.

  • out_channels (int, optional) – The channel number of the final output. Defaults to 1.

  • base_channels (int, optional) – The basic channel number of the discriminator. The other layers contains channels based on this number. Defaults to 96.

  • sn_eps (float, optional) – Epsilon value for spectral normalization. Defaults to 1e-6.

  • sn_style (str, optional) – The style of spectral normalization. If set to ajbrock, implementation by ajbrock(https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py) will be adopted. If set to torch, implementation by PyTorch will be adopted. Defaults to ajbrock.

  • act_cfg (dict, optional) – Config for the activation layer. Defaults to dict(type=’ReLU’).

  • with_spectral_norm (bool, optional) – Whether to use spectral normalization. Defaults to True.

  • blocks_cfg (dict, optional) – Config for the convolution block. Defaults to dict(type=’BigGANDiscResBlock’).

  • arch_cfg (dict, optional) – Config for the architecture of this discriminator. Defaults to None.

  • init_cfg (dict, optional) – Initialization config dict. If type is Pretrained, the pretrain model will be loaded. Otherwise, type will be parsed as the name of initialization method. Support values are ‘ortho’, ‘N02’, ‘xavier’. Defaults to dict(type=’ortho’).

_get_default_arch_cfg(input_scale, in_channels, base_channels)[source]
forward(x, label=None)[source]

Forward function.

Parameters
  • x (torch.Tensor) – Fake or real image tensor.

  • label (torch.Tensor | None) – Label Tensor. Defaults to None.

Returns

Prediction for the reality of the input image with

given label.

Return type

torch.Tensor

init_weights()[source]

Init weights for models.

class mmagic.models.editors.biggan.BigGANGenerator(output_scale, noise_size=120, num_classes=0, out_channels=3, base_channels=96, input_scale=4, with_shared_embedding=True, shared_dim=128, sn_eps=1e-06, sn_style='ajbrock', split_noise=True, act_cfg=dict(type='ReLU'), upsample_cfg=dict(type='nearest', scale_factor=2), with_spectral_norm=True, auto_sync_bn=True, blocks_cfg=dict(type='BigGANGenResBlock'), arch_cfg=None, out_norm_cfg=dict(type='BN'), rgb2bgr=False, init_cfg=dict(type='ortho'))[source]

Bases: mmengine.model.BaseModule

BigGAN Generator. The implementation refers to https://github.com/ajbrock/BigGAN-PyTorch/blob/master/BigGAN.py # noqa.

In BigGAN, we use a SAGAN-based architecture composing of an self-attention block and number of convolutional residual blocks with spectral normalization.

More details can be found in: Large Scale GAN Training for High Fidelity Natural Image Synthesis (ICLR2019).

The design of the model structure is highly corresponding to the output resolution. For the original BigGAN’s generator, you can set output_scale as you need and use the default value of arch_cfg and blocks_cfg. If you want to customize the model, you can set the arguments in this way:

arch_cfg: Config for the architecture of this generator. You can refer the _default_arch_cfgs in the _get_default_arch_cfg function to see the format of the arch_cfg. Basically, you need to provide information of each block such as the numbers of input and output channels, whether to perform upsampling, etc.

blocks_cfg: Config for the convolution block. You can replace the block type to your registered customized block and adjust block params here. However, you should notice that some params are shared among these blocks like act_cfg, with_spectral_norm, sn_eps, etc.

Parameters
  • output_scale (int) – Output scale for the generated image.

  • noise_size (int, optional) – Size of the input noise vector. Defaults to 120.

  • num_classes (int, optional) – The number of conditional classes. If set to 0, this model will be degraded to an unconditional model. Defaults to 0.

  • out_channels (int, optional) – Number of channels in output images. Defaults to 3.

  • base_channels (int, optional) – The basic channel number of the generator. The other layers contains channels based on this number. Defaults to 96.

  • input_scale (int, optional) – The scale of the input 2D feature map. Defaults to 4.

  • with_shared_embedding (bool, optional) – Whether to use shared embedding. Defaults to True.

  • shared_dim (int, optional) – The output channels of shared embedding. Defaults to 128.

  • sn_eps (float, optional) – Epsilon value for spectral normalization. Defaults to 1e-6.

  • sn_style (str, optional) – The style of spectral normalization. If set to ajbrock, implementation by ajbrock(https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py) will be adopted. If set to torch, implementation by PyTorch will be adopted. Defaults to ajbrock.

  • split_noise (bool, optional) – Whether to split input noise vector. Defaults to True.

  • act_cfg (dict, optional) – Config for the activation layer. Defaults to dict(type=’ReLU’).

  • upsample_cfg (dict, optional) – Config for the upsampling operation. Defaults to dict(type=’nearest’, scale_factor=2).

  • with_spectral_norm (bool, optional) – Whether to use spectral normalization. Defaults to True.

  • auto_sync_bn (bool, optional) – Whether to use synchronized batch normalization. Defaults to True.

  • blocks_cfg (dict, optional) – Config for the convolution block. Defaults to dict(type=’BigGANGenResBlock’).

  • arch_cfg (dict, optional) – Config for the architecture of this generator. Defaults to None.

  • out_norm_cfg (dict, optional) – Config for the norm of output layer. Defaults to dict(type=’BN’).

  • rgb2bgr (bool, optional) – Whether to reformat the output channels with order bgr. We provide several pre-trained BigGAN weights whose output channels order is rgb. You can set this argument to True to use the weights.

  • init_cfg (dict, optional) – Initialization config dict. If type is Pretrained, the pretrain model will be loaded. Otherwise, type will be parsed as the name of initialization method. Support values are ‘ortho’, ‘N02’, ‘xavier’. Defaults to dict(type=’ortho’).

_get_default_arch_cfg(output_scale, base_channels)[source]
forward(noise, label=None, num_batches=0, return_noise=False, truncation=- 1.0, use_outside_embedding=False)[source]

Forward function.

Parameters
  • noise (torch.Tensor | callable | None) – You can directly give a batch of noise through a torch.Tensor or offer a callable function to sample a batch of noise data. Otherwise, the None indicates to use the default noise sampler.

  • label (torch.Tensor | callable | None) – You can directly give a batch of label through a torch.Tensor or offer a callable function to sample a batch of label data. Otherwise, the None indicates to use the default label sampler. Defaults to None.

  • num_batches (int, optional) – The number of batch size. Defaults to 0.

  • return_noise (bool, optional) – If True, noise_batch and label will be returned in a dict with fake_img. Defaults to False.

  • truncation (float, optional) – Truncation factor. Give value not less than 0., the truncation trick will be adopted. Otherwise, the truncation trick will not be adopted. Defaults to -1..

  • use_outside_embedding (bool, optional) – Whether to use outside embedding or use shared_embedding. Set to True if embedding has already be performed outside this function. Default to False.

Returns

If not return_noise, only the output image

will be returned. Otherwise, a dict contains fake_img, noise_batch and label will be returned.

Return type

torch.Tensor | dict

init_weights()[source]

Init weights for models.

class mmagic.models.editors.biggan.BigGANConditionBN(num_features, linear_input_channels, bn_eps=1e-05, sn_eps=1e-06, sn_style='ajbrock', momentum=0.1, input_is_label=False, with_spectral_norm=True, auto_sync_bn=True)[source]

Bases: torch.nn.Module

Conditional Batch Normalization used in BigGAN.

Parameters
  • num_features (int) – The channel number of the input feature map tensor.

  • linear_input_channels (int) – The channel number of the linear layers’ input tensor.

  • bn_eps (float, optional) – Epsilon value for batch normalization. Defaults to 1e-5.

  • sn_eps (float, optional) – Epsilon value for spectral normalization. Defaults to 1e-6.

  • sn_style (str, optional) – The style of spectral normalization. If set to ajbrock, implementation by ajbrock(https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py) will be adopted. If set to torch, implementation by PyTorch will be adopted. Defaults to ajbrock.

  • momentum (float, optional) – The value used for the running_mean and running_var computation. Defaults to 0.1.

  • input_is_label (bool, optional) – Whether the input of BNs’ linear layer is raw label instead of class vector. Defaults to False.

  • with_spectral_norm (bool, optional) – Whether to use spectral normalization. Defaults to True.

  • auto_sync_bn (bool, optional) – Whether to use synchronized batch normalization. Defaults to True.

forward(x, y)[source]

Forward function.

Parameters
  • x (torch.Tensor) – Input feature map tensor.

  • y (torch.Tensor) – Label tensor or class embedding concatenated with noise tensor.

Returns

Output feature map tensor.

Return type

torch.Tensor

class mmagic.models.editors.biggan.BigGANDeepDiscResBlock(in_channels, out_channels, channel_ratio=4, act_cfg=dict(type='ReLU', inplace=False), sn_eps=1e-06, sn_style='ajbrock', with_downsample=True, with_spectral_norm=True)[source]

Bases: torch.nn.Module

Residual block used in BigGAN-Deep’s discriminator.

Parameters
  • in_channels (int) – The channel number of the input tensor.

  • out_channels (int) – The channel number of the output tensor.

  • channel_ratio (int, optional) – The ratio of the input channels’ number to the hidden channels’ number. Defaults to 4.

  • act_cfg (dict, optional) – Config for the activation layer. Defaults to dict(type=’ReLU’, inplace=False).

  • sn_eps (float, optional) – Epsilon value for spectral normalization. Defaults to 1e-6.

  • sn_style (str, optional) – The style of spectral normalization. If set to ajbrock, implementation by ajbrock(https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py) will be adopted. If set to torch, implementation by PyTorch will be adopted. Defaults to ajbrock.

  • with_downsample (bool, optional) – Whether to use downsampling in this block. Defaults to True.

  • with_spectral_norm (bool, optional) – Whether to use spectral normalization. Defaults to True.

forward_sc(x)[source]

Forward function of shortcut.

Parameters

x (torch.Tensor) – Input feature map tensor.

Returns

Output tensor of shortcut.

Return type

torch.Tensor

forward(x)[source]

Forward function.

Parameters

x (torch.Tensor) – Input feature map tensor.

Returns

Output feature map tensor.

Return type

torch.Tensor

class mmagic.models.editors.biggan.BigGANDeepGenResBlock(in_channels, out_channels, dim_after_concat, act_cfg=dict(type='ReLU'), upsample_cfg=dict(type='nearest', scale_factor=2), sn_eps=1e-06, sn_style='ajbrock', bn_eps=1e-05, with_spectral_norm=True, input_is_label=False, auto_sync_bn=True, channel_ratio=4)[source]

Bases: torch.nn.Module

Residual block used in BigGAN-Deep’s generator.

Parameters
  • in_channels (int) – The channel number of the input feature map.

  • out_channels (int) – The channel number of the output feature map.

  • dim_after_concat (int) – The channel number of the noise concatenated with the class vector.

  • act_cfg (dict, optional) – Config for the activation layer. Defaults to dict(type=’ReLU’).

  • upsample_cfg (dict, optional) – Config for the upsampling operation. Defaults to dict(type=’nearest’, scale_factor=2).

  • sn_eps (float, optional) – Epsilon value for spectral normalization. Defaults to 1e-6.

  • sn_style (str, optional) – The style of spectral normalization. If set to ajbrock, implementation by ajbrock(https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py) will be adopted. If set to torch, implementation by PyTorch will be adopted. Defaults to ajbrock.

  • bn_eps (float, optional) – Epsilon value for batch normalization. Defaults to 1e-5.

  • with_spectral_norm (bool, optional) – Whether to use spectral normalization in this block. Defaults to True.

  • input_is_label (bool, optional) – Whether the input of BNs’ linear layer is raw label instead of class vector. Defaults to False.

  • auto_sync_bn (bool, optional) – Whether to use synchronized batch normalization. Defaults to True.

  • channel_ratio (int, optional) – The ratio of the input channels’ number to the hidden channels’ number. Defaults to 4.

forward(x, y)[source]

Forward function.

Parameters
  • x (torch.Tensor) – Input feature map tensor.

  • y (torch.Tensor) – Label tensor or class embedding concatenated with noise tensor.

Returns

Output feature map tensor.

Return type

torch.Tensor

class mmagic.models.editors.biggan.BigGANDiscResBlock(in_channels, out_channels, act_cfg=dict(type='ReLU', inplace=False), sn_eps=1e-06, sn_style='ajbrock', with_downsample=True, with_spectral_norm=True, is_head_block=False)[source]

Bases: torch.nn.Module

Residual block used in BigGAN’s discriminator.

Parameters
  • in_channels (int) – The channel number of the input tensor.

  • out_channels (int) – The channel number of the output tensor.

  • act_cfg (dict, optional) – Config for the activation layer. Defaults to dict(type=’ReLU’, inplace=False).

  • sn_eps (float, optional) – Epsilon value for spectral normalization. Defaults to 1e-6.

  • sn_style (str, optional) – The style of spectral normalization. If set to ajbrock, implementation by ajbrock(https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py) will be adopted. If set to torch, implementation by PyTorch will be adopted. Defaults to ajbrock.

  • with_downsample (bool, optional) – Whether to use downsampling in this block. Defaults to True.

  • with_spectral_norm (bool, optional) – Whether to use spectral normalization. Defaults to True.

  • is_head_block (bool, optional) – Whether this block is the first block of BigGAN. Defaults to False.

forward_sc(x)[source]

Forward function of shortcut.

Parameters

x (torch.Tensor) – Input feature map tensor.

Returns

Output tensor of shortcut.

Return type

torch.Tensor

forward(x)[source]

Forward function.

Parameters

x (torch.Tensor) – Input feature map tensor.

Returns

Output feature map tensor.

Return type

torch.Tensor

class mmagic.models.editors.biggan.BigGANGenResBlock(in_channels, out_channels, dim_after_concat, act_cfg=dict(type='ReLU'), upsample_cfg=dict(type='nearest', scale_factor=2), sn_eps=1e-06, sn_style='ajbrock', with_spectral_norm=True, input_is_label=False, auto_sync_bn=True)[source]

Bases: torch.nn.Module

Residual block used in BigGAN’s generator.

Parameters
  • in_channels (int) – The channel number of the input feature map.

  • out_channels (int) – The channel number of the output feature map.

  • dim_after_concat (int) – The channel number of the noise concatenated with the class vector.

  • act_cfg (dict, optional) – Config for the activation layer. Defaults to dict(type=’ReLU’).

  • upsample_cfg (dict, optional) – Config for the upsampling operation. Defaults to dict(type=’nearest’, scale_factor=2).

  • sn_eps (float, optional) – Epsilon value for spectral normalization. Defaults to 1e-6.

  • sn_style (str, optional) – The style of spectral normalization. If set to ajbrock, implementation by ajbrock(https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py) will be adopted. If set to torch, implementation by PyTorch will be adopted. Defaults to ajbrock.

  • with_spectral_norm (bool, optional) – Whether to use spectral normalization in this block. Defaults to True.

  • input_is_label (bool, optional) – Whether the input of BNs’ linear layer is raw label instead of class vector. Defaults to False.

  • auto_sync_bn (bool, optional) – Whether to use synchronized batch normalization. Defaults to True.

forward(x, y)[source]

Forward function.

Parameters
  • x (torch.Tensor) – Input feature map tensor.

  • y (torch.Tensor) – Label tensor or class embedding concatenated with noise tensor.

Returns

Output feature map tensor.

Return type

torch.Tensor

class mmagic.models.editors.biggan.SelfAttentionBlock(in_channels, with_spectral_norm=True, sn_eps=1e-06, sn_style='ajbrock')[source]

Bases: torch.nn.Module

Self-Attention block used in BigGAN.

Parameters
  • in_channels (int) – The channel number of the input feature map.

  • with_spectral_norm (bool, optional) – Whether to use spectral normalization. Defaults to True.

  • sn_eps (float, optional) – Epsilon value for spectral normalization. Defaults to 1e-6.

  • sn_style (str, optional) – The style of spectral normalization. If set to ajbrock, implementation by ajbrock(https://github.com/ajbrock/BigGAN-PyTorch/blob/master/layers.py) will be adopted. If set to torch, implementation by PyTorch will be adopted. Defaults to ajbrock.

forward(x)[source]

Forward function.

Parameters

x (torch.Tensor) – Input feature map tensor.

Returns

Output feature map tensor.

Return type

torch.Tensor

class mmagic.models.editors.biggan.SNConvModule(*args, with_spectral_norm=False, spectral_norm_cfg=None, **kwargs)[source]

Bases: mmcv.cnn.ConvModule

Spectral Normalization ConvModule.

In this module, we inherit default mmcv.cnn.ConvModule and adopt spectral normalization. The spectral normalization is proposed in: Spectral Normalization for Generative Adversarial Networks.

Parameters
  • with_spectral_norm (bool, optional) – Whether to use Spectral Normalization. Defaults to False.

  • spectral_norm_cfg (dict, optional) – Config for Spectral Normalization. Defaults to None.

Read the Docs v: latest
Versions
latest
stable
0.x
Downloads
pdf
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.