hussamalafandi commited on
Commit
43fb226
·
verified ·
1 Parent(s): f819d16

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. .gitattributes +1 -0
  2. LICENSE +21 -0
  3. README.md +82 -0
  4. dcgan.py +78 -0
  5. dcgan_celeba.pth +3 -0
  6. gan_celeba.png +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ gan_celeba.png filter=lfs diff=lfs merge=lfs -text
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Hussam Alafandi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - dcgan
4
+ - generative-adversarial-network
5
+ - celeba
6
+ - image-generation
7
+ - deep-learning
8
+ datasets:
9
+ - CelebA
10
+ license: mit
11
+ ---
12
+
13
+ # DCGAN Model Card
14
+
15
+ ## Model Description
16
+ This is a Deep Convolutional Generative Adversarial Network (DCGAN) trained on the CelebA dataset to generate realistic 64x64 RGB images of human faces. The model was developed as part of the Generative AI course.
17
+
18
+ ## Training Details
19
+ - **Dataset**: CelebA
20
+ - **Subset Size**: 50,000 images
21
+ - **Image Size**: 64x64
22
+ - **Number of Channels**: 3 (RGB)
23
+ - **Latent Dimension**: 100
24
+ - **Generator Feature Map Size**: 64
25
+ - **Discriminator Feature Map Size**: 64
26
+ - **Batch Size**: 128
27
+ - **Epochs**: 50
28
+ - **Learning Rate**: 0.0002
29
+ - **Beta1**: 0.5
30
+ - **Weight Decay**: 0
31
+ - **Optimizer**: Adam
32
+ - **Hardware**: CUDA-enabled GPU
33
+ - **Logging**: Weights and Biases (wandb)
34
+
35
+ ### Weights and Biases Run
36
+
37
+ The training process was tracked using [Weights and Biases](https://wandb.ai). You can view the full training logs and metrics [here](https://wandb.ai/hussam-alafandi/DCGAN_CelebA/runs/32qhixp1?nw=nwuserhussamalafandi).
38
+
39
+ ## Usage
40
+
41
+ ### Loading the Model
42
+
43
+ To load the trained model, use the following code snippet:
44
+ ```python
45
+ import torch
46
+ from dcgan import Generator
47
+
48
+ # Load the configuration
49
+ config = {
50
+ "latent_dim": 100,
51
+ "ngf": 64,
52
+ "nc": 3
53
+ }
54
+
55
+ # Initialize the generator
56
+ generator = Generator(config)
57
+
58
+ # Load the trained weights
59
+ model_path = "./dcgan_celeba.pth"
60
+
61
+ generator.load_state_dict(torch.load(checkpoint_path, map_location=torch.device('cuda' if torch.cuda.is_available() else 'cpu')))
62
+
63
+ # Set the model to evaluation mode
64
+ generator.eval()
65
+
66
+ # Example: Generate an image
67
+ latent_vector = torch.randn(1, config["latent_dim"], 1, 1) # Batch size of 1
68
+
69
+ if torch.cuda.is_available():
70
+ latent_vector = latent_vector.cuda()
71
+ generator = generator.cuda()
72
+
73
+ generated_image = generator(latent_vector)
74
+ ```
75
+
76
+ ## Example Results
77
+
78
+ ![generate image](./gan_celeba.png)
79
+
80
+ ## Resources
81
+ - **Course Repository**: [Generative AI Course](https://github.com/hussamalafandi/Generative_AI)
82
+ - **WandB Run**: [DCGAN_CelebA Run](https://wandb.ai/hussam-alafandi/DCGAN_CelebA/runs/32qhixp1?nw=nwuserhussamalafandi)
dcgan.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from torch import nn
2
+
3
+ class Generator(nn.Module):
4
+ def __init__(self, config):
5
+ super(Generator, self).__init__()
6
+
7
+ self.latent_dim = config["latent_dim"]
8
+ self.ngf = config["ngf"]
9
+ self.nc = config["nc"]
10
+
11
+ # DCGAN generator architecture
12
+ self.main = nn.Sequential(
13
+
14
+ # Input: latent vector Z (batch_size, latent_dim, 1, 1)
15
+ nn.ConvTranspose2d(self.latent_dim, self.ngf * 8, 4, 1, 0, bias=False),
16
+ nn.BatchNorm2d(self.ngf * 8),
17
+ nn.ReLU(True),
18
+
19
+ # State: (ngf*8) x 4 x 4
20
+ nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
21
+ nn.BatchNorm2d(self.ngf * 4),
22
+ nn.ReLU(True),
23
+
24
+ # State: (ngf*4) x 8 x 8
25
+ nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
26
+ nn.BatchNorm2d(self.ngf * 2),
27
+ nn.ReLU(True),
28
+
29
+ # State: (ngf*2) x 16 x 16
30
+ nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
31
+ nn.BatchNorm2d(self.ngf),
32
+ nn.ReLU(True),
33
+
34
+ # State: (ngf) x 32 x 32
35
+ nn.ConvTranspose2d(self.ngf, self.nc, 4, 2, 1, bias=False),
36
+ nn.Tanh()
37
+ # Output: (nc) x 64 x 64
38
+ )
39
+
40
+ def forward(self, input):
41
+ return self.main(input)
42
+
43
+ class Discriminator(nn.Module):
44
+ def __init__(self, config):
45
+ super(Discriminator, self).__init__()
46
+
47
+ self.ndf = config["ndf"]
48
+ self.nc = config["nc"]
49
+
50
+ # DCGAN discriminator architecture
51
+ self.main = nn.Sequential(
52
+
53
+ # Input: (nc) x 64 x 64
54
+ nn.Conv2d(self.nc, self.ndf, 4, 2, 1, bias=False),
55
+ nn.LeakyReLU(0.2, inplace=True),
56
+
57
+ # State: (ndf) x 32 x 32
58
+ nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
59
+ nn.BatchNorm2d(self.ndf * 2),
60
+ nn.LeakyReLU(0.2, inplace=True),
61
+
62
+ # State: (ndf*2) x 16 x 16
63
+ nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
64
+ nn.BatchNorm2d(self.ndf * 4),
65
+ nn.LeakyReLU(0.2, inplace=True),
66
+
67
+ # State: (ndf*4) x 8 x 8
68
+ nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
69
+ nn.BatchNorm2d(self.ndf * 8),
70
+ nn.LeakyReLU(0.2, inplace=True),
71
+
72
+ # State: (ndf*8) x 4 x 4
73
+ nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False),
74
+ nn.Sigmoid()
75
+ )
76
+
77
+ def forward(self, input):
78
+ return self.main(input).view(-1, 1).squeeze(1)
dcgan_celeba.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6f6077b0f1bc929bca55bfa834291fb826849aa71d481e9b145888bc688f8ec3
3
+ size 25405907
gan_celeba.png ADDED

Git LFS Details

  • SHA256: 965d760ce06097972cfeef5202cecfa9726c3f2e6538948eb821a033e7573b73
  • Pointer size: 131 Bytes
  • Size of remote file: 170 kB