Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix pet detection never running on previously-indexed files

Amrithesh Kakkoth Ente05581e43ba3e
diff --git a/mobile/apps/photos/lib/db/ml/base.dart b/mobile/apps/photos/lib/db/ml/base.dart
index ab6bada38f..37ad3a9df8 100644
--- a/mobile/apps/photos/lib/db/ml/base.dart
+++ b/mobile/apps/photos/lib/db/ml/base.dart
@@ -125,4 +125,8 @@ abstract class IMLDataDB<T> {
Future<void> putClip(List<ClipEmbedding> embeddings);
Future<void> deleteClipEmbeddings(List<T> fileIDs);
Future<void> deleteClipIndexes();
+
+ Future<Map<int, int>> petIndexedFileIds({int minimumMlVersion});
+ Future<int> getPetIndexedFileCount({int minimumMlVersion});
+ Future<void> markPetIndexed(int fileID, int mlVersion);
}
diff --git a/mobile/apps/photos/lib/db/ml/db.dart b/mobile/apps/photos/lib/db/ml/db.dart
index ec31294cba..e5a1dacc2b 100644
--- a/mobile/apps/photos/lib/db/ml/db.dart
+++ b/mobile/apps/photos/lib/db/ml/db.dart
@@ -105,6 +105,7 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
createPetClusterSummaryTable,
createPetFaceVectorIdMappingTable,
createObjectVectorIdMappingTable,
+ createPetIndexedFilesTable,
];
static const List<String> _offlineMigrationScripts = [
..._defaultMigrationScripts,
@@ -563,6 +564,7 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
await db.execute(deletePetClusterSummaryTable);
await db.execute(deletePetFaceVectorIdMappingTable);
await db.execute(deleteObjectVectorIdMappingTable);
+ await db.execute(deletePetIndexedFilesTable);
for (final vdb in PetVectorDB.allInstances) {
await vdb.deleteIndexFile();
}
@@ -2341,6 +2343,46 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
return maps.first['count'] as int;
}
+ @override
+ Future<Map<int, int>> petIndexedFileIds({
+ int minimumMlVersion = petMlVersion,
+ }) async {
+ final db = await asyncDB;
+ final String query = '''
+ SELECT $fileIDColumn, $mlVersionColumn
+ FROM $petIndexedFilesTable
+ WHERE $mlVersionColumn >= $minimumMlVersion
+ ''';
+ final List<Map<String, dynamic>> maps = await db.getAll(query);
+ final Map<int, int> result = {};
+ for (final map in maps) {
+ result[map[fileIDColumn] as int] = map[mlVersionColumn] as int;
+ }
+ return result;
+ }
+
+ @override
+ Future<int> getPetIndexedFileCount({
+ int minimumMlVersion = petMlVersion,
+ }) async {
+ final db = await asyncDB;
+ final String query =
+ 'SELECT COUNT(*) as count FROM $petIndexedFilesTable WHERE $mlVersionColumn >= $minimumMlVersion';
+ final List<Map<String, dynamic>> maps = await db.getAll(query);
+ return maps.first['count'] as int;
+ }
+
+ @override
+ Future<void> markPetIndexed(int fileID, int mlVersion) async {
+ final db = await asyncDB;
+ await db.execute(
+ '''INSERT INTO $petIndexedFilesTable ($fileIDColumn, $mlVersionColumn)
+ VALUES (?, ?)
+ ON CONFLICT($fileIDColumn) DO UPDATE SET $mlVersionColumn = excluded.$mlVersionColumn''',
+ [fileID, mlVersion],
+ );
+ }
+
@override
Future<void> putClip(List<ClipEmbedding> embeddings) async {
if (embeddings.isEmpty) return;
diff --git a/mobile/apps/photos/lib/db/ml/schema.dart b/mobile/apps/photos/lib/db/ml/schema.dart
index ec6efb292b..bed7b3a7e0 100644
--- a/mobile/apps/photos/lib/db/ml/schema.dart
+++ b/mobile/apps/photos/lib/db/ml/schema.dart
@@ -301,3 +301,16 @@ CREATE TABLE IF NOT EXISTS $objectVectorIdMappingTable (
const deleteObjectVectorIdMappingTable =
'DELETE FROM $objectVectorIdMappingTable';
+
+// ── Pet Indexing Tracking Table ──
+
+const petIndexedFilesTable = 'pet_indexed_files';
+
+const createPetIndexedFilesTable = '''
+CREATE TABLE IF NOT EXISTS $petIndexedFilesTable (
+ $fileIDColumn INTEGER NOT NULL PRIMARY KEY,
+ $mlVersionColumn INTEGER NOT NULL DEFAULT -1
+);
+''';
+
+const deletePetIndexedFilesTable = 'DELETE FROM $petIndexedFilesTable';
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_indexing_isolate.dart b/mobile/apps/photos/lib/services/machine_learning/ml_indexing_isolate.dart
index c83315fff5..928f5d1c72 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_indexing_isolate.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_indexing_isolate.dart
@@ -88,7 +88,7 @@ class MLIndexingIsolate extends SuperIsolate {
"useRustMl": useRustMl,
"runFaces": instruction.shouldRunFaces,
"runClip": instruction.shouldRunClip,
- "runPets": useRustMl && flagService.petEnabled,
+ "runPets": useRustMl && instruction.shouldRunPets,
...rustRuntimeArgs,
"faceDetectionAddress": FaceDetectionService.instance.sessionAddress,
"faceEmbeddingAddress": FaceEmbeddingService.instance.sessionAddress,
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_result.dart b/mobile/apps/photos/lib/services/machine_learning/ml_result.dart
index 1f6bb9ea49..014ad5b2b1 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_result.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_result.dart
@@ -17,9 +17,10 @@ class MLResult {
Dimensions decodedImageSize;
- bool get ranML => facesRan || clipRan;
+ bool get ranML => facesRan || clipRan || petsRan;
bool get facesRan => faces != null;
bool get clipRan => clip != null;
+ bool get petsRan => petFaces != null || detectedObjects != null;
MLResult({
this.fileId = -1,
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
index 2856a69269..6ef0f89156 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@ -701,6 +701,9 @@ class MLService {
);
}
}
+ if (instruction.shouldRunPets) {
+ await mlDataDB.markPetIndexed(result.fileId, petMlVersion);
+ }
_logger.info("ML result for fileID ${result.fileId} stored remote+local");
return actuallyRanML;
} catch (e, s) {
@@ -729,6 +732,9 @@ class MLService {
instruction.file,
);
}
+ if (instruction.shouldRunPets) {
+ await mlDataDB.markPetIndexed(instruction.fileKey, petMlVersion);
+ }
return true;
}
_logger.severe(
diff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dart
index fa35d99ecd..7a3603183a 100644
--- a/mobile/apps/photos/lib/utils/ml_util.dart
+++ b/mobile/apps/photos/lib/utils/ml_util.dart
@@ -52,6 +52,7 @@ class FileMLInstruction {
final int? offlineFileKey;
bool shouldRunFaces;
bool shouldRunClip;
+ bool shouldRunPets;
FileDataEntity? existingRemoteFileML;
FileMLInstruction({
@@ -60,9 +61,9 @@ class FileMLInstruction {
this.offlineFileKey,
required this.shouldRunFaces,
required this.shouldRunClip,
+ this.shouldRunPets = false,
});
- // Returns true if the file should be indexed for either faces or clip
- bool get pendingML => shouldRunFaces || shouldRunClip;
+ bool get pendingML => shouldRunFaces || shouldRunClip || shouldRunPets;
bool get isOffline => mode == MLMode.offline;
int get fileKey => isOffline ? offlineFileKey! : file.uploadedFileID!;
}
@@ -75,7 +76,11 @@ Future<IndexStatus> getIndexStatus() async {
final int indexableFiles = await _getIndexableFileCount(mode: mode);
final int facesIndexedFiles = await mlDataDB.getFaceIndexedFileCount();
final int clipIndexedFiles = await mlDataDB.getClipIndexedFileCount();
- final int indexedFiles = math.min(facesIndexedFiles, clipIndexedFiles);
+ int indexedFiles = math.min(facesIndexedFiles, clipIndexedFiles);
+ if (flagService.petEnabled) {
+ final int petIndexedFiles = await mlDataDB.getPetIndexedFileCount();
+ indexedFiles = math.min(indexedFiles, petIndexedFiles);
+ }
final showIndexedFiles = math.min(indexedFiles, indexableFiles);
final showPendingFiles = math.max(indexableFiles - indexedFiles, 0);
@@ -104,6 +109,9 @@ Future<List<FileMLInstruction>> getFilesForMlIndexing() async {
final Map<int, int> faceIndexedFileIDs = await mlDataDB.faceIndexedFileIds();
final Map<int, int> clipIndexedFileIDs =
await mlDataDB.clipIndexedFileWithVersion();
+ final bool petEnabled = flagService.petEnabled;
+ final Map<int, int> petIndexedFileIDs =
+ petEnabled ? await mlDataDB.petIndexedFileIds() : const {};
final Set<int> queuedFiledIDs = {};
final Set<int> filesWithFDStatus = await mlDataDB.getFileIDsWithFDData();
@@ -132,7 +140,9 @@ Future<List<FileMLInstruction>> getFilesForMlIndexing() async {
_shouldRunIndexing(enteFile, faceIndexedFileIDs, faceMlVersion);
final shouldRunClip =
_shouldRunIndexing(enteFile, clipIndexedFileIDs, clipMlVersion);
- if (!shouldRunFaces && !shouldRunClip) {
+ final shouldRunPets = petEnabled &&
+ _shouldRunIndexing(enteFile, petIndexedFileIDs, petMlVersion);
+ if (!shouldRunFaces && !shouldRunClip && !shouldRunPets) {
continue;
}
final instruction = FileMLInstruction(
@@ -140,6 +150,7 @@ Future<List<FileMLInstruction>> getFilesForMlIndexing() async {
mode: MLMode.online,
shouldRunFaces: shouldRunFaces,
shouldRunClip: shouldRunClip,
+ shouldRunPets: shouldRunPets,
);
if ((enteFile.localID ?? '').isEmpty) {
filesWithoutLocalID.add(instruction);
@@ -162,7 +173,9 @@ Future<List<FileMLInstruction>> getFilesForMlIndexing() async {
_shouldRunIndexing(enteFile, faceIndexedFileIDs, faceMlVersion);
final shouldRunClip =
_shouldRunIndexing(enteFile, clipIndexedFileIDs, clipMlVersion);
- if (!shouldRunFaces && !shouldRunClip) {
+ final shouldRunPets = petEnabled &&
+ _shouldRunIndexing(enteFile, petIndexedFileIDs, petMlVersion);
+ if (!shouldRunFaces && !shouldRunClip && !shouldRunPets) {
continue;
}
final instruction = FileMLInstruction(
@@ -170,6 +183,7 @@ Future<List<FileMLInstruction>> getFilesForMlIndexing() async {
mode: MLMode.online,
shouldRunFaces: shouldRunFaces,
shouldRunClip: shouldRunClip,
+ shouldRunPets: shouldRunPets,
);
hiddenFilesToIndex.add(instruction);
}
@@ -210,6 +224,9 @@ Future<List<FileMLInstruction>> getOfflineFilesForMlIndexing() async {
final Map<int, int> faceIndexedFileIDs = await mlDataDB.faceIndexedFileIds();
final Map<int, int> clipIndexedFileIDs =
await mlDataDB.clipIndexedFileWithVersion();
+ final bool petEnabled = flagService.petEnabled;
+ final Map<int, int> petIndexedFileIDs =
+ petEnabled ? await mlDataDB.petIndexedFileIds() : const {};
final Set<int> queuedFileIDs = {};
final enteFiles = await SearchService.instance.getAllFilesForSearch();
@@ -253,7 +270,13 @@ Future<List<FileMLInstruction>> getOfflineFilesForMlIndexing() async {
clipIndexedFileIDs,
clipMlVersion,
);
- if (!shouldRunFaces && !shouldRunClip) {
+ final shouldRunPets = petEnabled &&
+ _shouldRunIndexingWithFileId(
+ localIntId,
+ petIndexedFileIDs,
+ petMlVersion,
+ );
+ if (!shouldRunFaces && !shouldRunClip && !shouldRunPets) {
continue;
}
instructions.add(
@@ -263,6 +286,7 @@ Future<List<FileMLInstruction>> getOfflineFilesForMlIndexing() async {
offlineFileKey: localIntId,
shouldRunFaces: shouldRunFaces,
shouldRunClip: shouldRunClip,
+ shouldRunPets: shouldRunPets,
),
);
}
diff --git a/mobile/apps/photos/rust/src/api/ml_indexing_api.rs b/mobile/apps/photos/rust/src/api/ml_indexing_api.rs
index 624f148e58..8bc36afca3 100644
--- a/mobile/apps/photos/rust/src/api/ml_indexing_api.rs
+++ b/mobile/apps/photos/rust/src/api/ml_indexing_api.rs
@@ -283,6 +283,12 @@ fn run_clip_text_rust_inner(req: RunClipTextRequest) -> MlResult<RunClipTextResu
face_embedding: String::new(),
clip_image: String::new(),
clip_text: model_path,
+ pet_face_detection: String::new(),
+ pet_face_embedding_dog: String::new(),
+ pet_face_embedding_cat: String::new(),
+ pet_body_detection: String::new(),
+ pet_body_embedding_dog: String::new(),
+ pet_body_embedding_cat: String::new(),
},
provider_policy: to_provider_policy(&provider_policy),
};