--- license: mit library_name: pytorch pipeline_tag: unconditional-image-generation tags: - flow-matching - diffusion-transformer - dit - gravitational-lensing - astrophysics - dark-matter - deeplense --- # FlowLensing — models, code and evaluation Companion artefacts for *FlowLensing: Simulating Gravitational Lensing with Flow Matching* (Sayed, Reddy, Toomey, Gleyzer). A ~30M-parameter DiT trained with flow matching to generate strong-lensing images, replacing ray-tracing simulation. ## Layout ``` checkpoints/ flowlensing_dit_class_cond.pth 3-class model (CDM / no_sub / axion), 29.81M params flowlensing_dit_param_cond.pth parameter-conditioned model, 30.07M params eval_classifier_resnet34.pt ResNet-34 classifier, for the 3-class evaluation eval_regressor.pt ImageRegressor, for the parameter evaluation code/ model.py NanoDiT, class-conditional (needs timm) model_regress.py NanoDiT, parameter-conditional dataset.py DiffusionDataset / VariableDataset / ClassifierDataset train_og.py training, 3-class train_regress.py training, parameter-conditioned regression_model.py ImageRegressor definition latency.py standalone sampling / timing evals/ common.py shared loading, normalization and sampling eval_regression.py parameter recovery (MSE / MAE / RMSE / R2) eval_classify.py accuracy, per-class AUC, confusion matrix eval_fid.py FID, real vs generated eval_image_quality.py PSNR / SSIM on paired images ``` ## Install ```bash pip install -r requirements.txt ``` `timm` is required only by `code/model.py` (the class-conditional model). ## Normalization contract — read this first Images are stored as raw simulation output in physical brightness units, spanning roughly 1e-12 to 4. Two steps put them on the scale the model was trained with: 1. **Stretch to [0, 1]** — each image by its own min and max (`minmax_to_unit`). 2. **Recentre to [-1, 1]** — `Normalize((0.5,), (0.5,))`. Sampling reverses step 2 (`(z + 1) / 2`) and returns images on [0, 1], which is the scale both the classifier and the regressor expect. Step 1 is idempotent, so leaving it on is safe for data that was already normalized offline. It is **not invertible** — the original per-image min and max are not stored, so generated images carry relative brightness, not physical flux. Historically step 1 was an offline pass that wrote `*_normalized` copies of the data, and no live code performed it. It is now applied inside the dataset classes (`normalize=True` by default) and inside every evaluation script, so the scripts work directly on raw `.npy` files. ## Running the evaluations Parameter-conditioned model (works with the released `cdm_regress_multi_param` data): ```bash python evals/eval_regression.py \ --data /path/to/cdm_regress_multi_param \ --flow-ckpt checkpoints/flowlensing_dit_param_cond.pth \ --regressor-ckpt checkpoints/eval_regressor.pt \ --n 1000 ``` 3-class model (needs the Model_II dataset): ```bash python evals/eval_classify.py \ --flow-ckpt checkpoints/flowlensing_dit_class_cond.pth \ --classifier-ckpt checkpoints/eval_classifier_resnet34.pt --arch resnet34 ``` `--real-data DIR` classifies real images instead of generated ones, as a baseline. FID and PSNR/SSIM take two directories of `.npy` files: ```bash python evals/eval_fid.py --real REAL_DIR --generated GEN_DIR python evals/eval_image_quality.py --real REAL_DIR --generated GEN_DIR ``` All scripts take `--device` (`auto` by default: cuda, then mps, then cpu), `--ode-steps`, `--cfg-scale` and `--seed`.