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

Colour-palette extractor

Drop in any image and get its five main colours, with k-means and a Gaussian-mixture version.

EST.5 h
REWARD+100 XP
AFTERoriginal

ML-ARENA BENCHMARKS

YOUR NUMBER VS THE INDUSTRY'S
METRICINDUSTRYMINE
inertia
GOAL · within 1%
sklearn KMeans (n_init=10)—
no results yet
YOUR RESULTS

Your recorded results appear here.

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

J=∑i∥xi−μc(i)∥2,J = \sum_i \lVert x_i - \mu_{c(i)} \rVert^2,

where μc(i)\mu_{c(i)} is the centre that pixel xix_i 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 πk\pi_k. The EM algorithm alternates two steps. The E-step computes each pixel's responsibility, the probability that blob kk made it: rik=πk N(xi∣μk,Σk)∑jπj N(xi∣μj,Σj)r_{ik} = \frac{\pi_k \, \mathcal{N}(x_i \mid \mu_k, \Sigma_k)}{\sum_j \pi_j \, \mathcal{N}(x_i \mid \mu_j, \Sigma_j)}. The M-step refits each blob's mean, covariance and weight using those responsibilities as weights.

VISUALFig. 1
STEP 1Assign to nearest centre STEP 2Move centre to mean
One k-means iteration on twelve points. Assign each point to its nearest centre, then move each centre to the mean of its points. Why it's here: Objective 2 is exactly these two steps. Seeing them first makes the NumPy code easier to write.
HINT · OBJECTIVE 2
Stuck? Hints stay hidden until you ask.
HINT · OBJECTIVE 5
Stuck? Hints stay hidden until you ask.

DATA · your images

Objectives

0 / 6 CLEARED

Read-only. Log in to update progress.

VICTORY CONDITIONS

  1. 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>"`.
  2. Inertia never increases across your k-means iterations, and the GMM log-likelihood never decreases across EM steps.
  3. 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

Plot inertia against k from 2 to 12 and decide whether the "elbow" really tells you the right number of colours.
Use your palette to recolour the image, replacing every pixel with its nearest centre, and compare with the original.

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.