Skip to content

armgit / ente

Public Imported from GitHub
0

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

[web][photos] Move to new clip with preprocessing included

laurenspriem560930966072
diff --git a/desktop/src/main/services/ml-worker.ts b/desktop/src/main/services/ml-worker.ts
index cd9a3e0404..fb4ec1817a 100644
--- a/desktop/src/main/services/ml-worker.ts
+++ b/desktop/src/main/services/ml-worker.ts
@@ -202,8 +202,9 @@ const createInferenceSession = async (modelPath: string) => {
};
const cachedCLIPImageSession = makeCachedInferenceSession(
- "mobileclip_s2_image.onnx",
+ "mobileclip_s2_image_opset18_rgba_sim.onnx",
143061211 /* 143 MB */,
+ // TODO: manav: check above number, because I got 143093992 but might be calculating wrong
);
/**
@@ -211,10 +212,14 @@ const cachedCLIPImageSession = makeCachedInferenceSession(
*
* The embeddings are computed using ONNX runtime, with MobileCLIP as the model.
*/
-export const computeCLIPImageEmbedding = async (input: Float32Array) => {
+export const computeCLIPImageEmbedding = async (
+ input: Uint8ClampedArray,
+ inputShape: number[],
+) => {
const session = await cachedCLIPImageSession();
+ const inputArray = new Uint8Array(input.buffer);
const feeds = {
- input: new ort.Tensor("float32", input, [1, 3, 256, 256]),
+ input: new ort.Tensor("uint8", inputArray, inputShape),
};
const t = Date.now();
const results = await session.run(feeds);
diff --git a/web/packages/base/types/ipc.ts b/web/packages/base/types/ipc.ts
index dd670e21ba..d5251f6e6f 100644
--- a/web/packages/base/types/ipc.ts
+++ b/web/packages/base/types/ipc.ts
@@ -558,7 +558,10 @@ export interface ElectronMLWorker {
*
* @returns A CLIP embedding (an array of 512 floating point values).
*/
- computeCLIPImageEmbedding: (input: Float32Array) => Promise<Float32Array>;
+ computeCLIPImageEmbedding: (
+ input: Uint8ClampedArray,
+ inputShape: number[],
+ ) => Promise<Float32Array>;
/**
* Return a CLIP embedding of the given image if we already have the model
diff --git a/web/packages/new/photos/services/ml/clip.ts b/web/packages/new/photos/services/ml/clip.ts
index 84a33c7964..5842182203 100644
--- a/web/packages/new/photos/services/ml/clip.ts
+++ b/web/packages/new/photos/services/ml/clip.ts
@@ -113,8 +113,9 @@ const computeEmbedding = async (
imageData: ImageData,
electron: ElectronMLWorker,
): Promise<Float32Array> => {
- const clipInput = convertToCLIPInput(imageData);
- return normalized(await electron.computeCLIPImageEmbedding(clipInput));
+ const { height, width, data: pixelData } = imageData;
+ const inputShape = [height, width, 4]; // [H, W, C]
+ return normalized(await electron.computeCLIPImageEmbedding(pixelData, inputShape));
};
/**