Skip to content

[Feature] Add CluMuT - #476

Open
FrankYiquan wants to merge 17 commits into
galilai-group:mainfrom
FrankYiquan:main
Open

FrankYiquan wants to merge 17 commits into
galilai-group:mainfrom
FrankYiquan:main

Conversation

@FrankYiquan

@FrankYiquan FrankYiquan commented Oct 14, 2026 •

Copy link
Copy Markdown

Description

This PR implements CluMuT (Cluster-based Mutual Targeting), a self-supervised learning method that learns useful image representations by discovering clusters in the embedding space, without requiring labeled data during pretraining, negative pairs, or explicit cluster balancing.

For each input image, CluMuT generates two augmented views and passes them through a shared encoder and projector. Learnable prototypes map the resulting representations to cluster assignments. Training combines two complementary objectives:

  • Symmetric mutual-targeting loss: encourages the cluster assignments from the two augmented views to provide consistent learning targets for each other.
  • Identitization loss: encourages cross-view cluster correlations to match an identity matrix, promoting correspondence between clusters across views.

The CIFAR-10 example follows a two-stage evaluation pipeline:

  1. Self-supervised pretraining: trains the encoder and projector on unlabeled CIFAR-10 images and saves encoder checkpoints at selected epochs.
  2. Linear evaluation: freezes each saved encoder, trains a fresh linear classifier on labeled training data, and evaluates the learned representations using CIFAR-10 test top-1 accuracy.

This evaluation protocol measures how well the pretrained encoder represents images without fine-tuning its parameters.

Paper: [Self-supervised learning from images: No negative pairs, no cluster-balancing](https://www.sciencedirect.com/science/article/pii/S003132032400832X)

The paper also indicates CluMut is implemented based on this code.

Full CluMuT Project Changes

  • stable_pretraining/methods/clumut.py — Implements the CluMuT model: a shared encoder and projector, learnable cluster prototypes, two-view assignments, and the combined mutual-targeting and identitization objectives.

  • stable_pretraining/losses/clumut.py — Implements the symmetric stop-gradient KL mutual-targeting loss, batch-wise cross-correlation, and identitization loss with a configurable off-diagonal scale.

  • examples/clumut_cifar10.py — Provides the CIFAR-10 experiment: data loading and two-view augmentation, ResNet-18 training, optimizer and schedule setup, checkpoint save/resume, diagnostics, and frozen-encoder linear evaluation. Includes --lambd (default 0.05) and --linear-eval-lr (default 0.01).

  • stable_pretraining/tests/unit/test_clumut.py — Tests CluMuT model behavior, loss calculations, CIFAR-10 transforms, diagnostics, and training/checkpoint integration.

  • stable_pretraining/__init__.py — Exposes CluMuT through the library’s top-level lazy-import API.

  • stable_pretraining/methods/__init__.py — Registers and exports the CluMuT method.

  • stable_pretraining/losses/__init__.py — Registers and exports CluMuTLoss.

  • METHODS.md — Adds CluMuT to the library’s method catalog.

Validation

Implementation and code quality

  • Unit tests: 6 passed, covering cluster assignments, both loss functions, gradient propagation, edge cases, and a Lightning training step.
  • Static checks: Ruff, formatting, Python syntax compilation, and git diff --check passed.
  • Smoke test: successfully ran a CPU-based CIFAR-10 experiment with batch size 16, one pretraining epoch limited to 5 batches, and one linear-evaluation epoch. Pretraining completed and saved an encoder checkpoint.

Initial comparison with the paper

We pretrained the CluMuT encoder on CIFAR-10 for 300 epochs with a batch size of 128 and λ=0.05. We saved encoder checkpoints during training, froze the encoder at selected checkpoints, and trained a linear classifier over the checkpoint we saved on the labeled CIFAR-10 training set for 300 epochs using a learning rate of 0.01. We evaluated each classifier by its top-1 accuracy on the CIFAR-10 test set and compared the results with those reported in the original paper.

Epoch Paper CluMuT (%) Your CluMuT (%)
100 83.57 81.23
150 85.49 82.56
200 86.41 83.88
250 87.53 84.74
300 87.98 85.27

Checklist

  • I have read the Contributing document.
  • The documentation is up-to-date with the changes I made (check build artifacts).
  • All tests passed, and additional code has been covered with new tests.
  • I have added the PR to the RELEASES.rst file.

@FrankYiquan FrankYiquan changed the title Implement CluMuT for Self-Supervised Image Learning Add CluMuT for Self-Supervised Image Learning Oct 15, 2026
@FrankYiquan
FrankYiquan marked this pull request as ready for review October 18, 2026 07:46
@FrankYiquan FrankYiquan changed the title Add CluMuT for Self-Supervised Image Learning Add CluMuT Oct 18, 2026
@FrankYiquan FrankYiquan changed the title Add CluMuT [WIP] Add CluMuT Oct 18, 2026
@FrankYiquan FrankYiquan changed the title [WIP] Add CluMuT [Feature] Add CluMuT Oct 19, 2026

@Leon-Leyang Leon-Leyang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hi @FrankYiquan, I've gone through the PR. Please check the issues below:

  • stable_pretraining/forward.py: there's no CluMuT forward function. Please add one and have CluMuT.forward call it.
  • stable_pretraining/methods/clumut.py:131: the batch-dict path only handles two-view batches, so a single-view validation batch raises a ValueError, and on two-view batches it returns no labels matching the stacked embedding (line 173), so OnlineProbe can't be used. The new CluMuT forward function should handle both cases, as forward.simclr does.
  • stable_pretraining/__init__.pyi: CluMuT isn't listed, so type checkers and IDEs don't see spt.CluMuT.
  • stable_pretraining/tests/unit/test_clumut.py:91: no test checks the identitization loss or the correlation matrix against a reference value. This test recomputes the loss with the same F.kl_div calls as the implementation.
  • stable_pretraining/tests/unit/test_clumut.py:111: no test checks that the loss and gradients stay finite on extreme inputs, such as saturated assignments.
  • stable_pretraining/tests/unit/test_clumut.py:232: no test trains CluMuT through spt.Manager (a small model and a few training steps are enough) and resumes from a checkpoint. This test runs one step with a bare pl.Trainer.
  • There's no real two-process DDP test, although the identitization loss uses the cross-correlation over the batch (stable_pretraining/losses/clumut.py:54-60).
  • examples/clumut_cifar10.py: please move the script behind the reported runs to benchmarks/cifar10/.
  • PR description: the global batch size (per-GPU batch × number of GPUs) isn't stated.
  • Please state in the docstrings whether the correlation statistics of the identitization loss (stable_pretraining/losses/clumut.py:54-60) are per GPU or global under DDP.
  • RELEASES.rst, docs/source/api/: there's no CluMuT release entry, and CluMuT and CluMuTLoss aren't listed in methods.rst and losses.rst, although the PR checklist ticks both.

@codecov

codecov Bot commented Dec 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.07921% with 8 lines in your changes missing coverage. Please review.

@@            Coverage Diff             @@
##             main     #476      +/-   ##
==========================================
- Coverage   98.17%   98.14%   -0.04%     
==========================================
  Files         163      165       +2     
  Lines       16406    16507     +101     
==========================================
+ Hits        16107    16201      +94     
- Misses        299      306       +7     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Leon-Leyang Leon-Leyang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hi @FrankYiquan, I've gone through the changes. This point from my review isn't addressed yet:

  • PR description: the global batch size of the reported runs (per-GPU batch × number of GPUs) still isn't stated. The script now says --batch-size is per GPU, but not how many GPUs the runs used.

Please also check these items, which weren't raised before:

  • stable_pretraining/forward.py:317: the CluMuT losses are logged only when stage == "train", but Module.training_step passes stage="fit", so training runs such as benchmarks/cifar10/clumut_cifar10.py no longer log them.
  • docs/source/api/forward.rst: there's no entry for the new forward.clumut.

This branch has not been deployed

No deployments
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