Brief
Designers pull colour palettes from photos all the time. You'll build the tool that does it. Drop in an image and get back its five main colours, found by clustering the pixels.
There is no dataset to download. Use your own photos, or the two sample images that ship with scikit-learn
(sklearn.datasets.load_sample_images). Each pixel is a point in 3D colour space with red, green and blue
coordinates, and a photo is a cloud of such points. The main colours are the centres of the densest parts of that
cloud.
Clustering finds groups without labels. k-means is the simplest version. Pick k centres, assign every pixel to its nearest centre (the same nearest-neighbour search you wrote in BL-04, here with squared Euclidean distance), move each centre to the mean of its pixels, and repeat until nothing changes. Each step lowers the inertia
where is the centre that pixel belongs to. k-means can get stuck in a poor answer, so you start from several random initialisations and keep the best.
A Gaussian mixture model is the soft version. Each cluster is a bell-shaped blob with a mean, a covariance and a weight . The EM algorithm alternates two steps. The E-step computes each pixel's responsibility, the probability that blob made it: . The M-step refits each blob's mean, covariance and weight using those responsibilities as weights.
DATA · your images
Objectives
Read-only. Log in to update progress.
VICTORY CONDITIONS
- On the same pixel sample with k = 5, your best-of-ten inertia is at most 1% above sklearn KMeans(n_clusters=5, n_init=10). Record the ratio with `python tools/mlarena.py record BL-06 "<your inertia / sklearn inertia>"`.
- Inertia never increases across your k-means iterations, and the GMM log-likelihood never decreases across EM steps.
- A script takes any image path and writes the five-colour palette image. The README shows it on at least three of your own photos.
Side quests
Terms in plain words
- Clustering
- Grouping data points so that points in the same group are close to each other, with no labels to learn from.
- k-means
- Clustering into k groups by repeating two steps. Assign each point to its nearest centre, then move each centre to the mean of its points.
- Inertia
- The sum of squared distances from each point to its assigned centre. k-means tries to make it small.
- Gaussian mixture model (GMM)
- A model that says the data came from k overlapping bell-shaped blobs, each with its own centre, spread and share of the points.
- EM algorithm
- Expectation-maximisation. Guess which blob each point belongs to (softly), refit the blobs to those guesses, and repeat.