From 74bbdd5e7208efaf86c66b368c99f46ea8ca6b37 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Tue, 15 Oct 2024 09:47:47 +0530 Subject: [PATCH] Random sampling --- web/packages/new/photos/services/ml/people.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/web/packages/new/photos/services/ml/people.ts b/web/packages/new/photos/services/ml/people.ts index 60636847ba..79320650a4 100644 --- a/web/packages/new/photos/services/ml/people.ts +++ b/web/packages/new/photos/services/ml/people.ts @@ -1,6 +1,7 @@ import { assertionFailed } from "@/base/assert"; import log from "@/base/log"; import type { EnteFile } from "@/media/file"; +import { shuffled } from "@/utils/array"; import { getLocalFiles } from "../files"; import { savedCGroups, type CGroup } from "../user-entity"; import type { FaceCluster } from "./cluster"; @@ -371,6 +372,12 @@ export const suggestionsForPerson = async (person: CGroupPerson) => { .flat() .filter((e) => !!e); + // Randomly sample faces to limit the O(n^2) cost. + const sampledPersonFaceEmbeddings = shuffled(personFaceEmbeddings).slice( + 0, + 100, + ); + const suggestedClusters: FaceCluster[] = []; for (const cluster of clusters) { const { id, faces } = cluster; @@ -384,7 +391,7 @@ export const suggestionsForPerson = async (person: CGroupPerson) => { for (const fi of faces) { const ei = embeddingByFaceID.get(fi); if (!ei) continue; - for (const ej of personFaceEmbeddings) { + for (const ej of sampledPersonFaceEmbeddings) { const csim = dotProduct(ei, ej); if (csim >= 0.6) { suggest = true;