Official PyTorch implementation of the Superpixel-Embedded Transformer (SET) published in Medical Image Analysis 2025. SET targets 2D medical image segmentation (ISIC skin-lesion benchmarks by default) and augments encoder–decoder models with pre-computed superpixel graphs that act as structural priors during training and inference.
datasets/: dataset loaders (isic18.pyby default), metadata utilities, and thesave_graphhelper for superpixel generation.model/: encoder/decoder modules (Swin/HRNet variants, UNETR-style decoders, ensemble heads).utils/: training utilities, loss functions, and evaluation metrics.workers/: high-leveltrain_net.pyandtest_net.pyorchestration scripts invoked byrun.py.configs.yaml: single source of truth for experiment hyperparameters.
| Library | Notes |
|---|---|
| Python ≥ 3.9 | Tested with CPython 3.10 |
| PyTorch ≥ 2.1 & torchvision | CUDA 11.8 build recommended |
| pytorch-lightning ≥ 2.2 | lightning.pytorch module |
| scikit-learn | K-fold splits |
| scikit-image | slic superpixel generator |
OpenCV (opencv-python) |
I/O + augmentations |
| Pillow, imageio | Image conversion & debug exports |
| numpy, scipy, tqdm, einops | Array ops and progress bars |
| wandb, tensorboard (optional) | Experiment tracking |
Install everything with pip (CUDA wheel omitted here):
python -m venv .venv
source .venv/bin/activate
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
pip install lightning wandb tensorboard scikit-learn scikit-image opencv-python pillow imageio einops tqdm pyyamlSET expects the ISIC2018 structure used in datasets/isic18.py (update the glob paths in load_name() to match your filesystem):
/path/to/ISIC2018/
├── Training_Data/*.jpg
└── Training_GroundTruth/{image_id}_segmentation.png
load_group_associations looks for .npy files under
/path/to/superpixels/{SIZE}_{SP_NUM}/T_sp/{SEED}/stage{1-5}/IMAGE_ID.npy
Each stage stores increasingly coarser associations (pixels → sp, sp → clusters, …). You can pre-compute them with the save_graph helper defined in datasets/superpixel_generator.py.
- Either export
SET_SUPERPIXEL_ROOT=/path/to/ISIC2018(the directory that will hold the{size}_{sp_num}/T_sp/...folders) or passoutput_root=...tosave_graph. - Run the helper on every training image:
python - <<'PY'
from glob import glob
from pathlib import Path
from datasets.superpixel_generator import save_graph
data_root = Path("/path/to/ISIC2018")
images = sorted((data_root / "Training_Data").glob("*.jpg"))
for img in images:
save_graph(img, seed=10, output_root=data_root) # seed controls SLIC compactness + folder name
PYsave_graph will:
- crop/resize each RGB image,
- run
skimage.segmentation.slic, - iteratively merge superpixels to produce stage2–stage5 relations via
format_sub_sp, - create the required
stage*/folders underoutput_root/{size}_{sp_num}/, and - write
.npyfiles thatdatasets/isic18.pyloads at training time.
If superpixels already exist, the function simply copies them into the _v2 layout. Regenerate whenever you change size, sp_num, or the compactness seed.
All runtime knobs live in configs.yaml. The keys mirror the argparse options in run.py, so CLI flags override YAML values when provided.
- Dataset paths: edit
datasets/isic18.py::load_name(and other dataset loaders if you plan to train on ISIC16/17/Archive/HAM) so thatinput_patternandtargetlistpoint to your local dataset directories. Likewise, ensure the superpixel directories passed toload_group_associationsexist on your system. - System:
workers,gpu,batch_size,test_batch_size,epoch,lr,optimizer,resume,seed. - Dataset:
dataset_name(defaultISIC18),percent,size,sp_num,stagelist,ensemble,inpainting. - Model:
model,transformer,patch_size,window_size,embed_dim,depths,num_heads. - Experiment metadata:
log_name,tags,description,save_name,save_results,dice_loss,kfold,evaluate,smoke_test. - Logging:
resume_wandb,use_tensorboard,id,update_config.
Typical workflow:
- Duplicate
configs.yamlif you need multiple runs, or edit in-place. - Update dataset paths in
datasets/isic18.py::load_nameif your ISIC root differs. - Point
save_nameto a writable directory (checkpoints & logs land there).
run.py wraps the full lifecycle: train → test. Example commands assume superpixel maps already exist.
python run.py --cfg configs.yaml --gpu 0Key optional flags:
--workers 8to control dataloader workers.--batch_size 32 --test_batch_size 4to override YAML values.--resume 3to continue from the 4th CV fold checkpoint.--no_crop,--dice_loss,--transformeretc. to toggle model behavior.
python run.py --cfg configs.yaml --gpu 0 --evaluateThis bypasses training and immediately runs workers/test_net.py with the checkpoints referenced in save_name.
- Default: Weights & Biases (
wandb). Setresume_wandboruse_tensorboardin the config to switch modes. - Offline/debug: enable
smoke_testto use a CSV logger and skip remote tracking.
- Missing superpixel files: verify the directory naming scheme (
{size}_{sp_num}/T_sp/{seed}/stageX). The loader prints the missing path when a.npyis absent. - CUDA OOM: lower
batch_sizeand consider reducingsizeorsp_numin the config (which also affects the path looked up byload_group_associations). - Different dataset root: edit
datasets/isic18.py::load_nameinput/target glob patterns or symlink your data to the expected path.
- Release pretrained weights of SET on ISIC2018.
If you find SET useful or build upon this codebase, please cite our Medical Image Analysis paper:
@article{wang2025set,
title={SET: Superpixel Embedded Transformer for skin lesion segmentation},
author={Wang, Zhonghua and Lyu, Junyan and Tang, Xiaoying},
journal={Medical Image Analysis},
pages={103738},
year={2025},
publisher={Elsevier}
}