vegans.models.unconditional package¶
Submodules¶
vegans.models.unconditional.AAE module¶
AAE¶
Implements 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: Binary cross-entropy + Mean-squared error
Generator: Mean-squared error
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
-
class
vegans.models.unconditional.AAE.
AAE
(generator, adversary, encoder, x_dim, z_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/AAE', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGenerativeModel.AbstractGenerativeModel
- 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.
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.
vegans.models.unconditional.AbstractGAN1v1 module¶
-
class
vegans.models.unconditional.AbstractGAN1v1.
AbstractGAN1v1
(generator, adversary, x_dim, z_dim, adv_type, optim=None, optim_kwargs=None, feature_layer=None, fixed_noise_size=32, device=None, folder=None, ngpu=0, secure=True, _called_from_conditional=False)[source]¶ Bases:
vegans.models.unconditional.AbstractGenerativeModel.AbstractGenerativeModel
Abstract class for GAN with structure of one generator and one discriminator / critic. Examples are the original VanillaGAN, WassersteinGAN and 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.
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, 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.unconditional.AbstractGANGAE module¶
-
class
vegans.models.unconditional.AbstractGANGAE.
AbstractGANGAE
(generator, adversary, encoder, x_dim, z_dim, optim=None, optim_kwargs=None, adv_type='Discriminator', feature_layer=None, fixed_noise_size=32, device=None, folder=None, ngpu=0, secure=True, _called_from_conditional=False)[source]¶ Bases:
vegans.models.unconditional.AbstractGenerativeModel.AbstractGenerativeModel
Abstract class for GAN with structure of one generator, one discriminator / critic and one encoder. Examples are the LRGAN, VAEGAN and 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.
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, 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.unconditional.AbstractGenerativeModel module¶
-
class
vegans.models.unconditional.AbstractGenerativeModel.
AbstractGenerativeModel
(x_dim, z_dim, optim, optim_kwargs, feature_layer, fixed_noise_size, device, ngpu, folder, secure)[source]¶ Bases:
abc.ABC
The AbstractGenerativeModel is the most basic building block of vegans. All GAN implementation should at least inherit from this class. If a conditional version is implemented look at AbstractConditionalGenerativeModel.
- 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.
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.
-
_build_images
(images, labels=None)[source]¶ Build matplotlib figure containing all images.
- Parameters
images (torch.Tensor()) – A torch Tensor containing the images to be plotted by matplotlib.
- Returns
fig, axs – Objects containing the figure as well as all separate axis objects.
- Return type
plt.Figure, np.array
-
_calculate_feature_loss
(X_real, X_fake)[source]¶ Calculates feature loss if self.feature_layer is not None.
Every network takes the feature_layer argument in its constructor. If it is not None, a layer of the discriminator / critic should be specified. A feature loss will be calculated which is the MSE between the output for real and fake samples of the specified self.feature_layer.
- Parameters
X_real (torch.Tensor) – Real samples.
X_fake (torch.Tensor) – Fake samples.
-
_check_dict_keys
(param_dict, where)[source]¶ Checks if param_dict has the correct form.
The correct form is {“Network1”: value1, “Network2”: value2, …} for every network. This utility function checks if every network in self.neural_nets has an entry in param_dict and if every entry in param_dict exits in self.neural_nets.
- Parameters
param_dict (TYPE) – Parameter dictionary
where (TYPE) – Specifies the location from which the function is called for meaningful KeyError.
- Raises
KeyError – If param_dict is not of the specified form.
-
_create_steps
(steps)[source]¶ Creates the self.steps dictionary.
The created dictionary must be of the form {“Network1”: steps1, “Network2”: steps2, …}. During training “Network1” will be trained for steps1 steps, and so on. Functionality similar to self_define_optimizers().
- Parameters
steps (None, dict) – If not None a dictionary of the form {“Network1”: steps1, “Network2”: steps2, …} is expected. This dictionary might also be partially filled.
-
_define_optimizers
(optim, optim_kwargs)[source]¶ Define the optimizers dictionary.
After this step self.optimizers must exist and be of the form {“Network1”: optimizer1, “Network2”: optimizer2, …}
- Parameters
optim (torch.optim or dict) – If dict, must be of form {“Network1”: torch.optim1, “Network2”: torch.optim2, …}. If one network is not specified as a key of the dictionary the default optimizer (self._default_optimizer()) will be fetched.
optim_kwargs (dict) – If dict, must be of form {“Network1”: optim_kwargs1, “Network2”: optim_kwargs1, …}. If one network is not specified as a key of the dictionary no arguments = {} are passed.
-
_get_batch
(X_train, y_train, X_test, y_test, batch_size)[source]¶ Returns the first batch of the training and testing data for the consistency checks.
-
_set_up_data
(X_train, y_train, X_test, y_test, batch_size)[source]¶ If X_train / X_test are not data loaders, create them.
Also asserts their input shapes for consistency.
-
_set_up_saver
(print_every, save_model_every, save_images_every, save_losses_every, nr_batches)[source]¶ Calculates saving indicators if strings are passed. Additionally corresponding folders are created in self.folder.
- Returns
- Return type
Returns all saving indicators as integers.
-
_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.
-
_string_to_batchnr
(log_string, nr_batches, name)[source]¶ Transforms string of the form “0.2e” into 0.2 and performs basic sanity checks.
-
_summarise_batch
(batch, max_batches, epoch, max_epochs, print_every)[source]¶ Print summary statistics after a specified amount of batches.
- Parameters
batch (int) – Current batch.
max_batches (int) – Maximum number of total batches.
epoch (int) – Current epoch.
max_epochs (int) – Maximum number of total epochs.
print_every (int) – After how many batches the summary should be printed.
-
fit
(X_train, X_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 or torch.utils.data.DataLoader) – Training data for the generative network. Usually images.
X_test (np.array, optional) – Testing data for the generative network. Must have same shape as X_train.
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
(z=None, n=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
-
get_hyperparameters
()[source]¶ Returns a dictionary containing all relevant hyperparameters.
- Returns
Dictionary containing all relevant hyperparameters.
- Return type
dict
-
get_losses
(by_epoch=False, agg=None)[source]¶ Get losses logged during training
- 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
-
get_number_params
()[source]¶ Returns the number of parameters in the model.
- Returns
Dictionary containing the number of parameters per network.
- Return type
dict
-
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
samples (np.array) – Images produced with the final model for the fixed_noise.
losses_dict (dict) – Dictionary containing all loss types logged during training
-
static
load
(path)[source]¶ Load an already trained model.
- Parameters
path (TYPE) – path to the saved file.
- Returns
Trained model
- Return type
-
predict
(x)[source]¶ Use the critic / discriminator to predict if input is real / fake.
- Parameters
x (np.array) – Images or samples to be predicted.
- Returns
Array with one output per x indicating the realness of an input.
- Return type
np.array
-
sample
(n)[source]¶ Sample from the latent distribution.
- Parameters
n (int) – Number of samples drawn from the latent distribution.
- Returns
Random numbers with shape of [n, *z_dim]
- Return type
torch.tensor
-
save
(name=None)[source]¶ Saves model in the model folder as torch / pickle object.
- Parameters
name (str, optional) – name of the saved file. folder specified in the constructor used in absolute path.
vegans.models.unconditional.BicycleGAN module¶
BicycleGAN¶
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
-
class
vegans.models.unconditional.BicycleGAN.
BicycleGAN
(generator, adversary, encoder, x_dim, z_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/BicycleGAN', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGANGAE.AbstractGANGAE
- 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.
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.unconditional.EBGAN module¶
EBGAN¶
Implements 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
-
class
vegans.models.unconditional.EBGAN.
EBGAN
(generator, adversary, x_dim, z_dim, optim=None, optim_kwargs=None, m=None, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/EBGAN', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGAN1v1.AbstractGAN1v1
- 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.
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.unconditional.InfoGAN module¶
InfoGAN¶
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
-
class
vegans.models.unconditional.InfoGAN.
InfoGAN
(generator, adversary, encoder, x_dim, z_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/InfoGAN', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGenerativeModel.AbstractGenerativeModel
- 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.
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.
vegans.models.unconditional.KLGAN module¶
KLGAN¶
Implements 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
-
class
vegans.models.unconditional.KLGAN.
KLGAN
(generator, adversary, x_dim, z_dim, optim=None, optim_kwargs=None, eps=1e-05, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/KLGAN', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGAN1v1.AbstractGAN1v1
- 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.
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}}.
eps (float) – Small value preventing overflow and nans when calculating the Kullback-Leibler divergence.
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.unconditional.LRGAN module¶
LRGAN¶
Implements 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
-
class
vegans.models.unconditional.LRGAN.
LRGAN
(generator, adversary, encoder, x_dim, z_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/LRGAN', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGANGAE.AbstractGANGAE
- 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.
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.unconditional.LSGAN module¶
LSGAN¶
Implements 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
-
class
vegans.models.unconditional.LSGAN.
LSGAN
(generator, adversary, x_dim, z_dim, optim=None, optim_kwargs=None, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/LSGAN', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGAN1v1.AbstractGAN1v1
- 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.
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.unconditional.VAEGAN module¶
VAEGAN¶
Implements 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
-
class
vegans.models.unconditional.VAEGAN.
VAEGAN
(generator, adversary, encoder, x_dim, z_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/VAEGAN', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGANGAE.AbstractGANGAE
- 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.
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.unconditional.VanillaGAN module¶
VanillaGAN¶
Implements 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
-
class
vegans.models.unconditional.VanillaGAN.
VanillaGAN
(generator, adversary, x_dim, z_dim, optim=None, optim_kwargs=None, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/VanillaGAN', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGAN1v1.AbstractGAN1v1
- 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.
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.unconditional.VanillaVAE module¶
VanillaVAE¶
Implements 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
-
class
vegans.models.unconditional.VanillaVAE.
VanillaVAE
(encoder, decoder, x_dim, z_dim, optim=None, optim_kwargs=None, lambda_KL=10, fixed_noise_size=32, device=None, ngpu=0, folder='./veganModels/VanillaVAE', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGenerativeModel.AbstractGenerativeModel
- 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.
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.
vegans.models.unconditional.WassersteinGAN module¶
WassersteinGAN¶
Implements 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
-
class
vegans.models.unconditional.WassersteinGAN.
WassersteinGAN
(generator, adversary, x_dim, z_dim, optim=None, optim_kwargs=None, clip_val=0.01, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/WassersteinGAN', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGAN1v1.AbstractGAN1v1
- 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.
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.unconditional.WassersteinGANGP module¶
WassersteinGANGP¶
Implements 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
-
class
vegans.models.unconditional.WassersteinGANGP.
WassersteinGANGP
(generator, adversary, x_dim, z_dim, optim=None, optim_kwargs=None, lmbda_grad=10, feature_layer=None, fixed_noise_size=32, device=None, ngpu=None, folder='./veganModels/WassersteinGANGP', secure=True)[source]¶ Bases:
vegans.models.unconditional.AbstractGAN1v1.AbstractGAN1v1
- 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.
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.