From 379474fd5e36093b448dceb50a47ae44dfe80233 Mon Sep 17 00:00:00 2001 From: Jean-Luc Parouty <Jean-Luc.Parouty@simap.grenoble-inp.fr> Date: Mon, 27 Feb 2023 17:16:17 +0100 Subject: [PATCH] Add Generator_2() with upscaling + conv2d --- DCGAN-PyTorch/01-DCGAN-PL.ipynb | 10 ++++---- DCGAN-PyTorch/modules/Generators.py | 38 +++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/DCGAN-PyTorch/01-DCGAN-PL.ipynb b/DCGAN-PyTorch/01-DCGAN-PL.ipynb index a0973d1..ea028f9 100644 --- a/DCGAN-PyTorch/01-DCGAN-PL.ipynb +++ b/DCGAN-PyTorch/01-DCGAN-PL.ipynb @@ -79,7 +79,7 @@ "source": [ "latent_dim = 128\n", " \n", - "generator_class = 'Generator_1'\n", + "generator_class = 'Generator_2'\n", "discriminator_class = 'Discriminator_1' \n", " \n", "scale = .1\n", @@ -169,7 +169,7 @@ "outputs": [], "source": [ "print('\\nInstantiation :\\n')\n", - "generator = Generator_1(latent_dim=latent_dim, data_shape=data_shape)\n", + "generator = Generator_2(latent_dim=latent_dim, data_shape=data_shape)\n", "discriminator = Discriminator_1(latent_dim=latent_dim, data_shape=data_shape)\n", "\n", "print('\\nFew tests :\\n')\n", @@ -308,9 +308,9 @@ ], "metadata": { "kernelspec": { - "display_name": "pytorch-gpu-1.13.0_py3.10.8", + "display_name": "fidle-env", "language": "python", - "name": "module-conda-env-pytorch-gpu-1.13.0_py3.10.8" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -322,7 +322,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.2" }, "vscode": { "interpreter": { diff --git a/DCGAN-PyTorch/modules/Generators.py b/DCGAN-PyTorch/modules/Generators.py index 0c49adb..cb18782 100644 --- a/DCGAN-PyTorch/modules/Generators.py +++ b/DCGAN-PyTorch/modules/Generators.py @@ -22,7 +22,7 @@ class Generator_1(nn.Module): super().__init__() self.latent_dim = latent_dim self.img_shape = data_shape - print('init generator : ',latent_dim,' to ',data_shape) + print('init generator 1 : ',latent_dim,' to ',data_shape) self.model = nn.Sequential( @@ -50,4 +50,38 @@ class Generator_1(nn.Module): def forward(self, z): img = self.model(z) img = img.view(img.size(0), *self.img_shape) - return img \ No newline at end of file + return img + + + +class Generator_2(nn.Module): + + def __init__(self, latent_dim=None, data_shape=None): + super().__init__() + self.latent_dim = latent_dim + self.img_shape = data_shape + print('init generator 2 : ',latent_dim,' to ',data_shape) + + self.model = nn.Sequential( + + nn.Linear(latent_dim, 7*7*64), + nn.Unflatten(1, (64,7,7)), + + nn.UpsamplingBilinear2d( scale_factor=2 ), + nn.Conv2d( 64,128, (3,3), stride=(1,1), padding=(1,1) ), + nn.ReLU(), + + nn.UpsamplingBilinear2d( scale_factor=2 ), + nn.Conv2d( 128,256, (3,3), stride=(1,1), padding=(1,1)), + nn.ReLU(), + + nn.Conv2d( 256,1, (5,5), stride=(1,1), padding=(2,2)), + nn.Sigmoid() + + ) + + def forward(self, z): + img = self.model(z) + img = img.view(img.size(0), *self.img_shape) + return img + -- GitLab