vegans.models.conditional package

Submodules

vegans.models.conditional.AbstractConditionalGAN1v1 module

class vegans.models.conditional.AbstractConditionalGAN1v1.AbstractConditionalGAN1v1(generator, adversary, x_dim, z_dim, y_dim, adv_type, optim=None, optim_kwargs=None, feature_layer=None, fixed_noise_size=32, device=None, ngpu=0, folder='./veganModels/cAbstractGAN1v1', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGenerativeModel.AbstractConditionalGenerativeModel, vegans.models.unconditional.AbstractGAN1v1.AbstractGAN1v1

Abstract class for conditional GAN with structure of one generator and one discriminator / critic. Examples are the original ConditionalVanillaGAN, ConditionalWassersteinGAN and ConditionalWassersteinGANGP.

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

calculate_losses(X_batch, Z_batch, y_batch, who=None)[source]

Calculates the losses for GANs using a 1v1 architecture.

This method is called within the AbstractGenerativeModel main fit() loop.

Parameters
  • X_batch (torch.Tensor) – Current x batch.

  • Z_batch (torch.Tensor) – Current z batch.

  • who (None, optional) – Name of the network that should be trained.

vegans.models.conditional.AbstractConditionalGANGAE module

class vegans.models.conditional.AbstractConditionalGANGAE.AbstractConditionalGANGAE(generator, adversary, encoder, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, adv_type='Discriminator', feature_layer=None, fixed_noise_size=32, device=None, folder='./veganModels/cAbstractConditionalGANGAE', ngpu=0, secure=True, _called_from_conditional=False)[source]

Bases: vegans.models.conditional.AbstractConditionalGenerativeModel.AbstractConditionalGenerativeModel, vegans.models.unconditional.AbstractGANGAE.AbstractGANGAE

Abstract class for conditional GAN with structure of one generator, one discriminator / critic and one encoder. Examples are the ConditionalLRGAN, ConditionalVAEGAN and ConditionalBicycleGAN.

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • encoder (nn.Module) – Encoder architecture. Produces predictions in the latent space.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • lambda_grad (float) – Weight for the reconstruction loss of the gradients. Pushes the norm of the gradients to 1.

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

calculate_losses(X_batch, Z_batch, y_batch, who=None)[source]

Calculates the losses for GANs using a 1v1 architecture.

This method is called within the AbstractGenerativeModel main fit() loop.

Parameters
  • X_batch (torch.Tensor) – Current x batch.

  • Z_batch (torch.Tensor) – Current z batch.

  • who (None, optional) – Name of the network that should be trained.

encode(x, y=None)[source]

vegans.models.conditional.AbstractConditionalGenerativeModel module

class vegans.models.conditional.AbstractConditionalGenerativeModel.AbstractConditionalGenerativeModel(x_dim, z_dim, y_dim, optim, optim_kwargs, feature_layer, fixed_noise_size, device, ngpu, folder, secure)[source]

Bases: vegans.models.unconditional.AbstractGenerativeModel.AbstractGenerativeModel

The AbstractConditionalGenerativeModel is the most basic building block of vegans for conditional models. All conditional GAN implementation should at least inherit from this class.

Parameters
  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

_set_up_training(X_train, y_train, X_test, y_test, epochs, batch_size, steps, print_every, save_model_every, save_images_every, save_losses_every, enable_tensorboard)[source]

Create the dataloaders, SummaryWriters for tensorboard and transform the saving indicators.

This function creates all data needed during training like the data loaders and save steps. It also creates the hyperparameter dictionary and the steps dictionary.

concatenate(tensor1, tensor2)[source]

Concatenates two tensors appropriately depending on their shape.

Tensor1 and Tensor2 can either have 2 or 4 dimensions.

Parameters
  • tensor1 (torch.Tensor) – First tensor.

  • tensor2 (torch.Tensor) – Second tensor.

Returns

Concatenated tensor.

Return type

torch.Tensor

fit(X_train, y_train, X_test=None, y_test=None, epochs=5, batch_size=32, steps=None, print_every='1e', save_model_every=None, save_images_every=None, save_losses_every='1e', enable_tensorboard=False)[source]

Trains the model, iterating over all contained networks.

Parameters
  • X_train (np.array) – Training data for the generative adversarial network. Usually images.

  • y_train (np.array) – Training labels for the generative adversarial network. Might be images or one-hot encoded vector.

  • X_test (np.array, optional) – Testing data for the generative adversarial network. Must have same shape as X_train.

  • y_train – Testing labels for the generative adversarial network. Might be images or one-hot encoded vector.

  • epochs (int, optional) – Number of epochs (passes over the training data set) performed during training.

  • batch_size (int, optional) – Batch size used when creating the data loader from X_train. Ignored if torch.utils.data.DataLoader is passed for X_train.

  • steps (dict, optional) – Dictionary with names of the networks to indicate how often they should be trained, i.e. {“Generator”: 5} indicates that the generator is trained 5 times while all other networks are trained once.

  • print_every (int, string, optional) – Indicates after how many batches the losses for the train data should be printed to the console. Can also be a string of the form “0.25e” (4 times per epoch), “1e” (once per epoch) or “3e” (every third epoch).

  • save_model_every (int, string, optional) – Indicates after how many batches the model should be saved. Can also be a string of the form “0.25e” (4 times per epoch), “1e” (once per epoch) or “3e” (every third epoch).

  • save_images_every (int, string, optional) – Indicates after how many batches the images for the losses and fixed_noise should be saved. Can also be a string of the form “0.25e” (4 times per epoch), “1e” (once per epoch) or “3e” (every third epoch).

  • save_losses_every (int, string, optional) – Indicates after how many batches the losses for the train and test data should be calculated. Can also be a string of the form “0.25e” (4 times per epoch), “1e” (once per epoch) or “3e” (every third epoch).

  • enable_tensorboard (bool, optional) – Flag to indicate whether subdirectory folder/tensorboard should be created to log losses and images.

generate(y=None, z=None)[source]

Generate output with generator.

Parameters
  • y (np.array) – Labels for outputs to be produced.

  • z (None, optional) – Latent input vector to produce an output from.

Returns

Output produced by generator.

Return type

np.array

get_fixed_labels()[source]
get_training_results(by_epoch=False, agg=None)[source]

Call after training to get fixed_noise samples and losses.

Parameters
  • by_epoch (bool, optional) – If true one loss value per epoch is returned for every logged_loss. Otherwise frequency is given by save_losses_every argument of fit, i.e. save_losses_every=10 saves losses every 10th batch, save_losses_every=”0.25e saves losses 4 times per epoch.

  • agg (None, optional) – Aggregation function used if by_epoch is true, otherwise ignored. Default is np.mean for all batches in one epoch.

Returns

losses_dict – Dictionary containing all loss types logged during training

Return type

dict

predict(x, y=None)[source]

Use the critic / discriminator to predict if input is real / fake.

Parameters
  • x (np.array) – Images or samples to be predicted.

  • y (np.array) – Labels for outputs to be predicted.

Returns

Array with one output per x indicating the realness of an input.

Return type

np.array

vegans.models.conditional.ConditionalAAE module

ConditionalAAE

Implements the conditional variant of the Adversarial Autoencoder[1].

Instead of using the Kullback Leibler divergence to improve the latent space distribution we use a discriminator to determine the “realness” of the latent vector.

Losses:
  • Encoder: Kullback-Leibler

  • Decoder: Binary cross-entropy

  • Adversary: Binary cross-entropy

Default optimizer:
  • torch.optim.Adam

Custom parameter:
  • lambda_z: Weight for the discriminator loss computing the realness of the latent z dimension.

References

1

https://arxiv.org/pdf/1511.05644.pdf

class vegans.models.conditional.ConditionalAAE.ConditionalAAE(generator, adversary, encoder, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, lambda_z=10, adv_type='Discriminator', feature_layer=None, fixed_noise_size=32, device=None, ngpu=0, folder='./veganModels/cAAE', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGenerativeModel.AbstractConditionalGenerativeModel, vegans.models.unconditional.AAE.AAE

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • encoder (nn.Module) – Encoder architecture. Produces predictions in the latent space.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • lambda_z (float) – Weight for the discriminator loss computing the realness of the latent z dimension.

  • adv_type ("Discriminator", "Critic" or "Autoencoder") – Indicating which adversarial architecture will be used.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

calculate_losses(X_batch, Z_batch, y_batch, who=None)[source]
encode(x, y=None)[source]

vegans.models.conditional.ConditionalBicycleGAN module

ConditionalBicycleGAN

Implements the BicycleGAN[1], a combination of the VAEGAN and the LRGAN.

It utilizes both steps of the Variational Autoencoder (Kullback-Leibler Loss) and uses the same encoder architecture for the latent regression of generated images.

Losses:
  • Generator: Binary cross-entropy + L1-latent-loss + L1-reconstruction-loss

  • Discriminator: Binary cross-entropy

  • Encoder: Kullback-Leibler Loss + L1-latent-loss + L1-reconstruction-loss

Default optimizer:
  • torch.optim.Adam

Custom parameter:
  • lambda_KL: Weight for the encoder loss computing the Kullback-Leibler divergence in the latent space.

  • lambda_x: Weight for the reconstruction loss of the real x dimensions.

  • lambda_z: Weight for the reconstruction loss of the latent z dimensions.

References

1

https://arxiv.org/pdf/1711.11586.pdf

class vegans.models.conditional.ConditionalBicycleGAN.ConditionalBicycleGAN(generator, adversary, encoder, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, lambda_KL=10, lambda_x=10, lambda_z=10, adv_type='Discriminator', feature_layer=None, fixed_noise_size=32, device=None, ngpu=0, folder='./veganModels/cBicycleGAN', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGANGAE.AbstractConditionalGANGAE, vegans.models.unconditional.BicycleGAN.BicycleGAN

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • encoder (nn.Module) – Encoder architecture. Produces predictions in the latent space.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • lambda_KL (float) – Weight for the encoder loss computing the Kullback-Leibler divergence in the latent space.

  • lambda_x (float) – Weight for the reconstruction loss of the real x dimensions.

  • lambda_z (float) – Weight for the reconstruction loss of the latent z dimensions.

  • adv_type ("Discriminator", "Critic" or "Autoencoder") – Indicating which adversarial architecture will be used.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

vegans.models.conditional.ConditionalCycleGAN module

CycleGAN

Implements the CycleGAN[1], a method for unpaired image to image translation tasks.

It consists of two generative adversarial network. one responsible for mapping input from space X to space Y. The other produces output in space X from space Y.

Losses:
  • GeneratorX_Y and GeneratorY_X: Binary cross-entropy + cycle consistency

  • DiscriminatorX_Y and DiscriminatorY_X: Binary cross-entropy

Default optimizer:
  • torch.optim.Adam

Custom parameter:
  • lambda_x: Weight for the reconstruction loss of the real x dimensions.

References

1

https://arxiv.org/abs/1703.10593

class vegans.models.conditional.ConditionalCycleGAN.ConditionalCycleGAN(generatorX_Y, adversaryX_Y, generatorY_X, adversaryY_X, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, lambda_x=10, adv_type='Discriminator', fixed_noise_size=32, device=None, ngpu=0, folder='./veganModels/cCycleGAN', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGenerativeModel.AbstractConditionalGenerativeModel

Parameters
  • generatorX_Y (nn.Module) – Generator architecture. Produces output in the real space Y from input space X.

  • adversaryX_Y (nn.Module) – Adversary architecture. Produces predictions for real and fake samples in space Y to differentiate them.

  • generatorY_X (nn.Module) – Generator architecture. Produces output in the real space X from input space Y.

  • adversaryY_X (nn.Module) – Adversary architecture. Produces predictions for real and fake samples in space X to differentiate them.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • lambda_x (float) – Weight for the reconstruction loss of the real x dimensions.

  • adv_type ("Discriminator", "Critic" or "Autoencoder") – Indicating which adversarial architecture will be used.

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

calculate_losses(X_batch, Z_batch, y_batch, who=None)[source]
generate(y, z=None, who='GeneratorX_Y')[source]

Generate output with generator.

Parameters
  • y (np.array) – Labels for outputs to be produced.

  • z (None, optional) – Latent input vector to produce an output from.

Returns

Output produced by generator.

Return type

np.array

predict(x, y, who='AdversaryX_Y')[source]

Use the critic / discriminator to predict if input is real / fake.

Parameters
  • x (np.array) – Images or samples to be predicted.

  • y (np.array) – Labels for outputs to be predicted.

Returns

Array with one output per x indicating the realness of an input.

Return type

np.array

vegans.models.conditional.ConditionalEBGAN module

ConditionalEBGAN

Implements conditional variant of the Energy based GAN[1].

Uses an auto-encoder as the adversary structure.

Losses:
  • Generator: L2 (Mean Squared Error)

  • Autoencoder: L2 (Mean Squared Error)

Default optimizer:
  • torch.optim.Adam

Custom parameter:
  • m: Cut off for the hinge loss. Look at reference for more information.

References

1

https://arxiv.org/pdf/1609.03126.pdf

class vegans.models.conditional.ConditionalEBGAN.ConditionalEBGAN(generator, adversary, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, m=None, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/cEBGAN', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGAN1v1.AbstractConditionalGAN1v1, vegans.models.unconditional.EBGAN.EBGAN

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • encoder (nn.Module) – Encoder architecture. Produces predictions in the latent space.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • m (float) – Cut off for the hinge loss. Look at reference for more information.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

vegans.models.conditional.ConditionalInfoGAN module

ConditionalInfoGAN

Implements the InfoGAN[1].

It introduces an encoder network which maps the generator output back to the latent input space. This should help to prevent mode collapse and improve image variety.

Losses:
  • Generator: Binary cross-entropy + Normal Log-Likelihood + Multinomial Log-Likelihood

  • Discriminator: Binary cross-entropy

  • Encoder: Normal Log-Likelihood + Multinomial Log-Likelihood

Default optimizer:
  • torch.optim.Adam

Custom parameter:
  • c_dim_discrete: Number of discrete multinomial dimensions (might be list of independent multinomial spaces).

  • c_dim_continuous: Number of continuous normal dimensions.

  • lambda_z: Weight for the reconstruction loss for the latent z dimensions.

References

1

https://dl.acm.org/doi/10.5555/3157096.3157340

class vegans.models.conditional.ConditionalInfoGAN.ConditionalInfoGAN(generator, adversary, encoder, x_dim, z_dim, y_dim, c_dim_discrete, c_dim_continuous, optim=None, optim_kwargs=None, lambda_z=10, feature_layer=None, fixed_noise_size=32, device=None, ngpu=0, folder='./veganModels/cInfoGAN', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGenerativeModel.AbstractConditionalGenerativeModel, vegans.models.unconditional.InfoGAN.InfoGAN

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • encoder (nn.Module) – Encoder architecture. Produces predictions in the latent space.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • c_dim_discrete (int, list) – Number of discrete multinomial dimensions (might be list of independent multinomial spaces).

  • c_dim_continuous (int) – Number of continuous normal dimensions.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • lambda_z (float) – Weight for the reconstruction loss for the latent z dimensions.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

calculate_losses(X_batch, Z_batch, y_batch, who=None)[source]
encode(x, y=None)[source]
generate(y, c=None, z=None)[source]

Generate output with generator / decoder.

Parameters
  • z (None, optional) – Latent input vector to produce an output from.

  • n (None, optional) – Number of outputs to be generated.

Returns

Output produced by generator / decoder.

Return type

np.array

vegans.models.conditional.ConditionalKLGAN module

ConditionalKLGAN

Implements the conditional variant of the Kullback Leibler GAN.

Uses the Kullback Leibler loss for the generator.

Losses:
  • Generator: Kullback-Leibler

  • Autoencoder: Binary cross-entropy

Default optimizer:
  • torch.optim.Adam

Custom parameter:
  • eps: Small value preventing overflow and nans when calculating the Kullback-Leibler divergence.

References

1

https://www.inference.vc/an-alternative-update-rule-for-generative-adversarial-networks/

class vegans.models.conditional.ConditionalKLGAN.ConditionalKLGAN(generator, adversary, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, eps=1e-05, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/cKLGAN', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGAN1v1.AbstractConditionalGAN1v1, vegans.models.unconditional.KLGAN.KLGAN

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

vegans.models.conditional.ConditionalLRGAN module

ConditionalLRGAN

Implements the conditional variant of the latent regressor GAN well described in the BicycleGAN paper[1].

It introduces an encoder network which maps the generator output back to the latent input space. This should help to prevent mode collapse and improve image variety.

Losses:
  • Generator: Binary cross-entropy + L1-latent-loss (Mean Absolute Error)

  • Discriminator: Binary cross-entropy

  • Encoder: L1-latent-loss (Mean Absolute Error)

Default optimizer:
  • torch.optim.Adam

Custom parameter:
  • lambda_z: Weight for the reconstruction loss for the latent z dimensions.

References

1

https://arxiv.org/pdf/1711.11586.pdf

class vegans.models.conditional.ConditionalLRGAN.ConditionalLRGAN(generator, adversary, encoder, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, lambda_z=10, adv_type='Discriminator', feature_layer=None, fixed_noise_size=32, device=None, ngpu=0, folder='./veganModels/cLRGAN', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGANGAE.AbstractConditionalGANGAE, vegans.models.unconditional.LRGAN.LRGAN

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • encoder (nn.Module) – Encoder architecture. Produces predictions in the latent space.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • lambda_z (float) – Weight for the reconstruction loss for the latent z dimensions.

  • adv_type ("Discriminator", "Critic" or "Autoencoder") – Indicating which adversarial architecture will be used.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

vegans.models.conditional.ConditionalLSGAN module

ConditionalLSGAN

Implements the conditional variant of the Least-Squares GAN[1].

Uses the L2 norm for evaluating the realness of real and fake images.

Losses:
  • Generator: L2 (Mean Squared Error)

  • Discriminator: L2 (Mean Squared Error)

Default optimizer:
  • torch.optim.Adam

References

1

https://openaccess.thecvf.com/content_ICCV_2017/papers/Mao_Least_Squares_Generative_ICCV_2017_paper.pdf

class vegans.models.conditional.ConditionalLSGAN.ConditionalLSGAN(generator, adversary, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/cLSGAN', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGAN1v1.AbstractConditionalGAN1v1, vegans.models.unconditional.LSGAN.LSGAN

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

vegans.models.conditional.ConditionalPix2Pix module

ConditionalPix2Pix

Implements the Pix2Pix GAN[1].

Uses the binary cross-entropy norm for evaluating the realness of real and fake images. Also enforces a L1 pixel wise penalty on the generated images.

Losses:
  • Generator: Binary cross-entropy + L1 (Mean Absolute Error)

  • Discriminator: Binary cross-entropy

Default optimizer:
  • torch.optim.Adam

Custom parameter:
  • lambda_x: Weight for the reconstruction loss for the real x dimensions.

References

1

https://arxiv.org/abs/1611.07004

class vegans.models.conditional.ConditionalPix2Pix.ConditionalPix2Pix(generator, adversary, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, lambda_x=10, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/cPix2Pix', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGAN1v1.AbstractConditionalGAN1v1

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • lambda_x (float) – Weight for the reconstruction loss of the real x dimensions.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

vegans.models.conditional.ConditionalVAEGAN module

ConditionalVAEGAN

Implements the conditional variant of the Variational Autoencoder Generative Adversarial Network[1].

Trains on Kullback-Leibler loss for the latent space and attaches a adversary to get better quality output. The Decoder acts as the generator.

Losses:
  • Encoder: Kullback-Leibler

  • Generator / Decoder: Binary cross-entropy

  • Adversary: Binary cross-entropy

Default optimizer:
  • torch.optim.Adam

Custom parameter:
  • lambda_KL: Weight for the encoder loss computing the Kullback-Leibler divergence in the latent space.

  • lambda_x: Weight for the reconstruction loss of the real x dimensions.

References

1

https://arxiv.org/pdf/1512.09300.pdf

class vegans.models.conditional.ConditionalVAEGAN.ConditionalVAEGAN(generator, adversary, encoder, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, lambda_KL=10, lambda_x=10, adv_type='Discriminator', feature_layer=None, fixed_noise_size=32, device=None, ngpu=0, folder='./veganModels/cVAEGAN', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGANGAE.AbstractConditionalGANGAE, vegans.models.unconditional.VAEGAN.VAEGAN

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • encoder (nn.Module) – Encoder architecture. Produces predictions in the latent space.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • lambda_KL (float) – Weight for the encoder loss computing the Kullback-Leibler divergence in the latent space.

  • lambda_x (float) – Weight for the reconstruction loss of the real x dimensions.

  • adv_type ("Discriminator", "Critic" or "Autoencoder") – Indicating which adversarial architecture will be used.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

vegans.models.conditional.ConditionalVanillaGAN module

ConditionalVanillaGAN

Implements the conditional variant of the original Generative adversarial network[1].

Uses the binary cross-entropy for evaluating the realness of real and fake images. The discriminator tries to output 1 for real images and 0 for fake images, whereas the generator tries to force the discriminator to output 1 for fake images.

Losses:
  • Generator: Binary cross-entropy

  • Discriminator: Binary cross-entropy

Default optimizer:
  • torch.optim.Adam

References

1

https://papers.nips.cc/paper/2014/file/5ca3e9b122f61f8f06494c97b1afccf3-Paper.pdf

class vegans.models.conditional.ConditionalVanillaGAN.ConditionalVanillaGAN(generator, adversary, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/cVanillaGAN', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGAN1v1.AbstractConditionalGAN1v1, vegans.models.unconditional.VanillaGAN.VanillaGAN

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

vegans.models.conditional.ConditionalVanillaVAE module

ConditionalVanillaVAE

Implements the conditional variant of the Variational Autoencoder[1].

Trains on Kullback-Leibler loss and mean squared error reconstruction loss.

Losses:
  • Encoder: Kullback-Leibler

  • Decoder: L2 (Mean Squared Error)

Default optimizer:
  • torch.optim.Adam

Custom parameter:
  • lambda_KL: Weight for the encoder loss computing the Kullback-Leibler divergence in the latent space.

References

1

https://arxiv.org/pdf/1906.02691.pdf

class vegans.models.conditional.ConditionalVanillaVAE.ConditionalVanillaVAE(encoder, decoder, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, lambda_KL=10, fixed_noise_size=32, device=None, ngpu=0, folder='./veganModels/cVanillaVAE', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGenerativeModel.AbstractConditionalGenerativeModel, vegans.models.unconditional.VanillaVAE.VanillaVAE

Parameters
  • encoder (nn.Module) – Encoder architecture. Produces predictions in the latent space.

  • decoder (nn.Module) – Decoder architecture. Produces output in the real space.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • lambda_KL (float) – Weight for the encoder loss computing the Kullback-Leibler divergence in the latent space.

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

calculate_losses(X_batch, Z_batch, y_batch, who=None)[source]
encode(x, y=None)[source]

vegans.models.conditional.ConditionalWassersteinGAN module

ConditionalWassersteinGAN

Implements the conditional variant of the Wasserstein GAN[1].

Uses the Wasserstein loss to determine the realness of real and fake images. The Wasserstein loss has several theoretical advantages over the Jensen-Shanon divergence minimised by the original GAN. In this architecture the critic (discriminator) is often trained multiple times for every generator step. Lipschitz continuity is “enforced” by weight clipping.

Losses:
  • Generator: Wasserstein

  • Critic: Wasserstein

Default optimizer:
  • torch.optim.RMSprop

Custom parameter:
  • clip_val: Clip value for the critic to maintain Lipschitz continuity.

References

1

https://export.arxiv.org/pdf/1701.07875

class vegans.models.conditional.ConditionalWassersteinGAN.ConditionalWassersteinGAN(generator, adversary, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, clip_val=0.01, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/cWassersteinGAN', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGAN1v1.AbstractConditionalGAN1v1, vegans.models.unconditional.WassersteinGAN.WassersteinGAN

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • clip_val (float) – Clip value for the critic to maintain Lipschitz continuity.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

vegans.models.conditional.ConditionalWassersteinGANGP module

ConditionalWassersteinGANGP

Implements the conditional variant of the Wasserstein GAN Gradient Penalized[1].

Uses the Wasserstein loss to determine the realness of real and fake images. The Wasserstein loss has several theoretical advantages over the Jensen-Shanon divergence minimised by the original GAN. In this architecture the critic (discriminator) is often trained multiple times for every generator step. Lipschitz continuity is “enforced” by gradient penalization.

Losses:
  • Generator: Wasserstein

  • Critic: Wasserstein + Gradient penalization

Default optimizer:
  • torch.optim.RMSprop

Custom parameter:
  • lambda_grad: Weight for the reconstruction loss of the gradients. Pushes the norm of the gradients to 1.

References

1

https://arxiv.org/abs/1704.00028

class vegans.models.conditional.ConditionalWassersteinGANGP.ConditionalWassersteinGANGP(generator, adversary, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, feature_layer=None, fixed_noise_size=32, lmbda_grad=10, device=None, ngpu=None, folder='./veganModels/cWassersteinGANGP', secure=True)[source]

Bases: vegans.models.conditional.AbstractConditionalGAN1v1.AbstractConditionalGAN1v1, vegans.models.unconditional.WassersteinGANGP.WassersteinGANGP

Parameters
  • generator (nn.Module) – Generator architecture. Produces output in the real space.

  • adversary (nn.Module) – Adversary architecture. Produces predictions for real and fake samples to differentiate them.

  • x_dim (list, tuple) – Number of the output dimensions of the generator and input dimension of the discriminator / critic. In the case of images this will be [nr_channels, nr_height_pixels, nr_width_pixels].

  • z_dim (int, list, tuple) – Number of the latent dimensions for the generator input. Might have dimensions of an image.

  • y_dim (int, list, tuple) – Number of dimensions for the target label. Might have dimensions of image for image to image translation, i.e. [nr_channels, nr_height_pixels, nr_width_pixels] or an integer representing a number of classes.

  • optim (dict or torch.optim) – Optimizer used for each network. Could be either an optimizer from torch.optim or a dictionary with network name keys and torch.optim as value, i.e. {“Generator”: torch.optim.Adam}.

  • optim_kwargs (dict) – Optimizer keyword arguments used for each network. Must be a dictionary with network name keys and dictionary with keyword arguments as value, i.e. {“Generator”: {“lr”: 0.0001}}.

  • lambda_grad (float) – Weight for the reconstruction loss of the gradients. Pushes the norm of the gradients to 1.

  • feature_layer (torch.nn.*) – Output layer used to compute the feature loss. Should be from either the discriminator or critic. If feature_layer is not None, the original generator loss is replaced by a feature loss, introduced [here](https://arxiv.org/abs/1606.03498v1).

  • fixed_noise_size (int) – Number of images shown when logging. The fixed noise is used to produce the images in the folder/images subdirectory, the tensorboard images tab and the samples in get_training_results().

  • device (string) – Device used while training the model. Either “cpu” or “cuda”.

  • ngpu (int) – Number of gpus used during training if device == “cuda”.

  • folder (string) – Creates a folder in the current working directory with this name. All relevant files like summary, images, models and tensorboard output are written there. Existing folders are never overwritten or deleted. If a folder with the same name already exists a time stamp is appended to make it unique.

Module contents