This repository implements Masked Autoencoders (MAE) on the Galaxy10-Decals dataset.
It includes two main stages:
- Self-Supervised Pretraining — learn visual representations by reconstructing masked image patches.
- Linear Probing — evaluate representation quality by training a linear classifier on top of the frozen encoder.
-
mae_model.py
Core MAE architecture:PatchEmbed: image → patch tokensMAEEncoder: Vision Transformer encoder (trained on visible patches)MAEDecoder: lightweight decoder (reconstructs masked patches)MAE: full pipeline (patchify → mask → encode → decode → loss)
-
linear_probe_model.py
Defines the Linear Probe model:- Loads a frozen MAE encoder
- Adds a linear classification head (embed_dim → num_classes)
- Used in
linear_probing_train.pyfor downstream evaluation
-
Training scripts
train_mae.py: self-supervised pretrainingtrain_linear_probe.py: freeze encoder and train a linear classifier
-
Utilities
reconstruct.py: visualize masked images and reconstructionsplot_logs.py/plot_multi_logs.py/plot_linear_probe_logs.py: visualize loss curves and metrics
-
Outputs
runs/mae_pretrain/: checkpoints, logs, configs for pretraininglinear_probe_logs/: logs and checkpoints for linear probing
module purge
module load cuda
python -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install torch torchvision timm datasets tqdm matplotlib pandas pillowmodule purge
module load cuda
source venv/bin/activate
cd galaxy_mae # ⚠️ replace with your project path
python train_mae.py \
--epochs 3 \
--batch_size 16 \
--exp_name debug \
--mask_ratio 0.75- Split each input image into fixed-size patches (16×16).
- Randomly mask a high proportion (e.g. 75%).
- Encode visible patches with a Vision Transformer.
- Decode to reconstruct all patches.
- Compute reconstruction loss only on masked patches.
Each pretraining run produces:
ckpts/best_encoder.pth: encoder checkpoint (for probing/fine-tuning)ckpts/final.pth: full encoder-decoder checkpointtrain_log.csv: logs withepoch,train_loss,val_loss,lr,mask_ratio,wall_timeconfig.json: configuration of the run
sbatch scripts/mask_ratio_test.shscripts/mask_ratio_test.shlaunches 3 jobs (mask ratios 0.25, 0.5, 0.75).
Linear probing evaluates how well the pretrained encoder learned useful features.
The encoder is frozen, and only a linear classification head is trained.
python train_linear_probe.py \
--ckpt runs/mae_pretrain/mask_0.75/ckpts/best_encoder.pth \
--epochs 50 \
--batch_size 64 \
--lr 1e-3 \
--wd 0.05sbatch scripts/linear.sh- Load the frozen encoder from
best_encoder.pth. - Attach a linear classifier (
LinearProbe). - Train only the classifier using galaxy labels (10 classes).
- Report accuracy on the test set.
Each probing run saves:
linear_probe_best.pth: best linear probe checkpointtrain_log.csv: logs withepoch,train_loss,val_loss,train_acc,val_acc
- Pretraining is unsupervised: the model reconstructs galaxies without labels.
- Linear probing measures how good the representations are for classification.
- Use
best_encoder.pth(notfinal.pth) for probing. - Multiple mask ratios (0.25, 0.5, 0.75) can be compared to study representation quality.
-
Pretraining:
- Train Loss (reconstruction)
- Validation Loss (reconstruction, used for early stopping)
-
Linear Probing:
- Train Loss & Accuracy
- Validation Loss & Accuracy
- Reconstructions: compare original, masked, reconstructed, and recon+visible images.
- Training curves: plot loss and accuracy across epochs.
- Multi-experiment overlays: compare different mask ratios on the same plot.
This project demonstrates how self-supervised pretraining with MAE combined with linear probing provides a systematic way to evaluate learned visual features on astronomy datasets.









