Session 04🧬

Generative Models & Face Recognition

GAN and diffusion training principles, followed by identification, verification, embeddings, and metric learning for faces.


Learning Objectives

  • Distinguish discriminative and generative modeling objectives
  • Explain the adversarial roles and alternating optimization in a GAN
  • Describe forward noising and learned reverse denoising in diffusion models
  • Separate face identification from face verification
  • Use contrastive or triplet objectives to learn a useful embedding space

Discriminative vs. Generative Modeling

A discriminative model focuses on P(YX)P(Y\mid X) or a decision boundary: given an image, which label is correct? A generative model learns enough of the data distribution to produce new samples or evaluate plausible ones. Autoencoders map an input through an encoder into a latent vector and reconstruct it with a decoder. A VAE makes the latent representation probabilistic and optimizes a reconstruction term together with a regularized latent distribution. GANs remove the explicit encoder from the generation path and learn through an adversarial critic.

GAN: Generator vs. Discriminator

A Generator GG maps random noise zz into a synthetic sample. A Discriminator DD estimates whether a sample came from real data rather than GG. Training alternates: update DD to separate real from fake, then update GG to make generated samples that DD accepts as real. Neither player has a fixed objective landscape because the opponent changes after every update. This min-max game can create sharp samples, but instability and mode collapse are central practical risks.

minGmaxD  Expdata[logD(x)]+Ezp(z)[log(1D(G(z)))]\min_G\max_D\;\mathbb{E}_{x\sim p_{data}}[\log D(x)]+\mathbb{E}_{z\sim p(z)}[\log(1-D(G(z)))]
The discriminator improves separation; the generator changes the fake distribution to defeat it.

One Alternating GAN Update

In each iteration, sample a real mini-batch and a noise mini-batch. Update the discriminator using real targets and detached fake samples so generator weights do not change. Then generate a new fake batch, pass it through the discriminator, and update only the generator toward real targets. Separate optimizers make the boundary explicit. Monitor both losses and sample quality; one loss winning completely usually means the game has become unbalanced.

GAN training skeletonPython
# 1) Train discriminator
d_optimizer.zero_grad()
fake = generator(noise).detach()
d_loss = bce(discriminator(real), real_targets)
d_loss += bce(discriminator(fake), fake_targets)
d_loss.backward()
d_optimizer.step()

# 2) Train generator
g_optimizer.zero_grad()
fake = generator(noise)
g_loss = bce(discriminator(fake), real_targets)
g_loss.backward()
g_optimizer.step()

Diffusion: Add Noise, Then Learn to Remove It

A diffusion model defines a forward process that gradually adds Gaussian noise to clean data until little structure remains. Training samples a timestep and asks a neural network, commonly a U-Net, to predict the added noise. Sampling starts from noise and repeatedly applies a learned reverse denoising step. Unlike a GAN's adversarial game, the standard training target is a supervised noise-prediction loss, but generation requires many denoising steps unless a faster sampler is used.

xt=αˉtx0+1αˉtϵL=Eϵϵθ(xt,t)22x_t=\sqrt{\bar{\alpha}_t}\,x_0+\sqrt{1-\bar{\alpha}_t}\,\epsilon \qquad \mathcal{L}=\mathbb{E}\|\epsilon-\epsilon_\theta(x_t,t)\|_2^2
The network learns the noise ϵ\epsilon present at timestep tt.

Face Identification vs. Verification

Identification asks "who is this?" and returns a distribution over enrolled identities, so a conventional implementation is multiclass classification with softmax and cross-entropy. Verification asks whether two images or an image-and-claimed-identity belong to the same person, so its output is a binary accept or reject decision. A fixed identity classifier becomes awkward when new people are enrolled; a similarity-based embedding system can add identities without retraining the entire classifier.

Metric Learning Builds the Comparison Space

Metric learning trains an encoder so examples of the same identity lie close together and different identities lie far apart. At inference, compare normalized embeddings using Euclidean distance or cosine similarity and calibrate a threshold on validation data. Contrastive loss works on positive and negative pairs. Triplet loss compares an anchor to a positive and a negative, requiring the negative to be farther away by at least margin mm. Hard or semi-hard sampling matters because trivial triplets contribute little learning signal.

Ltriplet=max(0,  d(f(a),f(p))d(f(a),f(n))+m)\mathcal{L}_{triplet}=\max\left(0,\;d(f(a),f(p))-d(f(a),f(n))+m\right)
The positive should be closer to the anchor than the negative by at least margin mm.

A Reliable Verification Pipeline

A real face system first detects and aligns the face, then computes a normalized embedding, compares it with enrolled templates, and applies a calibrated threshold. Split data by identity when evaluating so images of the same person do not leak across train and test. Report false-accept and false-reject behavior at the operating threshold rather than accuracy alone. Image quality, pose, illumination, demographics, spoofing, and privacy all affect deployment; the embedding loss is only one component of the system.