simplegan.gan

The gan sub-package provdies various generative adversarial networks that can be trained off the shelf with minimal fuss.

Vanilla GAN

class simplegan.gan.VanillaGAN(noise_dim=64, dropout_rate=0.4, gen_units=[128, 256, 512], disc_units=[512, 256, 128], activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=None, gen_path=None, disc_path=None)[source]

Vanilla GAN model

Parameters:
  • noise_dim (int, optional) – represents the dimension of the prior to sample values. Defaults to 64
  • dropout_rate (float, optional) – represents the amount of dropout regularization to be applied. Defaults to 0.4
  • gen_units (int, list, optional) – represents the number of units/neurons in the generator network. Defaults to [128, 256, 512]
  • disc_units (int, list, optional) – represents the number of units/neurons in the discriminator network. Defaults to [512, 256, 128]`
  • activation (str, optional) – type of non-linearity to be applied. Defaults to relu
  • kernel_initializer (str, optional) – initialization of kernel weights. Defaults to glorot_uniform
  • kernel_regularizer (str, optional) – type of regularization to be applied to the weights. Defaults to None
  • gen_path (str, optional) – path to generator checkpoint to load model weights. Defaults to None
  • disc_path (str, optional) – path to discriminator checkpoint to load model weights. Defaults to None
discriminator()[source]

Discriminator module for Vanilla GAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
fit(train_ds=None, epochs=100, gen_optimizer='Adam', disc_optimizer='Adam', verbose=1, gen_learning_rate=0.0001, disc_learning_rate=0.0001, tensorboard=False, save_model=None)[source]

Function to train the model

Parameters:
  • train_ds (tf.data object) – training data
  • epochs (int, optional) – number of epochs to train the model. Defaults to 100
  • gen_optimizer (str, optional) – optimizer used to train generator. Defaults to Adam
  • disc_optimizer (str, optional) – optimizer used to train discriminator. Defaults to Adam
  • verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to 1
  • gen_learning_rate (float, optional) – learning rate of the generator optimizer. Defaults to 0.0001
  • disc_learning_rate (float, optional) – learning rate of the discriminator optimizer. Defaults to 0.0001
  • tensorboard (bool, optional) – if true, writes loss values to logs/gradient_tape directory which aids visualization. Defaults to False
  • save_model (str, optional) – Directory to save the trained model. Defaults to None
generate_samples(n_samples=1, save_dir=None)[source]

Generate samples using the trained model

Parameters:
  • n_samples (int, optional) – number of samples to generate. Defaults to 1
  • save_dir (str, optional) – directory to save the generated images. Defaults to None
Returns:

returns None if save_dir is not None, otherwise returns a numpy array with generated samples

generator()[source]

Generator module for Vanilla GAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
get_sample(data=None, n_samples=1, save_dir=None)[source]

View sample of the data

Parameters:
  • data (tf.data object) – dataset to load samples from
  • n_samples (int, optional) – number of samples to load. Defaults to 1
  • save_dir (str, optional) – directory to save the sample images. Defaults to None
Returns:

None if save_dir is not None, otherwise returns numpy array of samples with shape (n_samples, img_shape)

load_data(data_dir=None, use_mnist=False, use_cifar10=False, batch_size=32, img_shape=(64, 64))[source]

Load data to train the model

Parameters:
  • data_dir (str, optional) – string representing the directory to load data from. Defaults to None
  • use_mnist (bool, optional) – use the MNIST dataset to train the model. Defaults to False
  • use_cifar10 (bool, optional) – use the CIFAR10 dataset to train the model. Defaults to False
  • batch_size (int, optional) – mini batch size for training the model. Defaults to 32
  • img_shape (int, tuple, optional) – shape of the image when loading data from custom directory. Defaults to (64, 64)
Returns:

a tensorflow dataset objects representing the training datset

DCGAN

class simplegan.gan.DCGAN(noise_dim=100, dropout_rate=0.4, gen_channels=[64, 32, 16], disc_channels=[16, 32, 64], kernel_size=(5, 5), activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=None, gen_path=None, disc_path=None)[source]

DCGAN model

Parameters:
  • noise_dim (int, optional) – represents the dimension of the prior to sample values. Defaults to 100
  • dropout_rate (float, optional) – represents the amount of dropout regularization to be applied. Defaults to 0.4
  • gen_channels (int, list, optional) – represents the number of filters in the generator network. Defaults to [64, 32, 16]
  • disc_channels (int, list, optional) – represents the number of filters in the discriminator network. Defaults to [16, 32, 64]`
  • kernel_size (int, tuple, optional) – repersents the size of the kernel to perform the convolution. Defaults to (5, 5)
  • activation (str, optional) – type of non-linearity to be applied. Defaults to relu
  • kernel_initializer (str, optional) – initialization of kernel weights. Defaults to glorot_uniform
  • kernel_regularizer (str, optional) – type of regularization to be applied to the weights. Defaults to None
  • gen_path (str, optional) – path to generator checkpoint to load model weights. Defaults to None
  • disc_path (str, optional) – path to discriminator checkpoint to load model weights. Defaults to None
discriminator()[source]

Discriminator module for DCGAN and WGAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
fit(train_ds=None, epochs=100, gen_optimizer='Adam', disc_optimizer='Adam', verbose=1, gen_learning_rate=0.0001, disc_learning_rate=0.0002, beta_1=0.5, tensorboard=False, save_model=None)[source]

Function to train the model

Parameters:
  • train_ds (tf.data object) – training data
  • epochs (int, optional) – number of epochs to train the model. Defaults to 100
  • gen_optimizer (str, optional) – optimizer used to train generator. Defaults to Adam
  • disc_optimizer (str, optional) – optimizer used to train discriminator. Defaults to Adam
  • verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to 1
  • gen_learning_rate (float, optional) – learning rate of the generator optimizer. Defaults to 0.0001
  • disc_learning_rate (float, optional) – learning rate of the discriminator optimizer. Defaults to 0.0002
  • beta_1 (float, optional) – decay rate of the first momement. set if Adam optimizer is used. Defaults to 0.5
  • tensorboard (bool, optional) – if true, writes loss values to logs/gradient_tape directory which aids visualization. Defaults to False
  • save_model (str, optional) – Directory to save the trained model. Defaults to None
generate_samples(n_samples=1, save_dir=None)[source]

Generate samples using the trained model

Parameters:
  • n_samples (int, optional) – number of samples to generate. Defaults to 1
  • save_dir (str, optional) – directory to save the generated images. Defaults to None
Returns:

returns None if save_dir is not None, otherwise returns a numpy array with generated samples

generator()[source]

Generator module for DCGAN and WGAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
get_sample(data=None, n_samples=1, save_dir=None)[source]

View sample of the data

Parameters:
  • data (tf.data object) – dataset to load samples from
  • n_samples (int, optional) – number of samples to load. Defaults to 1
  • save_dir (str, optional) – directory to save the sample images. Defaults to None
Returns:

None if save_dir is not None, otherwise returns numpy array of samples with shape (n_samples, img_shape)

load_data(data_dir=None, use_mnist=False, use_cifar10=False, use_cifar100=False, use_lsun=False, batch_size=32, img_shape=(64, 64))[source]

Load data to train the model

Parameters:
  • data_dir (str, optional) – string representing the directory to load data from. Defaults to None
  • use_mnist (bool, optional) – use the MNIST dataset to train the model. Defaults to False
  • use_cifar10 (bool, optional) – use the CIFAR10 dataset to train the model. Defaults to False
  • use_cifar100 (bool, optional) – use the CIFAR100 dataset to train the model. Defaults to False
  • use_lsun (bool, optional) – use the LSUN dataset to train the model. Defaults to False
  • batch_size (int, optional) – mini batch size for training the model. Defaults to 32
  • img_shape (int, tuple, optional) – shape of the image when loading data from custom directory. Defaults to (64, 64)
Returns:

a tensorflow dataset objects representing the training datset

Wasserstein GAN

class simplegan.gan.WGAN(noise_dim=100, dropout_rate=0.4, gen_channels=[64, 32, 16], disc_channels=[16, 32, 64], kernel_size=(5, 5), activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=None, gen_path=None, disc_path=None)[source]

WGAN model

Parameters:
  • noise_dim (int, optional) – represents the dimension of the prior to sample values. Defaults to 100
  • dropout_rate (float, optional) – represents the amount of dropout regularization to be applied. Defaults to 0.4
  • gen_channels (int, list, optional) – represents the number of filters in the generator network. Defaults to [64, 32, 16]
  • disc_channels (int, list, optional) – represents the number of filters in the discriminator network. Defaults to [16, 32, 64]`
  • kernel_size (int, tuple, optional) – repersents the size of the kernel to perform the convolution. Defaults to (5, 5)
  • activation (str, optional) – type of non-linearity to be applied. Defaults to relu
  • kernel_initializer (str, optional) – initialization of kernel weights. Defaults to glorot_uniform
  • kernel_regularizer (str, optional) – type of regularization to be applied to the weights. Defaults to None
  • gen_path (str, optional) – path to generator checkpoint to load model weights. Defaults to None
  • disc_path (str, optional) – path to discriminator checkpoint to load model weights. Defaults to None
discriminator()

Discriminator module for DCGAN and WGAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
fit(train_ds=None, epochs=100, gen_optimizer='RMSprop', disc_optimizer='RMSprop', verbose=1, gen_learning_rate=5e-05, disc_learning_rate=5e-05, beta_1=0.5, tensorboard=False, save_model=None)[source]

Function to train the model

Parameters:
  • train_ds (tf.data object) – training data
  • epochs (int, optional) – number of epochs to train the model. Defaults to 100
  • gen_optimizer (str, optional) – optimizer used to train generator. Defaults to RMSprop
  • disc_optimizer (str, optional) – optimizer used to train discriminator. Defaults to RMSprop
  • verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to 1
  • gen_learning_rate (float, optional) – learning rate of the generator optimizer. Defaults to 5e-5
  • disc_learning_rate (float, optional) – learning rate of the discriminator optimizer. Defaults to 5e-5
  • beta_1 (float, optional) – decay rate of the first momement. set if Adam optimizer is used. Defaults to 0.5
  • tensorboard (bool, optional) – if true, writes loss values to logs/gradient_tape directory which aids visualization. Defaults to False
  • save_model (str, optional) – Directory to save the trained model. Defaults to None
generate_samples(n_samples=1, save_dir=None)

Generate samples using the trained model

Parameters:
  • n_samples (int, optional) – number of samples to generate. Defaults to 1
  • save_dir (str, optional) – directory to save the generated images. Defaults to None
Returns:

returns None if save_dir is not None, otherwise returns a numpy array with generated samples

generator()

Generator module for DCGAN and WGAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
get_sample(data=None, n_samples=1, save_dir=None)

View sample of the data

Parameters:
  • data (tf.data object) – dataset to load samples from
  • n_samples (int, optional) – number of samples to load. Defaults to 1
  • save_dir (str, optional) – directory to save the sample images. Defaults to None
Returns:

None if save_dir is not None, otherwise returns numpy array of samples with shape (n_samples, img_shape)

load_data(data_dir=None, use_mnist=False, use_cifar10=False, use_cifar100=False, use_lsun=False, batch_size=32, img_shape=(64, 64))

Load data to train the model

Parameters:
  • data_dir (str, optional) – string representing the directory to load data from. Defaults to None
  • use_mnist (bool, optional) – use the MNIST dataset to train the model. Defaults to False
  • use_cifar10 (bool, optional) – use the CIFAR10 dataset to train the model. Defaults to False
  • use_cifar100 (bool, optional) – use the CIFAR100 dataset to train the model. Defaults to False
  • use_lsun (bool, optional) – use the LSUN dataset to train the model. Defaults to False
  • batch_size (int, optional) – mini batch size for training the model. Defaults to 32
  • img_shape (int, tuple, optional) – shape of the image when loading data from custom directory. Defaults to (64, 64)
Returns:

a tensorflow dataset objects representing the training datset

Conditional GAN

class simplegan.gan.CGAN(noise_dim=100, embed_dim=100, dropout_rate=0.4, gen_channels=[64, 32, 16], disc_channels=[16, 32, 64], kernel_size=(5, 5), activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=None, gen_path=None, disc_path=None)[source]

CGAN model

Parameters:
  • noise_dim (int, optional) – represents the dimension of the prior to sample values. Defaults to 100
  • embed_dim (int, optional) – represents the dimension of the embedding layer to convert classes to dense features. Defaults to 100
  • dropout_rate (float, optional) – represents the amount of dropout regularization to be applied. Defaults to 0.4
  • gen_channels (int, list, optional) – represents the number of filters in the generator network. Defaults to [64, 32, 16]
  • disc_channels (int, list, optional) – represents the number of filters in the discriminator network. Defaults to [16, 32, 64]`
  • kernel_size (int, tuple, optional) – repersents the size of the kernel to perform the convolution. Defaults to (5, 5)
  • activation (str, optional) – type of non-linearity to be applied. Defaults to relu
  • kernel_initializer (str, optional) – initialization of kernel weights. Defaults to glorot_uniform
  • kernel_regularizer (str, optional) – type of regularization to be applied to the weights. Defaults to None
  • gen_path (str, optional) – path to generator checkpoint to load model weights. Defaults to None
  • disc_path (str, optional) – path to discriminator checkpoint to load model weights. Defaults to None
discriminator()[source]

Discriminator module for Conditional GAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
fit(train_ds=None, epochs=100, gen_optimizer='Adam', disc_optimizer='Adam', verbose=1, gen_learning_rate=0.0001, disc_learning_rate=0.0002, beta_1=0.5, tensorboard=False, save_model=None)[source]

Function to train the model

Parameters:
  • train_ds (tf.data object) – training data
  • epochs (int, optional) – number of epochs to train the model. Defaults to 100
  • gen_optimizer (str, optional) – optimizer used to train generator. Defaults to Adam
  • disc_optimizer (str, optional) – optimizer used to train discriminator. Defaults to Adam
  • verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to 1
  • gen_learning_rate (float, optional) – learning rate of the generator optimizer. Defaults to 0.0001
  • disc_learning_rate (float, optional) – learning rate of the discriminator optimizer. Defaults to 0.0002
  • beta_1 (float, optional) – decay rate of the first momement. set if Adam optimizer is used. Defaults to 0.5
  • tensorboard (bool, optional) – if true, writes loss values to logs/gradient_tape directory which aids visualization. Defaults to False
  • save_model (str, optional) – Directory to save the trained model. Defaults to None
generate_samples(n_samples=1, labels_list=None, save_dir=None)[source]

Generate samples using the trained model

Parameters:
  • n_samples (int, optional) – number of samples to generate. Defaults to 1
  • labels_list (int, list) – list of labels representing the class of sample to generate
  • save_dir (str, optional) – directory to save the generated images. Defaults to None
Returns:

returns None if save_dir is not None, otherwise returns a numpy array with generated samples

generator()[source]

Generator module for Conditional GAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
get_sample(data=None, n_samples=1, save_dir=None)[source]

View sample of the data

Parameters:
  • data (tf.data object) – dataset to load samples from
  • n_samples (int, optional) – number of samples to load. Defaults to 1
  • save_dir (str, optional) – directory to save the sample images. Defaults to None
Returns:

None if save_dir is not None, otherwise returns numpy array of samples with shape (n_samples, img_shape)

load_data(data_dir=None, use_mnist=False, use_cifar10=False, batch_size=32, img_shape=(64, 64))[source]

Load data to train the model

Parameters:
  • data_dir (str, optional) – string representing the directory to load data from. Defaults to None
  • use_mnist (bool, optional) – use the MNIST dataset to train the model. Defaults to False
  • use_cifar10 (bool, optional) – use the CIFAR10 dataset to train the model. Defaults to False
  • use_lsun (bool, optional) – use the LSUN dataset to train the model. Defaults to False
  • batch_size (int, optional) – mini batch size for training the model. Defaults to 32
  • img_shape (int, tuple, optional) – shape of the image when loading data from custom directory. Defaults to (64, 64)
Returns:

a tensorflow dataset objects representing the training datset

InfoGAN

class simplegan.gan.InfoGAN(noise_dim=100, code_dim=2, dropout_rate=0.4, gen_channels=[128, 64], disc_channels=[64, 128], kernel_size=(5, 5), activation='leaky_relu', kernel_initializer='glorot_uniform', kernel_regularizer=None, gen_path=None, disc_path=None)[source]

InfoGAN model

Parameters:
  • noise_dim (int, optional) – represents the dimension of the prior to sample values. Defaults to 100
  • code_dim (int, list, optional) – dimension of the interpretable representation. Defaults to 2
  • dropout_rate (float, optional) – represents the amount of dropout regularization to be applied. Defaults to 0.4
  • gen_channels (int, list, optional) – represents the number of filters in the generator network. Defaults to [128, 64]
  • disc_channels (int, list, optional) – represents the number of filters in the discriminator network. Defaults to [64, 128]`
  • kernel_size (int, tuple, optional) – repersents the size of the kernel to perform the convolution. Defaults to (5, 5)
  • activation (str, optional) – type of non-linearity to be applied. Defaults to leaky_relu
  • kernel_initializer (str, optional) – initialization of kernel weights. Defaults to glorot_uniform
  • kernel_regularizer (str, optional) – type of regularization to be applied to the weights. Defaults to None
  • gen_path (str, optional) – path to generator checkpoint to load model weights. Defaults to None
  • disc_path (str, optional) – path to discriminator checkpoint to load model weights. Defaults to None
discriminator()[source]

Discriminator module for InfoGAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
fit(train_ds=None, epochs=100, gen_optimizer='Adam', disc_optimizer='Adam', verbose=1, gen_learning_rate=0.0001, disc_learning_rate=0.0002, beta_1=0.5, tensorboard=False, save_model=None)[source]

Function to train the model

Parameters:
  • train_ds (tf.data object) – training data
  • epochs (int, optional) – number of epochs to train the model. Defaults to 100
  • gen_optimizer (str, optional) – optimizer used to train generator. Defaults to Adam
  • disc_optimizer (str, optional) – optimizer used to train discriminator. Defaults to Adam
  • verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to 1
  • gen_learning_rate (float, optional) – learning rate of the generator optimizer. Defaults to 0.0001
  • disc_learning_rate (float, optional) – learning rate of the discriminator optimizer. Defaults to 0.0002
  • beta_1 (float, optional) – decay rate of the first momement. set if Adam optimizer is used. Defaults to 0.5
  • tensorboard (bool, optional) – if true, writes loss values to logs/gradient_tape directory which aids visualization. Defaults to False
  • save_model (str, optional) – Directory to save the trained model. Defaults to None
generate_samples(n_samples=1, save_dir=None)[source]

Generate samples using the trained model

Parameters:
  • n_samples (int, optional) – number of samples to generate. Defaults to 1
  • save_dir (str, optional) – directory to save the generated images. Defaults to None
Returns:

returns None if save_dir is not None, otherwise returns a numpy array with generated samples

generator()[source]

Generator module for InfoGAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
get_sample(data=None, n_samples=1, save_dir=None)[source]

View sample of the data

Parameters:
  • data (tf.data object) – dataset to load samples from
  • n_samples (int, optional) – number of samples to load. Defaults to 1
  • save_dir (str, optional) – directory to save the sample images. Defaults to None
Returns:

None if save_dir is not None, otherwise returns numpy array of samples with shape (n_samples, img_shape)

load_data(data_dir=None, use_mnist=False, use_cifar10=False, batch_size=32, img_shape=(64, 64))[source]

Load data to train the model

Parameters:
  • data_dir (str, optional) – string representing the directory to load data from. Defaults to None
  • use_mnist (bool, optional) – use the MNIST dataset to train the model. Defaults to False
  • use_cifar10 (bool, optional) – use the CIFAR10 dataset to train the model. Defaults to False
  • batch_size (int, optional) – mini batch size for training the model. Defaults to 32
  • img_shape (int, tuple, optional) – shape of the image when loading data from custom directory. Defaults to (64, 64)
Returns:

a tensorflow dataset objects representing the training datset

Pix2Pix

class simplegan.gan.Pix2Pix(dropout_rate=0.5, gen_enc_channels=[128, 256, 512, 512, 512, 512, 512], gen_dec_channels=[512, 512, 512, 512, 256, 128, 64], disc_channels=[64, 128, 256, 512], kernel_size=(4, 4), kernel_initializer=<sphinx.ext.autodoc.importer._MockObject object>, gen_path=None, disc_path=None)[source]

Pix2Pix model. During training, samples are saved at ./samples and rate specified by save_img_per_epoch

Parameters:
  • dropout_rate (float, optional) – represents the amount of dropout regularization to be applied. Defaults to 0.5
  • gen_enc_channels (int, list, optional) – represents the number of filters in the encoder part of Unet generator network. Defaults to [128, 256, 512, 512, 512, 512, 512]
  • gen_dec_channels (int, list, optional) – represents the number of filters in the decoder part of Unet generator network. Defaults to [512, 512, 512, 512, 256, 128, 64]
  • disc_channels (int, list, optional) – represents the number of filters in the discriminator network. Defaults to [64, 128, 256, 512]`
  • kernel_size (int, tuple, optional) – repersents the size of the kernel to perform the convolution. Defaults to (4, 4)
  • kernel_initializer (str, optional) – initialization of kernel weights. Defaults to tf.random_normal_initializer(0., 0.02)
  • gen_path (str, optional) – path to generator checkpoint to load model weights. Defaults to None
  • disc_path (str, optional) – path to discriminator checkpoint to load model weights. Defaults to None
discriminator()[source]

Discriminator module for Pix2Pix. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
fit(train_ds=None, test_ds=None, epochs=150, gen_optimizer='Adam', disc_optimizer='Adam', verbose=1, gen_learning_rate=0.0002, disc_learning_rate=0.0002, beta_1=0.5, tensorboard=False, save_model=None, LAMBDA=100, save_img_per_epoch=30)[source]

Function to train the model

Parameters:
  • train_ds (tf.data object) – training data
  • test_ds (tf.data object) – testing data
  • epochs (int, optional) – number of epochs to train the model. Defaults to 150
  • gen_optimizer (str, optional) – optimizer used to train generator. Defaults to Adam
  • disc_optimizer (str, optional) – optimizer used to train discriminator. Defaults to Adam
  • verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to 1
  • gen_learning_rate (float, optional) – learning rate of the generator optimizer. Defaults to 2e-4
  • disc_learning_rate (float, optional) – learning rate of the discriminator optimizer. Defaults to 2e-4
  • beta_1 (float, optional) – decay rate of the first momement. set if Adam optimizer is used. Defaults to 0.5
  • tensorboard (bool, optional) – if true, writes loss values to logs/gradient_tape directory which aids visualization. Defaults to False
  • save_model (str, optional) – Directory to save the trained model. Defaults to None
  • LAMBDA (int, optional) – used to calculate generator loss. Defaults to 100
  • save_img_per_epoch (int, optional) – frequency of saving images during training. Defaults to 30
generate_samples(test_ds=None, save_dir=None)[source]

Generate samples using the trained model

Parameters:
  • test_ds (tf.data object) – test data object used to generate samples`
  • save_dir (str, optional) – directory to save the generated images. Defaults to None
Returns:

returns None if save_dir is not None, otherwise returns a numpy array with generated samples

generator()[source]

Generator module for Pix2Pix and CycleGAN(both models use a U-Net as generator). Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
get_sample(data=None, n_samples=1, save_dir=None)[source]

View sample of the data

Parameters:
  • data (tf.data object) – dataset to load samples from
  • n_samples (int, optional) – number of samples to load. Defaults to 1
  • save_dir (str, optional) – directory to save the sample images. Defaults to None
Returns:

None if save_dir is not None, otherwise returns numpy array of samples with shape (n_samples, img_shape)

load_data(data_dir=None, use_cityscapes=False, use_edges2handbags=False, use_edges2shoes=False, use_facades=False, use_maps=False, batch_size=32)[source]

Load data to train the model

Parameters:
  • data_dir (str, optional) – string representing the directory to load data from. Defaults to None
  • use_cityscapes (bool, optional) – use the Cityscapes dataset to train the model. Defaults to False
  • use_edges2handbags (bool, optional) – use the edges2handbags dataset to train the model. Defaults to False
  • use_edges2shoes (bool, optional) – use the edges2shoes dataset to train the model. Defaults to False
  • use_facades (bool, optional) – use the facades dataset to train the model. Defaults to False
  • use_maps (bool, optional) – use the maps dataset to train the model. Defaults to False
  • batch_size (int, optional) – mini batch size for training the model. Defaults to 32
Returns:

two tensorflow dataset objects representing the training and testing datset

CycleGAN

class simplegan.gan.CycleGAN(dropout_rate=0.5, gen_enc_channels=[128, 256, 512, 512, 512, 512, 512], gen_dec_channels=[512, 512, 512, 512, 256, 128, 64], disc_channels=[64, 128, 256, 512], kernel_size=(4, 4), kernel_initializer=<sphinx.ext.autodoc.importer._MockObject object>, gen_g_path=None, gen_f_path=None, disc_x_path=None, disc_y_path=None)[source]

CycleGAN model. During training, samples are saved at ./samples and rate specified by save_img_per_epoch

Parameters:
  • dropout_rate (float, optional) – represents the amount of dropout regularization to be applied. Defaults to 0.5
  • gen_enc_channels (int, list, optional) – represents the number of filters in the encoder part of Unet generator network. Defaults to [128, 256, 512, 512, 512, 512, 512]
  • gen_dec_channels (int, list, optional) – represents the number of filters in the decoder part of Unet generator network. Defaults to [512, 512, 512, 512, 256, 128, 64]
  • disc_channels (int, list, optional) – represents the number of filters in the discriminator network. Defaults to [64, 128, 256, 512]`
  • kernel_size (int, tuple, optional) – repersents the size of the kernel to perform the convolution. Defaults to (4, 4)
  • kernel_initializer (str, optional) – initialization of kernel weights. Defaults to tf.random_normal_initializer(0., 0.02)
  • gen_g_path (str, optional) – path to generator G checkpoint to load model weights. Defaults to None
  • gen_f_path (str, optional) – path to generator F checkpoint to load model weights. Defaults to None
  • disc_x_path (str, optional) – path to discriminator X checkpoint to load model weights. Defaults to None
  • disc_y_path (str, optional) – path to discriminator Y checkpoint to load model weights. Defaults to None
discriminator()[source]

Discriminator module for CycleGAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
fit(trainA=None, trainB=None, testA=None, testB=None, epochs=150, gen_g_optimizer='Adam', gen_f_optimizer='Adam', disc_x_optimizer='Adam', disc_y_optimizer='Adam', verbose=1, gen_g_learning_rate=0.0002, gen_f_learning_rate=0.0002, disc_x_learning_rate=0.0002, disc_y_learning_rate=0.0002, beta_1=0.5, tensorboard=False, save_model=None, LAMBDA=100, save_img_per_epoch=30)[source]

Function to train the model

Parameters:
  • trainA (tf.data object) – training data A
  • trainB (tf.data object) – training data B
  • testA (tf.data object) – testing data A
  • testB (tf.data object) – testing data B
  • epochs (int, optional) – number of epochs to train the model. Defaults to 150
  • gen_g_optimizer (str, optional) – optimizer used to train generator G. Defaults to Adam
  • gen_F_optimizer (str, optional) – optimizer used to train generator F. Defaults to Adam
  • disc_x_optimizer (str, optional) – optimizer used to train discriminator X. Defaults to Adam
  • disc_y_optimizer (str, optional) – optimizer used to train discriminator Y. Defaults to Adam
  • verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to 1
  • gen_g_learning_rate (float, optional) – learning rate of the generator G optimizer. Defaults to 2e-4
  • gen_f_learning_rate (float, optional) – learning rate of the generator F optimizer. Defaults to 2e-4
  • disc_x_learning_rate (float, optional) – learning rate of the discriminator X optimizer. Defaults to 2e-4
  • disc_y_learning_rate (float, optional) – learning rate of the discriminator Y optimizer. Defaults to 2e-4
  • beta_1 (float, optional) – decay rate of the first momement. set if Adam optimizer is used. Defaults to 0.5
  • tensorboard (bool, optional) – if true, writes loss values to logs/gradient_tape directory which aids visualization. Defaults to False
  • save_model (str, optional) – Directory to save the trained model. Defaults to None
  • LAMBDA (int, optional) – used to calculate generator loss. Defaults to 100
  • save_img_per_epoch (int, optional) – frequency of saving images during training. Defaults to 30
generate_samples(test_ds=None, save_dir=None)[source]

Generate samples using the trained model

Parameters:
  • test_ds (tf.data object) – test data object used to generate samples`
  • save_dir (str, optional) – directory to save the generated images. Defaults to None
Returns:

returns None if save_dir is not None, otherwise returns a numpy array with generated samples

generator()

Generator module for Pix2Pix and CycleGAN(both models use a U-Net as generator). Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
get_sample(data=None, n_samples=1, save_dir=None)[source]

View sample of the data

Parameters:
  • data (tf.data object) – dataset to load samples from
  • n_samples (int, optional) – number of samples to load. Defaults to 1
  • save_dir (str, optional) – directory to save the sample images. Defaults to None
Returns:

None if save_dir is not None, otherwise returns numpy array of samples with shape (n_samples, img_shape)

load_data(data_dir=None, use_apple2orange=False, use_summer2winter_yosemite=False, use_horse2zebra=False, use_monet2photo=False, use_cezanne2photo=False, use_ukiyoe2photo=False, use_vangogh2photo=False, use_maps=False, use_cityscapes=False, use_facades=False, use_iphone2dslr_flower=False, batch_size=32)[source]

Load data to train the model

Parameters:
  • data_dir (str, optional) – string representing the directory to load data from. Defaults to None
  • use_apple2orange (bool, optional) – use the apple2orange dataset to train the model. Defaults to False
  • use_summer2winter_yosemite (bool, optional) – use the summer2winter_yosemite dataset to train the model. Defaults to False
  • use_horse2zebra (bool, optional) – use the horse2zebra dataset to train the model. Defaults to False
  • use_monet2photo (bool, optional) – use the monet2photo dataset to train the model. Defaults to False
  • use_cezanne2photo (bool, optional) – use the cezanne2photo dataset to train the model. Defaults to False
  • use_ukiyoe2photo (bool, optional) – use the ukiyoe2photo dataset to train the model. Defaults to False
  • use_vangogh2photo (bool, optional) – use the vangogh2photo dataset to train the model. Defaults to False
  • use_maps (bool, optional) – use the maps dataset to train the model. Defaults to False
  • use_cityscapes (bool, optional) – use the cityscapes dataset to train the model. Defaults to False
  • use_facades (bool, optional) – use the facades dataset to train the model. Defaults to False
  • use_iphone2dslr_flower (bool, optional) – use the iphone2dslr_flower dataset to train the model. Defaults to False
  • batch_size (int, optional) – mini batch size for training the model. Defaults to 32
Returns:

four tensorflow dataset objects representing trainA, trainB, testA, testB

VoxelGAN(3DGAN)

class simplegan.gan.VoxelGAN(noise_dim=200, gen_channels=[512, 256, 128, 64], disc_channels=[64, 128, 256, 512], kernel_size=(4, 4, 4), activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=None, gen_path=None, disc_path=None)[source]

Implementation of 3DGAN model

Parameters:
  • noise_dim (int, optional) – represents the dimension of the prior to sample values. Defaults to 200
  • gen_channels (int, list, optional) – represents the number of filters in the generator network. Defaults to [512, 256, 128, 64]
  • disc_channels (int, list, optional) – represents the number of filters in the discriminator network. Defaults to [64, 128, 256, 512]`
  • kernel_size (int, tuple, optional) – repersents the size of the kernel to perform the convolution. Defaults to (4, 4, 4)
  • activation (str, optional) – type of non-linearity to be applied. Defaults to relu
  • kernel_initializer (str, optional) – initialization of kernel weights. Defaults to glorot_uniform
  • kernel_regularizer (str, optional) – type of regularization to be applied to the weights. Defaults to None
  • gen_path (str, optional) – path to generator checkpoint to load model weights. Defaults to None
  • disc_path (str, optional) – path to discriminator checkpoint to load model weights. Defaults to None
discriminator()[source]

Discriminator module for VoxelGAN(3DGAN). Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
fit(train_ds=None, epochs=100, gen_optimizer='Adam', disc_optimizer='Adam', verbose=1, gen_learning_rate=0.0025, disc_learning_rate=1e-05, beta_1=0.5, tensorboard=False, save_model=None)[source]

Function to train the model

Parameters:
  • train_ds (tf.data object) – training data
  • epochs (int, optional) – number of epochs to train the model. Defaults to 100
  • gen_optimizer (str, optional) – optimizer used to train generator. Defaults to Adam
  • disc_optimizer (str, optional) – optimizer used to train discriminator. Defaults to Adam
  • verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to 1
  • gen_learning_rate (float, optional) – learning rate of the generator optimizer. Defaults to 0.0025
  • disc_learning_rate (float, optional) – learning rate of the discriminator optimizer. Defaults to 0.00001
  • beta_1 (float, optional) – decay rate of the first momement. set if Adam optimizer is used. Defaults to 0.5
  • tensorboard (bool, optional) – if true, writes loss values to logs/gradient_tape directory which aids visualization. Defaults to False
  • save_model (str, optional) – Directory to save the trained model. Defaults to None
generate_sample(n_samples=1, plot=False)[source]

Generate samples from the trained model and visualize them. For an interative visualization set n_samples = 1

Parameters:
  • n_samples (int, optional) – number of samples to generate. Defaults to 1
  • plot (bool, optional) – whether to plot the data for visualization
Returns:

None if plot is True else a numpy array of samples of shape (n_samples, side_length, side_length, side_length, 1)

generator()[source]

Generator module for VoxelGAN(3DGAN). Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
get_sample(data, n_samples=1, plot=False)[source]

get a sample of the data or visualize it by plotting the samples. For an interative visualization set n_samples = 1

Parameters:
  • data (tf.data object) – dataset to load samples from
  • n_samples (int, optional) – number of samples to load. Defaults to 1
  • plot (bool, optional) – whether to plot the data for visualization
Returns:

None if plot is True else a numpy array of samples of shape (n_samples, side_length, side_length, side_length, 1)

load_data(data_dir=None, use_modelnet=False, batch_size=100, side_length=64)[source]

Load data to train the voxelgan model

Parameters:
  • data_dir (str, optional) – string representing the directory to load data from. Defaults to None
  • use_modelnet (bool, optional) – use the ModelNet10 dataset to train the model. Defaults to False
  • batch_size (int, optional) – mini batch size for training the model. Defaults to 100
  • side_length (int, optional) – Dimension of the voxelized data. Defaults to 64
Returns:

a tensorflow dataset objects representing the training datset

Self Attention GAN

class simplegan.gan.SAGAN(noise_dim=128, gen_path=None, disc_path=None)[source]

Self-Attention GAN model

Parameters:
  • noise_dim (int, optional) – represents the dimension of the prior to sample values. Defaults to 128
  • gen_path (str, optional) – path to generator checkpoint to load model weights. Defaults to None
  • disc_path (str, optional) – path to discriminator checkpoint to load model weights. Defaults to None
discriminator()[source]

Discriminator module for Self-Attention GAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
fit(train_ds=None, epochs=100, gen_optimizer='Adam', disc_optimizer='Adam', verbose=1, gen_learning_rate=0.0001, disc_learning_rate=0.0004, beta_1=0, beta_2=0.9, tensorboard=False, save_model=None)[source]

Function to train the model

Parameters:
  • train_ds (tf.data object) – training data
  • epochs (int, optional) – number of epochs to train the model. Defaults to 100
  • gen_optimizer (str, optional) – optimizer used to train generator. Defaults to Adam
  • disc_optimizer (str, optional) – optimizer used to train discriminator. Defaults to Adam
  • verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to 1
  • gen_learning_rate (float, optional) – learning rate of the generator optimizer. Defaults to 0.0001
  • disc_learning_rate (float, optional) – learning rate of the discriminator optimizer. Defaults to 0.0002
  • beta_1 (float, optional) – decay rate of the first momement. set if Adam optimizer is used. Defaults to 0.5
  • beta_2 (float, optional) – decay rate of the second momement. set if Adam optimizer is used. Defaults to 0.5
  • tensorboard (bool, optional) – if true, writes loss values to logs/gradient_tape directory which aids visualization. Defaults to False
  • save_model (str, optional) – Directory to save the trained model. Defaults to None
generate_samples(n_samples=1, labels_list=None, save_dir=None)[source]

Generate samples using the trained model

Parameters:
  • n_samples (int, optional) – number of samples to generate. Defaults to 1
  • labels_list (int, list) – list of labels representing the class of sample to generate
  • save_dir (str, optional) – directory to save the generated images. Defaults to None
Returns:

returns None if save_dir is not None, otherwise returns a numpy array with generated samples

generator()[source]

Generator module for Self-Attention GAN. Use it as a regular TensorFlow 2.0 Keras Model.

Returns:A tf.keras model
get_sample(data=None, n_samples=1, save_dir=None)[source]

View sample of the data

Parameters:
  • data (tf.data object) – dataset to load samples from
  • n_samples (int, optional) – number of samples to load. Defaults to 1
  • save_dir (str, optional) – directory to save the sample images. Defaults to None
Returns:

None if save_dir is not None, otherwise returns numpy array of samples with shape (n_samples, img_shape)