The third dedup pass compares pairs only inside clusters
Comparing every pair of N images is about N² / 2 pairs. Comparing pairs only inside k equal clusters is N² / (2k): 4 clusters cut the work 4 times.
The same scene with a new crop or filter gets a different hash, and nearly the same embedding. The SemDeDup paper describes these duplicates: the same image with distortions that get past exact dedup, such as different margins, crops, aspect ratios and color filters.
Its method has two steps. First, cluster the embeddings with k-means, which splits the vectors into k groups around k centers. Then, within each cluster, compute the cosine similarity of every pair, and call pairs above a threshold duplicates. On a subset of LAION, SemDeDup could remove 50% of the data with minimal performance loss, effectively halving training time.
The clusters are what make it affordable. All pairs among a billion images is about . SemDeDup used 50,000 clusters for CLIP image embeddings. With 20,000 images in each:
That's 50,000 times fewer. With equal clusters, the saving always equals the number of clusters.
Each pair is a dot product over 768 dimensions: a multiply and an add for each one. So the pass is floating-point operations. Assume a GPU sustains 100 TFLOPS, 100 trillion floating-point operations a second, on large matrix multiplies. Then the pass takes about 150 seconds.
Real clusters aren't equal. The cost grows with the sum of the squared cluster sizes, so one giant cluster can dominate. Split any cluster above a size cap before the pairwise pass.
In the editor, write pairs(sizes) and saving(n, sizes), where sizes lists how many images each cluster holds. The last line checks SemDeDup's case: a billion images in 50,000 clusters of 20,000.