Overview
- MLcroscopy is a config-driven codebase for training and evaluating machine learning models on microscopy and XRF spectroscopy data. In its current state, the module works on materials characterized by the ADA Carbon platform. It provides dataset utilities, model architectures (classic CNN, MLP, DINO-style, and ResNet style variants), training modules, experiment sweeps, and handy scripts for analysis and visualization. Due to data scarcity, the module (which uses a pytorch and wandb backbone), cross-validation has been applied.
Repository Structure
- config/: Configuration files for models, tasks, experiments and sweeps. See config/.
- mlcroscopy/data/: Dataset and embedding utilities. See mlcroscopy/data/dataset.py.
- mlcroscopy/models/: Model architectures and Lightning modules. See mlcroscopy/models/pl_module.py.
- mlcroscopy/utils/: Builders, constants, and miscellaneous helpers. See mlcroscopy/utils/builders.py.
- scripts/: Utility scripts for baselines, embedding generation, visualization, and evaluation.
Quickstart
-
Create and activate a Python environment (example):
python -m venv .venvsource .venv/bin/activate -
Install dependencies (if a
requirements.txtis provided) or install common ML packages:pip install .or
pip install -r requirements.txt -
Run a single experiment locally with the main runner:
python run.py +experiments=axeUse the
config/folder to supply model/task settings via Hydra. For example,+experiments=axecomposes config/experiments/axe.yaml with the base config. -
Run hyperparameter sweeps or multi-run experiments with the tuner:
python tune.py -m sweep=sweep_dino
The code uses Hydra-style configuration. Edit or compose configs to select models, datasets, logging, and training hyperparameters.
Experiment Configs
-
Experiment configs live in config/experiments/. They are Hydra overrides on top of config/config.yaml, which starts from the default
classicmodel,scratchtask, andonlinelogging setup. -
Run one experiment by adding the experiment group to the base config:
python run.py +experiments=axe -
Run a sweep from config/sweep/ with the tuner:
python tune.py -m sweep=sweep_axe
The config files are organized around the input modality and model family:
low_fidelity.yaml,plm_l1.yaml,plm_l2.yaml, andplm_smooth.yamlusemodel: mlpwithtask: expt. These trainExptMLPon the five experimental descriptors only, without microscopy or XRF tensors. Theplm_*variants keep the same MLP architecture and compare the loss function (l1,l2, orsmoothl1).axe.yaml,spear.yaml, andscimitar.yamlusemodel: classicwithtask: scratch. These trainScopyClassicfrom microscopy images plus XRF channels.axereplaces pooling downsampling with stride-2 convolutions,spearskips the explicit downsampling block and uses stride-2 convolutions in each layer, andscimitaris a smaller classic CNN using global pooling.trident.yaml,trident_l1.yaml,trident_l2.yaml, andtrident_smooth.yamlextend the best classic CNN setup withtask: both, adding the experimental descriptors throughexpt_kwargs. The suffix variants compare the same loss choices as the MLP loss-comparison configs.pike.yaml,halberd.yaml,halberd_concat.yaml, andhalberd_vitb.yamluse DINOv3 image features for microscopy.pikeuses microscopy plus XRF only, whilehalberdadds experimental descriptors throughtask: stego.halberd_concatconcatenates CLS and mean patch features, andhalberd_vitbswitches from the small ViT-S DINO backbone to the larger ViT-B backbone.mace.yamlandmorningstar.yamluse the ResNet-styleScopyRespath. Both use a pretrained DINO backbone for microscopy and a residual XRF block;morningstaralso adds experimental descriptors.rapier.yamlis currently only a placeholder comment for a larger DINO experiment and does not define a runnable override.
Common Config Fields
defaultsselects the base model, task, and logging config to override. Most experiment files useoverride /model,override /task, andoverride /log.model.arch._target_is the Python class Hydra instantiates, such asScopyClassic,ScopyDino,ScopyRes, orExptMLP.micro_kwargs,xrf_kwargs, andexpt_kwargsconfigure the microscopy branch, XRF branch, and experimental-descriptor branch. Settingexpt_kwargs.do: Trueenables the five-value experimental input branch.layerslists hidden or channel widths for a branch. For convolutional branches, the values are channel counts; for MLP branches, they are linear-layer widths.kernelanddowncontrol convolution and downsampling behavior.down.type: pooluses pooling,down.type: convuses a learned stride-2 convolution, anddown.type: skiprelies on the branch convolution stride instead.mergecontrols how spatial feature maps become vectors before prediction.flattenkeeps the full spatial grid, whileglobalapplies adaptive global average pooling.pred_head_kwargsdefines the final regression head hidden widths before the two-output prediction layer.model.optimcontrols training behavior, includingloss,folds,epochs,batch_size,learning-rate,early-stopping, andes_patience.
Scripts & Utilities
scripts/contains helper tools: dataset splitting, embedding extraction, visualization, and evaluation utilities.
TO-DO
- In debug mode, add silent run options, where there is no results folder / printing etc.