AI from scratch · Elective · Faces and style

Faces and style

A phone unlocks for its owner's face and not for a stranger's, though it was trained on neither. A filter repaints a photo in the brushstrokes of a painting. Both rest on the same idea from this stage: a network's internal numbers describe an image in ways you can compare and recombine. This lesson builds the face-recognition method (without any faces: it works the same way on handwritten characters), looks hard at its errors and the law around it, and then uses the same networks to separate an image's content from its style.

1. Verification: same person or not?

Face recognition can't be an ordinary classifier: new people appear every day, each with a photo or two. Instead, a network maps every face to an embedding (128 numbers, say) trained so that photos of the same person are close and different people are far apart. Verification ("is this the phone's owner?") compares two embeddings against a distance threshold; identification ("who is this?") searches a database for the nearest one. Below, each dot is a synthetic embedding of a photo, colored by person; the threshold decides which pairs count as "same".

group A: clear photos group B: blurrier photos (each person's photos spread wider)

Lower the threshold and impostors stop getting through (fewer false accepts), but the owner gets refused more (more false rejects): chapter 12's trade-off again. And look at group B: with one threshold for everyone, people whose photos vary more get rejected far more often. Real systems have shown exactly this kind of gap between demographic groups.

Recognition compares embeddings by distance against a threshold, trading false accepts for false rejects, and one threshold can treat groups very differently.

2. Learning the embedding: Siamese networks and triplet loss

A Siamese setup runs two (or three) images through the same network with shared weights and trains on their distances. The triplet loss takes an anchor \(a\), a positive \(p\) (same person) and a negative \(n\) (someone else):

\[ L = \max\big(0,\ \lVert f(a) - f(p) \rVert^2 - \lVert f(a) - f(n) \rVert^2 + m \big) \]

It's zero once the positive is closer than the negative by at least a margin \(m\), and otherwise pulls the positive in and pushes the negative out. The payoff is generalization to new identities. In the practice notebook a small CNN learns from 964 handwritten characters of the Omniglot alphabets and is tested on 659 characters from alphabets it never saw: same-or-different decisions reach an AUC of 0.96, against 0.65 for comparing raw pixels. Trained on only 8 handwritten digits, the same recipe does worse than raw pixels on the two digits it never saw (0.70 against 0.75): it needs many identities to learn what "same" means in general, which is why face models train on hundreds of thousands of people.

A shared network trained with triplet loss places same-identity images closer than others by a margin, and with enough identities it generalizes to new ones.

3. Face recognition in the world

A verification system's threshold is set so that 0.1% of impostors get through, averaged over everyone. What should a team check before deploying?

Chapter 12's slices, with people's access and safety at stake. The widget's group B shows how a single threshold spreads errors unevenly.

Face recognition's errors vary across groups, its use is regulated, and consent and purpose decide whether it should be built at all.

4. Style transfer: content and texture, pulled apart

Gatys, Ecker and Bethge (2015) noticed that a trained image network (VGG, ImageNet-trained) describes a picture in two separable ways. Its deep feature maps say what is where: the content. The correlations between its feature channels, ignoring position, say which textures appear together: the style. Start from the photo and adjust its pixels by gradient descent to keep its deep features while matching a painting's channel correlations, and the photo is repainted. The correlations form the Gram matrix:

\[ G_{ij} = \frac{1}{HW} \sum_{\text{positions}} F_i \, F_j \]

for feature channels \(i, j\) of a layer with \(H \times W\) positions: how often channel \(i\) and channel \(j\) fire together, anywhere in the image.

Style transfer keeps a photo's deep features (content) and matches a painting's feature correlations (style), by gradient descent on the pixels.

Why a Gram matrix captures texture but not layout
  1. Each \(G_{ij}\) sums \(F_i F_j\) over every position, and a sum doesn't care about order: move the positions around and the sum is the same (the box above checks it).
  2. So the Gram matrix keeps which features occur together (brush strokes with certain colors, curls of a certain size) and throws away where they occur.
  3. That's a texture: statistics of local patterns, repeated anywhere. A picture of a texture can be rearranged and still be the same texture.
  4. Using Gram matrices from several layers captures texture at several sizes, from fine strokes (early layers) to larger motifs (deeper ones).
  5. Content needs layout, so it's matched with the raw feature map of one deep layer, where position still counts.
Where it breaks: the same tools, misused

5. What to carry forward

All lessons: AI from scratch. Notebook data: Omniglot (Lake, Salakhutdinov and Tenenbaum, MIT license). Papers: Schroff et al., "FaceNet" (2015); Gatys et al., "A Neural Algorithm of Artistic Style" (2015); NIST, "Face Recognition Vendor Test Part 3: Demographic Effects" (2019).

Last updated