Skip to content

Training and prediction

SoFTg includes a two-class UNet++ model, a PyTorch Lightning trainer, and tiled prediction. Training uses labelled intensity images; prediction requires compatible trained weights.

Install the dependencies

pip install 'WaLSAlib[softg]'
python -m WaLSAlib._vendor.softg.train --help
python -m WaLSAlib._vendor.softg.predict --help

Training, prediction, and model routines are also accessible through SoFTg.train, SoFTg.predict, and SoFTg.model.

Prepare labelled data

The trainer expects the following case-sensitive layout:

data/
  Train/
    field001.npz
    field002.npz
  Validate/
    independent_field001.npz

NPZ files store named NumPy arrays. Each file contains matching two-dimensional arrays named smap (intensity) and cmask_map (labels). The dataset loader binarises labels using cmask_map > 0: granules are class 1 and lanes/background are class 0. It normalises intensity by its maximum; use finite arrays with a positive maximum and dimensions comfortably larger than the chosen patch size.

Training uses rotations, deformations, sampled crops, and intensity augmentation. Validation uses randomly sampled crops without the training augmentations. Record the seeds and use a fixed, independent evaluation for reported metrics. Keep independent observations in validation and test splits to avoid leakage between adjacent frames or neighbouring patches.

Train a model

python -m WaLSAlib._vendor.softg.train \
  --data-root data/ \
  --channels 1 --scale 1 \
  --patch-size 256 --epochs 100 \
  --save-dir model_params \
  --log-dir lightning_logs

These settings use the training command’s default architecture, patch size, and epoch count. Read --help for batch size, learning rate, gradient accumulation, workers, and other controls.

Training and prediction use CUDA when available and otherwise run on CPU. Apple MPS is not automatically selected. Test the desired platform and available memory before committing to a training run.

Predict with matching weights

The training command exports its best model weights to model_params/best_model.pt. Place the intensity images to segment in observations/, then run:

python -m WaLSAlib._vendor.softg.predict \
  --input observations/ \
  --checkpoint model_params/best_model.pt \
  --output-dir predictions \
  --tile-size 512 --overlap 64 \
  --threshold 0.5 --min-size 500

The input can be a FITS/NPY file or directory of such files. The loader accepts raw state dictionaries and recognised wrapped dictionaries, strips common wrapper prefixes, and instantiates one input channel, two output classes, scale=1, and the loader setting bilinear=False. Use weights matching this architecture.

The UNet++ dense connections use bilinear upsampling internally.

Prediction forms a median image and applies contrast-limited adaptive histogram equalisation (CLAHE) and unsharp masking. --no-preprocess disables CLAHE and unsharp masking while retaining median stacking and min/max normalisation. Ensure training and inference preprocessing are scientifically consistent. Threshold and component filtering change the final mask; retain both settings with your results.

The predictor writes <input>_mask.npy and <input>_mask.png, plus <input>_mask.fits for FITS input. A three-dimensional input file produces one mask from its median image. The output is a binary granule/lane mask. Evaluate the segmentation against independent labels from representative observations.

Desktop annotation

pip install 'WaLSAlib[softg-gui]'
python -m WaLSAlib._vendor.softg.label_tool

The annotation tool requires a desktop display. Review label conventions and the saved data before using them in the training layout above.

Reproducibility

Record the source version, all training and prediction arguments, split definitions, preprocessing, evaluation method, and checkpoint hash. Assess boundaries, merged/split granules, false detections, and generalisation across instruments and observing conditions. Cite the method and WaLSAlib using the citation guide.

Implementation sources

train.py · predict.py · dataset.py