Shortcuts

mmedit.models.editors.biggan

Package Contents

Classes

BigGAN

Impelmentation 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 mmedit.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)[源代码]

Bases: mmedit.models.base_models.BaseConditionalGAN

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

Detailed architecture can be found in BigGANGenerator and BigGANDiscriminator

参数
  • 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 GenDataPreprocessor.

  • 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

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

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

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

返回

Loss value and a dict of log variables.

返回类型

tuple[Tensor, dict]

gen_loss(disc_pred_fake)

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

参数

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

返回

Loss value and a dict of log variables.

返回类型

tuple[Tensor, dict]

train_discriminator(inputs: dict, data_samples: List[mmedit.structures.EditDataSample], optimizer_wrapper: mmengine.optim.OptimWrapper) Dict[str, torch.Tensor]

Train discriminator.

参数
  • inputs (dict) – Inputs from dataloader.

  • data_samples (List[EditDataSample]) – Data samples from dataloader.

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

返回

A dict of tensor for logging.

返回类型

Dict[str, Tensor]

train_generator(inputs: dict, data_samples: List[mmedit.structures.EditDataSample], optimizer_wrapper: mmengine.optim.OptimWrapper) Dict[str, torch.Tensor]

Train generator.

参数
  • inputs (dict) – Inputs from dataloader.

  • data_samples (List[EditDataSample]) – Data samples from dataloader. Do not used in generator’s training.

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

返回

A dict of tensor for logging.

返回类型

Dict[str, Tensor]

class mmedit.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)[源代码]

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.

参数
  • 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 pretained 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)
forward(x, label=None)

Forward function.

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

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

返回

Prediction for the reality of the input image with

given label.

返回类型

torch.Tensor

init_weights(pretrained=None, init_type='ortho')

Init weights for models.

参数
  • pretrained (str | dict, optional) – Path for the pretrained model or dict containing information for pretained 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 mmedit.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)[源代码]

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.

参数
  • 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 pretained 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)
forward(noise, label=None, num_batches=0, return_noise=False, truncation=- 1.0, use_outside_embedding=False)

Forward function.

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

返回

If not return_noise, only the output image

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

返回类型

torch.Tensor | dict

init_weights(pretrained=None, init_type='ortho')

Init weights for models.

参数
  • pretrained (str | dict, optional) – Path for the pretrained model or dict containing information for pretained 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 mmedit.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', init_type='ortho', act_cfg=dict(type='ReLU'), with_spectral_norm=True, blocks_cfg=dict(type='BigGANDiscResBlock'), arch_cfg=None, pretrained=None)[源代码]

Bases: torch.nn.Module

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.

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

  • 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 pretained 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, in_channels, base_channels)
forward(x, label=None)

Forward function.

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

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

返回

Prediction for the reality of the input image with

given label.

返回类型

torch.Tensor

init_weights(pretrained=None, init_type='ortho')

Init weights for models.

参数
  • pretrained (str | dict, optional) – Path for the pretrained model or dict containing information for pretained 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 mmedit.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', init_type='ortho', 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'), pretrained=None, rgb2bgr=False)[源代码]

Bases: torch.nn.Module

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.

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

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

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

  • pretrained (str | dict, optional) – Path for the pretrained model or dict containing information for pretained 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 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)
forward(noise, label=None, num_batches=0, return_noise=False, truncation=- 1.0, use_outside_embedding=False)

Forward function.

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

返回

If not return_noise, only the output image

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

返回类型

torch.Tensor | dict

init_weights(pretrained=None, init_type='ortho')

Init weights for models.

参数
  • pretrained (str | dict, optional) – Path for the pretrained model or dict containing information for pretained 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 mmedit.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)[源代码]

Bases: torch.nn.Module

Conditional Batch Normalization used in BigGAN.

参数
  • 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)

Forward function.

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

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

返回

Output feature map tensor.

返回类型

torch.Tensor

class mmedit.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)[源代码]

Bases: torch.nn.Module

Residual block used in BigGAN-Deep’s discriminator.

参数
  • 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)

Forward function of shortcut.

参数

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

返回

Output tensor of shortcut.

返回类型

torch.Tensor

forward(x)

Forward function.

参数

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

返回

Output feature map tensor.

返回类型

torch.Tensor

class mmedit.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)[源代码]

Bases: torch.nn.Module

Residual block used in BigGAN-Deep’s generator.

参数
  • 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)

Forward function.

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

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

返回

Output feature map tensor.

返回类型

torch.Tensor

class mmedit.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)[源代码]

Bases: torch.nn.Module

Residual block used in BigGAN’s discriminator.

参数
  • 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)

Forward function of shortcut.

参数

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

返回

Output tensor of shortcut.

返回类型

torch.Tensor

forward(x)

Forward function.

参数

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

返回

Output feature map tensor.

返回类型

torch.Tensor

class mmedit.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)[源代码]

Bases: torch.nn.Module

Residual block used in BigGAN’s generator.

参数
  • 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)

Forward function.

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

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

返回

Output feature map tensor.

返回类型

torch.Tensor

class mmedit.models.editors.biggan.SelfAttentionBlock(in_channels, with_spectral_norm=True, sn_eps=1e-06, sn_style='ajbrock')[源代码]

Bases: torch.nn.Module

Self-Attention block used in BigGAN.

参数
  • 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)

Forward function.

参数

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

返回

Output feature map tensor.

返回类型

torch.Tensor

class mmedit.models.editors.biggan.SNConvModule(*args, with_spectral_norm=False, spectral_norm_cfg=None, **kwargs)[源代码]

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.

参数
  • 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
master
latest
stable
zyh-doc-notfound-extend
Downloads
pdf
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.