Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Agenda

Manav Rathi8d4629254ca7
diff --git a/web/packages/new/photos/services/ml/clip.ts b/web/packages/new/photos/services/ml/clip.ts
new file mode 100644
index 0000000000..87f03034f9
--- /dev/null
+++ b/web/packages/new/photos/services/ml/clip.ts
@@ -0,0 +1,77 @@
+import type { EnteFile } from "@/new/photos/types/file";
+import type { ImageBitmapAndData } from "./bitmap";
+import type { MLWorkerElectron } from "./worker-electron";
+
+/**
+ * The version of the CLIP indexing pipeline implemented by the current client.
+ */
+export const clipIndexingVersion = 1;
+
+/**
+ * The CLIP embedding for a file (and some metadata).
+ *
+ * See {@link FaceIndex} for a similar structure with more comprehensive
+ * documentation.
+ */
+export interface CLIPIndex {
+ /** The ID of the {@link EnteFile} whose index this is. */
+ fileID: number;
+ /** An integral version number of the indexing algorithm / pipeline. */
+ version: number;
+ /** The UA for the client which generated this embedding. */
+ client: string;
+ /** The CLIP embedding itself. */
+ embedding: number[];
+}
+
+/**
+ * Compute the CLIP embedding of a given file.
+ *
+ * This function is the entry point to the CLIP indexing pipeline. The file goes
+ * through various stages:
+ *
+ * 1. Downloading the original if needed.
+ * 2. Convert (if needed) and pre-process.
+ * 3. Compute embeddings using ONNX/CLIP.
+ *
+ * Once all of it is done, it CLIP embedding (wrapped as a {@link CLIPIndex} so
+ * that it can be saved locally and also uploaded to the user's remote storage
+ * for use on their other devices).
+ *
+ * @param enteFile The {@link EnteFile} to index.
+ *
+ * @param uploadItem If we're called during the upload process, then this will
+ * be set to the {@link UploadItem} that was uploaded. This way, we can directly
+ * use the on-disk file instead of needing to download the original from remote.
+ *
+ * @param electron The {@link MLWorkerElectron} instance that allows us to call
+ * our Node.js layer for various functionality.
+ *
+ * @param userAgent The UA of the client that is doing the indexing (us).
+ */
+export const indexCLIP = async (
+ enteFile: EnteFile,
+ image: ImageBitmapAndData,
+ electron: MLWorkerElectron,
+ userAgent: string,
+): Promise<CLIPIndex> => {
+ const { data: imageData } = image;
+ const fileID = enteFile.id;
+
+ return {
+ fileID,
+ version: clipIndexingVersion,
+ client: userAgent,
+ embedding: await indexCLIP_(imageData, electron),
+ };
+};
+
+const indexCLIP_ = async (
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ _imageData: ImageData,
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ _electron: MLWorkerElectron,
+ // eslint-disable-next-line @typescript-eslint/require-await
+): Promise<number[]> => {
+ throw new Error("TODO");
+};
diff --git a/web/packages/new/photos/services/ml/db.ts b/web/packages/new/photos/services/ml/db.ts
index 9a71b39761..e1363f5fae 100644
--- a/web/packages/new/photos/services/ml/db.ts
+++ b/web/packages/new/photos/services/ml/db.ts
@@ -4,34 +4,42 @@ import type { EmbeddingModel } from "./embedding";
import type { FaceIndex } from "./face";
/**
- * Face DB schema.
+ * ML DB schema.
*
- * There "face" database is made of two object stores:
+ * The "ML" database is made of three object stores:
+ *
+ * - "file-status": Contains {@link FileStatus} objects, one for each
+ * {@link EnteFile} that the ML subsystem knows about. Periodically (and when
+ * required), this is synced with the list of files that the current client
+ * knows about locally.
*
* - "face-index": Contains {@link FaceIndex} objects, either indexed locally or
* fetched from remote storage.
*
- * - "file-status": Contains {@link FileStatus} objects, one for each
- * {@link EnteFile} that the current client knows about.
+ * - "clip-index": Contains {@link CLIPIndex} objects, either indexed locally or
+ * fetched from remote storage.
*
- * Both the stores are keyed by {@link fileID}, and are expected to contain the
- * exact same set of {@link fileID}s. The face-index can be thought of as the
- * "original" indexing result, whilst file-status bookkeeps information about
- * the indexing process (whether or not a file needs indexing, or if there were
- * errors doing so).
+ * All the stores are keyed by {@link fileID}. The "file-status" contains
+ * book-keeping about the indexing process (whether or not a file needs
+ * indexing, or if there were errors doing so), while the other stores contain
+ * the actual indexing results.
*
* In tandem, these serve as the underlying storage for the functions exposed by
* this file.
*/
interface FaceDBSchema extends DBSchema {
+ "file-status": {
+ key: number;
+ value: FileStatus;
+ indexes: { status: FileStatus["status"] };
+ };
"face-index": {
key: number;
value: FaceIndex;
};
- "file-status": {
+ "clip-index": {
key: number;
- value: FileStatus;
- indexes: { status: FileStatus["status"] };
+ value: CLIPIndex;
};
}
@@ -45,13 +53,20 @@ interface FileStatus {
* to be indexed.
*
* - "indexed" - We have a corresponding entry for this file in the
- * "face-index" object (either indexed locally or fetched from remote).
+ * "face-index" _and_ "clip-index" object stores (either indexed locally
+ * or fetched from remote).
*
* - "failed" - Indexing was attempted but failed.
*
- * We also have a (IndexedDB) "index" on this field to allow us to
- * efficiently select or count {@link fileIDs} that fall into various
- * buckets.
+ * There can arise situations in which a file has one, but not all, indexes.
+ * e.g. it may have a "face-index" but "clip-index" might've not yet
+ * happened (or failed). In such cases, the status of the file will be
+ * "indexable": it transitions to "indexed" only after all indexes have been
+ * computed or fetched.
+ *
+ * If you have't heard the word "index" to the point of zoning out, we also
+ * have a (IndexedDB) "index" on the status field to allow us to efficiently
+ * select or count {@link fileIDs} that fall into various buckets.
*/
status: "indexable" | "indexed" | "failed";
/**
@@ -64,6 +79,8 @@ interface FileStatus {
/**
* The number of times attempts to index this file failed.
*
+ * It counts failure across all index types.
+ *
* This is guaranteed to be `0` for files with status "indexed".
*/
failureCount: number;
diff --git a/web/packages/new/photos/services/ml/face.ts b/web/packages/new/photos/services/ml/face.ts
index 149e922b09..5c1d3a6763 100644
--- a/web/packages/new/photos/services/ml/face.ts
+++ b/web/packages/new/photos/services/ml/face.ts
@@ -196,24 +196,26 @@ export interface Box {
/**
* Index faces in the given file.
*
- * This function is the entry point to the indexing pipeline. The file goes
+ * This function is the entry point to the face indexing pipeline. The file goes
* through various stages:
*
* 1. Downloading the original if needed.
* 2. Detect faces using ONNX/YOLO
* 3. Align the face rectangles, compute blur.
- * 4. Compute embeddings for the detected face (crops).
+ * 4. Compute embeddings using ONNX/MFNT for the detected face (crop).
*
* Once all of it is done, it returns the face rectangles and embeddings so that
- * they can be saved locally for offline use, and encrypts and uploads them to
- * the user's remote storage so that their other devices can download them
- * instead of needing to reindex.
+ * they can be saved locally (for offline use), and also uploaded to the user's
+ * remote storage so that their other devices can download them instead of
+ * needing to reindex.
+ *
+ * As an optimization, it also saves the face crops of the detected faces to the
+ * local cache (they can be regenerated independently too by using
+ * {@link regenerateFaceCrops}).
*
* @param enteFile The {@link EnteFile} to index.
*
- * @param uploadItem If we're called during the upload process, then this will
- * be set to the {@link UploadItem} that was uploaded. This way, we can directly
- * use the on-disk file instead of needing to download the original from remote.
+ * @param image The file's contents.
*
* @param electron The {@link MLWorkerElectron} instance that allows us to call
* our Node.js layer for various functionality.