simplegan.autoencoder¶
The autoencoder
sub-package provdies various autoencoder models that can be trained off the shelf with minimal fuss.
Vanilla Autoencoder¶
-
class
simplegan.autoencoder.
VanillaAutoencoder
(interm_dim=64, enc_units=[256, 128], dec_units=[128, 256], activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=None)[source]¶ Vanilla Autoencoder model
Parameters: - interm_dim (int, optional) – represents the dimension of the bottleneck layer. Defaults to
64
- enc_units (int, list, optional) – represents the number of units/neurons in the encoder part of the network. Defaults to
[256, 128]
- dec_units (int, list, optional) – represents the number of units/neurons in the decoder part of the network. Defaults to
[128, 256]`
- 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
-
fit
(train_ds=None, epochs=100, optimizer='Adam', verbose=1, learning_rate=0.001, 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
- optimizer (str, optional) – optimizer used to train the model. Defaults to
Adam
- verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to
1
- learning_rate (float, optional) – learning rate of the optimizer. Defaults to
0.001
- tensorboard (bool, optional) – if true, writes loss values to
logs/gradient_tape
directory which aids visualization. Defaults toFalse
- save_model (str, optional) – Directory to save the trained model. Defaults to
None
-
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 isnot None
, otherwise returns a numpy array with generated samples
-
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 isnot 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: two tensorflow dataset objects representing the train and test datset
- data_dir (str, optional) – string representing the directory to load data from. Defaults to
- interm_dim (int, optional) – represents the dimension of the bottleneck layer. Defaults to
Convolutional Autoencoder¶
-
class
simplegan.autoencoder.
ConvolutionalAutoencoder
(interm_dim=128, enc_channels=[32, 64], dec_channels=[64, 32], kernel_size=(5, 5), activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=None)[source]¶ Convolutional Autoencoder model
Parameters: - interm_dim (int, optional) – represents the dimension of the bottleneck layer. Defaults to
128
- enc_channels (int, list, optional) – represents the number of filters in the encoder part of the network. Defaults to
[32, 64]
- dec_channels (int, list, optional) – represents the number of filters in the decoder part of the network. Defaults to
[64, 32]`
- 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
-
fit
(train_ds=None, epochs=100, optimizer='Adam', verbose=1, learning_rate=0.001, 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
- optimizer (str, optional) – optimizer used to train the model. Defaults to
Adam
- verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to
1
- learning_rate (float, optional) – learning rate of the optimizer. Defaults to
0.001
- tensorboard (bool, optional) – if true, writes loss values to
logs/gradient_tape
directory which aids visualization. Defaults toFalse
- save_model (str, optional) – Directory to save the trained model. Defaults to
None
-
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 isnot None
, otherwise returns a numpy array with generated samples
-
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 isnot 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: two tensorflow dataset objects representing the train and test datset
- data_dir (str, optional) – string representing the directory to load data from. Defaults to
- interm_dim (int, optional) – represents the dimension of the bottleneck layer. Defaults to
Variational Autoencoder¶
-
class
simplegan.autoencoder.
VAE
(interm_dim=256, latent_dim=32, enc_units=[256, 128], dec_units=[128, 256], activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=None)[source]¶ Variational Autoencoder model
Parameters: - interm_dim (int, optional) – represents the dimension of the bottleneck layer. Defaults to
256
- latent_dim (int, optional) – represents the dimension of the distribution to sample from. Defaults to
32
- enc_units (int, list, optional) – represents the number of units/neurons in the encoder part of the network. Defaults to
[256, 128]
- dec_units (int, list, optional) – represents the number of units/neurons in the decoder part of the network. Defaults to
[128, 256]`
- 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
-
fit
(train_ds=None, epochs=100, optimizer='Adam', verbose=1, learning_rate=0.001, 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
- optimizer (str, optional) – optimizer used to train the model. Defaults to
Adam
- verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to
1
- learning_rate (float, optional) – learning rate of the optimizer. Defaults to
0.001
- tensorboard (bool, optional) – if true, writes loss values to
logs/gradient_tape
directory which aids visualization. Defaults toFalse
- save_model (str, optional) – Directory to save the trained model. Defaults to
None
-
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 isnot None
, otherwise returns a numpy array with generated samples
-
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 isnot 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: two tensorflow dataset objects representing the train and test datset
- data_dir (str, optional) – string representing the directory to load data from. Defaults to
- interm_dim (int, optional) – represents the dimension of the bottleneck layer. Defaults to
Vector-Quantized Variational Autoencoder¶
-
class
simplegan.autoencoder.
VQ_VAE
(num_hiddens=128, num_residual_hiddens=32, num_residual_layers=2, num_embeddings=512, embedding_dim=64, commiment_cost=0.25)[source]¶ Vector-Quantized Variational Autoencoder model
Parameters: - num_hiddens (int, optional) – number of filters in the convolution operation in residual block. Defaults to
128
- num_residual_hiddens (int, optional) – number of filters in the convolution operation in residual block. Defaults to
32
- num_residual_layers (int, optional) – number of residual blocks. Defaults to
2
- num_embeddings (int, optional) – capacity of information bottleneck. Defaults to
512
- embedding_dim (int, optional) – size of the embedding. does not affect peformance of the model much. Defaults to
64
- commiment_cost (float, optional) – scale the embedding latent loss. Defaults to
0.25
-
fit
(train_ds=None, epochs=100, optimizer='Adam', verbose=1, learning_rate=0.0003, 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
- optimizer (str, optional) – optimizer used to train the model. Defaults to
Adam
- verbose (int, optional) – 1 - prints training outputs, 0 - no outputs. Defaults to
1
- learning_rate (float, optional) – learning rate of the optimizer. Defaults to
0.001
- tensorboard (bool, optional) – if true, writes loss values to
logs/gradient_tape
directory which aids visualization. Defaults toFalse
- save_model (str, optional) – Directory to save the trained model. Defaults to
None
-
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 isnot None
, otherwise returns a numpy array with generated samples
-
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 isnot 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: two tensorflow dataset objects representing the train and test datset
- data_dir (str, optional) – string representing the directory to load data from. Defaults to
- num_hiddens (int, optional) – number of filters in the convolution operation in residual block. Defaults to