ResNets

class pythae.models.nn.benchmarks.celeba.Encoder_ResNet_AE_CELEBA(args)[source]

A ResNet encoder suited for CELEBA and Autoencoder-based models.

It can be built as follows:

>>> from pythae.models.nn.benchmarks.celeba import Encoder_ResNet_AE_CELEBA
>>> from pythae.models import AEConfig
>>> model_config = AEConfig(input_dim=(3, 64, 64), latent_dim=16)
>>> encoder = Encoder_ResNet_AE_CELEBA(model_config)
>>> encoder
... Encoder_ResNet_AE_CELEBA(
...   (layers): ModuleList(
...     (0): Sequential(
...       (0): Conv2d(3, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
...     )
...     (1): Sequential(
...       (0): Conv2d(64, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
...     )
...     (2): Sequential(
...       (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
...     )
...     (3): Sequential(
...       (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
...     )
...     (4): Sequential(
...       (0): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...       (1): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...     )
...   )
...   (embedding): Linear(in_features=2048, out_features=16, bias=True)
... )

and then passed to a pythae.models instance

>>> from pythae.models import AE
>>> model = AE(model_config=model_config, encoder=encoder)
>>> model.encoder == encoder
... True

Note

Please note that this encoder is only suitable for Autoencoder based models since it only outputs the embeddings of the input data under the key embedding.

>>> import torch
>>> input = torch.rand(2, 3, 64, 64)
>>> out = encoder(input)
>>> out.embedding.shape
... torch.Size([2, 16])
forward(x, output_layer_levels=None)[source]

Forward method

Parameters

output_layer_levels (List[int]) – The levels of the layers where the outputs are extracted. If None, the last layer’s output is returned. Default: None.

Returns

An instance of ModelOutput containing the embeddings of the input data under the key embedding. Optional: The outputs of the layers specified in output_layer_levels arguments are available under the keys embedding_layer_i where i is the layer’s level.

Return type

ModelOutput

class pythae.models.nn.benchmarks.celeba.Encoder_ResNet_VAE_CELEBA(args)[source]

A ResNet encoder suited for CELEBA and Variational Autoencoder-based models.

It can be built as follows:

>>> from pythae.models.nn.benchmarks.celeba import Encoder_ResNet_VAE_CELEBA
>>> from pythae.models import VAEConfig
>>> model_config = VAEConfig(input_dim=(3, 64, 64), latent_dim=16)
>>> encoder = Encoder_ResNet_VAE_CELEBA(model_config)
>>> encoder
... Encoder_ResNet_VAE_CELEBA(
...   (layers): ModuleList(
...     (0): Sequential(
...       (0): Conv2d(3, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
...     )
...     (1): Sequential(
...       (0): Conv2d(64, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
...     )
...     (2): Sequential(
...       (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
...     )
...     (3): Sequential(
...       (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
...     )
...     (4): Sequential(
...       (0): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...       (1): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...     )
...   )
...   (embedding): Linear(in_features=2048, out_features=16, bias=True)
...   (log_var): Linear(in_features=2048, out_features=16, bias=True)
... )

and then passed to a pythae.models instance

>>> from pythae.models import VAE
>>> model = VAE(model_config=model_config, encoder=encoder)
>>> model.encoder == encoder
... True

Note

Please note that this encoder is only suitable for Autoencoder based models since it only outputs the embeddings of the input data under the key embedding.

>>> import torch
>>> input = torch.rand(2, 3, 64, 64)
>>> out = encoder(input)
>>> out.embedding.shape
... torch.Size([2, 16])
forward(x, output_layer_levels=None)[source]

Forward method

Parameters

output_layer_levels (List[int]) – The levels of the layers where the outputs are extracted. If None, the last layer’s output is returned. Default: None.

Returns

An instance of ModelOutput containing the embeddings of the input data under the key embedding. Optional: The outputs of the layers specified in output_layer_levels arguments are available under the keys embedding_layer_i where i is the layer’s level.

Return type

ModelOutput

class pythae.models.nn.benchmarks.celeba.Encoder_ResNet_SVAE_CELEBA(args)[source]

A ResNet encoder suited for CELEBA and Hyperspherical VAE models.

It can be built as follows:

>>> from pythae.models.nn.benchmarks.celeba import Encoder_ResNet_SVAE_CELEBA
>>> from pythae.models import SVAEConfig
>>> model_config = SVAEConfig(input_dim=(3, 64, 64), latent_dim=16)
>>> encoder = Encoder_ResNet_SVAE_CELEBA(model_config)
>>> encoder
... Encoder_ResNet_SVAE_CELEBA(
...   (layers): ModuleList(
...     (0): Sequential(
...       (0): Conv2d(3, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
...     )
...     (1): Sequential(
...       (0): Conv2d(64, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
...     )
...     (2): Sequential(
...       (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
...     )
...     (3): Sequential(
...       (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
...     )
...     (4): Sequential(
...       (0): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...       (1): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...     )
...   )
...   (embedding): Linear(in_features=2048, out_features=16, bias=True)
...   (log_concentration): Linear(in_features=2048, out_features=1, bias=True)
... )

and then passed to a pythae.models instance

>>> from pythae.models import SVAE
>>> model = SVAE(model_config=model_config, encoder=encoder)
>>> model.encoder == encoder
... True

Note

Please note that this encoder is only suitable for Autoencoder based models since it only outputs the embeddings of the input data under the key embedding.

>>> import torch
>>> input = torch.rand(2, 3, 64, 64)
>>> out = encoder(input)
>>> out.embedding.shape
... torch.Size([2, 16])
forward(x, output_layer_levels=None)[source]

Forward method

Parameters

output_layer_levels (List[int]) – The levels of the layers where the outputs are extracted. If None, the last layer’s output is returned. Default: None.

Returns

An instance of ModelOutput containing the embeddings of the input data under the key embedding. Optional: The outputs of the layers specified in output_layer_levels arguments are available under the keys embedding_layer_i where i is the layer’s level.

Return type

ModelOutput

class pythae.models.nn.benchmarks.celeba.Encoder_ResNet_VQVAE_CELEBA(args)[source]

A ResNet encoder suited for CELEBA and Vector Quantized VAE models.

It can be built as follows:

>>> from pythae.models.nn.benchmarks.celeba import Encoder_ResNet_VQVAE_CELEBA
>>> from pythae.models import VQVAEConfig
>>> model_config = VQVAEConfig(input_dim=(3, 64, 64), latent_dim=16)
>>> encoder = Encoder_ResNet_VQVAE_CELEBA(model_config)
>>> encoder
... Encoder_ResNet_VQVAE_CELEBA(
...   (layers): ModuleList(
...     (0): Sequential(
...       (0): Conv2d(3, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
...     )
...     (1): Sequential(
...       (0): Conv2d(64, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
...     )
...     (2): Sequential(
...       (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
...     )
...     (3): Sequential(
...       (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
...     )
...     (4): Sequential(
...       (0): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...       (1): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...       (2): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...       (3): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...     )
...   )
...   (pre_qantized): Conv2d(128, 16, kernel_size=(1, 1), stride=(1, 1))
... )

and then passed to a pythae.models instance

>>> from pythae.models import VQVAE
>>> model = VQVAE(model_config=model_config, encoder=encoder)
>>> model.encoder == encoder
... True

Note

Please note that this encoder is only suitable for Autoencoder based models since it only outputs the embeddings of the input data under the key embedding.

>>> import torch
>>> input = torch.rand(2, 3, 64, 64)
>>> out = encoder(input)
>>> out.embedding.shape
... torch.Size([2, 16, 4,  4])
forward(x, output_layer_levels=None)[source]

Forward method

Parameters

output_layer_levels (List[int]) – The levels of the layers where the outputs are extracted. If None, the last layer’s output is returned. Default: None.

Returns

An instance of ModelOutput containing the embeddings of the input data under the key embedding. Optional: The outputs of the layers specified in output_layer_levels arguments are available under the keys embedding_layer_i where i is the layer’s level.

Return type

ModelOutput

class pythae.models.nn.benchmarks.celeba.Decoder_ResNet_AE_CELEBA(args)[source]

A ResNet decoder suited for CELEBA and Autoencoder-based models.

>>> from pythae.models.nn.benchmarks.celeba import Decoder_ResNet_AE_CELEBA
>>> from pythae.models import VAEConfig
>>> model_config = VAEConfig(input_dim=(3, 64, 64), latent_dim=16)
>>> decoder = Decoder_ResNet_AE_CELEBA(model_config)
>>> decoder
... Decoder_ResNet_AE_CELEBA(
...   (layers): ModuleList(
...     (0): Linear(in_features=16, out_features=2048, bias=True)
...     (1): ConvTranspose2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
...     (2): Sequential(
...       (0): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...       (1): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...     )
...     (3): Sequential(
...       (0): ConvTranspose2d(128, 128, kernel_size=(5, 5), stride=(2, 2), padding=(1, 1))
...       (1): Sigmoid()
...     )
...     (4): Sequential(
...       (0): ConvTranspose2d(128, 64, kernel_size=(5, 5), stride=(2, 2), padding=(1, 1), output_padding=(1, 1))
...     )
...     (5): Sequential(
...       (0): ConvTranspose2d(64, 3, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
...       (1): Sigmoid()
...     )
...   )
... )

and then passed to a pythae.models instance

>>> from pythae.models import VAE
>>> model = VAE(model_config=model_config, decoder=decoder)
>>> model.decoder == decoder
... True

Note

Please note that this decoder is suitable for all models.

>>> import torch
>>> input = torch.randn(2, 16)
>>> out = decoder(input)
>>> out.reconstruction.shape
... torch.Size([2, 3, 64, 64])
forward(z, output_layer_levels=None)[source]

Forward method

Parameters

output_layer_levels (List[int]) – The levels of the layers where the outputs are extracted. If None, the last layer’s output is returned. Default: None.

Returns

An instance of ModelOutput containing the reconstruction of the latent code under the key reconstruction. Optional: The outputs of the layers specified in output_layer_levels arguments are available under the keys reconstruction_layer_i where i is the layer’s level.

Return type

ModelOutput

class pythae.models.nn.benchmarks.celeba.Decoder_ResNet_VQVAE_CELEBA(args)[source]

A ResNet decoder suited for CELEBA and Vector Quantized VAE models.

>>> from pythae.models.nn.benchmarks.celeba import Decoder_ResNet_VQVAE_CELEBA
>>> from pythae.models import VQVAEConfig
>>> model_config = VQVAEConfig(input_dim=(3, 64, 64), latent_dim=16)
>>> decoder = Decoder_ResNet_VQVAE_CELEBA(model_config)
>>> decoder
... Decoder_ResNet_VQVAE_CELEBA(
...   (layers): ModuleList(
...     (0): ConvTranspose2d(16, 128, kernel_size=(1, 1), stride=(1, 1))
...     (1): ConvTranspose2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
...     (2): Sequential(
...       (0): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...       (1): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...       (2): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...       (3): ResBlock(
...         (conv_block): Sequential(
...           (0): ReLU()
...           (1): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
...           (2): ReLU()
...           (3): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))
...         )
...       )
...     )
...     (3): Sequential(
...       (0): ConvTranspose2d(128, 128, kernel_size=(5, 5), stride=(2, 2), padding=(1, 1))
...     )
...     (4): Sequential(
...       (0): ConvTranspose2d(128, 64, kernel_size=(5, 5), stride=(2, 2), padding=(1, 1), output_padding=(1, 1))
...     )
...     (5): Sequential(
...       (0): ConvTranspose2d(64, 3, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
...       (1): Sigmoid()
...     )
...   )
... )

and then passed to a pythae.models instance

>>> from pythae.models import VQVAE
>>> model = VQVAE(model_config=model_config, decoder=decoder)
>>> model.decoder == decoder
... True

Note

Please note that this decoder is suitable for all models.

>>> import torch
>>> input = torch.randn(2, 16, 4, 4)
>>> out = decoder(input)
>>> out.reconstruction.shape
... torch.Size([2, 3, 64, 64])
forward(z, output_layer_levels=None)[source]

Forward method

Parameters

output_layer_levels (List[int]) – The levels of the layers where the outputs are extracted. If None, the last layer’s output is returned. Default: None.

Returns

An instance of ModelOutput containing the reconstruction of the latent code under the key reconstruction. Optional: The outputs of the layers specified in output_layer_levels arguments are available under the keys reconstruction_layer_i where i is the layer’s level.

Return type

ModelOutput