Skip to content

saurav714/Building-a-simple-Generative-Adversarial-Network-GAN-using-TensorFlow

Repository files navigation

🎨 GANs in TensorFlow (Google Colab)

This project implements a basic Generative Adversarial Network (GAN) using TensorFlow, inspired by a DigitalOcean tutorial. The GAN is trained on the MNIST dataset to generate realistic images of handwritten digits. Designed to run smoothly on Google Colab with GPU acceleration.


πŸš€ Features

  • πŸ—‚ Dataset: MNIST (28Γ—28 grayscale images)
  • βš™ Generator:
    • Input: 100-dimensional random noise vector
    • Output: 784-dimensional vector reshaped to 28Γ—28 image
  • πŸ›‘ Discriminator:
    • Input: 784-dimensional image
    • Output: Probability of image being real (0–1)
  • 🎯 Training:
    • Binary cross-entropy loss
    • Optimizer: Adam (lr = 0.0001)
    • Alternating Generator/Discriminator updates
  • πŸ–Ό Visualization:
    • Saves 4Γ—4 image grid after each epoch

πŸ“‹ Prerequisites

Google Colab comes pre-installed with most libraries. If needed, install manually:

!pip install tensorflow numpy matplotlib
πŸ§ͺ Setup Instructions (Google Colab)
πŸ”— Open Google Colab: https://colab.research.google.com

βž• New Notebook: File β†’ New Notebook

πŸ“¦ Install Dependencies (if required):

python
Copy
Edit
!pip install tensorflow numpy matplotlib
πŸ“€ Upload Code:

Paste the contents of main.py into a cell, or

Use sidebar β†’ "Files" tab β†’ Upload main.py

πŸ’Ύ Optional: Mount Google Drive (for saving outputs):

python
Copy
Edit
from google.colab import drive
drive.mount('/content/drive')
πŸ”„ Clone Your GitHub Repo (optional):

bash
Copy
Edit
!git config --global user.name "your-username"
!git config --global user.email "[email protected]"
!git clone https://github.com/your-username/your-repo-name.git
πŸ’‘ Usage
Train the GAN:

Default: 50 epochs

Losses printed every epoch

View Outputs:

Images saved as image_at_epoch_000X.png

Found in "Files" tab or in Google Drive (if mounted)

πŸ’Ύ Save & Push to GitHub:
bash
Copy
Edit
%cd /content/your-repo-name
!cp /content/image_at_epoch_*.png .
!git add .
!git commit -m "Add GAN code and generated images from Colab"
!git push origin main
πŸ” You may need a GitHub personal access token.

πŸ“ Project Structure
bash
Copy
Edit
gan_project/
β”œβ”€β”€ main.py                # Main GAN training script
β”œβ”€β”€ image_at_epoch_*.png  # Generated images
└── requirements.txt       # (Optional) Python dependencies
🧠 How It Works
πŸ”„ Data Preprocessing
Normalize MNIST images to [-1, 1]

Flatten 28Γ—28 images β†’ 784D vectors

🧬 Generator Architecture
text
Copy
Edit
Input: 100D noise
β†’ Dense(256) β†’ LeakyReLU
β†’ Dense(512) β†’ LeakyReLU
β†’ Dense(784, activation='tanh')
πŸ›‘ Discriminator Architecture
text
Copy
Edit
Input: 784D image
β†’ Dense(512) β†’ LeakyReLU
β†’ Dense(256) β†’ LeakyReLU
β†’ Dense(1, activation='sigmoid')
πŸŽ“ Training Strategy
Discriminator learns real vs fake

Generator improves to fool Discriminator

Alternate training with binary cross-entropy loss

βš™οΈ Colab Tips
Enable GPU: Runtime β†’ Change runtime type β†’ Select "GPU"

Stability: If training is unstable, try tweaking:

Learning rate

Batch size

Architecture layers

Storage: Save images to Google Drive before Colab resets

πŸ“¦ Requirements
txt
Copy
Edit
tensorflow>=2.0
numpy
matplotlib
🀝 Contributing
Fork the repo

Create a new branch:

bash
Copy
Edit
git checkout -b feature/your-feature
Commit & Push:

bash
Copy
Edit
git add .
git commit -m "Add your feature"
git push origin feature/your-feature
Open a Pull Request

πŸ“„ License
This project is licensed under the MIT License.
See the LICENSE file for details.

πŸ™ Acknowledgments
Based on a DigitalOcean tutorial

Built using TensorFlow, NumPy, and Matplotlib

Powered by Google Colab

🌐 Live Demo (Optional)
You can run it in Colab by uploading main.py.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published