Add random block wise mask from Pixio#1919
Conversation
…n masking Based on Pixio's MAE implementation. Masks patches in coherent blocks rather than individually for spatial coherence in image patch masking.
Tests cover: - Basic functionality and index coverage - Class token preservation - Block alignment verification - Various mask ratios and block sizes - Device handling (CPU/CUDA) - Error cases (non-square, invalid block size) - Determinism and batch consistency
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1919 +/- ##
==========================================
+ Coverage 86.19% 86.25% +0.06%
==========================================
Files 169 169
Lines 7071 7110 +39
==========================================
+ Hits 6095 6133 +38
- Misses 976 977 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds a new token-index masking utility to support Pixio-style block-wise masking (masking contiguous patch blocks instead of individual tokens), along with unit tests to validate expected behavior and prefix-token handling (CLS/REG tokens).
Changes:
- Introduce
random_block_wise_mask(...)inlightly/models/utils.pyto generate keep/mask indices using block granularity andnum_prefix_tokens. - Add a dedicated test suite in
tests/models/test_ModelUtils.pycovering prefix tokens, block sizes, mask ratios, determinism, and device placement.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
lightly/models/utils.py |
Adds the new block-wise masking function returning (idx_keep, idx_mask) indices. |
tests/models/test_ModelUtils.py |
Adds unit tests for random_block_wise_mask behavior, error cases, determinism, and device handling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| if num_patches <= 0: | ||
| raise ValueError( | ||
| "Sequence length must be greater than prefix tokens (1 if not masking class token)." |
| if num_prefix_tokens > 0: | ||
| noise[:, 0] = -1 | ||
|
|
| .expand(batch_size, -1) | ||
| ) | ||
| idx_keep = torch.cat([prefix_indices, patch_indices], dim=1) | ||
| idx_keep = torch.sort(torch.unique(idx_keep, dim=1), dim=1)[0] |
| assert idx_keep.shape[1] == idx_keep[0].shape[0] | ||
| assert idx_mask.shape[1] == idx_mask[0].shape[0] |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
#1894
This is part of a series of PR's to implement the Pixio paper.
The only difference is the
num_prefix_tokensvsmask_class_token. Since Pixio relies on register tokens, themask_class_tokenwas not suitable for choosing whether to mask or not the REG/CLS tokens.