Upload 16 files
Browse files
Stable_diffusion_augmentation/plan_and_materialize_balanced_milk10k.py
CHANGED
|
@@ -31,7 +31,9 @@ def parse_args(argv=None):
|
|
| 31 |
p.add_argument("--augmented-metadata", type=Path, default=None)
|
| 32 |
p.add_argument("--synthetic-input-dir", type=Path, default=None)
|
| 33 |
p.add_argument("--qc-summary", type=Path, default=None)
|
|
|
|
| 34 |
p.add_argument("--report-dir", type=Path, required=True)
|
|
|
|
| 35 |
p.add_argument("--materialize-dir", type=Path, default=None)
|
| 36 |
p.add_argument("--bcc-cap-ratio", type=float, default=1.5)
|
| 37 |
p.add_argument("--tail-floor", type=int, default=150)
|
|
@@ -56,7 +58,7 @@ def resolve_paths(args):
|
|
| 56 |
info = base / "augmented_info"
|
| 57 |
aug_gt = args.augmented_groundtruth or info / "MILK10k_Training_GroundTruth(2).csv"
|
| 58 |
aug_meta = args.augmented_metadata or info / "MILK10k_Training_Metadata(3).csv"
|
| 59 |
-
required = [gt, meta, input_dir
|
| 60 |
missing = [str(path) for path in required if not path.exists()]
|
| 61 |
if missing: raise FileNotFoundError("Missing inputs: " + ", ".join(missing))
|
| 62 |
return gt, meta, input_dir, aug_gt.expanduser().resolve(), aug_meta.expanduser().resolve()
|
|
@@ -75,11 +77,16 @@ def source_id(lesion_id): return str(lesion_id).split("__sdpair_", 1)[0]
|
|
| 75 |
|
| 76 |
def load_inventory(args):
|
| 77 |
gt_path, meta_path, input_dir, aug_gt_path, aug_meta_path = resolve_paths(args)
|
| 78 |
-
base_gt_raw = pd.read_csv(gt_path); base_meta = pd.read_csv(meta_path);
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
synth_meta = aug_meta[aug_meta.lesion_id.astype(str).isin(set(synth_gt.lesion_id.astype(str)))].copy()
|
| 82 |
-
if
|
| 83 |
if synth_gt.lesion_id.duplicated().any(): raise ValueError("Duplicate synthetic lesion IDs.")
|
| 84 |
synth_gt["source_lesion_id"] = synth_gt.lesion_id.map(source_id)
|
| 85 |
modality_counts = synth_meta.groupby("lesion_id").image_type.agg(lambda values: set(map(str, values)))
|
|
@@ -267,10 +274,12 @@ def command_script(args, plan, report_dir):
|
|
| 267 |
"python Stable_diffusion_augmentation/plan_and_materialize_balanced_milk10k.py --base-data-dir \"$BASE_DATA\" --augmented-groundtruth \"$CANDIDATE_DIR/MILK10k_Training_GroundTruth.csv\" --augmented-metadata \"$CANDIDATE_DIR/MILK10k_Training_Metadata.csv\" --synthetic-input-dir \"$CANDIDATE_DIR/MILK10k_Training_Input\" --qc-summary \"$GEN_DIR/effb2_qc_summary.csv\" --report-dir \"$REPORT_DIR/final_audit\" --materialize-dir \"$FINAL_DIR\" --require-target-pred --overwrite", "",
|
| 268 |
"# Train safely: synthetic IDs stay train-only.",
|
| 269 |
"# python milk10k_effb2_dermoscopic_metadata/train_milk10k_effb2_dermoscopic_metadata.py --data-dir \"$FINAL_DIR\" --output-dir /path/to/run --split-manifest /path/to/run/split_v2.json --synthetic-train-only --metadata-mode none --loss ldam", ""]
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
| 274 |
|
| 275 |
|
| 276 |
def report_markdown(args, plan, sources, manifest, bcc_cap, usable_count, missing_pairs, missing_image_files):
|
|
|
|
| 31 |
p.add_argument("--augmented-metadata", type=Path, default=None)
|
| 32 |
p.add_argument("--synthetic-input-dir", type=Path, default=None)
|
| 33 |
p.add_argument("--qc-summary", type=Path, default=None)
|
| 34 |
+
p.add_argument("--fresh-start", action="store_true", help="Ignore every existing synthetic CSV and plan from base real data only.")
|
| 35 |
p.add_argument("--report-dir", type=Path, required=True)
|
| 36 |
+
p.add_argument("--scripts-dir", type=Path, default=None, help="Command output folder; defaults to Stable_diffusion_augmentation/.")
|
| 37 |
p.add_argument("--materialize-dir", type=Path, default=None)
|
| 38 |
p.add_argument("--bcc-cap-ratio", type=float, default=1.5)
|
| 39 |
p.add_argument("--tail-floor", type=int, default=150)
|
|
|
|
| 58 |
info = base / "augmented_info"
|
| 59 |
aug_gt = args.augmented_groundtruth or info / "MILK10k_Training_GroundTruth(2).csv"
|
| 60 |
aug_meta = args.augmented_metadata or info / "MILK10k_Training_Metadata(3).csv"
|
| 61 |
+
required = [gt, meta, input_dir] + ([] if args.fresh_start else [aug_gt, aug_meta])
|
| 62 |
missing = [str(path) for path in required if not path.exists()]
|
| 63 |
if missing: raise FileNotFoundError("Missing inputs: " + ", ".join(missing))
|
| 64 |
return gt, meta, input_dir, aug_gt.expanduser().resolve(), aug_meta.expanduser().resolve()
|
|
|
|
| 77 |
|
| 78 |
def load_inventory(args):
|
| 79 |
gt_path, meta_path, input_dir, aug_gt_path, aug_meta_path = resolve_paths(args)
|
| 80 |
+
base_gt_raw = pd.read_csv(gt_path); base_meta = pd.read_csv(meta_path); base_gt = attach_labels(base_gt_raw)
|
| 81 |
+
base_ids = set(base_gt.lesion_id.astype(str))
|
| 82 |
+
if args.fresh_start:
|
| 83 |
+
synth_gt = pd.DataFrame(columns=[*base_gt.columns, "source_lesion_id", "pair_metadata_complete"])
|
| 84 |
+
synth_meta = pd.DataFrame(columns=base_meta.columns)
|
| 85 |
+
return base_gt_raw, base_gt, base_meta, synth_gt, synth_meta, input_dir
|
| 86 |
+
aug_gt_raw = pd.read_csv(aug_gt_path); aug_meta = pd.read_csv(aug_meta_path); aug_gt = attach_labels(aug_gt_raw)
|
| 87 |
+
synth_gt = aug_gt[~aug_gt.lesion_id.astype(str).isin(base_ids)].copy()
|
| 88 |
synth_meta = aug_meta[aug_meta.lesion_id.astype(str).isin(set(synth_gt.lesion_id.astype(str)))].copy()
|
| 89 |
+
if base_ids - set(aug_gt.lesion_id.astype(str)): raise ValueError("Augmented ground truth omits base lesions.")
|
| 90 |
if synth_gt.lesion_id.duplicated().any(): raise ValueError("Duplicate synthetic lesion IDs.")
|
| 91 |
synth_gt["source_lesion_id"] = synth_gt.lesion_id.map(source_id)
|
| 92 |
modality_counts = synth_meta.groupby("lesion_id").image_type.agg(lambda values: set(map(str, values)))
|
|
|
|
| 274 |
"python Stable_diffusion_augmentation/plan_and_materialize_balanced_milk10k.py --base-data-dir \"$BASE_DATA\" --augmented-groundtruth \"$CANDIDATE_DIR/MILK10k_Training_GroundTruth.csv\" --augmented-metadata \"$CANDIDATE_DIR/MILK10k_Training_Metadata.csv\" --synthetic-input-dir \"$CANDIDATE_DIR/MILK10k_Training_Input\" --qc-summary \"$GEN_DIR/effb2_qc_summary.csv\" --report-dir \"$REPORT_DIR/final_audit\" --materialize-dir \"$FINAL_DIR\" --require-target-pred --overwrite", "",
|
| 275 |
"# Train safely: synthetic IDs stay train-only.",
|
| 276 |
"# python milk10k_effb2_dermoscopic_metadata/train_milk10k_effb2_dermoscopic_metadata.py --data-dir \"$FINAL_DIR\" --output-dir /path/to/run --split-manifest /path/to/run/split_v2.json --synthetic-train-only --metadata-mode none --loss ldam", ""]
|
| 277 |
+
code_dir=(args.scripts_dir.expanduser().resolve() if args.scripts_dir else Path(__file__).resolve().parent)
|
| 278 |
+
code_dir.mkdir(parents=True,exist_ok=True)
|
| 279 |
+
exported={"run_fresh_balance_01_generate.sh":header+generate,
|
| 280 |
+
"run_fresh_balance_02_qc_materialize.sh":header+qc_materialize}
|
| 281 |
+
for name,lines in exported.items():
|
| 282 |
+
path=code_dir/name;path.write_text("\n".join(lines),encoding="utf-8");path.chmod(0o755)
|
| 283 |
|
| 284 |
|
| 285 |
def report_markdown(args, plan, sources, manifest, bcc_cap, usable_count, missing_pairs, missing_image_files):
|
Stable_diffusion_augmentation/run_fresh_balance_01_generate.sh
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
BASE_DATA="/mnt/data/imp301/data_related"
|
| 5 |
+
BASE_INPUT="/mnt/data/imp301/MILK10k_Training_Input"
|
| 6 |
+
REPORT_DIR="/mnt/data/imp301/data_related/augmented_info/fresh_balance_plan"
|
| 7 |
+
GEN_DIR="$REPORT_DIR/generated_balance_pairs"
|
| 8 |
+
|
| 9 |
+
python Stable_diffusion_augmentation/generate_milk10k_sd_pairs.py \
|
| 10 |
+
--data-dir "$BASE_DATA" \
|
| 11 |
+
--input-dir "$BASE_INPUT" \
|
| 12 |
+
--output-dir "$GEN_DIR" \
|
| 13 |
+
--class-names BEN_OTH DF INF VASC \
|
| 14 |
+
--num-per-lesion 3 \
|
| 15 |
+
--max-source-lesions 34 \
|
| 16 |
+
--shuffle \
|
| 17 |
+
--skip-existing
|
Stable_diffusion_augmentation/run_fresh_balance_02_qc_materialize.sh
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
BASE_DATA="/mnt/data/imp301/data_related"
|
| 5 |
+
BASE_INPUT="/mnt/data/imp301/MILK10k_Training_Input"
|
| 6 |
+
REPORT_DIR="/mnt/data/imp301/data_related/augmented_info/fresh_balance_plan"
|
| 7 |
+
CHECKPOINT_DIR="/path/to/convnext_5fold_run"
|
| 8 |
+
GEN_DIR="$REPORT_DIR/generated_balance_pairs"
|
| 9 |
+
CANDIDATE_DIR="$REPORT_DIR/candidate_augmented"
|
| 10 |
+
FINAL_DIR="/path/to/milk10k_balanced_augmented"
|
| 11 |
+
|
| 12 |
+
python Stable_diffusion_augmentation/run_effb2_qc.py \
|
| 13 |
+
--checkpoint-dir "$CHECKPOINT_DIR" \
|
| 14 |
+
--output-dir "$GEN_DIR"
|
| 15 |
+
|
| 16 |
+
python Stable_diffusion_augmentation/filter_paired_augmentation_by_qc.py \
|
| 17 |
+
--manifest "$GEN_DIR/paired_augmentation_manifest.csv" \
|
| 18 |
+
--qc-summary "$GEN_DIR/effb2_qc_summary.csv" \
|
| 19 |
+
--output "$GEN_DIR/filtered_manifest.csv" \
|
| 20 |
+
--min-target-prob 0.4 \
|
| 21 |
+
--require-target-pred
|
| 22 |
+
|
| 23 |
+
python Stable_diffusion_augmentation/materialize_augmented_milk10k_dataset.py \
|
| 24 |
+
--input-dir "$BASE_INPUT" \
|
| 25 |
+
--metadata-csv "$BASE_DATA/MILK10k_Training_Metadata.csv" \
|
| 26 |
+
--groundtruth-csv "$BASE_DATA/MILK10k_Training_GroundTruth.csv" \
|
| 27 |
+
--augmentation-manifest "$GEN_DIR/filtered_manifest.csv" \
|
| 28 |
+
--output-dir "$CANDIDATE_DIR" \
|
| 29 |
+
--symlink \
|
| 30 |
+
--synthetic-metadata neutral \
|
| 31 |
+
--overwrite
|
| 32 |
+
|
| 33 |
+
python Stable_diffusion_augmentation/plan_and_materialize_balanced_milk10k.py \
|
| 34 |
+
--base-data-dir "$BASE_DATA" \
|
| 35 |
+
--augmented-groundtruth "$CANDIDATE_DIR/MILK10k_Training_GroundTruth.csv" \
|
| 36 |
+
--augmented-metadata "$CANDIDATE_DIR/MILK10k_Training_Metadata.csv" \
|
| 37 |
+
--synthetic-input-dir "$CANDIDATE_DIR/MILK10k_Training_Input" \
|
| 38 |
+
--qc-summary "$GEN_DIR/effb2_qc_summary.csv" \
|
| 39 |
+
--report-dir "$REPORT_DIR/final_audit" \
|
| 40 |
+
--materialize-dir "$FINAL_DIR" \
|
| 41 |
+
--require-target-pred \
|
| 42 |
+
--overwrite
|
Stable_diffusion_augmentation/tests/test_balance_planner.py
CHANGED
|
@@ -63,5 +63,12 @@ class BalancePlannerTests(unittest.TestCase):
|
|
| 63 |
result=run(parse_args(["--base-data-dir",str(base),"--report-dir",str(root/"report")]))
|
| 64 |
self.assertEqual(len(result["manifest"]),4);self.assertEqual(len(result["usable"]),0)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if __name__=="__main__":unittest.main()
|
|
|
|
| 63 |
result=run(parse_args(["--base-data-dir",str(base),"--report-dir",str(root/"report")]))
|
| 64 |
self.assertEqual(len(result["manifest"]),4);self.assertEqual(len(result["usable"]),0)
|
| 65 |
|
| 66 |
+
def test_fresh_start_ignores_existing_synthetic_inventory(self):
|
| 67 |
+
with tempfile.TemporaryDirectory() as tmp:
|
| 68 |
+
root=Path(tmp);base,_,_=fixture(root)
|
| 69 |
+
result=run(parse_args(["--base-data-dir",str(base),"--fresh-start","--report-dir",str(root/"fresh")]))
|
| 70 |
+
self.assertEqual(len(result["manifest"]),0)
|
| 71 |
+
self.assertEqual(int(result["plan"].raw_synthetic_inventory.sum()),0)
|
| 72 |
+
|
| 73 |
|
| 74 |
if __name__=="__main__":unittest.main()
|