Skip to content

feat: add BEiT support#1958

Open
aymen-000 wants to merge 3 commits into
lightly-ai:masterfrom
aymen-000:add-beit-support
Open

feat: add BEiT support#1958
aymen-000 wants to merge 3 commits into
lightly-ai:masterfrom
aymen-000:add-beit-support

Conversation

@aymen-000

Copy link
Copy Markdown

Description

This PR adds support for BEiT (BERT Pre-Training of Image Transformers) for masked image modeling to Lightly.

What was added

  • Implemented the BEiT masked image modeling architecture.
  • Added the BEiT image tokenizer.
  • Added the BEiT masking transform.
  • Added the masked image modeling prediction head.
  • Added training examples (single-GPU and distributed).
  • Added notebooks demonstrating BEiT training.
  • Added documentation and API references.
  • Added unit tests covering the new components.

Relation to the existing MAE implementation

While both MAE and BEiT are masked image modeling methods, they differ in their reconstruction targets:

  • MAE reconstructs the raw pixel values of masked image patches.
  • BEiT predicts discrete visual tokens generated by a pretrained image tokenizer instead of reconstructing pixels.

References

Documentation

  • I have updated the documentation.

Tests

  • I have updated the tests.

@liopeer

liopeer commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

/review

@liopeer

liopeer commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Thanks a lot @aymen-000, what a cool contribution! This will probably take a bit of time to review, but we will try to do it as quickly as possible.

@liopeer liopeer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only an initial review with already some remarks. Will continue over the next few days. 🙂

Comment thread examples/pytorch/beit.py
Comment on lines +5 to +19
# NOTE: The ImageTokenizer must be pre-trained before BEIT pre-training.
# The tokenizer follows the DALL-E discrete VAE architecture and can be
# trained as follows:
#
# tokenizer = ImageTokenizer(vocab_size=8192)
# optimizer = torch.optim.Adam(tokenizer.parameters(), lr=1e-3)
#
# for images in dataloader:
# logits, recon = tokenizer(images)
# loss = F.mse_loss(recon, images) # Reconstruction loss
# loss.backward()
# optimizer.step()
# optimizer.zero_grad()
#
# torch.save(tokenizer.state_dict(), "tokenizer.pth")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that it's quite that easy (but unfortunately DALL-E does not have a code release and neither has Microsoft for the tokenize pretraining). Since I think that this is unlikely to yield any good results, I would rather remove it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I found that BEiT seems to use a smooth L1 loss https://github.com/microsoft/unilm/blob/master/beit/modeling_discrete_vae.py#L124

But we don't have a training recipe, so even with that piece of information it's unlikely to get anything good.

Comment thread lightly/models/beit.py
@@ -0,0 +1,187 @@
"""BEiT: BERT Pre-Training of Image Transformers.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't actually want these model modules anymore. Instead we want the full implementations to be inside the notebooks/examples. Sorry if this is not clear from the contributing instructions, will try to make this better in the future.

BEiT
====

implementation of the BEiT (BERT Pre-Training of Image Transformers)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
implementation of the BEiT (BERT Pre-Training of Image Transformers)
Implementation of the BEiT (BERT Pre-Training of Image Transformers)

# Copyright (c) 2021. Lightly AG and its affiliates.
# All Rights Reserved

from lightly.models.modules.beit_tokenizer import ImageTokenizer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call it BEiTImageTokenizer, so that it's clear to which pipeline it belongs. And I mean not only the import but the actual class.

DINOProjectionHead,
DINOv2ProjectionHead,
LeJEPAProjectionHead,
MIMHead,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: BEiTMIMHead.

Comment thread examples/pytorch/beit.py
)

criterion = MaskedImageModelingLoss()
optimizer = torch.optim.AdamW(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you mention in the comment at the top that the parameters should not require grad, I would apply that here as well. I am aware that the tokenizer uses the @no_grad decorator, but better to completely deactivate the gradients. You can also do it above in the BEIT class.

Comment thread examples/pytorch/beit.py
from lightly.transforms import BEITTransform


class BEIT(nn.Module):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rename to BEiT in order to match the paper's name better.

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.

2 participants