A minimal adversarial training framework for medical image segmentation.
All commands should be run from the repository root:
cd /home/divwardhan/Desktop/Coding/GAN_experiments/main_experimentsThis project uses the local uv environment configured in the repository.
- Install dependencies via
uvif not already done:
uv sync- Run Python scripts using the
uvenvironment:
uv run python main.py --helpThe dataset should follow the structure expected by the project. For the glioma segmentation dataset, the path used in examples is:
./glioma/DATASET/Segmentation/GliomaThe repository also contains other dataset folders:
glioma/DATASET/classification/Training/...glioma/DATASET/classification/Testing/...glioma/DATASET/Segmentation/Gliomaglioma/DATASET/Segmentation/Meningiomaglioma/DATASET/Segmentation/Pituitary tumor
dataset/loader.py expects paired image files and mask files, where each image is a PNG and its mask is named with _mask.png appended.
main.py- CLI entrypoint, parses arguments, starts training.framework.py- orchestrates the training workflow:- dataset loading
- model/generator initialization
- pretraining on clean data
- alternating adversarial cycles
dataset/loader.py- containsTumorDatasetandDatasetLoader.main_model/- main segmentation model code:unet.py- UNet architecturemodel.py-init_model()factorytrain.py- training functions for clean and adversarial training
generator/- generator training and model codeutils/save.py- saving adversarial sample outputs
main.pyparses CLI options and callsFrameworkRun(...).framework.pyloads the dataset usingDatasetLoader.framework.pyinitializes the segmentation model and adversarial generator.train_model_clean()pretrains the model on clean data.- The framework runs alternating cycles:
- generator training (
train_generator) - model adversarial training (
train_model_adv)
- generator training (
- Adversarial samples are optionally saved under
outputs/adv_samples.
Use this command from the repository root:
uv run main.py --dataset_path ./glioma/DATASET/Segmentation/Glioma --device cuda --batch_size 32 --save_images --model_epochs 15 --pretrain_epochs 50 --lr_model 1e-4If CUDA is unavailable, the script will fall back to CPU automatically.
--save_imagesstores adversarial examples in theoutputs/folder.--max_buffer_sizecontrols how many adversarial samples are kept during training.- Use
--img_sizeif you need a different input resolution.
Enjoy training and experimenting with the project!