[Enhancement] Add batch processing and embedding caching to face clustering for better performance on large galleries
Description
Face clustering with FaceNet and DBSCAN is great for grouping similar faces offline, but for users with large photo libraries, sequential embedding computation can lead to long wait times and high memory usage. Implementing batch processing (feed multiple images to FaceNet/ONNX at once) and caching (store computed embeddings in SQLite to skip recomputes on unchanged images) would optimize this, making PictoPy more scalable.
Proposed Changes
- In the clustering service (e.g., backend/app/services/clustering.py or equivalent):
- Modify embedding extraction to use batching: Load images in chunks (e.g., via OpenCV imread in loop, then np.stack for ONNX input).
- Add caching: Before computing, check/query SQLite for existing embeddings by image hash/path; store new ones post-compute.
- Handle batch size as configurable (e.g., via .env, default 32 based on hardware).
- Update any related routers/endpoints to return progress (if async) or handle interruptions.
- Add unit tests in backend/tests/ for batch vs. single performance and cache hits.
- Ensure compatibility with CPU/GPU via ONNX Runtime.
References
- Recent clustering fix: Commit 7649865 (improve accuracy with thresholds).
- ONNX batching docs: https://onnxruntime.ai/docs/ort/python/api_summary.html#inference
- Similar optimizations in other ML apps (e.g., via torch DataLoader analogs).
I'm experienced with Python/ML and would love to implement it via PR if assigned!
👋 Thanks for opening this issue! The maintainers will review it shortly. Till then, do not open any PR. This is a strict rule we like to follow around here :)
Hi @rohan-pandeyy ,
I analyzed the current face clustering workflow and noticed that processing images sequentially leads to high latency and memory overhead on larger galleries.
To resolve this performance bottleneck, I'm working on the following enhancements:
-
Batch Processing: Processing images in configurable chunks (16/32 batch size) via OpenCV and stacking tensors (np.stack) for ONNX model inference to speed up processing significantly.
-
Embedding Caching: Implementing a SQLite caching layer (image_hash ---> face_embeddings) to skip re-computation for unchanged images during clustering.
-
Async / Progress Handling: Updating the backend endpoints to deliver progress updates smoothly. .
I'm currently testing the changes locally. Could you please assign this issue to me so I can open a PR ?
Hi @rohan-pandeyy ,
I first investigated the current implementation and benchmarked the existing face clustering flow before making any production changes.
What I found:
Sequential processing: The 115-image benchmark averaged ~51.7s across three runs.
Batch processing: A batch size of 16 is currently rejected because the FaceNet ONNX model expects an input shape of [1, 3, 160, 160].
Embedding caching: FaceNet embeddings are already stored in SQLite, but I couldn't find a FaceNet-specific cache validation or reuse step before inference.
Image changes: The existing sync flow tracks file size and modification time, but these values aren't tied to the stored FaceNet embeddings.
I haven't made any production changes yet. I'm using these findings to finalize the implementation approach before moving ahead.
I recorded the investigation and benchmark results here:
https://github.com/user-attachments/assets/16d23dfa-eaec-4480-8977-af0e422d4e33
Hi @rohan-pandeyy
Based on the investigation and benchmarking, this is how I'm planning to approach the implementation ->
Batch processing: I'll update the FaceNet inference flow to process face crops in configurable batches. Since the current ONNX model expects a batch dimension of 1, I'll first handle that model/input constraint instead of directly passing batched tensors to the existing model.
Embedding caching: I'll reuse the existing SQLite face-embedding storage and add a way to check whether an embedding belongs to the current version of the image. If the image is unchanged, the stored embedding can be reused. If it has changed, the embedding will be recomputed.
Correctness: I'll make sure cached and newly generated embeddings follow the same downstream clustering flow, and I'll add tests for cache reuse and invalidation.
Compatibility: I'll keep the existing FaceNet, ONNX Runtime, SQLite, and clustering flow working as before, and verify that the changes don't affect the existing face-related workflows.
Benchmarking: I'll compare the updated implementation with the current sequential baseline using the same dataset and benchmark setup.
Progress handling: I'll check the current processing flow and add progress reporting where it's needed for long-running gallery processing.
I've attached the investigation and benchmark recording in my previous comment. If this approach looks good, could you please assign #961 to me so I can go ahead with the implementation and open a PR?