torchgan.models¶
This models subpackage is a collection of popular GAN architectures. It has the support for existing architectures and provides a base class for extending to any form of new architecture. Currently the following models are supported:
You can construct a new model by simply calling its constructor.
>>> import torchgan.models as models
>>> dcgan_discriminator = DCGANDiscriminator()
>>> dcgan_generator = DCGANGenerator()
All models follow the same structure. There are additional customization options. Look into the individual documentation for such capabilities.
Vanilla GAN¶
Generator¶

class
torchgan.models.
Generator
(encoding_dims, label_type='none')[source]¶ Base class for all Generator models. All Generator models must subclass this.
Parameters:  encoding_dims (int) – Dimensions of the sample from the noise prior.
 label_type (str, optional) – The type of labels expected by the Generator. The available choices are ‘none’ if no label is needed, ‘required’ if the original labels are needed and ‘generated’ if labels are to be sampled from a distribution.

_weight_initializer
()[source]¶ Default weight initializer for all generator models. Models that require custom weight initialization can override this method

sampler
(sample_size, device)[source]¶ Function to allow sampling data at inference time. Models requiring input in any other format must override it in the subclass.
Parameters:  sample_size (int) – The number of images to be generated
 device (torch.device) – The device on which the data must be generated
Returns: A list of the items required as input
Discriminator¶
Deep Convolutional GAN¶
DCGANGenerator¶

class
torchgan.models.
DCGANGenerator
(encoding_dims=100, out_size=32, out_channels=3, step_channels=64, batchnorm=True, nonlinearity=None, last_nonlinearity=None, label_type='none')[source]¶ Deep Convolutional GAN (DCGAN) generator from “Unsupervised Representation Learning With Deep Convolutional Generative Aversarial Networks by Radford et. al. “ paper
Parameters:  encoding_dims (int, optional) – Dimension of the encoding vector sampled from the noise prior.
 out_size (int, optional) – Height and width of the input image to be generated. Must be at least 16 and should be an exact power of 2.
 out_channels (int, optional) – Number of channels in the output Tensor.
 step_channels (int, optional) – Number of channels in multiples of which the DCGAN steps up the convolutional features. The step up is done as dim \(z \rightarrow d \rightarrow 2 \times d \rightarrow 4 \times d \rightarrow 8 \times d\) where \(d\) = step_channels.
 batchnorm (bool, optional) – If True, use batch normalization in the convolutional layers of the generator.
 nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the intermediate
convolutional layers. Defaults to
LeakyReLU(0.2)
when None is passed.  last_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the final
convolutional layer. Defaults to
Tanh()
when None is passed.  label_type (str, optional) – The type of labels expected by the Generator. The available choices are ‘none’ if no label is needed, ‘required’ if the original labels are needed and ‘generated’ if labels are to be sampled from a distribution.

forward
(x, feature_matching=False)[source]¶ Calculates the output tensor on passing the encoding
x
through the Generator.Parameters:  x (torch.Tensor) – A 2D torch tensor of the encoding sampled from a probability distribution.
 feature_matching (bool, optional) – Returns the activation from a predefined intermediate layer.
Returns: A 4D torch.Tensor of the generated image.
DCGANDiscriminator¶

class
torchgan.models.
DCGANDiscriminator
(in_size=32, in_channels=3, step_channels=64, batchnorm=True, nonlinearity=None, last_nonlinearity=None, label_type='none')[source]¶ Deep Convolutional GAN (DCGAN) discriminator from “Unsupervised Representation Learning With Deep Convolutional Generative Aversarial Networks by Radford et. al. “ paper
Parameters:  in_size (int, optional) – Height and width of the input image to be evaluated. Must be at least 16 and should be an exact power of 2.
 in_channels (int, optional) – Number of channels in the input Tensor.
 step_channels (int, optional) – Number of channels in multiples of which the DCGAN steps up the convolutional features. The step up is done as dim \(z \rightarrow d \rightarrow 2 \times d \rightarrow 4 \times d \rightarrow 8 \times d\) where \(d\) = step_channels.
 batchnorm (bool, optional) – If True, use batch normalization in the convolutional layers of the generator.
 nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the intermediate
convolutional layers. Defaults to
LeakyReLU(0.2)
when None is passed.  last_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the final
convolutional layer. Defaults to
Tanh()
when None is passed.  label_type (str, optional) – The type of labels expected by the Generator. The available choices are ‘none’ if no label is needed, ‘required’ if the original labels are needed and ‘generated’ if labels are to be sampled from a distribution.

forward
(x, feature_matching=False)[source]¶ Calculates the output tensor on passing the image
x
through the Discriminator.Parameters:  x (torch.Tensor) – A 4D torch tensor of the image.
 feature_matching (bool, optional) – Returns the activation from a predefined intermediate layer.
Returns: A 1D torch.Tensor of the probability of each image being real.
Conditional GAN¶
ConditionalGANGenerator¶

class
torchgan.models.
ConditionalGANGenerator
(num_classes, encoding_dims=100, out_size=32, out_channels=3, step_channels=64, batchnorm=True, nonlinearity=None, last_nonlinearity=None)[source]¶ Conditional GAN (CGAN) generator based on a DCGAN model from “Conditional Generative Adversarial Nets by Mirza et. al. “ paper
Parameters:  num_classes (int) – Total classes present in the dataset.
 encoding_dims (int, optional) – Dimension of the encoding vector sampled from the noise prior.
 out_size (int, optional) – Height and width of the input image to be generated. Must be at least 16 and should be an exact power of 2.
 out_channels (int, optional) – Number of channels in the output Tensor.
 step_channels (int, optional) – Number of channels in multiples of which the DCGAN steps up the convolutional features. The step up is done as dim \(z \rightarrow d \rightarrow 2 \times d \rightarrow 4 \times d \rightarrow 8 \times d\) where \(d\) = step_channels.
 batchnorm (bool, optional) – If True, use batch normalization in the convolutional layers of the generator.
 nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the intermediate
convolutional layers. Defaults to
LeakyReLU(0.2)
when None is passed.  last_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the final
convolutional layer. Defaults to
Tanh()
when None is passed.

forward
(z, y)[source]¶ Calculates the output tensor on passing the encoding
z
through the Generator.Parameters:  z (torch.Tensor) – A 2D torch tensor of the encoding sampled from a probability distribution.
 y (torch.Tensor) – The labels corresponding to the encoding
z
.
Returns: A 4D torch.Tensor of the generated Images conditioned on
y
.

sampler
(sample_size, device)[source]¶ Function to allow sampling data at inference time. Models requiring input in any other format must override it in the subclass.
Parameters:  sample_size (int) – The number of images to be generated
 device (torch.device) – The device on which the data must be generated
Returns: A list of the items required as input
ConditionalGANDiscriminator¶

class
torchgan.models.
ConditionalGANDiscriminator
(num_classes, in_size=32, in_channels=3, step_channels=64, batchnorm=True, nonlinearity=None, last_nonlinearity=None)[source]¶ Condititional GAN (CGAN) discriminator based on a DCGAN model from “Conditional Generative Adversarial Nets by Mirza et. al. “ paper
Parameters:  num_classes (int) – Total classes present in the dataset.
 in_size (int, optional) – Height and width of the input image to be evaluated. Must be at least 16 and should be an exact power of 2.
 in_channels (int, optional) – Number of channels in the input Tensor.
 step_channels (int, optional) – Number of channels in multiples of which the DCGAN steps up the convolutional features. The step up is done as dim \(z \rightarrow d \rightarrow 2 \times d \rightarrow 4 \times d \rightarrow 8 \times d\) where \(d\) = step_channels.
 batchnorm (bool, optional) – If True, use batch normalization in the convolutional layers of the generator.
 nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the intermediate
convolutional layers. Defaults to
LeakyReLU(0.2)
when None is passed.  last_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the final
convolutional layer. Defaults to
Tanh()
when None is passed.

forward
(x, y, feature_matching=False)[source]¶ Calculates the output tensor on passing the image
x
through the Discriminator.Parameters:  x (torch.Tensor) – A 4D torch tensor of the image.
 y (torch.Tensor) – Labels corresponding to the images
x
.  feature_matching (bool, optional) – Returns the activation from a predefined intermediate layer.
Returns: A 1D torch.Tensor of the probability of each image being real.
InfoGAN¶
InfoGANGenerator¶

class
torchgan.models.
InfoGANGenerator
(dim_dis, dim_cont, encoding_dims=100, out_size=32, out_channels=3, step_channels=64, batchnorm=True, nonlinearity=None, last_nonlinearity=None)[source]¶ Generator for InfoGAN based on the Deep Convolutional GAN (DCGAN) architecture, from “InfoGAN : Interpretable Representation Learning With Information Maximizing Generative Aversarial Nets by Chen et. al. “ paper
Parameters:  dim_dis (int) – Dimension of the discrete latent code sampled from the prior.
 dim_cont (int) – Dimension of the continuous latent code sampled from the prior.
 encoding_dims (int, optional) – Dimension of the encoding vector sampled from the noise prior.
 out_size (int, optional) – Height and width of the input image to be generated. Must be at least 16 and should be an exact power of 2.
 out_channels (int, optional) – Number of channels in the output Tensor.
 step_channels (int, optional) – Number of channels in multiples of which the DCGAN steps up the convolutional features. The step up is done as dim \(z \rightarrow d \rightarrow 2 \times d \rightarrow 4 \times d \rightarrow 8 \times d\) where \(d\) = step_channels.
 batchnorm (bool, optional) – If True, use batch normalization in the convolutional layers of the generator.
 nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the intermediate
convolutional layers. Defaults to
LeakyReLU(0.2)
when None is passed.  last_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the final
convolutional layer. Defaults to
Tanh()
when None is passed.
Example
>>> import torchgan.models as models >>> G = models.InfoGANGenerator(10, 30) >>> z = torch.randn(10, 100) >>> c_cont = torch.randn(10, 10) >>> c_dis = torch.randn(10, 30) >>> x = G(z, c_cont, c_dis)

forward
(z, c_dis=None, c_cont=None)[source]¶ Calculates the output tensor on passing the encoding
x
through the Generator.Parameters:  x (torch.Tensor) – A 2D torch tensor of the encoding sampled from a probability distribution.
 feature_matching (bool, optional) – Returns the activation from a predefined intermediate layer.
Returns: A 4D torch.Tensor of the generated image.
InfoGANDiscriminator¶

class
torchgan.models.
InfoGANDiscriminator
(dim_dis, dim_cont, in_size=32, in_channels=3, step_channels=64, batchnorm=True, nonlinearity=None, last_nonlinearity=None, latent_nonlinearity=None)[source]¶ Discriminator for InfoGAN based on the Deep Convolutional GAN (DCGAN) architecture, from “InfoGAN : Interpretable Representation Learning With Information Maximizing Generative Aversarial Nets by Chen et. al. “ paper
The approximate conditional probability distribution over the latent code Q(cx) is chosen to be a factored Gaussian for the continuous latent code and a Categorical distribution for the discrete latent code
Parameters:  dim_dis (int) – Dimension of the discrete latent code sampled from the prior.
 dim_cont (int) – Dimension of the continuous latent code sampled from the prior.
 encoding_dims (int, optional) – Dimension of the encoding vector sampled from the noise prior.
 in_size (int, optional) – Height and width of the input image to be evaluated. Must be at least 16 and should be an exact power of 2.
 in_channels (int, optional) – Number of channels in the input Tensor.
 step_channels (int, optional) – Number of channels in multiples of which the DCGAN steps up the convolutional features. The step up is done as dim \(z \rightarrow d \rightarrow 2 \times d \rightarrow 4 \times d \rightarrow 8 \times d\) where \(d\) = step_channels.
 batchnorm (bool, optional) – If True, use batch normalization in the convolutional layers of the generator.
 nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the intermediate
convolutional layers. Defaults to
LeakyReLU(0.2)
when None is passed.  last_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the final
convolutional layer. Defaults to
Tanh()
when None is passed.  latent_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the
dist_conv
. Defaults toLeakyReLU(0.2)
when None is passed.
Example
>>> import torchgan.models as models >>> D = models.InfoGANDiscriminator(10, 30) >>> x = torch.randn(10, 3, 32, 32) >>> score, q_categorical, q_gaussian = D(x, return_latents=True)

forward
(x, return_latents=False, feature_matching=False)[source]¶ Calculates the output tensor on passing the image
x
through the Discriminator.Parameters:  x (torch.Tensor) – A 4D torch tensor of the image.
 feature_matching (bool, optional) – Returns the activation from a predefined intermediate layer.
Returns: A 1D torch.Tensor of the probability of each image being real.
AutoEncoders¶
AutoEncodingGenerator¶

class
torchgan.models.
AutoEncodingGenerator
(encoding_dims=100, out_size=32, out_channels=3, step_channels=64, scale_factor=2, batchnorm=True, nonlinearity=None, last_nonlinearity=None, label_type='none')[source]¶ Autoencoding Generator for Boundary Equilibrium GAN (BEGAN) from “BEGAN : Boundary Equilibrium Generative Adversarial Networks by Berthelot et. al.” paper
Parameters:  encoding_dims (int, optional) – Dimension of the encoding vector sampled from the noise prior.
 out_size (int, optional) – Height and width of the input image to be generated. Must be at least 16 and should be an exact power of 2.
 out_channels (int, optional) – Number of channels in the output Tensor.
 step_channels (int, optional) – Number of channels in multiples of which the DCGAN steps up the convolutional features. The step up is done as dim \(z \rightarrow d \rightarrow 2 \times d \rightarrow 4 \times d \rightarrow 8 \times d\) where \(d\) = step_channels.
 scale_factor (int, optional) – The scale factor is used to infer properties of the model like
upsample_pad
,upsample_filters
,upsample_stride
andupsample_output_pad
.  batchnorm (bool, optional) – If True, use batch normalization in the convolutional layers of the generator.
 nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the intermediate
convolutional layers. Defaults to
LeakyReLU(0.2)
when None is passed.  last_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the final
convolutional layer. Defaults to
Tanh()
when None is passed.  label_type (str, optional) – The type of labels expected by the Generator. The available choices are ‘none’ if no label is needed, ‘required’ if the original labels are needed and ‘generated’ if labels are to be sampled from a distribution.

forward
(z)[source]¶ Calculates the output tensor on passing the encoding
z
through the Generator.Parameters: z (torch.Tensor) – A 2D torch tensor of the encoding sampled from a probability distribution. Returns: A 4D torch.Tensor of the generated image.
AutoEncodingDiscriminator¶

class
torchgan.models.
AutoEncodingDiscriminator
(in_size=32, in_channels=3, encoding_dims=100, step_channels=64, scale_factor=2, batchnorm=True, nonlinearity=None, last_nonlinearity=None, energy=True, embeddings=False, label_type='none')[source]¶ Autoencoding Generator for Boundary Equilibrium GAN (BEGAN) from “BEGAN : Boundary Equilibrium Generative Adversarial Networks by Berthelot et. al.” paper
Parameters:  in_size (int, optional) – Height and width of the input image to be evaluated. Must be at least 16 and should be an exact power of 2.
 in_channels (int, optional) – Number of channels in the input Tensor.
 step_channels (int, optional) – Number of channels in multiples of which the DCGAN steps up the convolutional features. The step up is done as dim \(z \rightarrow d \rightarrow 2 \times d \rightarrow 4 \times d \rightarrow 8 \times d\) where \(d\) = step_channels.
 scale_factor (int, optional) – The scale factor is used to infer properties of the model like
downsample_pad
,downsample_filters
anddownsample_stride
.  batchnorm (bool, optional) – If True, use batch normalization in the convolutional layers of the generator.
 nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the intermediate
convolutional layers. Defaults to
LeakyReLU(0.2)
when None is passed.  last_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the final
convolutional layer. Defaults to
Tanh()
when None is passed.  energy (bool, optional) – If set to True returns the energy instead of the decoder output.
 embeddings (bool, optional) – If set to True the embeddings will be returned.
 label_type (str, optional) – The type of labels expected by the Generator. The available choices are ‘none’ if no label is needed, ‘required’ if the original labels are needed and ‘generated’ if labels are to be sampled from a distribution.

forward
(x, feature_matching=False)[source]¶ Calculates the output tensor on passing the image
x
through the Discriminator.Parameters:  x (torch.Tensor) – A 4D torch tensor of the image.
 feature_matching (bool, optional) – Returns the activation from a predefined intermediate layer.
Returns: A 1D torch.Tensor of the energy value of each image.
Auxiliary Classifier GAN¶
ACGANGenerator¶

class
torchgan.models.
ACGANGenerator
(num_classes, encoding_dims=100, out_size=32, out_channels=3, step_channels=64, batchnorm=True, nonlinearity=None, last_nonlinearity=None)[source]¶ Auxiliary Classifier GAN (ACGAN) generator based on a DCGAN model from “Conditional Image Synthesis With Auxiliary Classifier GANs by Odena et. al. “ paper
Parameters:  num_classes (int) – Total classes present in the dataset.
 encoding_dims (int, optional) – Dimension of the encoding vector sampled from the noise prior.
 out_size (int, optional) – Height and width of the input image to be generated. Must be at least 16 and should be an exact power of 2.
 out_channels (int, optional) – Number of channels in the output Tensor.
 step_channels (int, optional) – Number of channels in multiples of which the DCGAN steps up the convolutional features. The step up is done as dim \(z \rightarrow d \rightarrow 2 \times d \rightarrow 4 \times d \rightarrow 8 \times d\) where \(d\) = step_channels.
 batchnorm (bool, optional) – If True, use batch normalization in the convolutional layers of the generator.
 nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the intermediate
convolutional layers. Defaults to
LeakyReLU(0.2)
when None is passed.  last_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the final
convolutional layer. Defaults to
Tanh()
when None is passed.

forward
(z, y)[source]¶ Calculates the output tensor on passing the encoding
z
through the Generator.Parameters:  z (torch.Tensor) – A 2D torch tensor of the encoding sampled from a probability distribution.
 y (torch.Tensor) – The labels corresponding to the encoding
z
.
Returns: A 4D torch.Tensor of the generated Images conditioned on
y
.

sampler
(sample_size, device)[source]¶ Function to allow sampling data at inference time. Models requiring input in any other format must override it in the subclass.
Parameters:  sample_size (int) – The number of images to be generated
 device (torch.device) – The device on which the data must be generated
Returns: A list of the items required as input
ACGANDiscriminator¶

class
torchgan.models.
ACGANDiscriminator
(num_classes, in_size=32, in_channels=3, step_channels=64, batchnorm=True, nonlinearity=None, last_nonlinearity=None)[source]¶ Auxiliary Classifier GAN (ACGAN) discriminator based on a DCGAN model from “Conditional Image Synthesis With Auxiliary Classifier GANs by Odena et. al. “ paper
Parameters:  num_classes (int) – Total classes present in the dataset.
 in_size (int, optional) – Height and width of the input image to be evaluated. Must be at least 16 and should be an exact power of 2.
 in_channels (int, optional) – Number of channels in the input Tensor.
 step_channels (int, optional) – Number of channels in multiples of which the DCGAN steps up the convolutional features. The step up is done as dim \(z \rightarrow d \rightarrow 2 \times d \rightarrow 4 \times d \rightarrow 8 \times d\) where \(d\) = step_channels.
 batchnorm (bool, optional) – If True, use batch normalization in the convolutional layers of the generator.
 nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the intermediate
convolutional layers. Defaults to
LeakyReLU(0.2)
when None is passed.  last_nonlinearity (torch.nn.Module, optional) – Nonlinearity to be used in the final
convolutional layer. Defaults to
Tanh()
when None is passed.

forward
(x, mode='discriminator', feature_matching=False)[source]¶ Calculates the output tensor on passing the image
x
through the Discriminator.Parameters:  x (torch.Tensor) – A 4D torch tensor of the image.
 mode (str, optional) – Option to choose the mode of the ACGANDiscriminator. Setting it to ‘discriminator’ gives the probability of the image being fake/real, ‘classifier’ allows it to make a prediction about the class of the image and anything else leads to returning both the values.
 feature_matching (bool, optional) – Returns the activation from a predefined intermediate layer.
Returns: A 1D torch.Tensor of the probability of each image being real.