Skip to content

Release duplicate face inputs before recognition inference - #1773

Open
Afloat16 wants to merge 1 commit into
serengil:masterfrom
Afloat16:fix/representation-input-lifetime-20261129
Open

Afloat16 wants to merge 1 commit into
serengil:masterfrom
Afloat16:fix/representation-input-lifetime-20261129

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Tickets

Newly measured memory retention in the existing representation path; no existing issue is claimed. Checked @Afloat16's 107 earlier repository PRs, related upstream representation/memory/concatenation PRs and issues, and the recent history of representation.py. This is separate from detector crop batching and model classifier weights.

What has been done

represent() collects a float32 array for every preprocessed face, then copies those arrays into batch_images_np. The original batch_images list and the final loop variable img remain alive throughout model.forward(), retaining a second copy of the complete model input alongside inference workspaces.

Release those two references immediately after concatenation. Use the batch array's length for the existing single-face embedding return branch. Face preprocessing, batch dimensions, model calls, result grouping, anti-spoofing and return_face values are unchanged.

Measured effect

A controlled public DeepFace.represent() call processes 192 repeats of tests/unit/dataset/img1.jpg into 224×224 BGR float32 inputs. It uses the actual TorchFacialRecognition adapter and a deterministic small CNN (1×1 Conv2d 3→4, ReLU, adaptive average pooling, flatten), providing a reproducible inference workspace without downloaded weights.

Three alternating baseline/modified pairs ran in separate CPU processes, with one Torch thread. Median measurements:

Measurement Base This change
Redundant live preprocessed input arrays at inference entry 110.25 MiB 0 MiB
Process RSS at inference entry 483.48 MiB 372.16 MiB
Whole-process peak RSS 783.36 MiB 672.15 MiB

The peak decrease in this controlled workload is 111.21 MiB (14.20%). All six inference-input hashes and complete-result hashes match exactly. Concatenation itself still temporarily needs both copies. These measurements describe this CPU workspace; they do not establish the peak reduction for every pretrained model, execution provider or allocator.

How to test

Base: 8cec026045be164e14b510c55c1264e743dc4999. Linux x86-64, Python 3.12.14, NumPy 2.5.5, OpenCV 4.13.0 and PyTorch 2.15.0+cpu.

DEEPFACE_BACKEND_ENGINE=pytorch OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 \
  python -m pytest tests/unit/test_represent_input_lifetime.py \
  tests/unit/test_represent_skip_antispoof.py \
  tests/unit/test_representation_empty_faces.py -q
# 36 passed

python -m mypy deepface/modules/representation.py --follow-imports=silent
# Success: no issues found in 1 source file

python -m ruff check --line-length 100 --select E,F \
  deepface/modules/representation.py tests/unit/test_represent_input_lifetime.py
# All checks passed!

git diff --check
# passed

All 13 new cases fail against the original production file because the redundant arrays are still alive during inference. After the change, they pass while preserving pixels, embeddings, image grouping and metadata across single images, image lists, NumPy batches, both tested normalizations and return_face. The final case executes the repository's real DeepID PyTorch network with deterministic initialized weights and compares its embeddings exactly; it is a functional check, not a pretrained accuracy measurement.

Black 26.10.1 passes for the new test. Its requested production-file reformatting is identical on the base and this change and concerns existing lines outside this patch. Repository-pinned Pylint 2.15.10 crashes on both versions because astroid 2.13.5 cannot parse a dependency's Python 3.12 TypeAlias (visit_typealias); that local check is not reported as passing. The complete model/download test matrix and other inference backends were not run locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant