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
- Error rates differ by group. The U.S. National Institute of Standards and Technology's 2019 test of 189 face recognition algorithms found false positive rates that varied with the demographic group of the people photographed, by factors of 10 to 100 for many algorithms, with the gaps depending strongly on the algorithm and its training data.
- The law. Illinois's Biometric Information Privacy Act requires written consent before collecting faceprints; New York City requires businesses that collect biometric identifiers to post notice and forbids selling them; several cities, starting with San Francisco in 2019, banned or restricted government use of face recognition. Rules differ by place and change often: check before building.
- Consent and purpose. Unlocking your own phone, with the template stored on the device, is a different act from identifying strangers in a crowd. Many teams decide the second isn't theirs to build.
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?
- Nothing: 0.1% is very low
- False accept and false reject rates per demographic group, since one global threshold can be far worse for some groups
- Only how fast it runs
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
- 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).
- 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.
- That's a texture: statistics of local patterns, repeated anywhere. A picture of a texture can be rearranged and still be the same texture.
- Using Gram matrices from several layers captures texture at several sizes, from fine strokes (early layers) to larger motifs (deeper ones).
- 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
- Identification without consent. Embeddings that verify you can also be searched against scraped photos to name you; companies doing this have faced lawsuits and fines in several countries.
- Spoofing. A printed photo or a video can fool a verifier that only compares embeddings; real systems add liveness checks (depth, motion).
- Synthetic faces and deepfakes. The image-generation methods of the diffusion lesson can swap faces in video. Detection is an arms race; provenance standards (signed capture, content credentials) are the more durable answer.
- Style and artists. Style transfer and generators can imitate living artists' styles; whether that's fair use is contested. Know what you're imitating.
5. What to carry forward
- Verification = embedding distance below a threshold; identification = nearest embedding in a database.
- Siamese networks share weights; triplet loss enforces a margin; many identities are needed to generalize.
- Measure false accepts and false rejects per group; know the law and the purpose.
- Style transfer: deep features for content, Gram matrices for style, gradient descent on pixels.
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