CoolFace
Modelpublic

momentarek1/Text-to-Face_Generation

sourceHugging Faceupdated 1mo agoView on Hugging Face
0likes
Model Card

๐Ÿง‘โ€๐ŸŽจ Text-to-Face Generation with BERT, GAN & CelebA

A deep learning project for text-to-face image generation, where natural-language descriptions are converted into realistic facial images using Sentence-BERT embeddings and a conditional Generative Adversarial Network (GAN).

The project contains two text-conditioned face generation models:

  1. 1.Baseline Text-to-Face Generator โ€” a lightweight conditional generator.
  2. 2.Attention-based Text-to-Face GAN โ€” an improved architecture using Self-Attention, Spectral Normalization, and a conditional Discriminator.

๐Ÿ“Œ Project Overview

The goal of this project is to generate a face image from a textual description.

For example, given:

"The female has high cheekbones. Her hair is black. She has arched eyebrows, a big nose and bushy eyebrows. She is young, smiling and wearing lipstick."

the model attempts to generate a corresponding face.

The overall pipeline is:

text
Text Description
       โ”‚
       โ–ผ
Sentence-BERT
       โ”‚
       โ–ผ
768-D Text Embedding
       โ”‚
       โ–ผ
Text Conditioning
       โ”‚
       +
Random Noise
       โ”‚
       โ–ผ
Generator
       โ”‚
       โ–ผ
Generated Face

The project uses the CelebA dataset with natural-language descriptions associated with facial images.


๐ŸŽฏ Objectives

The project aims to explore:

  • โ€”Text-to-image generation
  • โ€”Conditional GANs
  • โ€”Text embeddings
  • โ€”Sentence-BERT
  • โ€”Face generation
  • โ€”GAN architecture design
  • โ€”Self-Attention
  • โ€”Spectral Normalization
  • โ€”Conditional Discriminators
  • โ€”Image-text matching
  • โ€”Generative model training

๐Ÿ—‚๏ธ Dataset

The project uses the CelebA (CelebFaces Attributes Dataset).

CelebA contains large-scale celebrity face images together with facial attribute annotations.

The project additionally uses textual descriptions generated from the facial attributes.

Dataset sources

  • โ€”Kaggle CelebA Dataset
  • โ€”CUHK Multimedia Lab โ€” CelebA

The project expects a dataset containing:

text
Face Images
      +
Text Descriptions
      +
CelebA Attribute Information

๐Ÿ“ Text Descriptions

Each image is associated with one or more natural-language descriptions.

Example:

text
The female has pretty high cheekbones and an oval face.
She has brown hair.
She has arched eyebrows and a pointy nose.
She is smiling, seems attractive and young.
She has rosy cheeks and heavy makeup.
She is wearing earrings and lipstick.

Another example:

text
He wears a 5 o'clock shadow.
His hair is brown and straight.
He has a slightly open mouth and a pointy nose.
He looks attractive and young and is smiling.
He is wearing a necktie.

These descriptions provide the semantic information used to condition the image generator.


๐Ÿง  Text Encoder

The project uses:

text
SentenceTransformer

with:

text
all-mpnet-base-v2

The model converts each textual description into a 768-dimensional semantic embedding.

python
SentenceTransformer("all-mpnet-base-v2")

The descriptions are split into sentences and each sentence is encoded individually.

The resulting sentence embeddings are then averaged:

text
Text
 โ”‚
 โ”œโ”€โ”€ Sentence 1 โ”€โ”€โ–บ Embedding
 โ”œโ”€โ”€ Sentence 2 โ”€โ”€โ–บ Embedding
 โ”œโ”€โ”€ Sentence 3 โ”€โ”€โ–บ Embedding
 โ””โ”€โ”€ Sentence N โ”€โ”€โ–บ Embedding
                    โ”‚
                    โ–ผ
              Mean Embedding
                    โ”‚
                    โ–ผ
                 768-D

This produces a fixed-size representation regardless of the number of sentences.


๐Ÿ”ข Text Embedding

The initial embedding size is:

text
768

The embedding is then reduced to:

text
256

using a projection layer.

text
Sentence-BERT
     โ”‚
     โ–ผ
768 dimensions
     โ”‚
     โ–ผ
Linear Layer
     โ”‚
     โ–ผ
256 dimensions

๐Ÿ—๏ธ Model 1 โ€” Baseline Text-to-Face Generator

The first model is a conditional image generator that combines:

  • โ€”Random noise
  • โ€”Text embeddings
  • โ€”Transposed convolution layers
  • โ€”Batch Normalization
  • โ€”ReLU / LeakyReLU
  • โ€”Tanh output

The model generates RGB face images.


Architecture

The input consists of:

text
Random Noise
100 dimensions

and:

text
Text Embedding
768 dimensions

The text embedding is projected to:

text
256 dimensions

The generator then concatenates the noise and text representation.

text
                Text Description
                       โ”‚
                       โ–ผ
                Sentence-BERT
                       โ”‚
                       โ–ผ
                    768-D
                       โ”‚
                       โ–ผ
                 Projection
                       โ”‚
                       โ–ผ
                    256-D
                       โ”‚
                       โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                       โ”‚            โ”‚
                       โ–ผ            โ–ผ
                 Random Noise     Text
                   100-D          256-D
                       โ”‚            โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ–ผ
                    Conditional Input
                             โ”‚
                             โ–ผ
                    ConvTranspose2D
                             โ”‚
                             โ–ผ
                          4 ร— 4
                             โ”‚
                             โ–ผ
                          8 ร— 8
                             โ”‚
                             โ–ผ
                         16 ร— 16
                             โ”‚
                             โ–ผ
                         32 ร— 32
                             โ”‚
                             โ–ผ
                         64 ร— 64
                             โ”‚
                             โ–ผ
                       RGB Image

Generator Configuration

python
model = Generator(
    100,     # noise size
    128,     # feature size
    3,       # RGB channels
    768,     # embedding size
    256      # reduced embedding size
)

Main parameters

ParameterValue
Noise Dimension100
Initial Feature Size128
Text Embedding768
Reduced Embedding256
Output Channels3
OptimizerAdam
Learning Rate0.0002
Betas(0.5, 0.5)

๐Ÿ–ผ๏ธ Generated Image Resolution

The baseline generator progressively upsamples the feature representation:

text
4 ร— 4
  โ†“
8 ร— 8
  โ†“
16 ร— 16
  โ†“
32 ร— 32
  โ†“
64 ร— 64
  โ†“
128 ร— 128

The final output is an RGB face image.


๐Ÿ’พ Pretrained Generator

A trained generator checkpoint can be loaded using:

python
model.load_state_dict(
    torch.load(
        "generator_50k.pth",
        map_location="cpu"
    )
)

model.eval()

This allows the model to generate faces directly from new text descriptions without retraining.


๐Ÿงช Baseline Inference

Example:

python
test_noise = torch.randn(
    size=(1, 100)
)

test_embeddings = sentence_encoder.convert_text_to_embeddings([
    "The female has pretty high cheekbones and an oval face. "
    "She has brown hair. She has arched eyebrows and a pointy nose. "
    "She is smiling, seems attractive, young, has rosy cheeks and heavy makeup. "
    "She is wearing earrings and lipstick."
])

test_image = model(
    test_noise,
    test_embeddings
)

The generated image is then visualized using torchvision.


๐Ÿง  Model 2 โ€” Attention-Based Text-to-Face GAN

The second model is a more advanced conditional GAN architecture.

It contains:

  • โ€”Conditional Generator
  • โ€”Conditional Discriminator
  • โ€”Sentence-BERT
  • โ€”Self-Attention
  • โ€”Spectral Normalization
  • โ€”Batch Normalization
  • โ€”Transposed Convolution
  • โ€”Conditional image-text discrimination

The architecture is designed to improve image quality and capture long-range spatial relationships in facial features.


๐Ÿ—๏ธ Generator Architecture

The second generator receives:

text
Text Embedding
+
Random Noise

The text embedding is processed through:

text
768
 โ†“
256
 โ†“
100

The resulting representation is combined with the random noise through element-wise multiplication.

python
concat_input = torch.mul(
    noise,
    encoded_text
)

Generator Pipeline

text
                    Text
                     โ”‚
                     โ–ผ
              Sentence-BERT
                     โ”‚
                     โ–ผ
                   768-D
                     โ”‚
                     โ–ผ
                  Linear
                     โ”‚
                     โ–ผ
                   256-D
                     โ”‚
                     โ–ผ
                  Linear
                     โ”‚
                     โ–ผ
                   100-D
                     โ”‚
                     โ”‚
Random Noise โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
                     โ–ผ
              Element-wise
               Multiplication
                     โ”‚
                     โ–ผ
                  1 ร— 1
                     โ”‚
                     โ–ผ
               4 ร— 4 Feature
                     โ”‚
                     โ–ผ
               8 ร— 8 Feature
                     โ”‚
                     โ–ผ
              16 ร— 16 Feature
                     โ”‚
                     โ–ผ
                Self-Attention
                     โ”‚
                     โ–ผ
              32 ร— 32 Feature
                     โ”‚
                     โ–ผ
                Self-Attention
                     โ”‚
                     โ–ผ
              64 ร— 64 Feature
                     โ”‚
                     โ–ผ
                Self-Attention
                     โ”‚
                     โ–ผ
             128 ร— 128 Image

๐Ÿ‘๏ธ Self-Attention

The generator includes custom Self-Attention modules.

The attention mechanism allows the model to establish relationships between distant spatial locations.

This is useful for face generation because facial features are not completely independent.

For example:

text
Eyes
 โ”‚
 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ–บ Nose
 โ”‚
 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ–บ Mouth
 โ”‚
 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Face Shape

Instead of only processing local convolutional features, Self-Attention allows the network to model broader spatial relationships.


๐Ÿ”ฌ Self-Attention Architecture

The module uses three projections:

text
Query
Key
Value

implemented using convolution layers:

python
self.query_conv
self.key_conv
self.value_conv

Attention scores are calculated using matrix multiplication:

text
Query ร— Key
     โ”‚
     โ–ผ
 Softmax
     โ”‚
     โ–ผ
Attention Map
     โ”‚
     โ–ผ
Value

The attention output is then combined with the original feature map through a learnable parameter:

text
Output = ฮณ ร— Attention + Input

๐Ÿ›ก๏ธ Spectral Normalization

The advanced architecture also implements Spectral Normalization for convolutional layers.

Spectral normalization constrains the magnitude of the network weights and helps stabilize GAN training.

It is applied to:

text
Generator
    โ”‚
    โ”œโ”€โ”€ ConvTranspose2D
    โ””โ”€โ”€ ConvTranspose2D

Discriminator
    โ”‚
    โ””โ”€โ”€ Conv2D

Conceptually:

text
Convolution
     โ”‚
     โ–ผ
Spectral Normalization
     โ”‚
     โ–ผ
Controlled Weight Magnitude
     โ”‚
     โ–ผ
More Stable GAN Training

๐Ÿ•ต๏ธ Discriminator

The discriminator determines whether an image is:

text
Real
   or
Fake

but this model also receives the corresponding text description.

Therefore, it performs conditional discrimination.

Instead of asking only:

Is this image real?

the discriminator effectively evaluates:

Is this image realistic and consistent with the given text?

๐Ÿ”„ Conditional Discriminator

The architecture is:

text
                 Image
                   โ”‚
                   โ–ผ
             CNN Encoder
                   โ”‚
                   โ–ผ
            Image Features
                   โ”‚
                   โ”‚
Text โ”€โ”€โ–บ Sentence-BERT
                   โ”‚
                   โ–ผ
             Text Encoder
                   โ”‚
                   โ–ผ
            Text Features
                   โ”‚
             โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”
             โ”‚           โ”‚
             โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜
                   โ–ผ
           Image + Text
              Features
                   โ”‚
                   โ–ผ
             CNN Layers
                   โ”‚
                   โ–ผ
          Real / Fake Score

๐Ÿงฉ Wrong Image Training

An important part of the discriminator training is the use of wrong image-text pairs.

For each text description:

text
Correct Pair

Text โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Correct Face

and:

text
Incorrect Pair

Text โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Different Face

The discriminator learns to distinguish between:

text
Real Image + Correct Text

and:

text
Wrong Image + Text
Fake Image + Text

This encourages the generated image to be semantically related to the description.


๐Ÿ“Š Dataset Pipeline

The custom dataset returns:

python
true_image
true_text
wrong_image

The pipeline is:

text
CelebA
   โ”‚
   โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Real Image
   โ”‚
   โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Text Description
   โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Random Wrong Image

Images are resized to:

text
128 ร— 128

and normalized using:

python
transforms.Normalize(
    mean=(0.5),
    std=(0.5)
)

โš™๏ธ Training Configuration

The advanced model uses:

ParameterValue
Epochs20
Batch Size16
Dataset Subset20,000
Noise Size100
Feature Size64
Image Size128 ร— 128
Channels3
Text Embedding768
Reduced Embedding256
Generator LR0.0002
Discriminator LR0.0002
AttentionEnabled
OptimizerAdam

โš”๏ธ GAN Training

The training process alternates between the Generator and Discriminator.

text
                 Text
                  โ”‚
                  โ–ผ
            Text Encoder
                  โ”‚
                  โ–ผ
             Embeddings
                  โ”‚
                  โ–ผ
              Generator
                  โ–ฒ
                  โ”‚
             Random Noise
                  โ”‚
                  โ–ผ
             Fake Image
                  โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ”‚                โ”‚
          โ–ผ                โ–ผ
      Generator        Discriminator
        Loss               โ”‚
                           โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚            โ”‚            โ”‚
              โ–ผ            โ–ผ            โ–ผ
           Real Image   Wrong Image   Fake Image

๐ŸŽฏ Generator Objective

The generator attempts to fool the discriminator.

text
Text + Noise
     โ”‚
     โ–ผ
Generator
     โ”‚
     โ–ผ
Fake Face
     โ”‚
     โ–ผ
Discriminator
     โ”‚
     โ–ผ
Real?

The generator loss is calculated using:

python
nn.BCELoss()

and attempts to make the discriminator classify generated images as real.


๐Ÿ›ก๏ธ Discriminator Objective

The discriminator receives three types of examples:

1. Real Image + Correct Text

Expected:

text
1

2. Wrong Image + Text

Expected:

text
0

3. Generated Image + Text

Expected:

text
0

Therefore:

text
Discriminator Loss
      โ”‚
      โ”œโ”€โ”€ Real Loss
      โ”œโ”€โ”€ Wrong Pair Loss
      โ””โ”€โ”€ Fake Loss

๐Ÿ“ˆ Experiment Tracking

The project uses:

Weights & Biases (W&B) for experiment tracking.

python
wandb.init(
    project="text-to-face",
    name="n-sagan"
)

The following metrics are tracked:

text
Generator Loss
Discriminator Loss
Generated Images

Generated images are logged after each configured epoch.


๐Ÿ–ผ๏ธ Visualization

The project visualizes generated images using:

text
Matplotlib
+
Torchvision

Generated images can be arranged into grids:

python
torchvision.utils.make_grid(
    output,
    normalize=True
)

This makes it possible to monitor image quality during training.


๐Ÿ”ฌ Model Comparison

The project contains two approaches:

FeatureBaseline GeneratorAttention GAN
Text Conditioningโœ“โœ“
Sentence-BERTโœ“โœ“
Random Noiseโœ“โœ“
ConvTransposeโœ“โœ“
Conditional Generationโœ“โœ“
Discriminatorโ€”โœ“
Self-Attentionโ€”โœ“
Spectral Normalizationโ€”โœ“
Wrong Image Pairsโ€”โœ“
W&B Trackingโ€”โœ“
GAN TrainingPartialโœ“
OutputFaceFace

๐Ÿ”„ Overall Architecture

text
                         Text Description
                                โ”‚
                                โ–ผ
                      Sentence-BERT
                       all-mpnet-base-v2
                                โ”‚
                                โ–ผ
                         768-D Embedding
                                โ”‚
                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚                       โ”‚
                    โ–ผ                       โ–ผ
              Baseline Model          Attention GAN
                    โ”‚                       โ”‚
                    โ”‚                  Text Projection
                    โ”‚                       โ”‚
                    โ”‚                       โ–ผ
                    โ”‚                  Random Noise
                    โ”‚                       โ”‚
                    โ”‚                       โ–ผ
                    โ”‚                  Generator
                    โ”‚                       โ”‚
                    โ”‚              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚              โ”‚                 โ”‚
                    โ”‚              โ–ผ                 โ–ผ
                    โ”‚        Self-Attention   Spectral Norm
                    โ”‚              โ”‚
                    โ”‚              โ–ผ
                    โ”‚         Generated Face
                    โ”‚              โ”‚
                    โ”‚              โ–ผ
                    โ”‚         Discriminator
                    โ”‚              โ–ฒ
                    โ”‚              โ”‚
                    โ”‚         Real / Wrong
                    โ”‚            Images
                    โ”‚
                    โ–ผ
              Generated Face

๐Ÿ› ๏ธ Technologies

Deep Learning

  • โ€”PyTorch
  • โ€”Torchvision
  • โ€”PyTorch Neural Networks

Natural Language Processing

  • โ€”Sentence Transformers
  • โ€”all-mpnet-base-v2
  • โ€”Text Embeddings

Computer Vision

  • โ€”OpenCV
  • โ€”PIL
  • โ€”Torchvision
  • โ€”Matplotlib

Machine Learning

  • โ€”NumPy
  • โ€”Pandas

GAN / Generative AI

  • โ€”Conditional GAN
  • โ€”Self-Attention
  • โ€”Spectral Normalization
  • โ€”Transposed Convolution

Experiment Tracking

  • โ€”Weights & Biases

Dataset

  • โ€”CelebA

๐Ÿ“ฆ Installation

Clone the repository:

bash
git clone https://github.com/kad99kev/FGTD.git

Move into the project directory:

bash
cd FGTD

Install dependencies:

bash
pip install -r requirements.txt

Install the main deep-learning dependencies if required:

bash
pip install torch torchvision

Install Sentence Transformers:

bash
pip install sentence-transformers

Install experiment tracking:

bash
pip install wandb

๐Ÿš€ Running the Project

1. Load the Text Encoder

python
from sentence_transformers import SentenceTransformer

sentence_encoder = SentenceTransformer(
    "all-mpnet-base-v2"
)

2. Generate an Image from Text

Prepare a text description:

python
text = [
    "The female has high cheekbones and black hair. "
    "She is young and smiling."
]

Convert it into an embedding:

text
Text
 โ†“
Sentence-BERT
 โ†“
768-D Embedding

Generate a random latent vector:

python
noise = torch.randn(
    1,
    100
)

Then pass both into the generator:

python
image = generator(
    noise,
    text_embeddings
)

๐Ÿงช Example Prompts

Female Face

text
The female has pretty high cheekbones and an oval face.
Her hair is black.
She has arched eyebrows and a pointy nose.
She is smiling and looks young.
She is wearing earrings and lipstick.

Male Face

text
The man is young and attractive.
He has brown straight hair and a pointy nose.
He is smiling and wearing a necktie.

Different Facial Attributes

text
The man has a double chin and high cheekbones.
He has black hair and big lips.
He looks young.

๐Ÿ“ Suggested Repository Structure

text
Text-to-Face-GAN/
โ”‚
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ requirements.txt
โ”‚
โ”œโ”€โ”€ notebooks/
โ”‚   โ”œโ”€โ”€ baseline_text_to_face.ipynb
โ”‚   โ””โ”€โ”€ attention_text_to_face_gan.ipynb
โ”‚
โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ generator.py
โ”‚   โ”œโ”€โ”€ discriminator.py
โ”‚   โ”œโ”€โ”€ attention.py
โ”‚   โ””โ”€โ”€ spectral_norm.py
โ”‚
โ”œโ”€โ”€ text_encoder/
โ”‚   โ””โ”€โ”€ sentence_encoder.py
โ”‚
โ”œโ”€โ”€ dataset/
โ”‚   โ”œโ”€โ”€ text_5_descr_celeba.csv
โ”‚   โ””โ”€โ”€ list_attr_celeba.csv
โ”‚
โ”œโ”€โ”€ checkpoints/
โ”‚   โ””โ”€โ”€ generator_50k.pth
โ”‚
โ”œโ”€โ”€ outputs/
โ”‚   โ””โ”€โ”€ generated_faces/
โ”‚
โ””โ”€โ”€ results/
    โ””โ”€โ”€ wandb/

๐Ÿง  Key Concepts Demonstrated

Natural Language Processing

  • โ€”Sentence Embeddings
  • โ€”Sentence-BERT
  • โ€”Semantic Representation
  • โ€”Text Conditioning

Computer Vision

  • โ€”Face Generation
  • โ€”Image Preprocessing
  • โ€”Image Normalization
  • โ€”Image Visualization

Deep Learning

  • โ€”PyTorch
  • โ€”CNNs
  • โ€”Transposed Convolution
  • โ€”Batch Normalization
  • โ€”ReLU
  • โ€”Tanh

Generative AI

  • โ€”GANs
  • โ€”Conditional GANs
  • โ€”Text-to-Image Generation
  • โ€”Latent Noise
  • โ€”Generator / Discriminator Training

Advanced GAN Techniques

  • โ€”Self-Attention
  • โ€”Spectral Normalization
  • โ€”Conditional Discrimination
  • โ€”Wrong Image-Text Pairing

๐Ÿ” Important Design Decisions

Why Sentence-BERT?

Instead of treating text as individual words, Sentence-BERT provides a semantic representation of the entire description.

text
Natural Language
      โ†“
Semantic Embedding
      โ†“
768-D Vector
      โ†“
Generator Conditioning

This allows descriptions containing multiple facial attributes to be represented in a compact vector.


Why Conditional GAN?

A normal GAN learns:

text
Random Noise โ†’ Image

This project instead learns:

text
Random Noise + Text โ†’ Image

Therefore, the generated image can be influenced by the supplied description.


Why Self-Attention?

Convolutional layers are excellent at learning local patterns, while Self-Attention helps the network model relationships between distant regions of an image.

This can be useful when generating coherent facial structures.


Why Spectral Normalization?

GAN training can be unstable.

Spectral Normalization helps constrain the network's weight matrices and can improve training stability, particularly in the discriminator.


๐Ÿ“Š Expected Workflow

text
              CelebA Dataset
                    โ”‚
                    โ–ผ
            Image + Caption
                    โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ”‚                   โ”‚
          โ–ผ                   โ–ผ
       Image              Text
          โ”‚                   โ”‚
          โ”‚                   โ–ผ
          โ”‚             Sentence-BERT
          โ”‚                   โ”‚
          โ”‚                   โ–ผ
          โ”‚                768-D
          โ”‚                   โ”‚
          โ”‚                   โ–ผ
          โ”‚              Projection
          โ”‚                   โ”‚
          โ”‚                   โ–ผ
          โ”‚                Text Code
          โ”‚                   โ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ–ผ
                  GAN
                     โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ–ผ                     โ–ผ
      Generator           Discriminator
          โ”‚                     โ”‚
          โ–ผ                     โ”‚
      Fake Face โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
                                โ”‚
      Real Face โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
                                โ”‚
      Wrong Face โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
                                โ–ผ
                         Real / Fake

โš ๏ธ Implementation Notes

The project is primarily an experimental research implementation.

Several aspects can be improved for a cleaner production/research implementation:

  • โ€”Separate training and inference scripts.
  • โ€”Add explicit validation/testing datasets.
  • โ€”Save both Generator and Discriminator checkpoints.
  • โ€”Add automatic checkpoint recovery.
  • โ€”Track additional GAN metrics.
  • โ€”Evaluate generated image quality quantitatively.
  • โ€”Use a dedicated configuration file.
  • โ€”Avoid hard-coded CUDA calls and use the configured device consistently.
  • โ€”Add reproducible random seeds.
  • โ€”Add automated experiment logging.

For example, instead of:

python
generator.cuda()

a more portable approach is:

python
generator.to(cfg.device)

This allows the project to run on either GPU or CPU.


๐Ÿš€ Future Improvements

Possible extensions include:

  • โ€”Increase image resolution to 256ร—256.
  • โ€”Experiment with larger text encoders.
  • โ€”Use CLIP-based text-image alignment.
  • โ€”Add perceptual loss.
  • โ€”Add text-image similarity metrics.
  • โ€”Add FID evaluation.
  • โ€”Add Inception Score.
  • โ€”Improve caption diversity.
  • โ€”Experiment with different GAN architectures.
  • โ€”Compare Self-Attention vs standard convolution.
  • โ€”Implement progressive image generation.
  • โ€”Add mixed-precision training.
  • โ€”Add distributed training.
  • โ€”Improve dataset balancing.
  • โ€”Add automated checkpointing.
  • โ€”Build a web interface for text-to-face generation.

๐Ÿ“š Learning Outcomes

This project provides practical experience with:

  • โ€”Text-to-image generation
  • โ€”Conditional GANs
  • โ€”PyTorch
  • โ€”Sentence Transformers
  • โ€”BERT-based embeddings
  • โ€”CNN architectures
  • โ€”Transposed convolution
  • โ€”GAN optimization
  • โ€”Generator/Discriminator training
  • โ€”Self-Attention
  • โ€”Spectral Normalization
  • โ€”CelebA preprocessing
  • โ€”Image-text conditioning
  • โ€”Experiment tracking with W&B

๐Ÿ”‘ Keywords

text
Text-to-Image
Text-to-Face
Face Generation
Generative AI
GAN
Conditional GAN
cGAN
PyTorch
Sentence-BERT
Sentence Transformers
all-mpnet-base-v2
CelebA
Computer Vision
Deep Learning
Self-Attention
Spectral Normalization
Image Generation
Natural Language Processing
Multimodal AI
Generative Models

๐Ÿ“œ Disclaimer

This project is intended for educational, research, and experimental purposes.

Generated faces are synthetic outputs produced by a machine-learning model and should not be interpreted as photographs or evidence of real individuals.


๐Ÿ‘จโ€๐Ÿ’ป Project Summary

Text-to-Face Generation with BERT & Conditional GANs is a multimodal generative AI project that connects natural-language descriptions with facial image generation.

The project progresses from a baseline text-conditioned generator to an advanced GAN architecture incorporating Sentence-BERT embeddings, Self-Attention, Spectral Normalization, and a conditional Discriminator.

text
Text
 โ†“
Sentence-BERT
 โ†“
768-D Embedding
 โ†“
Text Conditioning
 +
Random Noise
 โ†“
Generator
 โ†“
Generated Face
 โ†“
Conditional Discriminator
 โ†“
GAN Training

The project demonstrates how NLP and Computer Vision can be combined into a single generative AI pipeline for text-guided face synthesis.