π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Neighbours
Manav Rathi760b1f3f857a
diff --git a/web/apps/photos/src/pages/cluster-debug.tsx b/web/apps/photos/src/pages/cluster-debug.tsxindex 9d45ada7ab..25f9b36031 100644--- a/web/apps/photos/src/pages/cluster-debug.tsx+++ b/web/apps/photos/src/pages/cluster-debug.tsx@@ -1,8 +1,11 @@import { SelectionBar } from "@/base/components/Navbar";import { pt } from "@/base/i18n";-import { faceCrop, wipClusterPageContents } from "@/new/photos/services/ml";-import type { Face } from "@/new/photos/services/ml/face";-import { EnteFile } from "@/new/photos/types/file";+import {+ faceCrop,+ wipClusterPageContents,+ type FaceFileNeighbour,+ type FaceFileNeighbours,+} from "@/new/photos/services/ml";import {FlexWrapper,FluidContainer,@@ -10,22 +13,17 @@ import {} from "@ente/shared/components/Container";import EnteSpinner from "@ente/shared/components/EnteSpinner";import BackButton from "@mui/icons-material/ArrowBackOutlined";-import { Box, IconButton, styled } from "@mui/material";+import { Box, IconButton, styled, Typography } from "@mui/material";import { useRouter } from "next/router";import { AppContext } from "pages/_app";import React, { useContext, useEffect, useMemo, useRef, useState } from "react";import AutoSizer from "react-virtualized-auto-sizer";import { VariableSizeList } from "react-window";-export interface UICluster {- files: EnteFile[];- face: Face;-}-// TODO-Cluster Temporary component for debugging-export default function Deduplicate() {+export default function ClusterDebug() {const { startLoading, finishLoading, showNavBar } = useContext(AppContext);- const [clusters, setClusters] = useState<UICluster[]>(null);+ const [faceFNs, setFaceFNs] = useState<FaceFileNeighbours[]>(null);useEffect(() => {showNavBar(true);@@ -34,26 +32,20 @@ export default function Deduplicate() {const cluster = async () => {startLoading();- const faceAndFiles = await wipClusterPageContents();- setClusters(- faceAndFiles.map(({ face, file }) => ({- files: [file],- face,- })),- );+ setFaceFNs(await wipClusterPageContents());finishLoading();};return (<>- {clusters ? (+ {faceFNs ? (<Container><AutoSizer>{({ height, width }) => (<ClusterPhotoListwidth={width}height={height}- clusters={clusters}+ faceFNs={faceFNs}/>)}</AutoSizer>@@ -99,13 +91,13 @@ const Container = styled("div")`interface ClusterPhotoListProps {height: number;width: number;- clusters: UICluster[];+ faceFNs: FaceFileNeighbours[];}const ClusterPhotoList: React.FC<ClusterPhotoListProps> = ({height,width,- clusters,+ faceFNs,}) => {const [itemList, setItemList] = useState<ItemListItem[]>([]);const listRef = useRef(null);@@ -119,24 +111,23 @@ const ClusterPhotoList: React.FC<ClusterPhotoListProps> = ({const listItemHeight = 120 * shrinkRatio + 4;useEffect(() => {- setItemList(itemListFromClusters(clusters, columns));- }, [columns, clusters]);+ setItemList(itemListFromFaceFNs(faceFNs, columns));+ }, [columns, faceFNs]);useEffect(() => {listRef.current?.resetAfterIndex(0);}, [itemList]);const getItemSize = (i: number) =>- itemList[i].score !== undefined ? 36 : listItemHeight;+ typeof itemList[i] == "number" ? 36 : listItemHeight;const generateKey = (i: number) =>- itemList[i].score !== undefined- ? `${itemList[i].score}-${i}`- : `${itemList[i].files[0].id}-${itemList[i].files.slice(-1)[0].id}`;+ typeof itemList[i] == "number"+ ? `${itemList[i]}-${i}`+ : `${itemList[i][0].enteFile.id}/${itemList[i][0].face.faceID}-${itemList[i].slice(-1)[0].enteFile.id}/${itemList[i].slice(-1)[0].face.faceID}-${i}`;return (<VariableSizeList- key={`${0}`}itemData={{ itemList, columns, shrinkRatio }}ref={listRef}itemSize={getItemSize}@@ -156,18 +147,13 @@ const ClusterPhotoList: React.FC<ClusterPhotoListProps> = ({columns={columns}shrinkRatio={shrinkRatio}>- {item.score !== undefined ? (+ {typeof item == "number" ? (<LabelContainer span={columns}>- {`${item.fileCount} files, score ${item.score.toFixed(2)}`}+ {`score ${item.toFixed(2)}`}</LabelContainer>) : (- item.files.map((enteFile) => (- <FaceChip key={`${enteFile.id}`}>- <FaceCropImageView- enteFile={enteFile}- faceID={item.face?.faceID}- />- </FaceChip>+ item.map((ffn, i) => (+ <FaceItem key={i.toString()} {...ffn} />)))}</ListContainer>@@ -178,20 +164,41 @@ const ClusterPhotoList: React.FC<ClusterPhotoListProps> = ({);};-const FaceChip = styled(Box)`- width: 120px;- height: 120px;-`;+type ItemListItem = number | FaceFileNeighbour[];-interface FaceCropImageViewProps {- faceID: string;- enteFile: EnteFile;-}+const itemListFromFaceFNs = (+ faceFNs: FaceFileNeighbours[],+ columns: number,+) => {+ const result: ItemListItem[] = [];+ for (let index = 0; index < faceFNs.length; index++) {+ const { face, neighbours } = faceFNs[index];+ result.push(face.score);+ let lastIndex = 0;+ while (lastIndex < neighbours.length) {+ result.push(neighbours.slice(lastIndex, lastIndex + columns));+ lastIndex += columns;+ }+ }+ return result;+};++const getFractionFittableColumns = (width: number) =>+ (width - 2 * getGapFromScreenEdge(width) + 4) / (120 + 4);++const getGapFromScreenEdge = (width: number) => (width > 4 * 120 ? 24 : 4);-const FaceCropImageView: React.FC<FaceCropImageViewProps> = ({- faceID,+const getShrinkRatio = (width: number, columns: number) =>+ (width - 2 * getGapFromScreenEdge(width) - (columns - 1) * 4) /+ (columns * 120);++const FaceItem: React.FC<FaceFileNeighbour> = ({+ face,enteFile,+ cosineSimilarity,}) => {+ const { faceID } = face;+const [objectURL, setObjectURL] = useState<string | undefined>();useEffect(() => {@@ -209,17 +216,33 @@ const FaceCropImageView: React.FC<FaceCropImageViewProps> = ({};}, [faceID, enteFile]);- return objectURL ? (- <img- style={{ objectFit: "cover", width: "100%", height: "100%" }}- src={objectURL}- />- ) : (- <div />+ return (+ <FaceChip>+ {objectURL && (+ <img+ style={{+ objectFit: "cover",+ width: "100%",+ height: "100%",+ }}+ src={objectURL}+ />+ )}+ <Typography variant="small" textAlign="right">+ {cosineSimilarity.toFixed(2)}+ </Typography>+ </FaceChip>);};-const ListContainer = styled(Box)<{+const FaceChip = styled(Box)`+ width: 120px;+ height: 120px;+`;++const ListContainer = styled(Box, {+ shouldForwardProp: (propName) => propName != "shrinkRatio",+})<{columns: number;shrinkRatio: number;}>`@@ -240,49 +263,7 @@ const LabelContainer = styled(ListItemContainer)`height: 32px;`;-interface ItemListItem {- score?: number;- face?: Face;- files?: EnteFile[];- itemStartIndex?: number;- fileCount?: number;-}-const ListItem = styled("div")`display: flex;justify-content: center;`;--function getFractionFittableColumns(width: number): number {- return (width - 2 * getGapFromScreenEdge(width) + 4) / (120 + 4);-}--const getGapFromScreenEdge = (width: number) => (width > 4 * 120 ? 24 : 4);--function getShrinkRatio(width: number, columns: number) {- return (- (width - 2 * getGapFromScreenEdge(width) - (columns - 1) * 4) /- (columns * 120)- );-}--const itemListFromClusters = (clusters: UICluster[], columns: number) => {- const result: ItemListItem[] = [];- for (let index = 0; index < clusters.length; index++) {- const dupes = clusters[index];- result.push({- score: dupes.face.score,- fileCount: dupes.files.length,- });- let lastIndex = 0;- while (lastIndex < dupes.files.length) {- result.push({- files: dupes.files.slice(lastIndex, lastIndex + columns),- face: dupes.face,- itemStartIndex: index,- });- lastIndex += columns;- }- }- return result;-};diff --git a/web/packages/new/photos/services/ml/cluster-new.ts b/web/packages/new/photos/services/ml/cluster-new.tsindex 3c6b4e01e3..e67688abc0 100644--- a/web/packages/new/photos/services/ml/cluster-new.ts+++ b/web/packages/new/photos/services/ml/cluster-new.ts@@ -112,6 +112,17 @@ export interface CGroup {displayFaceID: string | undefined;}+// TODO-Cluster+export interface FaceNeighbours {+ face: Face;+ neighbours: FaceNeighbour[];+}++interface FaceNeighbour {+ face: Face;+ cosineSimilarity: number;+}+/*** Cluster faces into groups.*@@ -176,6 +187,8 @@ export const clusterFaces = async (faceIndexes: FaceIndex[]) => {// A function to generate new cluster IDs.const newClusterID = () => newNonSecureID("cluster_");+ const faceAndNeigbours: FaceNeighbours[] = [];+// For each face,for (const [i, { faceID, embedding }] of faces.entries()) {// If the face is already part of a cluster, then skip it.@@ -184,11 +197,13 @@ export const clusterFaces = async (faceIndexes: FaceIndex[]) => {// Find the nearest neighbour from among all the other faces.let nn: Face | undefined;let nnCosineSimilarity = 0;+ let neighbours: FaceNeighbour[] = [];for (let j = 0; j < faces.length; j++) {// ! This is an O(n^2) loop, be careful when adding more code here.- // Skip ourselves.- if (i == j) continue;+ // TODO-Cluster+ // // Skip ourselves.+ // if (i == j) continue;// Can't find a way of avoiding the null assertion here.// eslint-disable-next-line @typescript-eslint/no-non-null-assertion@@ -197,12 +212,27 @@ export const clusterFaces = async (faceIndexes: FaceIndex[]) => {// The vectors are already normalized, so we can directly use their// dot product as their cosine similarity.const csim = dotProduct(embedding, n.embedding);++ // Skip ourselves.+ if (i == j) {+ neighbours.push({ face: n, cosineSimilarity: csim });+ continue;+ }+if (csim > 0.76 && csim > nnCosineSimilarity) {nn = n;nnCosineSimilarity = csim;}++ neighbours.push({ face: n, cosineSimilarity: csim });}+ neighbours = neighbours.sort(+ (a, b) => b.cosineSimilarity - a.cosineSimilarity,+ );+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion+ faceAndNeigbours.push({ face: faces[i]!, neighbours });+if (nn) {// Found a neighbour near enough.@@ -287,7 +317,7 @@ export const clusterFaces = async (faceIndexes: FaceIndex[]) => {`Clustered ${faces.length} faces into ${validClusters.length} clusters (${Date.now() - t} ms)`,);- return { faces, clusters: validClusters, cgroups };+ return { faces, clusters: validClusters, cgroups, faceAndNeigbours };};/**diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.tsindex aeb10c5d35..0d1b2f105e 100644--- a/web/packages/new/photos/services/ml/index.ts+++ b/web/packages/new/photos/services/ml/index.ts@@ -343,6 +343,17 @@ export const wipSearchPersons = async () => {return _wip_searchPersons ?? [];};+export interface FaceFileNeighbours {+ face: Face;+ neighbours: FaceFileNeighbour[];+}++export interface FaceFileNeighbour {+ face: Face;+ enteFile: EnteFile;+ cosineSimilarity: number;+}+export const wipClusterPageContents = async () => {if (!(await wipClusterEnable())) return [];@@ -351,21 +362,24 @@ export const wipClusterPageContents = async () => {_wip_searchPersons = undefined;triggerStatusUpdate();- const { faces } = await clusterFaces(await faceIndexes());+ const { faceAndNeigbours } = await clusterFaces(await faceIndexes());// const searchPersons = await convertToSearchPersons(clusters, cgroups);const localFiles = await getAllLocalFiles();const localFileByID = new Map(localFiles.map((f) => [f.id, f]));-- const result1: { file: EnteFile; face: Face }[] = [];- for (const face of faces) {- const file = ensure(- localFileByID.get(ensure(fileIDFromFaceID(face.faceID))),- );- result1.push({ file, face });- }-- const result = result1.sort((a, b) => b.face.score - a.face.score);+ const fileForFace = ({ faceID }: Face) =>+ localFileByID.get(ensure(fileIDFromFaceID(faceID)));++ const result = faceAndNeigbours+ .map(({ face, neighbours }) => ({+ face,+ neighbours: neighbours.map(({ face, cosineSimilarity }) => ({+ face,+ enteFile: fileForFace(face),+ cosineSimilarity,+ })),+ }))+ .sort((a, b) => b.face.score - a.face.score);_wip_isClustering = false;// _wip_searchPersons = searchPersons;