GPT-Image / code /sample_pack.py
MaybeRichard's picture
Add training and evaluation code
3c366de verified
Raw
History Blame Contribute Delete
2.11 kB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Sample N real images per class per dataset and pack into one zip for inspection.
Pools images across train/val/test, picks N with a fixed seed, copies into
dataset_samples/<Disease_Dataset>/<label>_<class_name>/<original_name>
then zips to dataset_samples.zip.
"""
import os, csv, random, shutil
from collections import defaultdict
PROJ = "/mnt/tidal-alsh-share2/dataset/qinshengqian/research/c3/GPT-Image"
DS = f"{PROJ}/Dataset"
OUT = f"{PROJ}/dataset_samples"
N, SEED = 20, 42
DSETS = {
"mmac": ("Myopia/Classification_of_Myopic_Maculopathy", "Myopia_MMAC"),
"adam": ("AMD/adamdataset", "AMD_ADAM"),
"airogs": ("Glaucoma/eyepacs-airogs-light", "Glaucoma_AIROGS"),
"papila": ("Glaucoma/papila-retinal-fundus-images", "Glaucoma_PAPILA"),
"idrid": ("DR/idrid-dataset", "DR_IDRiD"),
"aptos": ("DR/aptos2019", "DR_APTOS"),
"deepdrid": ("DR/deepdrid", "DR_DeepDRiD"),
}
def main():
rnd = random.Random(SEED)
shutil.rmtree(OUT, ignore_errors=True)
total = 0
for k, (rel, prefix) in DSETS.items():
base = os.path.join(DS, rel)
rows = list(csv.DictReader(open(os.path.join(base, "labels.csv"))))
by = defaultdict(list)
for r in rows:
by[(r["label"], r.get("class_name", ""))].append(r["filepath"])
print(f"### {prefix}")
for (lab, cn), files in sorted(by.items(), key=lambda x: int(x[0][0])):
pick = sorted(files)
rnd.shuffle(pick)
pick = pick[:N]
dst = os.path.join(OUT, prefix, f"{lab}_{cn}")
os.makedirs(dst, exist_ok=True)
for fp in pick:
shutil.copy2(os.path.join(base, fp), os.path.join(dst, os.path.basename(fp)))
total += len(pick)
print(f" {lab}_{cn:10} picked {len(pick):2d} / {len(files)} available")
zip_path = shutil.make_archive(OUT, "zip", OUT)
mb = os.path.getsize(zip_path) / (1024 * 1024)
print(f"\nTOTAL images: {total}")
print(f"ZIP: {zip_path} ({mb:.1f} MB)")
if __name__ == "__main__":
main()