Upload folder using huggingface_hub
Browse files- LICENSE +21 -0
- README.md +91 -0
- c_gan.py +93 -0
- cgan_mnist.png +0 -0
- discriminator.pth +3 -0
- generator.pth +3 -0
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,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- cgan
|
4 |
+
- conditional-gan
|
5 |
+
- generative-adversarial-network
|
6 |
+
- image-generation
|
7 |
+
- deep-learning
|
8 |
+
datasets:
|
9 |
+
- MNIST
|
10 |
+
license: mit
|
11 |
+
---
|
12 |
+
|
13 |
+
# Conditional GAN Model Card
|
14 |
+
|
15 |
+
## Model Description
|
16 |
+
This is a Conditional GAN (cGAN) trained on the MNIST dataset to generate realistic 28x28 grayscale images of handwritten digits. The model leverages label information to guide image generation and was developed as part of the [Generative AI](https://github.com/hussamalafandi/Generative_AI) course.
|
17 |
+
|
18 |
+
## Training Details
|
19 |
+
- **Dataset**: MNIST
|
20 |
+
- **Subset Size**: 60,000 images
|
21 |
+
- **Image Size**: 28x28
|
22 |
+
- **Number of Channels**: 1 (grayscale)
|
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/cGAN-MNIST/runs/w11n93e5?nw=nwuserhussamalafandi).
|
38 |
+
|
39 |
+
## Usage
|
40 |
+
|
41 |
+
### Downloading the Model from the Hub
|
42 |
+
|
43 |
+
You can download the model checkpoint directly from the hub using the huggingface_hub library:
|
44 |
+
```python
|
45 |
+
from huggingface_hub import hf_hub_download
|
46 |
+
|
47 |
+
# Download the model checkpoint from the hub
|
48 |
+
checkpoint_path = hf_hub_download(repo_id="hussamalafandi/cGAN-MNIST", filename="generator.pth")
|
49 |
+
```
|
50 |
+
|
51 |
+
### Loading the Model Locally
|
52 |
+
|
53 |
+
```python
|
54 |
+
import torch
|
55 |
+
from c_gan import Generator
|
56 |
+
|
57 |
+
# Load the configuration
|
58 |
+
config = {
|
59 |
+
"latent_dim": 100,
|
60 |
+
"ngf": 64,
|
61 |
+
"nc": 1,
|
62 |
+
"num_classes": 10,
|
63 |
+
"embed_dim": 50
|
64 |
+
}
|
65 |
+
|
66 |
+
# Initialize the generator
|
67 |
+
generator = Generator(config)
|
68 |
+
|
69 |
+
# Load the downloaded checkpoint (or a local path)
|
70 |
+
generator.load_state_dict(torch.load(checkpoint_path, map_location=torch.device('cuda' if torch.cuda.is_available() else 'cpu')))
|
71 |
+
|
72 |
+
# Set the model to evaluation mode
|
73 |
+
generator.eval()
|
74 |
+
|
75 |
+
# Example: Generate an image
|
76 |
+
latent_vector = torch.randn(1, config["latent_dim"], 1, 1) # Batch size of 1
|
77 |
+
|
78 |
+
if torch.cuda.is_available():
|
79 |
+
latent_vector = latent_vector.cuda()
|
80 |
+
generator = generator.cuda()
|
81 |
+
|
82 |
+
generated_image = generator(latent_vector, torch.tensor([7])) # Example label: 7
|
83 |
+
```
|
84 |
+
|
85 |
+
## Example Results
|
86 |
+
|
87 |
+

|
88 |
+
|
89 |
+
## Resources
|
90 |
+
- **Course Repository**: [Generative AI Course](https://github.com/hussamalafandi/Generative_AI)
|
91 |
+
- **WandB Run**: [cGAN-MNIST Run](https://wandb.ai/hussam-alafandi/cGAN-MNIST/runs/w11n93e5?nw=nwuserhussamalafandi)
|
c_gan.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
|
4 |
+
|
5 |
+
class Generator(nn.Module):
|
6 |
+
def __init__(self, config):
|
7 |
+
super(Generator, self).__init__()
|
8 |
+
|
9 |
+
self.latent_dim = config["latent_dim"]
|
10 |
+
self.ngf = config["ngf"]
|
11 |
+
self.nc = config["nc"]
|
12 |
+
|
13 |
+
self.n_classes = config["num_classes"]
|
14 |
+
self.embed_dim = config["embed_dim"]
|
15 |
+
|
16 |
+
# Label embedding: maps labels to vectors of size embed_dim.
|
17 |
+
self.label_embed = nn.Embedding(self.n_classes, self.embed_dim)
|
18 |
+
|
19 |
+
# DCGAN generator architecture
|
20 |
+
self.main = nn.Sequential(
|
21 |
+
# Combine noise and label embedding -> output shape: (latent_dim + embed_dim, 1, 1)
|
22 |
+
# upscale to 7x7 with ngf*4 channels.
|
23 |
+
nn.ConvTranspose2d(self.latent_dim + self.embed_dim, self.ngf * 4,
|
24 |
+
kernel_size=7, stride=1, padding=0, bias=False),
|
25 |
+
nn.BatchNorm2d(self.ngf * 4),
|
26 |
+
nn.ReLU(True),
|
27 |
+
|
28 |
+
# 7x7 -> 14x14
|
29 |
+
nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2,
|
30 |
+
kernel_size=4, stride=2, padding=1, bias=False),
|
31 |
+
nn.BatchNorm2d(self.ngf * 2),
|
32 |
+
nn.ReLU(True),
|
33 |
+
|
34 |
+
# 14x14 -> 28x28.
|
35 |
+
nn.ConvTranspose2d(self.ngf * 2, self.ngf,
|
36 |
+
kernel_size=4, stride=2, padding=1, bias=False),
|
37 |
+
nn.BatchNorm2d(self.ngf),
|
38 |
+
nn.ReLU(True),
|
39 |
+
|
40 |
+
# Final layer: convert to 1 channel, preserving 28x28.
|
41 |
+
nn.ConvTranspose2d(self.ngf, self.nc, kernel_size=3,
|
42 |
+
stride=1, padding=1, bias=False),
|
43 |
+
nn.Tanh()
|
44 |
+
)
|
45 |
+
|
46 |
+
def forward(self, noise, labels):
|
47 |
+
# Embed labels and reshape to (batch, embed_dim, 1, 1)
|
48 |
+
label_embedding = self.label_embed(labels).unsqueeze(2).unsqueeze(3)
|
49 |
+
# Concatenate noise and embedded labels along the channel dimension.
|
50 |
+
gen_input = torch.cat([noise, label_embedding], dim=1)
|
51 |
+
return self.main(gen_input)
|
52 |
+
|
53 |
+
|
54 |
+
class Discriminator(nn.Module):
|
55 |
+
def __init__(self, config):
|
56 |
+
super(Discriminator, self).__init__()
|
57 |
+
|
58 |
+
self.ndf = config["ndf"]
|
59 |
+
self.nc = config["nc"]
|
60 |
+
|
61 |
+
self.n_classes = config["num_classes"]
|
62 |
+
self.embed_dim = config["embed_dim"]
|
63 |
+
|
64 |
+
# Label embedding: maps labels to vectors of size embed_dim.
|
65 |
+
self.label_embed = nn.Embedding(self.n_classes, self.embed_dim)
|
66 |
+
|
67 |
+
# DCGAN discriminator architecture
|
68 |
+
self.main = nn.Sequential(
|
69 |
+
|
70 |
+
# Input: (nc + embed_dim) x 28 x 28
|
71 |
+
nn.Conv2d(self.nc + self.embed_dim, self.ndf, kernel_size=4, stride=2, padding=1, bias=False),
|
72 |
+
nn.LeakyReLU(0.2, inplace=True),
|
73 |
+
|
74 |
+
# State: (ndf) x 14 x 14
|
75 |
+
nn.Conv2d(self.ndf, self.ndf * 2, kernel_size=4, stride=2, padding=1, bias=False),
|
76 |
+
nn.BatchNorm2d(self.ndf * 2),
|
77 |
+
nn.LeakyReLU(0.2, inplace=True),
|
78 |
+
|
79 |
+
# State: (ndf*2) x 1 x 1
|
80 |
+
nn.Conv2d(self.ndf * 2, 1, kernel_size=7, stride=1, padding=0, bias=False),
|
81 |
+
nn.Sigmoid()
|
82 |
+
)
|
83 |
+
|
84 |
+
def forward(self, img, labels):
|
85 |
+
# Embed the labels and replicate them spatially
|
86 |
+
label_embedding = self.label_embed(labels).unsqueeze(2).unsqueeze(3)
|
87 |
+
# Assume img is of shape (batch, nc, H, W)
|
88 |
+
label_embedding = label_embedding.expand(-1, -1, img.size(2), img.size(3))
|
89 |
+
|
90 |
+
# Concatenate the image with the label embedding
|
91 |
+
d_in = torch.cat((img, label_embedding), dim=1)
|
92 |
+
|
93 |
+
return self.main(d_in).view(-1, 1).squeeze(1)
|
cgan_mnist.png
ADDED
![]() |
discriminator.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:be8809f80b6c844cd2e763fd2b0d9448d4193a3e8674f2053aa4ded093daa66e
|
3 |
+
size 765962
|
generator.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5a29a4bd5c0eab11f7f920701a56cc8512a479a074c24110c7e2c1a303210c61
|
3 |
+
size 10165962
|