r/Python • u/hdw_coder • Mar 13 '26
Discussion Perceptual hash clustering can create false duplicate groups (hash chaining) — here’s a simple fix
While testing a photo deduplication tool I’m building (DedupTool), I ran into an interesting clustering edge case that I hadn’t noticed before.
The tool works by generating perceptual hashes (dHash, pHash and wHash), comparing images, and clustering similar images. Overall, it works well, but I noticed something subtle.
The situation
I had a cluster with four images. Two were actual duplicates. The other two were slightly different photos from the same shoot.
The tool still detected the duplicates correctly and selected the right keeper image, but the cluster itself contained images that were not duplicates.
So, the issue wasn’t duplicate detection, but cluster purity.
The root cause: transitive similarity
The clustering step builds a similarity graph and then groups images using connected components.
That means the following can happen: A similar to B, B similar to C, C similar to D. Even if A not similar to C, A not similar to D, B not similar to D all four images still end up in the same cluster.
This is a classic artifact in perceptual hash clustering sometimes called hash chaining or transitive similarity. You see similar behaviour reported by users of tools like PhotoSweeper or Duplicate Cleaner when similarity thresholds are permissive.
The fix: seed-centred clustering
The solution turned out to be very simple. Instead of relying purely on connected components, I added a cluster refinement step.
The idea: Every image in a cluster must also be similar to the cluster seed. The seed is simply the image that the keeper policy would choose (highest resolution / quality).
The pipeline now looks like this:
hash_all()
↓
cluster() (DSU + perceptual hash comparisons)
↓
refine_clusters() ← new step
↓
choose_keepers()
During refinement: Choose the best image in the cluster as the seed. Compare every cluster member with that seed. Remove images that are not sufficiently similar to the seed.
So, a cluster like this:
A B C D
becomes:
Cluster 1: A D
Cluster 2: B
Cluster 3: C
Implementation
Because the engine already had similarity checks and keeper scoring, the fix was only a small helper:
def refine_clusters(self, clusters, feats):
refined = {}
for cid, idxs in clusters.items():
if len(idxs) <= 2:
refined[cid] = idxs
continue
seed = max((feats[i] for i in idxs), key=self._keeper_key)
seed_i = feats.index(seed)
new_cluster = [seed_i]
for i in idxs:
if i == seed_i:
continue
if self.similar(seed, feats[i]):
new_cluster.append(i)
if len(new_cluster) > 1:
refined[cid] = new_cluster
return refined
This removes most chaining artefacts without affecting performance because the expensive hash comparisons have already been done.
Result
Clusters are now effectively seed-centred star clusters rather than chains. Duplicate detection remains the same, but cluster purity improves significantly.
Curious if others have run into this
I’m curious how others deal with this problem when building deduplication or similarity search systems. Do you usually: enforce clique/seed clustering, run a medoid refinement step or use some other technique?
If people are interested, I can also share the architecture of the deduplication engine (bucketed hashing + DSU clustering + refinement).
View all comments
Show parent comment
1
u/hdw_coder Mar 17 '26
Good questions.
On the first one: the non-keeper images are not reprocessed in a second similarity pass. Once the duplicate family / cluster is formed, the keeper policy is applied inside that cluster and the selected keeper is retained while the others are marked for the configured action path (dry-run report, quarantine, recycle bin, etc.). So the extra “roundtrip” is not another duplicate-detection round; it’s just the action/reporting stage on the already formed cluster.
On the second point: yes, that is basically the important subtlety of single-linkage / union-find style clustering.
If:
then A, B, and C can still end up in the same connected component because similarity is treated as transitive through B.
So in graph terms, the current logic is closer to:
not:
That means cluster membership is driven by connectivity, not by a center/seed radius.
And yes, in that kind of setup the effective cluster can depend on which comparisons are admitted, so a “bridge” image can pull together images that are not mutually close enough.
That is not necessarily wrong — it is a deliberate tradeoff. It tends to improve recall for near-duplicate families, but it can over-cluster in borderline cases.
Your suggested alternative is a real one: impose an additional cluster coherence rule, for example:
In practice, for perceptual-hash deduplication, I generally prefer the current connected-component approach as the base, but with an optional hardening step afterwards, because it stays efficient and predictable.
A very practical compromise is:
So effectively:
That usually gives the best of both worlds:
Using average distance can help, but I would be careful relying on average alone. Averages can hide a bad outlier. For deduplication, a stronger condition like max distance to representative or max cluster diameter is usually safer.
So the short answer is:
The cleanest next step would probably be:
That is usually simpler and safer than replacing the whole clustering method.