← All Base Form ML projects
BL-08NOT STARTEDBASE FORM · CLASSICAL MLMLPYTHON

Face-or-not detector

Train a support vector machine with kernels to spot faces in small image patches.

EST.7 h
REWARD+100 XP
AFTERoriginal

ML-ARENA BENCHMARKS

YOUR NUMBER VS THE INDUSTRY'S
METRICINDUSTRYMINE
accuracy
GOAL · within 1%
sklearn SVC—
no results yet
YOUR RESULTS

Your recorded results appear here.

Brief

Face detection asks one question over and over: is this small square of the image a face or not? Before deep learning, the best answers came from support vector machines. In this project you build one from scratch, feed it face and non-face patches, and use it to scan a photo.

For faces, use the Olivetti faces dataset, which ships with scikit-learn as sklearn.datasets.fetch_olivetti_faces. It has 400 grayscale 64 × 64 images, ten each of 40 people. For non-faces, cut random 64 × 64 patches from photos with no people in them. The two sample images in sklearn.datasets.load_sample_images are a start. Add a few of your own landscape or object photos so the negatives aren't all from two pictures.

A linear SVM finds the line (in high dimensions, the hyperplane) w⊤x+b=0w^\top x + b = 0 with the widest margin between the classes. With labels yi=±1y_i = \pm 1, the soft-margin version minimises

12∥w∥2+C∑imax⁡(0, 1−yi(w⊤xi+b)).\frac{1}{2} \lVert w \rVert^2 + C \sum_i \max\big(0, \, 1 - y_i (w^\top x_i + b)\big).

The trick that makes SVMs powerful is the kernel. Written in its dual form, the classifier only uses dot products between points: f(x)=∑iαiyi k(xi,x)+bf(x) = \sum_i \alpha_i y_i \, k(x_i, x) + b. Swap the dot product for a kernel such as the RBF kernel k(x,z)=exp⁡(−γ∥x−z∥2)k(x, z) = \exp(-\gamma \lVert x - z \rVert^2) and the boundary can curve, with no extra features computed. Most αi\alpha_i end up zero. The points with αi\alpha_i above zero are the support vectors.

VISUALFig. 1
BOUNDARY squares: faces · circles: patches · rings: support vectors
A linear SVM. The boundary sits in the middle of the widest gap, and the circled support vectors fix where it goes. Why it's here: Shows the margin and support vectors that the soft-margin formula and the SMO solver in objective 4 are about.
HINT · OBJECTIVE 2
Stuck? Hints stay hidden until you ask.
HINT · OBJECTIVE 4
Stuck? Hints stay hidden until you ask.

DATA · Olivetti faces + random patches

Objectives

0 / 6 CLEARED

Read-only. Log in to update progress.

VICTORY CONDITIONS

  1. Your SVM's test accuracy is within 1% of sklearn SVC with the same kernel, C and gamma, on a split where no person appears in both train and test. Record it with `python tools/mlarena.py record BL-08 "<your accuracy>"`.
  2. The PCA and any scaling are fitted on training images only.
  3. The README shows a grid of the test images your model got wrong and a sentence on what they have in common.

Side quests

Tune C and gamma with a grid search on a validation split and plot accuracy as a heat map. Where does the model overfit?
Show the images of your support vectors. Are they the hardest faces and the most face-like patches?

Terms in plain words

Support vector machine (SVM)
A classifier that draws the boundary between two classes with the widest possible gap, or margin, on each side.
Support vectors
The training points that sit on or inside the margin. Only they decide where the boundary goes.
Soft margin
Allowing some points to break the margin for a penalty, set by C. Large C punishes mistakes hard, small C allows a wider, looser margin.
Kernel
A similarity function k(x, z) that acts like a dot product in a bigger feature space, so a linear method can draw curved boundaries.
RBF kernel
The Gaussian kernel exp(-γ‖x − z‖²). Nearby points score near 1 and far points near 0, and γ sets what counts as near.