diff --git a/mediapipe/tasks/web/components/utils/cosine_similarity.test.ts b/mediapipe/tasks/web/components/utils/cosine_similarity.test.ts index f442caa20..2a82f388d 100644 --- a/mediapipe/tasks/web/components/utils/cosine_similarity.test.ts +++ b/mediapipe/tasks/web/components/utils/cosine_similarity.test.ts @@ -70,12 +70,12 @@ describe('computeCosineSimilarity', () => { it('succeeds with quantized embeddings', () => { const u: Embedding = { - quantizedEmbedding: new Uint8Array([255, 128, 128, 128]), + quantizedEmbedding: new Uint8Array([127, 0, 0, 0]), headIndex: 0, headName: '' }; const v: Embedding = { - quantizedEmbedding: new Uint8Array([0, 128, 128, 128]), + quantizedEmbedding: new Uint8Array([128, 0, 0, 0]), headIndex: 0, headName: '' }; diff --git a/mediapipe/tasks/web/components/utils/cosine_similarity.ts b/mediapipe/tasks/web/components/utils/cosine_similarity.ts index 1f483b9b6..b512478f4 100644 --- a/mediapipe/tasks/web/components/utils/cosine_similarity.ts +++ b/mediapipe/tasks/web/components/utils/cosine_similarity.ts @@ -38,7 +38,7 @@ export function computeCosineSimilarity(u: Embedding, v: Embedding): number { } function convertToBytes(data: Uint8Array): number[] { - return Array.from(data, v => v - 128); + return Array.from(data, v => v > 127 ? v - 256 : v); } function compute(u: number[], v: number[]) {