Skip to content

Release unused VGG-Face classifier weights after loading - #1771

Open
Afloat16 wants to merge 1 commit into
serengil:masterfrom
Afloat16:perf/vgg-descriptor-classifier-memory-20261129
Open

Afloat16 wants to merge 1 commit into
serengil:masterfrom
Afloat16:perf/vgg-descriptor-classifier-memory-20261129

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Tickets

Newly measured PyTorch VGG-Face model-memory improvement; no existing issue is being closed.

What has been done

The PyTorch VGG-Face embedding factory loads the full 2622-class checkpoint and retains its fc8 classifier inside the returned descriptor, although VggFaceDescriptor.forward() only calls embed(). These unused weights stay in the cached model and are included when the client moves it to its inference device.

Release fc8 after the full checkpoint has loaded strictly and before wrapping the embedding descriptor. This removes 10,742,334 parameters / 42,969,336 bytes (40.98 MiB in float32) from the resident model. The initialized full model has 145,002,878 parameters; the descriptor needs 134,260,544.

The factory still loads the existing complete pretrained checkpoint, including its classifier keys, with strict checking. base_model() continues to construct a classifier, and directly constructed VggFaceDescriptor(base) objects keep their supplied base unchanged. Existing evaluation-mode setup and the embedding forward computation are unchanged.

Visible model-state change: the descriptor returned by load_model() now has model.fc8 = None, and its state_dict() omits model.fc8.weight and model.fc8.bias. Applications that manually saved an old descriptor's state and strictly reload it into the new descriptor must drop those two unused keys. This does not change the format or loading of the downloadable base-model checkpoint. Callers that need the 2622-class head should keep using the base classifier.

How to test

Linux x86-64, Python 3.12.14, PyTorch 2.15.0+cpu; base 8cec026045be164e14b510c55c1264e743dc4999.

  • New regressions on the original factory: 1 failed, 2 passed because the classifier remains alive.
  • OMP_NUM_THREADS=2 DEEPFACE_BACKEND_ENGINE=pytorch PYTHONPATH=. python -m pytest tests/unit/test_vgg_descriptor_memory.py -q: 3 passed. Tests write a compact synthetic checkpoint with every actual parameter shape and key, load it through the real strict loader, compare the real factory's (2, 4096) output exactly against the loaded base's embeddings, check that exactly the two classifier state keys disappear, retain the base classifier, and still reject missing embedding weights. No pretrained-weight download is needed.
  • A separate full-model CPU measurement on two repository images with deterministic initialized weights gave bitwise-equal embeddings before and after removing the head, and process RSS fell by 42,962,944 bytes in that run. The classifier's weak reference expired. This measures retained storage, not recognition accuracy, inference speed or peak loading memory.
  • Targeted MyPy 1.14.1 (--follow-imports=silent): passed; Ruff critical rules and git diff --check: passed. Black passes on the new test; the production file has the same pre-existing line-wrap finding as the base.

Pretrained recognition accuracy, GPU memory and the full repository suite 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