e-means Meets DBSCAN: Two Moons Said Yes, Cairo Said No
Back in 2016, I wrote DBSCAN on Spark. I like DBSCAN because you do not have to give it the number of clusters. But it uses a lot of time and memory when the input is large. This month, Meta FAIR and École des Ponts released e-means in faiss 1.16.0. It does the work of k-means in a few passes over the data.
So I asked a simple question: can e-means make the data small, so that DBSCAN can do the rest? In this post, I show how I combined the two and how fast e-means is on a GPU. Then I test the method on real satellite data of Cairo. It did not win there. The source is on GitHub: mraad/emeans-dbscan.
The terms
- Vector. One row of numbers. There are n vectors, and each vector has d numbers.
- Centroid. The center point of a cluster. There are k centroids.
- Epoch. One pass over all the training vectors.
- Noise. The vectors that DBSCAN does not put in a cluster. Their label is -1.
- ARI. The adjusted Rand index. It compares cluster labels with reference labels. 1 is a full match. Random labels give a value near 0.
The two algorithms
e-means solves the k-means problem. Lloyd, the usual k-means algorithm, moves the centroids one time per epoch, and faiss runs 25 epochs by default. e-means moves the centroids after each batch of vectors. It uses an exponential moving average, and the step becomes smaller during training. It calculates the batch size and the first step from n, k and the number of epochs. The default is 5 epochs. It can also move a centroid that has too few vectors. This is named revival.
e-means has the limits of k-means. You must set k. Each cluster is a convex cell, so a curved cluster becomes many pieces. There is no noise.
DBSCAN has two settings: eps and min_pts. A vector is a core vector when at least min_pts vectors are within the distance eps. DBSCAN connects the core vectors that are near each other. It finds clusters of any shape, and it finds noise. But it must search around each vector, and that is slow when n is large.
The combined method
- Run e-means with a k that is much larger than the number of real clusters. Each result is a micro-cluster.
- Count the vectors in each micro-cluster.
- Run DBSCAN on the centroids. The count is the weight of each centroid.
- Give each vector the DBSCAN label of its centroid.
The idea of a summary before the clustering is not new. BIRCH and DenStream do the same. The new part is a fast e-means for the summary. Optuna searches for k, eps and min_pts, and it keeps the settings with the highest ARI on the tuning data. The final score uses different data.
Two moons
I started with the classic: two curved clusters, 20,000 vectors, d = 2. Optuna did 150 trials for each tuned method.
| Method | Settings | ARI |
|---|---|---|
| e-means | k = 2 | 0.2544 |
| Combined method | k = 111, eps = 0.164, min_pts = 74 | 0.9998 |
| DBSCAN | eps = 0.124, min_pts = 56 | 0.9991 |
The combined method works. DBSCAN works too, in about the same time. At 20,000 vectors, DBSCAN has no speed problem to solve.
GPU and CPU
Next, speed. The data is synthetic: 1,000,000 vectors, d = 128, k = 4,096. The GPU is an NVIDIA RTX A4000. The CPU is a Xeon E5-1620 v3 from 2014. The error is the mean squared distance from a held-out vector to its nearest centroid.
| Method | Epochs | Training time | Error |
|---|---|---|---|
| CPU Lloyd | 25 | 104.8 s | 39.17 |
| GPU Lloyd | 25 | 6.02 s | 39.56 |
| CPU e-means | 5 | 29.0 s | 31.72 |
| GPU e-means | 5 | 0.18 s | 31.78 |
GPU e-means is 162 times faster than CPU e-means. On the same GPU, it is 33.6 times faster than my Lloyd. My Lloyd is plain PyTorch in FP32, so do not read that number as "faster than every GPU k-means". The error is 19% lower than Lloyd, but this data has clusters of very different sizes, and that helps e-means.
Cairo
Synthetic data is easy to like. I wanted real data. My first choice was Beirut, but the two windows were 42% and 73% sea. So I moved to Cairo: the Nile, the city, the crops and the edge of the desert.
The vectors are the AlphaEarth Foundations embeddings for 2021. Each 10 m pixel has one vector with d = 64. The reference labels are from ESA WorldCover 2021. There are two windows of 4,096 × 4,096 pixels that do not overlap. Optuna uses the tune window. The final score uses the test window, which has 16,777,216 vectors. I call the score land ARI, because it does not count the water pixels.
| Method | Tune land ARI | Test land ARI |
|---|---|---|
| Combined method, k = 199 (tuned on the Mac) | 0.812 | 0.353 |
| Combined method, k = 823 (tuned on the GPU) | 0.785 | 0.307 |
| Combined method, k = 7,393 (tuned on the Xeon) | 0.808 | 0.494 |
| DBSCAN, 320,000 vectors, best of two settings | 0.802 | 0.493 |
| e-means, k = 3 | 0.715 | 0.666 |
And guess what? e-means with k = 3 wins, with one setting and no DBSCAN. The combined method has three settings. It fits the tune window better, and then it loses that score on the test window. The two windows are not alike: built-up land is 8% of the tune window and 37% of the test window.
Look at the map again. The top right map, with 122 clusters, looks almost like WorldCover. Its land ARI is 0.353. Each small cluster takes the color of its most frequent class, so many small clusters always look good. Do not trust the picture. Check the number.
I must be clear about one thing. I added the test of k = 2 to 16 after I saw the first results. The tune window selected k = 3 on all three backends (Mac CPU, Xeon CPU and GPU), but the idea to look there came late.
Two answers for k = 3
With k = 3, e-means does not always find the same answer on the test window. I ran ten seeds on each machine:
| Answer | Mean cosine | Land ARI | CPU | GPU |
|---|---|---|---|---|
| Good | 0.8656 | 0.666 | 8 of 10 | 9 of 10 |
| Bad | 0.8453 | 0.365 | 2 of 10 | 1 of 10 |
The mean cosine is the mean similarity of each vector to its nearest centroid. It needs no labels, and it is lower for the bad answer. So run a few seeds and keep the highest mean cosine. At 0.8 s per run on the GPU, ten seeds cost about 8 seconds. The same run takes 32.6 s on the Mac CPU and 71.3 s on the Xeon.
Two things that went wrong
- I tuned DBSCAN on a sample and then used the same
min_ptson all the vectors.min_ptsis a count of vectors, so it must grow with n. I threw the first results away and ran everything again. - The Mac and the Xeon selected different DBSCAN settings from the same data and the same seed. The first suspect was the library versions. That was wrong: three sets of versions on the Mac give the same 60 trials. I still do not know the cause.
The explorer
I wanted to see these results, so there is a small web app. It is vanilla JavaScript with the ArcGIS Maps SDK for JavaScript 5.2 web components and Calcite. Both load from the Esri CDN. There is no framework and no build step. A Web Worker runs my JavaScript port of e-means on about 1 million pixels per window. For the same seed and without revival, its labels match the faiss labels exactly.
You can change k, the epochs and the seed. You can swipe the clusters against WorldCover, and click a pixel to see its nearest centroids.
To start it, you need uv and about 3 GB of disk space:
- Clone the repository:
git clone https://github.com/mraad/emeans-dbscan - Go to the folder:
cd emeans-dbscan - Download the data:
uv run --python 3.12 aef_fetch.py - Prepare the data:
uv run --python 3.12 app/prepare_data.py - Start the server:
python3 -m http.server 8123 --bind 127.0.0.1 -d app - Open
http://localhost:8123in a browser.
To see the two answers for k = 3:
- Set Clusters (k) to 3.
- Set Seed to 1.
- Click Run e-means.
- Wait for the status "Done".
- Do steps 2 to 4 again for the seeds 2, 3 and 4.
- Compare the Cosine values in the Runs table.
What this does not show
- Two moons and the speed test use synthetic data. The speed test uses one seed.
- Cairo is one area and one year.
- WorldCover is the output of a model. It is not ground truth.
- Optuna used the reference labels. Most clustering work has no labels.
- The combined method had 60 Optuna trials. e-means had 15 values of k with three seeds each.
- GPU runs do not repeat exactly. For k = 199, the land ARI moves by up to 0.005.
The combined method can find curved clusters. Two moons shows that. On 16.8 million real vectors, e-means with three clusters was better and simpler. Lesson learned: run the simple method first. Next, I want to try HDBSCAN on the centroids, because it does not need eps. More to come.
Comments