Skip to content

Haseebasif7/DeepFaceEdit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 Project Overview

DeepFaceEdit is an interactive face generation and editing tool built on top of StyleGAN2-ADA, designed for manipulating facial attributes in a highly intuitive way. It enables users to generate, project, and semantically edit high-quality face images using latent space traversal techniques all through a user-friendly GUI.

Whether you're experimenting with synthetic identity creation or exploring the interpretability of GANs, DeepFaceEdit provides powerful tools to visualize and manipulate the latent space of human faces.


✨ Features

  • 🧬 Face Generation
    Generate realistic and diverse human faces from random seeds or latent vectors.

  • πŸ› οΈ Face Editing
    Modify facial attributes such as age, gender, expression, and more through semantic vector controls.

  • πŸ–ΌοΈ Face Projection
    Project real images into StyleGAN2’s latent space using inversion techniques, enabling you to edit real faces.

  • πŸ–±οΈ Interactive GUI
    Built with FreeSimpleGUI, the interface offers an intuitive editing experience without writing code.

  • ⚑ Real-time Preview
    Instantly visualize changes as you adjust latent parameters or apply vector edits.

  • πŸ’Ύ Vector Reuse
    Save and reload latent vectors for consistent editing and iterative experimentation.


πŸ”‘ Key Capabilities

🎲 Face Generation

Generate unique faces using multiple techniques:

  • Random seed generation – Explore diverse identities with different seeds.
  • Z-vector based generation – Sample from the standard latent space.
  • W-vector based generation – Leverage StyleGAN2’s intermediate latent space for better control.

🧭 Latent Space Manipulation

  • Traverse interpretable directions in latent space to control age, gender, smile, and other high-level features.
  • Combine multiple edits with fine-grained control over intensity.

πŸ“Έ Face Projection

  • Align real faces using dlib's facial landmarks.
  • Perform GAN inversion to embed real images into the latent space for further editing.


Initially Passed Z Vector for Age (Changed the atribute of smiling) :

Age Example

Initially Passed Z Vector for Mouth to Nose (Changed the atribute of gender) :

Mouth to Nose Example


πŸ“¦ Repository

πŸ“ Clone this repo with submodules:

git clone --recurse-submodules https://github.com/Haseebasif7/GAN.git
cd GAN

🧩 The --recurse-submodules flag ensures the stylegan2-ada-pytorch directory is properly cloned.

If you forgot --recurse-submodules, fix it with:

git submodule update --init --recursive

🐍 Python Setup

1. βœ… Install Python 3.11

Ensure Python 3.11 is installed. You can download it from: https://www.python.org/downloads/release/python-3110/

Check installation:

python --version

⚠️ On Windows, check "Add Python to PATH" during installation.

2. πŸ§ͺ Create and Activate a Virtual Environment

Create a virtual environment in the project root:

python -m venv venv

Activate it:

Windows:

venv\Scripts\activate

Linux/macOS:

source venv/bin/activate

3. πŸ“₯ Install Dependencies

Upgrade pip:

pip install --upgrade pip

Then install all required packages:

pip install -r requirements.txt

πŸ’‘ If you face issues installing dlib, make sure C++ build tools are available:

  • Windows: Install Microsoft Build Tools
  • Linux/macOS: Install cmake, g++, and Python headers

🎯 Download Pretrained Models

Two model files are required in the models/ folder.

βœ… 1. ffhq.pkl (StyleGAN2-ADA Pretrained Model)

Download from:

https://nvlabs-fi-cdn.nvidia.com/stylegan2-ada-pytorch/pretrained/ffhq.pkl

Then place it in the models/ folder:

GAN/
β”œβ”€β”€ models/
β”‚   └── ffhq.pkl

βœ… 2. shape_predictor_68_face_landmarks.dat (Face Landmark Detector)

Download the compressed file:

http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

Place it in the models/ folder:

GAN/
β”œβ”€β”€ models/
β”‚   └── shape_predictor_68_face_landmarks.dat.bz2

Decompress it using the included script:

python models/decompress.py

βœ… After decompression, you will have:

GAN/
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ shape_predictor_68_face_landmarks.dat
β”‚   └── shape_predictor_68_face_landmarks.dat.bz2

🧱 Git Submodule Configuration

This project uses the StyleGAN2-ADA repo as a Git submodule.

Here’s the .gitmodules content:

[submodule "stylegan2-ada-pytorch"]
  path = stylegan2-ada-pytorch
  url = https://github.com/NVlabs/stylegan2-ada-pytorch.git

Git reads this automatically when you clone with --recurse-submodules.


🧩 Project Components

  • Generator – Responsible for generating high-quality face images using the StyleGAN2-ADA model.
  • Shifter – Handles latent space edits to apply semantic transformations (e.g., smile, age).
  • Projector – Projects real-world face images into the latent space of the generator.
  • Controller – Orchestrates the interaction between backend modules and manages workflow logic.
  • Graphical Interface (GUI) – A user-friendly front-end built with FreeSimpleGUI for interactive editing and visualization.

## πŸ“ Final Project Structure (After Setup)

gan-face-editor/
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ align_faces.py
β”‚   β”œβ”€β”€ generator.py
β”‚   β”œβ”€β”€ projector.py
β”‚   └── shifter.py
β”œβ”€β”€ controller.py
β”œβ”€β”€ facegan-env/
β”œβ”€β”€ gui/
β”‚   β”œβ”€β”€ main.py
β”‚   └── layouts/
β”‚       β”œβ”€β”€ interface.py
β”‚       β”œβ”€β”€ project.py
β”‚       └── sliders.py
β”œβ”€β”€ LICENSE
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ ffhq.pkl
β”‚   β”œβ”€β”€ shape_predictor_68_face_landmarks.dat
β”‚   β”œβ”€β”€ shape_predictor_68_face_landmarks.dat.bz2
β”‚   └── decompress.py
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ results/
β”‚   β”œβ”€β”€ age(2).png
β”‚   └── m_n(2).png
β”œβ”€β”€ run.py
β”œβ”€β”€ settings/
β”‚   β”œβ”€β”€ config.py
β”‚   └── __init__.py
β”œβ”€β”€ stylegan2-ada-pytorch/
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ helpers.py
β”‚   └── __init__.py
β”œβ”€β”€ vectors/
β”‚   β”œβ”€β”€ age.npy
β”‚   β”œβ”€β”€ eye_distance.npy
β”‚   β”œβ”€β”€ eye_eyebrow_distance.npy
β”‚   β”œβ”€β”€ eye_ratio.npy
β”‚   β”œβ”€β”€ eyes_open.npy
β”‚   β”œβ”€β”€ gender.npy
β”‚   β”œβ”€β”€ lip_ratio.npy
β”‚   β”œβ”€β”€ mouth_open.npy
β”‚   β”œβ”€β”€ mouth_ratio.npy
β”‚   β”œβ”€β”€ nose_mouth_distance.npy
β”‚   β”œβ”€β”€ nose_ratio.npy
β”‚   β”œβ”€β”€ nose_tip.npy
β”‚   β”œβ”€β”€ pitch.npy
β”‚   β”œβ”€β”€ roll.npy
β”‚   β”œβ”€β”€ smile.npy
β”‚   └── yaw.npy

πŸ› οΈ Technologies Used

  • Python
  • VGG16
  • PyTorch
  • StyleGAN2-ADA
  • dlib
  • FreeSimpleGUI

About

This project enables the inversion of real facial images into the latent space of StyleGAN2-ADA, leveraging dlib based facial landmark detection for accurate alignment, and supports semantic attribute editing through controlled latent vector perturbations along interpretable directions

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages