π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Prefer existing clusters when adding
Manav Rathia6359f075642
diff --git a/web/packages/new/photos/services/ml/cluster.ts b/web/packages/new/photos/services/ml/cluster.tsindex 271cd6d491..6513b9432e 100644--- a/web/packages/new/photos/services/ml/cluster.ts+++ b/web/packages/new/photos/services/ml/cluster.ts@@ -393,7 +393,8 @@ interface ClusterLinearResult {clusters: EmbeddingCluster[];}-const clusterLinear = (+// TODO-Cluster remove me+export const clusterLinear_Direct = (embeddings: number[][],threshold: number,): ClusterLinearResult => {@@ -454,3 +455,82 @@ const clusterLinear = (return { clusters: validClusters };};++const clusterLinear = (+ embeddings: number[][],+ threshold: number,+): ClusterLinearResult => {+ const clusters: EmbeddingCluster[] = [];+ const clusterIndexForEmbeddingIndex = new Map<number, number>();+ // For each embedding+ for (const [i, ei] of embeddings.entries()) {+ // If the embedding is already part of a cluster, then skip it.+ if (clusterIndexForEmbeddingIndex.get(i)) continue;++ // Find the nearest neighbour from among all the other embeddings.+ let nnIndex: number | undefined;+ let nnCosineSimilarity = 0;+ // Find the nearest cluster from among all the existing clusters.+ let nClusterIndex: number | undefined;+ let nClusterCosineSimilarity = 0;+ for (const [j, ej] of embeddings.entries()) {+ // ! This is an O(n^2) loop, be careful when adding more code here.++ // Skip ourselves.+ if (i == j) continue;++ // The vectors are already normalized, so we can directly use their+ // dot product as their cosine similarity.+ const csim = dotProduct(ei, ej);+ if (csim > threshold) {+ if (csim > nnCosineSimilarity) {+ nnIndex = j;+ nnCosineSimilarity = csim;+ }+ if (csim > nClusterCosineSimilarity) {+ const jClusterIndex = clusterIndexForEmbeddingIndex.get(j);+ if (jClusterIndex) {+ nClusterIndex = jClusterIndex;+ nClusterCosineSimilarity = csim;+ }+ }+ }+ }++ if (nClusterIndex) {+ // Found a neighbouring cluster close enough, add ourselves to that.++ ensure(clusters[nClusterIndex]).push(i);+ clusterIndexForEmbeddingIndex.set(i, nClusterIndex);+ } else if (nnIndex) {+ // Find the cluster the nearest neighbour belongs to, if any.+ const nnClusterIndex = clusterIndexForEmbeddingIndex.get(nnIndex);++ if (nnClusterIndex) {+ // If the neighbour is already part of a cluster, also add+ // ourselves to that cluster.++ ensure(clusters[nnClusterIndex]).push(i);+ clusterIndexForEmbeddingIndex.set(i, nnClusterIndex);+ } else {+ // Otherwise create a new cluster with us and our nearest+ // neighbour.++ clusterIndexForEmbeddingIndex.set(i, clusters.length);+ clusterIndexForEmbeddingIndex.set(nnIndex, clusters.length);+ clusters.push([i, nnIndex]);+ }+ } else {+ // We didn't find a neighbour within the threshold. Create a new+ // cluster with only this embedding.++ clusterIndexForEmbeddingIndex.set(i, clusters.length);+ clusters.push([i]);+ }+ }++ // Prune singleton clusters.+ const validClusters = clusters.filter((cs) => cs.length > 1);++ return { clusters: validClusters };+};