Skip to content

feat: add BEPH patch encoder - #232

Open
dibalokechanda wants to merge 1 commit into
mahmoodlab:mainfrom
dibalokechanda:feat/beph-encoder
Open

dibalokechanda wants to merge 1 commit into
mahmoodlab:mainfrom
dibalokechanda:feat/beph-encoder

Conversation

@dibalokechanda

Copy link
Copy Markdown

Summary

Adds BEPH as a TRIDENT patch encoder.

BEPH is a pathology-pretrained BEiT-v2 Base model. This integration loads the official BEPH backbone through timm by converting the released MMSelfSup/OpenMMLab checkpoint format to the equivalent timm parameter structure.

Changes

  • Adds BEPHInferenceEncoder
  • Registers --patch_encoder beph
  • Adds BEPH to local_ckpts.json
  • Converts the official BEPH checkpoint to the timm BEiT parameter layout at load time
  • Handles the shared relative-position bias used by the released checkpoint
  • Uses ImageNet-normalized 224×224 preprocessing
  • Returns 768-dimensional patch embeddings
  • Adds an integration test that verifies both the forward pass and embedding dimensionality
  • Verified that the preprocessing transform is picklable for DataLoader multiprocessing

Checkpoint download

Unlike several Hugging Face-hosted encoders, BEPH's official pretrained backbone is distributed separately by the authors. Users therefore need to download BEPH_backbone.pth from the official BEPH repository and provide its path either through weights_path or TRIDENT's local_ckpts.json.

For example:

 "beph": "/path/to/BEPH_backbone.pth"

The repository keeps this entry empty by default:

"beph": ""

so no machine-specific checkpoint path is committed.

Testing

TRIDENT_RUN_INTEGRATION_TESTS=1 python -m pytest \
 tests/test_patch_encoders.py -k beph -v

Result:

1 passed

Verified output shape:

 torch.Size([1, 768])

Reference

BEPH: A foundation model for generalizable cancer diagnosis and survival prediction from histopathological images, Nature Communications, 2025.

Official implementation/checkpoint: Zhcyoung/BEPH

@dibalokechanda

Copy link
Copy Markdown
Author

Hi @guillaumejaume just a soft ping on this PR:

It looks like the CI workflow expired while waiting for maintainer approval, so the tests were never able to run. Whenever you have a chance, would you mind approving/retriggering the workflow?

The same failure happened for the other Patch Encoder PRs I made.

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