From 1e72a3ba345ecd81e97bfacb97c5fc57f8e06993 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 16 Oct 2024 20:31:06 +0530 Subject: [PATCH] Cleanup --- web/packages/new/photos/services/ml/people.ts | 51 ++++++------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/web/packages/new/photos/services/ml/people.ts b/web/packages/new/photos/services/ml/people.ts index 80f8a5795c..7164aeebe6 100644 --- a/web/packages/new/photos/services/ml/people.ts +++ b/web/packages/new/photos/services/ml/people.ts @@ -407,43 +407,24 @@ export const _suggestionsAndChoicesForPerson = async ( if (personClusterIDs.has(id)) continue; if (ignoredClusterIDs.has(id)) continue; - if (process.env.NEXT_PUBLIC_ENTE_WIP_CL_TODO) { - /* direct compare method TODO-Cluster remove me */ - let suggest = false; - for (const fi of faces) { - const ei = embeddingByFaceID.get(fi); - if (!ei) continue; - for (const ej of sampledPersonEmbeddings) { - const csim = dotProduct(ei, ej); - if (csim >= 0.6) { - suggest = true; - break; - } - } - if (suggest) break; + const sampledOtherEmbeddings = randomSample(faces, 50) + .map((id) => embeddingByFaceID.get(id)) + .filter((e) => !!e); + + // Sort all cosine similarities pairs, and consider their median. + const csims: number[] = []; + for (const other of sampledOtherEmbeddings) { + for (const embedding of sampledPersonEmbeddings) { + csims.push(dotProduct(embedding, other)); } + } + csims.sort(); - if (suggest) candidateClustersAndSimilarity.push([cluster, 0]); - } else { - const sampledOtherEmbeddings = randomSample(faces, 50) - .map((id) => embeddingByFaceID.get(id)) - .filter((e) => !!e); + if (csims.length == 0) continue; - /* cosine similarities */ - const csims: number[] = []; - for (const other of sampledOtherEmbeddings) { - for (const embedding of sampledPersonEmbeddings) { - csims.push(dotProduct(embedding, other)); - } - } - csims.sort(); - - if (csims.length == 0) continue; - - const medianSim = ensure(csims[Math.floor(csims.length / 2)]); - if (medianSim > 0.48) { - candidateClustersAndSimilarity.push([cluster, medianSim]); - } + const medianSim = ensure(csims[Math.floor(csims.length / 2)]); + if (medianSim > 0.48) { + candidateClustersAndSimilarity.push([cluster, medianSim]); } } @@ -451,6 +432,7 @@ export const _suggestionsAndChoicesForPerson = async ( console.time("post"); + // Sort suggestions by the (median) cosine similarity. candidateClustersAndSimilarity.sort(([, a], [, b]) => b - a); const suggestedClusters = candidateClustersAndSimilarity.map(([c]) => c); @@ -513,7 +495,6 @@ export const _suggestionsAndChoicesForPerson = async ( const choices = [firstChoice, ...restChoices]; - // sortBySize(suggestedClusters); // Limit to the number of suggestions shown in a single go. const suggestions = toPreviewableList(suggestedClusters.slice(0, 80));