This repository contains the fundus image segmentation implementation of DRESS: Domain repartitioning with structure and style augmentation for domain generalization in medical image segmentation. DRESS initializes data-driven pseudo-domains from frozen VGG16 features, progressively updates the partitions using learned style representations, and combines mask-guided structural augmentation with dual contrastive learning.
Python 3.10 or newer is recommended.
Using uv:
uv syncUsing pip:
python -m pip install -r requirements.txtThe first training run may download ImageNet-pretrained VGG16 weights through torchvision. In an offline environment, place the corresponding torchvision checkpoint in the standard PyTorch cache before training.
The experiments use the four-center fundus benchmark assembled from Drishti-GS, RIM-ONE-r3, REFUGE-train, and REFUGE-val:
| Center ID | Dataset split | Images (train/test) |
|---|---|---|
| 1 | Drishti-GS | 50 / 51 |
| 2 | RIM-ONE-r3 | 99 / 60 |
| 3 | REFUGE-train | 320 / 80 |
| 4 | REFUGE-val | 320 / 80 |
Please obtain the datasets from their official providers and comply with their licenses and access conditions. This repository does not redistribute medical images, masks, or author-preprocessed data.
The loader expects PNG files with this structure:
imagedata/
├── Domain1/
│ ├── train/
│ │ ├── image/<name>.png
│ │ └── mask/<name>_mask.png
│ └── test/
│ ├── image/<name>.png
│ └── mask/<name>_mask.png
├── Domain2/
├── Domain3/
├── Domain4/
└── struct_aug/
├── Domain1/train/{image,mask}/
├── Domain2/train/{image,mask}/
├── Domain3/train/{image,mask}/
└── Domain4/train/{image,mask}/
Images are loaded as RGB and resized to 256 x 256. Pixel values are normalized to [-1, 1]. Masks are single-channel PNG files using 0 for background, 127 for optic cup, and 255 for the non-cup optic-disc region. The loader converts them into two nested binary channels: OC and OD (OD includes OC).
Structure-augmented image-mask pairs are generated offline with the legacy sphere-projection implementation used by the reported experiments. The preprocessing is independent of the leave-one-center-out split, so all four centers can be processed once before running the four folds:
cd code
python preprocess_struct_aug.py \
--data_dir /path/to/imagedata \
--output_dir /path/to/imagedata/struct_aug \
--domains 1 2 3 4 \
--ball_curve 0.5 \
--img_size 256During a given fold, the training loader uses structure-augmented samples only from the centers listed in --datasetTrain; augmented data from the held-out center are not loaded into training. Ground-truth masks are used only for offline augmentation generation, and the projection module is not used during inference.
code/dress_core.py is the complete DRESS training entry point. From the repository root, train with Center 1 held out as follows:
python code/dress_core.py \
--data_dir /path/to/imagedata \
--struct_aug_dir /path/to/imagedata/struct_aug \
--datasetTrain 2 3 4 \
--datasetTest 1 \
--exp_dir outputs/training \
--gpu 0The four leave-one-center-out folds are selected with --datasetTrain and --datasetTest:
python code/dress_core.py --data_dir /path/to/imagedata --datasetTrain 2 3 4 --datasetTest 1 --exp_dir outputs/training --gpu 0
python code/dress_core.py --data_dir /path/to/imagedata --datasetTrain 1 3 4 --datasetTest 2 --exp_dir outputs/training --gpu 0
python code/dress_core.py --data_dir /path/to/imagedata --datasetTrain 1 2 4 --datasetTest 3 --exp_dir outputs/training --gpu 0
python code/dress_core.py --data_dir /path/to/imagedata --datasetTrain 1 2 3 --datasetTest 4 --exp_dir outputs/training --gpu 0The default run uses 200 epochs, batch size 10, learning rate 1e-3, seed 123, PCA dimension 16, candidate cluster counts 3--8, and dynamic repartitioning every 100 epochs. With zero-based internal epoch indexing, the default 200-epoch run performs dynamic repartitioning once, before training epoch 101. The initial VGG16/PCA16 domain initialization is performed before training.
Outputs are written below outputs/training/. A separate run directory is created for each source/target split and timestamp.
The deployment segmentation path uses only AEncoder and Segmentor. test.py strictly loads these two modules from the original full training checkpoint; the style encoder and decoder are not executed.
python code/test.py \
--data_dir /path/to/imagedata \
--datasetTest 1 \
--model-file /path/to/best_model.pth \
--device cuda:0 \
--output-dir outputs/test_center_1Add --save-imgs to save prediction visualizations. The script reports per-image and aggregate OC/OD Dice, ASD, and HD95 and writes summary.json and CSV results.
Model input, prediction, and Dice evaluation use the 256 x 256 grid. ASD and HD95 are first computed on that grid and, by default, multiplied by 800/256 so that their pixel units match the 800-pixel reporting scale used in the paper. Set --eval_scale 0 only when an unscaled 256-grid distance is explicitly required.
code/dress_core.py DRESS initialization, repartitioning, losses, and training loop
code/test.py Deployment-path checkpoint evaluation
code/preprocess_struct_aug.py Offline legacy sphere-projection preprocessing
code/dataloader/ Fundus datasets and transformations
code/models/ Encoders, segmentor, decoder, and projection implementation
code/utils/ Fundus losses, metrics, and utilities
- K-means depends on initialization, the candidate cluster range, and the learned representation. The default implementation retains the original fixed random state used by the reported experiments.
- Sphere projection is mask-guided and uses an optic-cup-centered geometric prior. It is a training-only augmentation and should not be interpreted as validated for arbitrary anatomy.
- Dependency lower bounds are provided, but a fully locked original environment is not currently available.
This implementation builds on ideas and code structure from CDDSA/DAG4MIA and SDNet, and uses the MedPy metric package. DAG4MIA is distributed under the MIT License, while MedPy is distributed under GPLv3. Users should consult the licenses and citation requirements of the original datasets and all third-party projects. A final repository-wide license will be added after author and institutional confirmation; until then, no additional redistribution permission is implied.
The manuscript is currently under revision. Verified publication metadata and a BibTeX entry will be added when available.