[mob][photos] Move cosine function

This commit is contained in:
laurenspriem
2024-07-02 13:30:23 +05:30
parent 4871755140
commit 9e76c31655
2 changed files with 19 additions and 16 deletions

View File

@@ -67,3 +67,21 @@ double cosineDistForNormVectors(List<double> vector1, List<double> vector2) {
}
return 1.0 - dotProduct;
}
/// NOTE: This function assumes that both embeddings are normalized!
@pragma("vm:prefer-inline")
double computeCosineSimilarity(
List<double> embedding1,
List<double> embedding2,
) {
assert(
embedding1.length == embedding2.length,
"The two embeddings should have the same length",
);
double cosineSimilarity = 0;
final length = embedding1.length;
for (int index = 0; index < length; index++) {
cosineSimilarity += embedding1[index] * embedding2[index];
}
return cosineSimilarity;
}

View File

@@ -18,6 +18,7 @@ import "package:photos/events/machine_learning_control_event.dart";
import "package:photos/models/embedding.dart";
import "package:photos/models/file/file.dart";
import "package:photos/services/collections_service.dart";
import "package:photos/services/machine_learning/face_ml/face_clustering/cosine_distance.dart";
import 'package:photos/services/machine_learning/semantic_search/embedding_store.dart';
import 'package:photos/services/machine_learning/semantic_search/frameworks/ggml.dart';
import 'package:photos/services/machine_learning/semantic_search/frameworks/ml_framework.dart';
@@ -504,22 +505,6 @@ List<QueryResult> computeBulkSimilarities(Map args) {
return queryResults;
}
double computeCosineSimilarity(
List<double> imageEmbedding,
List<double> textEmbedding,
) {
assert(
imageEmbedding.length == textEmbedding.length,
"The two embeddings should have the same length",
);
double cosineSimilarity = 0;
final length = imageEmbedding.length;
for (int index = 0; index < length; index++) {
cosineSimilarity += imageEmbedding[index] * textEmbedding[index];
}
return cosineSimilarity;
}
class QueryResult {
final int id;
final double score;