lightly icon indicating copy to clipboard operation
lightly copied to clipboard

Provide Benchmark Backbone Checkpoint Weights

Open guarin opened this issue 2 years ago • 3 comments

Right now we only upload checkpoint weights for the full SSL models. But those can be a bit hard to work with as you have to modify fields in the state dict if you only want to load the backbone. We should upload the weights for the backbone only to make them more easily usable.

Tasks

  • [ ] Save backbone state dict after pretraining (here). This can be done with torch.save(model.backbone.state_dict(), path). Path should be a path in the same directory as the full checkpoint. Do the same for vit benchmarks.
  • [ ] Save backbone state dict for existing pretrained models and upload them to S3. This can be done with
checkpoint = torch.hub.load_state_dict_from_url(url, map_location="cpu")

# Remove 'backbone.' prefix from keys in state_dict
backbone_state_dict = {
    k.replace("backbone.", ""): v
    for k, v in checkpoint["state_dict"].items()
    if k.startswith("backbone.")
}
torch.save(backbone_state_dict, path)
  • [ ] Add backbone checkpoint links in readme and docs
  • [ ] Update the tutorial to use these weights: https://docs.lightly.ai/self-supervised-learning/tutorials/package/tutorial_checkpoint_finetuning.html

guarin avatar Jul 31 '24 08:07 guarin

Hey @guarin please assign this to me. I'll work on this.

charchit7 avatar Oct 23 '24 17:10 charchit7

Hi @guarin, I'd like to pick this up. I see @charchit7 asked for assignment back in Oct 2024 — if it's still unclaimed I'm happy to take it, or to collaborate if you're still interested @charchit7.

Problem

The benchmark checkpoints linked in the README and benchmarks.rst (14 .ckpt files across 10 ResNet-50 methods) are full Lightning checkpoints — the Barlow Twins one is ~1.4 GB including optimizer state — and as this issue describes, using just the backbone requires manually stripping the backbone. prefix from the state dict. #1672 separately proposes Hugging Face Hub hosting for discoverability. The two are separable, so I propose two phases; Phase 1 is useful on its own even if Hub integration is deferred.

Phase 1 — this issue

Following the task list and conversion snippet already in the description:

  • A small utility module (e.g. lightly/utils/checkpoint.py, naming open to suggestions) with extract_backbone_state_dict(checkpoint, prefix="backbone.") and a thin loading helper wrapping torch.hub.load_state_dict_from_url plus prefix handling — with unit tests (network calls mocked).
  • Saving the backbone state dict after pretraining in benchmarks/imagenet/resnet50/main.py (and the ViT benchmarks), next to the full checkpoint.
  • A conversion script for the existing checkpoints. I can run it and verify the outputs, but S3 upload needs someone with bucket access — happy to hand over verified files.
  • README / benchmarks.rst link updates, and updating the checkpoint-finetuning tutorial to use the backbone weights.

Phase 2 — #1672 (optional, separate PR)

A push_to_hub helper behind an optional huggingface_hub extra, with library_name: lightly tagging per the integration guide Niels linked, auto-generated model cards, and a migration script for the existing checkpoints. This depends on decisions only you can make (hosting org, naming scheme), so I'd only design it once there's a signal it's wanted.

Validation

For every converted checkpoint: tensor-level equality between original and extracted state dicts, plus reproducing the published KNN/linear-eval numbers within noise on a subset for a few representative methods with different head structures (e.g. SimCLR, BYOL, DINO).

Questions

  1. Is plain torch.save (per your snippet) fine for the backbone files, or would you prefer safetensors (adds a dependency)?
  2. Preferred module location/name for the utility?
  3. For Phase 2 — what's the status of the internal discussion mentioned in #1672? Worth designing now, or should I keep the first PR strictly Phase 1?

Open to alternatives on any of this — if you'd prefer a pure script without new public API surface, that works too.

saud5150 avatar Jul 13 '26 16:07 saud5150

Hi @saud5150, I think you could definitely take this one over!

Phase 1 looks good.

  • For the loading with prefixes: You should be able to use the nn.Module's hooks such as register_load_state_dict_pre_hook to help with that.
  • For the saving: Maybe Lightning has a utility to save only weights at the end of training – if not we can do it manually as described.

For the conversion:

  • Would be great if you could push the conversion script in v0 of your PR. I would then take care of conversion and upload to S3, once that's done we can remove it again (since subsequent benchmarks with e.g. new models or fixes will anyway also export the right format).

I would leave Phase 2 out of scope for now, if we migrate from our own buckets to HF, I would like to do this more holistically.

About your questions:

Is plain torch.save (per your snippet) fine for the backbone files, or would you prefer safetensors (adds a dependency)?

Not for now.

Preferred module location/name for the utility?

Fine the way you suggested it. Alternatives could be export.py or checkpointing.py.

liopeer avatar Aug 06 '26 11:08 liopeer