Shortcuts

mmedit.models.editors.dcgan

Package Contents

Classes

DCGAN

Impelmentation of `Unsupervised Representation Learning with Deep

DCGANDiscriminator

Discriminator for DCGAN.

DCGANGenerator

Generator for DCGAN.

class mmedit.models.editors.dcgan.DCGAN(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, ema_config: Optional[Dict] = None, loss_config: Optional[Dict] = None)[源代码]

Bases: mmedit.models.base_models.BaseGAN

Impelmentation of Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks.

Paper link:

<https://arxiv.org/abs/1511.06434>`_ (DCGAN).

Detailed architecture can be found in DCGANGenerator # noqa and DCGANDiscriminator # noqa

disc_loss(disc_pred_fake: torch.Tensor, disc_pred_real: torch.Tensor) Tuple

Get disc loss. DCGAN use the vanilla gan 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: torch.Tensor) Tuple

Get gen loss. DCGAN use the vanilla gan 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.dcgan.DCGANDiscriminator(input_scale, output_scale, out_channels, in_channels=3, base_channels=128, default_norm_cfg=dict(type='BN'), default_act_cfg=dict(type='LeakyReLU'), out_act_cfg=None, pretrained=None)[源代码]

Bases: torch.nn.Module

Discriminator for DCGAN.

Implementation Details for DCGAN architecture:

  1. Adopt convolution in the discriminator;

  2. Use batchnorm in the discriminator except for the input and final output layer;

  3. Use LeakyReLU in the discriminator in addition to the output layer.

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

  • output_scale (int) – The final scale of the convolutional feature.

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

  • in_channels (int, optional) – The channel number of the input image. 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 128.

  • default_norm_cfg (dict, optional) – Norm config for all of layers except for the final output layer. Defaults to dict(type='BN').

  • default_act_cfg (dict, optional) – Activation config for all of layers except for the final output layer. Defaults to dict(type='ReLU').

  • out_act_cfg (dict, optional) – Activation config for the final output layer. Defaults to dict(type='Tanh').

  • pretrained (str, optional) – Path for the pretrained model. Default to None.

forward(x)

Forward function.

参数

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

返回

Prediction for the reality of the input image.

返回类型

torch.Tensor

init_weights(pretrained=None)

Init weights for models.

We just use the initialization method proposed in the original paper.

参数

pretrained (str, optional) – Path for pretrained weights. If given None, pretrained weights will not be loaded. Defaults to None.

class mmedit.models.editors.dcgan.DCGANGenerator(output_scale, out_channels=3, base_channels=1024, input_scale=4, noise_size=100, default_norm_cfg=dict(type='BN'), default_act_cfg=dict(type='ReLU'), out_act_cfg=dict(type='Tanh'), pretrained=None)[源代码]

Bases: torch.nn.Module

Generator for DCGAN.

Implementation Details for DCGAN architecture:

  1. Adopt transposed convolution in the generator;

  2. Use batchnorm in the generator except for the final output layer;

  3. Use ReLU in the generator in addition to the final output layer.

More details can be found in the original paper: Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks http://arxiv.org/abs/1511.06434

参数
  • output_scale (int | tuple[int]) – Output scale for the generated image. If only a integer is provided, the output image will be a square shape. The tuple of two integers will set the height and width for the output image, respectively.

  • out_channels (int, optional) – The channel number of the output feature. Default to 3.

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

  • input_scale (int | tuple[int], optional) – Output scale for the generated image. If only a integer is provided, the input feature ahead of the convolutional generator will be a square shape. The tuple of two integers will set the height and width for the input convolutional feature, respectively. Defaults to 4.

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

  • default_norm_cfg (dict, optional) – Norm config for all of layers except for the final output layer. Defaults to dict(type='BN').

  • default_act_cfg (dict, optional) – Activation config for all of layers except for the final output layer. Defaults to dict(type='ReLU').

  • out_act_cfg (dict, optional) – Activation config for the final output layer. Defaults to dict(type='Tanh').

  • pretrained (str, optional) – Path for the pretrained model. Default to None.

forward(noise, num_batches=0, return_noise=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.

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

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

返回

If not return_noise, only the output image

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

返回类型

torch.Tensor | dict

init_weights(pretrained=None)

Init weights for models.

We just use the initialization method proposed in the original paper.

参数

pretrained (str, optional) – Path for pretrained weights. If given None, pretrained weights will not be loaded. 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.