File size: 8,854 Bytes
398ce88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52dae7d
398ce88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae6b1d3
398ce88
 
ae6b1d3
 
 
 
 
 
398ce88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae6b1d3
398ce88
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import gradio as gr
import torch
import numpy as np
from PIL import Image
import time
from model import Generator, Discriminator

# Configuration
LATENT_DIM = 100
IMG_SHAPE = (1, 28, 28)
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Load the trained models
generator = Generator(latent_dim=LATENT_DIM, img_shape=IMG_SHAPE).to(DEVICE)
generator.load_state_dict(torch.load('generator.pth', map_location=DEVICE))
generator.eval()

discriminator = Discriminator(img_shape=IMG_SHAPE).to(DEVICE)
discriminator.load_state_dict(torch.load('discriminator.pth', map_location=DEVICE))
discriminator.eval()

def generate_digits(num_images, seed, show_confidence):
    """Generate digits and return with optional confidence scores"""
    start_time = time.time()
    
    with torch.no_grad():
        # Set seed for reproducibility if provided
        if seed != 0:
            torch.manual_seed(seed)
            np.random.seed(int(seed))
        
        # Generate random noise
        num_images = int(num_images)
        z = torch.randn(num_images, LATENT_DIM).to(DEVICE)
        
        # Generate images
        generated_imgs = generator(z)
        
        # Get discriminator confidence
        confidence_scores = discriminator(generated_imgs)
        avg_confidence = confidence_scores.mean().item() * 100
        
        # Convert to numpy and denormalize
        generated_imgs = generated_imgs.cpu().numpy()
        generated_imgs = ((generated_imgs + 1) / 2 * 255).astype(np.uint8)
        
        # Create grid
        grid_size = int(np.ceil(np.sqrt(num_images)))
        grid_img = Image.new('L', (280 * grid_size, 280 * grid_size), color=255)
        
        for idx in range(num_images):
            img_pil = Image.fromarray(generated_imgs[idx][0], mode='L')
            img_pil = img_pil.resize((280, 280), Image.NEAREST)
            row = idx // grid_size
            col = idx % grid_size
            grid_img.paste(img_pil, (col * 280, row * 280))
    
    generation_time = time.time() - start_time
    
    # Build info text
    info_text = f"Generated {num_images} digit(s) in {generation_time:.3f}s"
    if show_confidence:
        info_text += f"\nDiscriminator Confidence: {avg_confidence:.1f}% (how 'real' the digits appear)"
    
    return grid_img, info_text

def create_comparison_grid():
    """Create a comparison showing training progress"""
    # Create sample images at different seeds
    seeds = [42, 123, 456, 789]
    images = []
    
    with torch.no_grad():
        for seed in seeds:
            torch.manual_seed(seed)
            z = torch.randn(1, LATENT_DIM).to(DEVICE)
            img = generator(z)
            img = ((img[0, 0].cpu().numpy() + 1) / 2 * 255).astype(np.uint8)
            img_pil = Image.fromarray(img, mode='L')
            img_pil = img_pil.resize((140, 140), Image.NEAREST)
            images.append(img_pil)
    
    # Create grid
    grid = Image.new('L', (280, 280), color=255)
    for idx, img in enumerate(images):
        row = idx // 2
        col = idx % 2
        grid.paste(img, (col * 140, row * 140))
    
    return grid

# Create interface
with gr.Blocks(title="MNIST GAN Generator", theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # Handwritten Digit Generator
        ### Using Generative Adversarial Networks (GAN)
        
        ### About This Project
        
        This GAN was trained to generate handwritten digits by learning from the MNIST dataset. 
        The generator creates images from random noise, while the discriminator learns to distinguish real from fake images.
        Through adversarial training, the generator improves until it produces realistic digits.
        
        **Created by Rohan Jain** | [LinkedIn](https://www.linkedin.com/in/jaroh23/) | [GitHub](https://github.com/rohanjain2312)
        """
    )
    
    with gr.Tabs():
        # Tab 1: Generate Digits
        with gr.TabItem("Generate Digits"):
            gr.Markdown("Generate new handwritten digits using the trained GAN model.")
            
            with gr.Row():
                with gr.Column(scale=1):
                    num_images = gr.Slider(
                        minimum=1, 
                        maximum=16, 
                        value=4, 
                        step=1, 
                        label="Number of Digits",
                        info="Generate 1-16 digits at once"
                    )
                    seed = gr.Number(
                        value=0, 
                        label="Random Seed",
                        info="Use 0 for random, or set a number for reproducible results"
                    )
                    show_confidence = gr.Checkbox(
                        value=True,
                        label="Show Discriminator Confidence",
                        info="Display how 'real' the generator fooled the discriminator"
                    )
                    generate_btn = gr.Button("Generate", variant="primary", size="lg")
                
                with gr.Column(scale=2):
                    output_image = gr.Image(label="Generated Digits", type="pil")
                    output_info = gr.Textbox(label="Generation Info", lines=2)
            
            generate_btn.click(
                fn=generate_digits,
                inputs=[num_images, seed, show_confidence],
                outputs=[output_image, output_info]
            )
            
            gr.Markdown("### Quick Examples")
            gr.Examples(
                examples=[
                    [4, 42, True],
                    [9, 123, True],
                    [16, 456, False],
                ],
                inputs=[num_images, seed, show_confidence],
                outputs=[output_image, output_info],
                fn=generate_digits,
                cache_examples=True,
            )
        
        # Tab 2: Model Information
        with gr.TabItem("Model Details"):
            gr.Markdown("### Training Loss Curves")
            gr.Image(value="loss_curve.png", label="Generator and Discriminator Loss During Training")
            
            gr.Markdown(
                """
                **Loss Analysis:**
                - The discriminator loss (orange) stabilizes around 0.3-0.4, indicating it effectively distinguishes real from fake
                - The generator loss (blue) shows typical adversarial dynamics, settling around 1.5-2.0
                - The fluctuating losses indicate healthy adversarial balance between the two networks
                """
            )
            
            with gr.Row():
                with gr.Column():
                    gr.Markdown(
                        """
                        ### Training Details
                        
                        **Architecture**: Fully Connected GAN  
                        **Dataset**: MNIST (60,000 images)  
                        **Epochs**: 200  
                        **Batch Size**: 128  
                        
                        **Generator**:
                        - Input: 100-dim random vector
                        - Layers: 128 β†’ 256 β†’ 512 β†’ 784
                        - Output: 28Γ—28 grayscale image
                        
                        **Discriminator**:
                        - Input: 28Γ—28 image (784 dims)
                        - Layers: 512 β†’ 256 β†’ 1
                        - Output: Real/Fake probability
                        """
                    )
                
                with gr.Column():
                    gr.Markdown(
                        """
                        ### Training Results
                        
                        **Loss Metrics** (Final):
                        - Discriminator Loss: ~0.32
                        - Generator Loss: ~1.99
                        
                        **Training Evolution**:
                        - Epoch 1: Random noise
                        - Epoch 50: Faint structures
                        - Epoch 100: Recognizable digits
                        - Epoch 200: High-quality digits
                        
                        The adversarial training successfully balanced both networks, 
                        resulting in realistic digit generation.
                        """
                    )
            
            gr.Markdown("### Sample Outputs (Different Seeds)")
            comparison_img = create_comparison_grid()
            gr.Image(value=comparison_img, label="Generated Samples", type="pil")
    
    gr.Markdown(
        """
        ---
                
        **Tech Stack**: PyTorch, Gradio, NumPy | **Training**: Google Colab (GPU) | **Deployment**: Hugging Face Spaces
        """
    )

if __name__ == "__main__":
    demo.launch()