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.
- π 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
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.