This repository contains a reproducible Phase 2 / Phase 3 pipeline for the HAM10000 skin lesion dataset. It supports dataset audit and grouped train/validation/test splits, then trains and benchmarks multiple models with leakage-safe evaluation.
configs/default.yaml- default experiment configurationscripts/prepare_dataset.py- run Phase 2 dataset audit and grouped split generationscripts/train.py- train and benchmark the model suitescripts/evaluate.py- generate aggregated evaluation reports and plotssrc/data_pipeline.py- metadata loading, path resolution, grouped split creation, dataset buildersrc/models/build_models.py- model factory for baseline CNN and TIMM backbonessrc/train.py- benchmark training loop, checkpointing, class imbalance strategysrc/evaluate.py- metrics computation, result saving, plot generationsrc/utils.py- reproducibility helpers, config loading, common utilitiessrc/explainability/- modular post-hoc explanation methods and artifactsscripts/explain.py- generate a reproducible explanation for one canonical-split samplecheckpoints/- saved best model weightsresults/- dataset audit artifacts, split files, comparison tables, and figures
The project uses PyTorch, TorchVision, TIMM, Albumentations, OpenCV, and standard ML libraries. Install dependencies with:
python -m pip install -r requirements.txtNote: The local Python environment must support the installed
torchbinary. If you seeWinError 193or DLL import failures, install a matching CUDA/cpu build for your OS and Python version.
Place the HAM10000 dataset inside:
SUAPUai/skin-cancer-detection-ham10000/
and ensure configs/default.yaml points to the dataset root:
dataset_path: "./SUAPUai/skin-cancer-detection-ham10000/"
metadata_filename: "HAM10000_metadata.csv"Run the dataset preparation pipeline to create audit files, class distribution reports, and a leakage-safe grouped split.
python scripts/prepare_dataset.py --config configs/default.yamlOutputs:
results/dataset_audit.csvresults/data_split.csvresults/class_distribution.csvresults/class_distribution.png
Train the model suite using the reproducible benchmark pipeline:
python scripts/train.py --config configs/default.yamlThis script will:
- build grouped train/val/test datasets from
results/data_split.csv - train each model in
model_names - save the best checkpoint by validation
macro_f1 - evaluate the best model on the test set
- save
results/model_comparison.csvandresults/model_comparison.json - save training history plots to
results/figures/
After training, generate evaluation figures and a summary report with:
python scripts/evaluate.py --config configs/default.yamlThis will create:
results/model_comparison.csvresults/model_comparison.jsonresults/evaluation_summary.jsonresults/figures/model_comparison.pngresults/figures/<best_model>_confusion_matrix.pngresults/figures/<best_model>_per_class_f1.png
The explainability engine provides post-hoc Grad-CAM, Grad-CAM++, Integrated
Gradients, saliency maps, and occlusion analysis. It supports the benchmark
architectures (baseline_cnn, ResNet18/34/50, DenseNet121, and EfficientNet-B0)
through architecture-aware target-layer resolution. Each run preserves the
method, target/predicted class, probability, checkpoint, sample ID, seed,
configuration, and runtime information.
Generate an explanation only for a sample already in the canonical grouped split:
python scripts/explain.py --config configs/default.yaml --model densenet121 \
--checkpoint checkpoints/densenet121_best.pth --sample ISIC_0024306 --method gradcamFor batch mode, replace --sample with --samples-file sample_ids.txt, where
the file has one image_id from results/data_split.csv per line.
Artifacts are written without overwriting prior runs under:
results/explainability/<method>/<model>/<sample>__target-<class>/
original.png raw_map.npy heatmap.png overlay.png metadata.json
Comparison metrics can quantify similarity and overlap among generated maps, but localization metrics are explicitly unavailable because this repository provides no lesion-segmentation masks. Highlighted regions are model evidence, not clinical findings; this academic prototype is not clinically validated or diagnostic.
Calibration, uncertainty, OOD detection, selective prediction, robustness, and multimodal methods are planned only after explainability verification.
Key parameters in configs/default.yaml:
image_size- input resolution for all modelsbatch_size- training batch sizeepochs- number of training epochslearning_rate- optimizer learning rateweight_decay- weight decay for AdamWscheduler- learning rate scheduler typeclass_strategy- imbalance handling strategy:standard,class_weighted_loss,focal_loss, orweighted_samplingmodel_names- list of models to benchmark
- The current benchmark includes:
baseline_cnnresnet18resnet34resnet50densenet121efficientnet_b0
- The split is leakage-aware and based on
patient_idorlesion_idfrom the metadata. - Use the generated
results/data_split.csvas the canonical split for all experiments.
- If training fails because
torchcannot import, verify the installed PyTorch wheel matches your Python version and Windows architecture. - If dataset images are missing,
src/data_pipeline.pywill drop rows with missing paths and warn you. - If
data_split.csvis absent,scripts/train.pywill regenerate it using the Phase 2 pipeline.