A newer version of this model is available: moonshotai/Kimi-K2-Instruct

🧠 DCGAN: Generate Realistic Human Faces using PyTorch

This project uses Deep Convolutional GAN (DCGAN) architecture implemented in PyTorch to generate realistic human face images from a noise vector. It includes full training and image generation pipeline.


πŸ”§ Features

  • Uses torchvision.datasets.ImageFolder to load real face images.
  • Implements a DCGAN:
    • Generator using ConvTranspose2d layers.
    • Discriminator using Conv2d layers without Sigmoid.
  • Trains on 64x64 images.
  • Supports CUDA (GPU) acceleration.
  • Saves sample images every 10 epochs.
  • Includes function to generate and display one random face at the end.

πŸ“ Folder Structure

project/
β”‚
β”œβ”€β”€ images/ # Folder with real training face images
β”‚ β”œβ”€β”€ class_x/ # ImageFolder requires class subdirectories
β”‚ β”œβ”€β”€ image1.jpg
β”‚ β”œβ”€β”€ image2.jpg
β”‚
β”œβ”€β”€ generated_epoch_10.png # Sample outputs saved every 10 epochs
β”œβ”€β”€ generator.pth # Saved Generator weights after training
β”œβ”€β”€ gan_faces.py # Python script with model and training code
└── README.md # This file

πŸš€ Getting Started

βœ… Prerequisites

Install Python and required libraries:

pip install torch torchvision matplotlib tqdm

Make sure you have a folder ./images/ structured like:

images/
└── faces/
    β”œβ”€β”€ img1.jpg
    β”œβ”€β”€ img2.jpg

Where faces/ is any class name (required by ImageFolder).

🧠 How It Works

  • Generator: Takes a random noise vector and generates a 64x64x3 image.

  • Discriminator: Takes a real or fake image and returns a score (real or fake).

  • Loss Function: BCEWithLogitsLoss is used for both Generator and Discriminator.

  • Label Smoothing: Real images use soft labels (0.9 instead of 1.0) to stabilize training.

βš™οΈ Training

To start training:

python gan_faces.py

The script will:

  • Load images from ./images/

  • Train for 100 epochs (can be changed)

  • Save sample images every 10 epochs as generated_epoch_X.png

  • Save final model as generator.pth

πŸ§ͺ Generate One Face

At the end of training, a sample face will be displayed using matplotlib.

  • You can also use this standalone function:
generate_one_face("generator.pth")

To generate and visualize a face anytime.

Downloads last month
-
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for ankitkushwaha90/DCGAN

Adapter
(2)
this model

Dataset used to train ankitkushwaha90/DCGAN