Repository navigation
[BTY-62] MEDFAIR: subgroup fairness metrics, fairness-aware model selection and resampling, with a HAM10000 setup #1560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8c780be
feat: add MEDFAIR subgroup fairness evaluation for imaging (BTY-62)
sanniheruwala dc6b009
fix: keep group sampler off shared dataloader code
sanniheruwala 2f0c411
refactor: move MEDFAIR example to examples/derm, clarify sampler test
sanniheruwala f99c902
fix: validate Dataverse metadata before either reader uses it
sanniheruwala e180e2d
fix: ignore a tab-separated Dataverse file when the groupings CSV is …
sanniheruwala 2cb7ad5
test: cover the group loader's remaining branches
sanniheruwala 2c88f79
docs: point to MEDFAIR's tables instead of quoting their numbers
sanniheruwala 0a77892
fix: warn for every metric a single-class subgroup leaves undefined
sanniheruwala 5ddc50b
docs: note the training-only demographics and the all-pairs Pareto check
sanniheruwala c9b2f3a
fix: validate inputs at the edges of the fairness, sampler and HAM100…
sanniheruwala 0779a21
fix: say why the HAM10000 task skips images, and test the loader on a…
sanniheruwala 35046b8
fix: reject undefined F1 tuning and non-finite selection scores
sanniheruwala 5f1c85a
fix: tune the threshold only when used, draw resamples with numpy
sanniheruwala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| pyhealth.metrics.fairness_selection | ||
| =================================== | ||
|
|
||
| .. automodule:: pyhealth.metrics.fairness_selection | ||
|
|
||
| .. currentmodule:: pyhealth.metrics.fairness_selection | ||
|
|
||
| .. autofunction:: dto_select | ||
|
|
||
| .. autofunction:: pareto_front | ||
|
|
||
| .. autofunction:: minimax_pareto_select |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| pyhealth.metrics.group_fairness | ||
| =================================== | ||
|
|
||
| .. automodule:: pyhealth.metrics.group_fairness | ||
|
|
||
| .. currentmodule:: pyhealth.metrics.group_fairness | ||
|
|
||
| .. autofunction:: group_fairness_metrics_fn | ||
|
|
||
| .. autofunction:: best_f1_threshold |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
docs/api/sampler/pyhealth.sampler.GroupBalancedSampler.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| pyhealth.sampler.GroupBalancedSampler | ||
| ===================================== | ||
|
|
||
| Subgroup resampling: draw every sensitive-attribute group, or every | ||
| (group, label) cell, equally often during training. | ||
|
|
||
| .. autoclass:: pyhealth.sampler.GroupBalancedSampler | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: | ||
|
|
||
| .. autofunction:: pyhealth.sampler.get_group_balanced_dataloader |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
docs/api/tasks/pyhealth.tasks.HAM10000MalignancyClassification.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| pyhealth.tasks.HAM10000MalignancyClassification | ||
| =============================================== | ||
|
|
||
| Malignant vs. benign HAM10000 images with sex, age and age group on every | ||
| sample, the HAM10000 setup of the MEDFAIR fairness benchmark. | ||
|
|
||
| .. autoclass:: pyhealth.tasks.HAM10000MalignancyClassification | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| """MEDFAIR on HAM10000: ERM vs. subgroup resampling, three selection rules. | ||
| The HAM10000 experiment of MEDFAIR (Zong, Yang and Hospedales, "MEDFAIR: | ||
| Benchmarking Fairness for Medical Imaging", ICLR 2023, | ||
| https://arxiv.org/abs/2210.01725). A ResNet-18 learns to tell malignant (MEL, | ||
| AKIEC) from benign lesions with plain ERM and with subgroup resampling, for | ||
| every learning rate and seed. Per method, a learning rate is then chosen | ||
| three ways from validation scores averaged over seeds: best overall AUC, | ||
| smallest distance to optimal (DTO) and minimax Pareto, the latter judged on | ||
| the group that is worst under ERM, as in MEDFAIR. The table gives the test | ||
| scores of each choice as mean (std) over seeds. | ||
| Without ``--root`` the script writes a small synthetic ISIC 2018 layout to a | ||
| temporary directory and runs the whole pipeline as a smoke test; its numbers | ||
| mean nothing. On the real data (open access, see ``HAM10000Dataset``; put the | ||
|
sanniheruwala marked this conversation as resolved.
Resolved
|
||
| Dataverse ``HAM10000_metadata.csv`` next to the ISIC 2018 folders): | ||
| python examples/derm/medfair_ham10000.py --root /path/to/isic2018_task3 \\ | ||
| --cache-dir /path/to/cache --attribute sex --epochs 15 | ||
| Compare ``--attribute sex`` with the "HAM10000 / Sex" rows of MEDFAIR's | ||
|
sanniheruwala marked this conversation as resolved.
Resolved
|
||
| appendix Table A9 (arXiv:2210.01725v2; ERM and resampling, minimax Pareto) | ||
| and Table A8 (ERM under each selection rule). Its age results use four | ||
| groups taken from the five bins of its Table A2 (0-20 to 80+) in a way the | ||
| paper does not state, so they are not directly comparable to | ||
| ``--attribute age_group``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| import torch | ||
| from PIL import Image | ||
|
|
||
| from pyhealth.datasets import HAM10000Dataset, PatientSplit, get_dataloader | ||
| from pyhealth.metrics.fairness_selection import dto_select, minimax_pareto_select | ||
| from pyhealth.metrics.group_fairness import ( | ||
| best_f1_threshold, | ||
| group_fairness_metrics_fn, | ||
| ) | ||
| from pyhealth.models import TorchvisionModel | ||
| from pyhealth.sampler import get_group_balanced_dataloader | ||
| from pyhealth.tasks import HAM10000MalignancyClassification | ||
| from pyhealth.trainer import Trainer | ||
|
|
||
| CLASSES = ["MEL", "NV", "BCC", "AKIEC", "BKL", "DF", "VASC"] | ||
| REPORTED = ["auc", "worst_auc", "auc_gap"] | ||
|
|
||
|
|
||
| def write_synthetic_ham10000(root: Path, n_lesions: int, seed: int) -> None: | ||
| """Writes random images in the ISIC 2018 Task 3 layout plus Dataverse | ||
| metadata. Malignant images are redder, more so for men, so the subgroups | ||
| differ in difficulty.""" | ||
| rng = np.random.default_rng(seed) | ||
| image_dir = root / "ISIC2018_Task3_Training_Input" | ||
| gt_dir = root / "ISIC2018_Task3_Training_GroundTruth" | ||
| image_dir.mkdir(parents=True) | ||
| gt_dir.mkdir() | ||
| rows = [] | ||
| for i in range(n_lesions): | ||
| image = f"ISIC_{i:07d}" | ||
| diagnosis = CLASSES[rng.integers(len(CLASSES))] | ||
| sex = "male" if rng.random() < 0.55 else "female" | ||
| malignant = diagnosis in ("MEL", "AKIEC") | ||
| red = 90 + (100 if malignant else 0) * (1.0 if sex == "male" else 0.4) | ||
| pixels = rng.normal([red, 90, 120], 30, size=(32, 32, 3)).clip(0, 255) | ||
| Image.fromarray(pixels.astype(np.uint8)).save(image_dir / f"{image}.jpg") | ||
| rows.append( | ||
| { | ||
| "image": image, | ||
| "lesion_id": f"HAM_{i:07d}", | ||
| "diagnosis": diagnosis, | ||
| "age": float(rng.choice(np.arange(5, 90, 5))), | ||
| "sex": sex, | ||
| } | ||
| ) | ||
| frame = pd.DataFrame(rows) | ||
| onehot = {c: (frame["diagnosis"] == c).astype(float) for c in CLASSES} | ||
| pd.DataFrame({"image": frame["image"], **onehot}).to_csv( | ||
| gt_dir / f"{gt_dir.name}.csv", index=False | ||
| ) | ||
| frame.assign(diagnosis_confirm_type="histo")[ | ||
| ["image", "lesion_id", "diagnosis_confirm_type"] | ||
| ].to_csv(root / "ISIC2018_Task3_Training_LesionGroupings.csv", index=False) | ||
| frame.rename(columns={"image": "image_id", "diagnosis": "dx"}).assign( | ||
| dx_type="histo", localization="back" | ||
| ).to_csv(root / "HAM10000_metadata.csv", index=False) | ||
|
|
||
|
|
||
| def train_one(train, val, train_groups, args, method: str, lr: float, seed: int): | ||
| torch.manual_seed(seed) | ||
| if method == "resampling": | ||
| loader = get_group_balanced_dataloader( | ||
| train, args.batch_size, groups=train_groups, seed=seed | ||
| ) | ||
| else: | ||
| loader = get_dataloader(train, args.batch_size, shuffle=True, seed=seed) | ||
| model = TorchvisionModel( | ||
| dataset=train, | ||
| model_name="resnet18", | ||
| model_config={"weights": "DEFAULT" if args.pretrained else None}, | ||
| ) | ||
| trainer = Trainer(model=model, metrics=["roc_auc"], enable_logging=False) | ||
| trainer.train( | ||
| train_dataloader=loader, | ||
| val_dataloader=get_dataloader(val, args.batch_size), | ||
| epochs=args.epochs, | ||
| optimizer_params={"lr": lr}, | ||
| weight_decay=1e-4, | ||
| monitor="loss", | ||
| monitor_criterion="min", | ||
| patience=5, | ||
| ) | ||
| return trainer | ||
|
|
||
|
|
||
| def score_run(trainer, val, test, groups, batch_size) -> dict[str, dict]: | ||
| """Val and test metrics, the test set at the threshold tuned on val.""" | ||
| y_val, p_val, _ = trainer.inference(get_dataloader(val, batch_size)) | ||
| y_test, p_test, _ = trainer.inference(get_dataloader(test, batch_size)) | ||
| threshold = best_f1_threshold(y_val.ravel(), p_val.ravel()) | ||
| return { | ||
| "val": group_fairness_metrics_fn( | ||
| y_val, p_val, groups["val"], threshold=threshold | ||
| ), | ||
| "test": group_fairness_metrics_fn( | ||
| y_test, p_test, groups["test"], threshold=threshold | ||
| ), | ||
| } | ||
|
|
||
|
|
||
| def seed_stats(runs: list[dict], part: str) -> tuple[pd.Series, pd.Series]: | ||
| frame = pd.DataFrame([r[part] for r in runs]) | ||
| return frame.mean(), frame.std(ddof=0) | ||
|
|
||
|
|
||
| def select(settings: dict, names: list[str], ref: int | None) -> dict[str, float]: | ||
| """Learning rate picked by each rule from seed-averaged val scores.""" | ||
| lrs = list(settings) | ||
| means = [seed_stats(settings[lr], "val")[0] for lr in lrs] | ||
| aucs = np.array([[m.get(f"auc_{g}", np.nan) for g in names] for m in means]) | ||
| return { | ||
| "overall": lrs[int(np.argmax([m["auc"] for m in means]))], | ||
| "dto": lrs[dto_select(aucs)], | ||
| "minimax": lrs[minimax_pareto_select(aucs, group=ref)], | ||
| } | ||
|
|
||
|
|
||
| def defined_groups(settings: dict, names: list[str]) -> list[str]: | ||
| """Groups with a val AUC in every run; selection needs all of them.""" | ||
| runs = [r for rs in settings.values() for r in rs] | ||
| return [ | ||
| g | ||
| for g in names | ||
| if all(np.isfinite(r["val"].get(f"auc_{g}", np.nan)) for r in runs) | ||
| ] | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) | ||
| parser.add_argument("--root", help="ISIC 2018 Task 3 root; synthetic if unset") | ||
| parser.add_argument("--cache-dir", help="PyHealth cache; temporary if unset") | ||
| parser.add_argument("--attribute", default="sex", choices=["sex", "age_group"]) | ||
| parser.add_argument("--age-bins", type=int, nargs="+", default=[60]) | ||
| parser.add_argument("--image-size", type=int, default=224) | ||
| parser.add_argument("--epochs", type=int, default=15) | ||
| parser.add_argument("--batch-size", type=int, default=128) | ||
| parser.add_argument("--lrs", type=float, nargs="+", default=[1e-3, 1e-4, 1e-5]) | ||
| parser.add_argument("--seeds", type=int, nargs="+", default=[0, 1, 2]) | ||
| parser.add_argument("--no-pretrained", dest="pretrained", action="store_false") | ||
| args = parser.parse_args() | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmp: | ||
| root = args.root | ||
| if root is None: | ||
| root = Path(tmp) / "isic2018" | ||
| write_synthetic_ham10000(root, n_lesions=400, seed=0) | ||
| args.image_size, args.epochs, args.pretrained = 32, 2, False | ||
| args.lrs, args.seeds = [1e-3, 1e-4], [0, 1] | ||
| elif not (Path(root) / "HAM10000_metadata.csv").exists(): | ||
| raise SystemExit( | ||
| f"No HAM10000_metadata.csv in {root}; the task needs its age and " | ||
| "sex columns (see HAM10000Dataset)." | ||
| ) | ||
| dataset = HAM10000Dataset( | ||
| root=str(root), cache_dir=args.cache_dir or str(Path(tmp) / "cache") | ||
| ) | ||
| task = HAM10000MalignancyClassification( | ||
| image_size=args.image_size, age_bins=args.age_bins | ||
| ) | ||
| train, val, test = dataset.set_task( | ||
| task, split=PatientSplit((0.8, 0.1, 0.1), seed=0) | ||
| ) | ||
| # Reading a field decodes the whole cached sample, so collect the | ||
| # groups in one pass per split and reuse them for every run. | ||
| groups = { | ||
| name: [part[i][args.attribute] for i in range(len(part))] | ||
|
sanniheruwala marked this conversation as resolved.
Resolved
|
||
| for name, part in (("train", train), ("val", val), ("test", test)) | ||
| } | ||
| names = ["F", "M"] if args.attribute == "sex" else task.age_groups | ||
|
|
||
| results = {} | ||
| for method in ("erm", "resampling"): | ||
| results[method] = { | ||
| lr: [ | ||
| score_run( | ||
| train_one(train, val, groups["train"], args, method, lr, s), | ||
| val, | ||
| test, | ||
| groups, | ||
| args.batch_size, | ||
| ) | ||
| for s in args.seeds | ||
| ] | ||
| for lr in args.lrs | ||
| } | ||
|
|
||
| defined = [set(defined_groups(r, names)) for r in results.values()] | ||
| kept = [g for g in names if all(g in d for d in defined)] | ||
| if not kept: | ||
| raise SystemExit("No group has a val AUC in every run; nothing to select.") | ||
| if len(kept) < len(names): | ||
| print( | ||
| f"Selecting on {kept}; no val AUC for {sorted(set(names) - set(kept))}." | ||
| ) | ||
| erm_means = [seed_stats(runs, "val")[0] for runs in results["erm"].values()] | ||
| erm_best = np.array([[m[f"auc_{g}"] for g in kept] for m in erm_means]).max(0) | ||
| ref = int(np.argmin(erm_best)) | ||
|
|
||
| rows = [] | ||
| for method, settings in results.items(): | ||
| for rule, lr in select(settings, kept, ref).items(): | ||
| mean, std = seed_stats(settings[lr], "test") | ||
| rows.append( | ||
| { | ||
| "method": method, | ||
| "selection": rule, | ||
| "lr": lr, | ||
| **{k: f"{mean[k]:.4f} ({std[k]:.4f})" for k in REPORTED}, | ||
| } | ||
| ) | ||
| print(f"Minimax reference group (worst under ERM): {kept[ref]}") | ||
| print(pd.DataFrame(rows).to_string(index=False)) | ||
| for part in (train, val, test): | ||
| part.close() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,6 @@ tables: | |
| - "split" | ||
| - "diagnosis" | ||
| - "diagnosis_confirm_type" | ||
| - "age" | ||
| - "sex" | ||
| - "localization" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.