| |
| """Create ImageFolder symlink views for Kermany2018 from master_labels.csv.""" |
| from __future__ import annotations |
|
|
| import argparse |
| import os |
| from pathlib import Path |
|
|
| import pandas as pd |
|
|
|
|
| CLASSES = ["CNV", "DME", "DRUSEN", "NORMAL"] |
|
|
|
|
| def safe_name(src: Path, i: int) -> str: |
| return f"{i:08d}_{src.name}" |
|
|
|
|
| def build_split(df: pd.DataFrame, split: str, out_root: Path) -> int: |
| sub = df[df["split"] == split].reset_index(drop=True) |
| total = 0 |
| for cls in CLASSES: |
| cls_dir = out_root / split / cls |
| cls_dir.mkdir(parents=True, exist_ok=True) |
| rows = sub[sub["dx"] == cls].reset_index(drop=True) |
| for i, row in rows.iterrows(): |
| src = Path(row["image_path"]) |
| dst = cls_dir / safe_name(src, i) |
| if dst.exists() or dst.is_symlink(): |
| continue |
| os.symlink(src, dst) |
| total += 1 |
| return total |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser() |
| parser.add_argument( |
| "--labels-csv", |
| type=Path, |
| default=Path("/data/temp/qinshengqian/c3/kermany_downstream/master_labels.csv"), |
| ) |
| parser.add_argument( |
| "--out-root", |
| type=Path, |
| default=Path("/data/temp/qinshengqian/c3/kermany_imagefolder"), |
| ) |
| args = parser.parse_args() |
|
|
| df = pd.read_csv(args.labels_csv) |
| df = df[df["dx"].isin(CLASSES)].copy() |
| args.out_root.mkdir(parents=True, exist_ok=True) |
|
|
| for split in ["train", "val", "test"]: |
| added = build_split(df, split, args.out_root) |
| print(f"{split}: added {added}") |
|
|
| for split in ["train", "val", "test"]: |
| print(f"\n{split}") |
| for cls in CLASSES: |
| n = len(list((args.out_root / split / cls).glob("*"))) |
| print(f" {cls}: {n}") |
| print(f"\nKERMANY_IMAGEFOLDER_READY {args.out_root}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|