Skip to content

armgit / ente

Public Imported from GitHub
0

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

Continue

Manav Rathicfb1b8423be7
diff --git a/web/packages/new/photos/services/ml/clip.ts b/web/packages/new/photos/services/ml/clip.ts
index 87f03034f9..f8202d89ca 100644
--- a/web/packages/new/photos/services/ml/clip.ts
+++ b/web/packages/new/photos/services/ml/clip.ts
@@ -1,5 +1,10 @@
+// See: [Note: Allowing non-null assertions selectively]
+/* eslint-disable @typescript-eslint/no-non-null-assertion */
+
import type { EnteFile } from "@/new/photos/types/file";
+import { ensure } from "@/utils/ensure";
import type { ImageBitmapAndData } from "./bitmap";
+import { pixelRGBBicubic } from "./image";
import type { MLWorkerElectron } from "./worker-electron";
/**
@@ -12,6 +17,47 @@ export const clipIndexingVersion = 1;
*
* See {@link FaceIndex} for a similar structure with more comprehensive
* documentation.
+ *
+ * ---
+ *
+ * [Note: Natural language search using CLIP]
+ *
+ * CLIP (Contrastive Language-Image Pretraining) is a neural network trained on
+ * (image, text) pairs. It can be thought of as two separate (but jointly
+ * trained) encoders - one for images, and one for text - that both map to the
+ * same embedding space.
+ *
+ * We use this for natural language search within the app:
+ *
+ * 1. Pre-compute an embedding for each image.
+ *
+ * 2. When the user searches, compute an embedding for the search term.
+ *
+ * 3. Use cosine similarity to find the find the image (embedding) closest to
+ * the text (embedding).
+ *
+ * More details are in our [blog
+ * post](https://ente.io/blog/image-search-with-clip-ggml/) that describes the
+ * initial launch of this feature using the GGML runtime.
+ *
+ * Since the initial launch, we've switched over to another runtime,
+ * [ONNX](https://onnxruntime.ai) and have made other implementation changes,
+ * but the overall gist remains the same.
+ *
+ * Note that we don't train the neural network - we only use one of the publicly
+ * available pre-trained neural networks for inference. These neural networks
+ * are wholly defined by their connectivity and weights. ONNX, our ML runtimes,
+ * loads these weights and instantiates a running network that we can use to
+ * compute the embeddings.
+ *
+ * Theoretically, the same CLIP model can be loaded by different frameworks /
+ * runtimes, but in practice each runtime has its own preferred format, and
+ * there are also quantization tradeoffs. So there is a specific model (a binary
+ * encoding of weights) tied to our current runtime that we use.
+ *
+ * To ensure that the embeddings, for the most part, can be shared, whenever
+ * possible we try to ensure that all the preprocessing steps, and the model
+ * itself, is the same across clients - web and mobile.
*/
export interface CLIPIndex {
/** The ID of the {@link EnteFile} whose index this is. */
@@ -68,10 +114,67 @@ export const indexCLIP = async (
const indexCLIP_ = async (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
- _imageData: ImageData,
+ imageData: ImageData,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
- _electron: MLWorkerElectron,
+ electron: MLWorkerElectron,
// eslint-disable-next-line @typescript-eslint/require-await
): Promise<number[]> => {
- throw new Error("TODO");
+ const clipInput = convertToCLIPInput(imageData);
+ const embedding = await electron.computeCLIPImageEmbedding(clipInput);
+ return Array.from(normalizeEmbedding(embedding));
+};
+
+/**
+ * Convert {@link imageData} into the format that the CLIP model expects.
+ */
+const convertToCLIPInput = (imageData: ImageData) => {
+ const requiredWidth = 224;
+ const requiredHeight = 224;
+
+ const mean: number[] = [0.48145466, 0.4578275, 0.40821073];
+ const std: number[] = [0.26862954, 0.26130258, 0.27577711];
+
+ const { width, height, data: pixelData } = imageData;
+
+ // Maintain aspect ratio.
+ const scale = Math.max(requiredWidth / width, requiredHeight / height);
+
+ const scaledWidth = Math.round(width * scale);
+ const scaledHeight = Math.round(height * scale);
+ const widthOffset = Math.max(0, scaledWidth - requiredWidth) / 2;
+ const heightOffset = Math.max(0, scaledHeight - requiredHeight) / 2;
+
+ const clipInput = new Float32Array(3 * requiredWidth * requiredHeight);
+
+ // Populate the Float32Array with normalized pixel values.
+ let pi = 0;
+ const cOffsetG = requiredHeight * requiredWidth; // ChannelOffsetGreen
+ const cOffsetB = 2 * requiredHeight * requiredWidth; // ChannelOffsetBlue
+ for (let h = 0 + heightOffset; h < scaledHeight - heightOffset; h++) {
+ for (let w = 0 + widthOffset; w < scaledWidth - widthOffset; w++) {
+ const { r, g, b } = pixelRGBBicubic(
+ w / scale,
+ h / scale,
+ pixelData,
+ width,
+ height,
+ );
+ clipInput[pi] = (r / 255.0 - mean[0]!) / std[0]!;
+ clipInput[pi + cOffsetG] = (g / 255.0 - mean[1]!) / std[1]!;
+ clipInput[pi + cOffsetB] = (b / 255.0 - mean[2]!) / std[2]!;
+ pi++;
+ }
+ }
+ return clipInput;
+};
+
+const normalizeEmbedding = (embedding: Float32Array) => {
+ let normalization = 0;
+ for (const v of embedding) normalization += v * v;
+
+ const sqrtNormalization = Math.sqrt(normalization);
+ for (let index = 0; index < embedding.length; index++)
+ embedding[index] = ensure(embedding[index]) / sqrtNormalization;
+
+ return embedding;
};
diff --git a/web/packages/new/photos/services/ml/image.ts b/web/packages/new/photos/services/ml/image.ts
index b3b078041f..d1608e96e0 100644
--- a/web/packages/new/photos/services/ml/image.ts
+++ b/web/packages/new/photos/services/ml/image.ts
@@ -80,7 +80,7 @@ const pixelRGBA = (
* Returns the pixel value (RGB) at the given coordinates ({@link fx},
* {@link fy}) using bicubic interpolation.
*/
-const pixelRGBBicubic = (
+export const pixelRGBBicubic = (
fx: number,
fy: number,
imageData: Uint8ClampedArray,
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index 62f4637894..1f6ff828f1 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -47,6 +47,7 @@ const createComlinkWorker = async () => {
appVersion: electron.appVersion,
detectFaces: electron.detectFaces,
computeFaceEmbeddings: electron.computeFaceEmbeddings,
+ computeCLIPImageEmbedding: electron.computeCLIPImageEmbedding,
};
const cw = new ComlinkWorker<typeof MLWorker>(
diff --git a/web/packages/new/photos/services/ml/worker-electron.ts b/web/packages/new/photos/services/ml/worker-electron.ts
index 250db9cf5d..0e187a178a 100644
--- a/web/packages/new/photos/services/ml/worker-electron.ts
+++ b/web/packages/new/photos/services/ml/worker-electron.ts
@@ -10,4 +10,5 @@ export interface MLWorkerElectron {
appVersion: () => Promise<string>;
detectFaces: (input: Float32Array) => Promise<Float32Array>;
computeFaceEmbeddings: (input: Float32Array) => Promise<Float32Array>;
+ computeCLIPImageEmbedding: (input: Float32Array) => Promise<Float32Array>;
}
diff --git a/web/packages/next/types/ipc.ts b/web/packages/next/types/ipc.ts
index 4f68d50d73..be53612981 100644
--- a/web/packages/next/types/ipc.ts
+++ b/web/packages/next/types/ipc.ts
@@ -337,9 +337,10 @@ export interface Electron {
/**
* Return a CLIP embedding of the given image.
*
- * See: [Note: CLIP based magic search]
+ * See: [Note: Natural language search using CLIP]
*
- * @param jpegImageData The raw bytes of the image encoded as an JPEG.
+ * The input is a opaque binary data whose internal structure is specific to
+ * our implementation and the ML model (CLIP) we use.
*
* @returns A CLIP embedding.
*/
@@ -361,7 +362,7 @@ export interface Electron {
* embeddings are used as part of deducing user initiated search results,
* and we don't want to block that interaction on a large network request.
*
- * See: [Note: CLIP based magic search]
+ * See: [Note: Natural language search using CLIP]
*
* @param text The string whose embedding we want to compute.
*