File size: 2,107 Bytes
3c366de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/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()