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 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 maps random noise into a synthetic sample. A Discriminator estimates whether a sample came from real data rather than . Training alternates: update to separate real from fake, then update to make generated samples that 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.
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.
# 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.
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 . Hard or semi-hard sampling matters because trivial triplets contribute little learning signal.
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.