-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_kaggle_data.py
More file actions
142 lines (100 loc) · 4.42 KB
/
Copy pathprepare_kaggle_data.py
File metadata and controls
142 lines (100 loc) · 4.42 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import argparse
import os
import random
import shutil
from pathlib import Path
import numpy as np
from PIL import Image
IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".bmp", ".webp"}
def find_images(root: Path):
return [p for p in root.rglob("*") if p.is_file() and p.suffix.lower() in IMAGE_EXTS]
def label_from_path(path: Path):
lowered_parts = [part.lower() for part in path.parts]
lowered_name = path.name.lower()
if any("cat" in part for part in lowered_parts) or lowered_name.startswith("cat"):
return "cat"
if (
any("dog" in part for part in lowered_parts)
or any("noncat" in part for part in lowered_parts)
or lowered_name.startswith("dog")
):
return "noncat"
return None
def copy_labeled_images(images, label, destination, max_count):
labeled = [img for img in images if label_from_path(img) == label]
random.shuffle(labeled)
if max_count is not None:
labeled = labeled[:max_count]
destination.mkdir(parents=True, exist_ok=True)
for index, source in enumerate(labeled):
target = destination / f"{label}_{index:05d}{source.suffix.lower()}"
shutil.copy2(source, target)
return len(labeled)
def prepare_from_h5(source_file: Path, output_root: Path, max_per_class, seed: int):
import h5py
random.seed(seed)
with h5py.File(source_file, "r") as f:
images = np.array(f["train_set_x"])
labels = np.array(f["train_set_y"])
cats_dir = output_root / "cats"
noncats_dir = output_root / "noncats"
if cats_dir.exists():
shutil.rmtree(cats_dir)
if noncats_dir.exists():
shutil.rmtree(noncats_dir)
cats_dir.mkdir(parents=True, exist_ok=True)
noncats_dir.mkdir(parents=True, exist_ok=True)
cat_indices = [i for i, y in enumerate(labels) if int(y) == 1]
noncat_indices = [i for i, y in enumerate(labels) if int(y) == 0]
random.shuffle(cat_indices)
random.shuffle(noncat_indices)
if max_per_class is not None:
cat_indices = cat_indices[:max_per_class]
noncat_indices = noncat_indices[:max_per_class]
for idx, img_index in enumerate(cat_indices):
image = Image.fromarray(images[img_index])
image = image.convert("L").resize((64, 64))
image.save(cats_dir / f"cat_{idx:05d}.png")
for idx, img_index in enumerate(noncat_indices):
image = Image.fromarray(images[img_index])
image = image.convert("L").resize((64, 64))
image.save(noncats_dir / f"noncat_{idx:05d}.png")
print(f"Prepared dataset at {output_root}")
print(f"Cats: {len(cat_indices)}")
print(f"Noncats: {len(noncat_indices)}")
def main():
parser = argparse.ArgumentParser(description="Prepare cats/noncats folders from a Kaggle dataset")
parser.add_argument("--source-root", type=str, default="data_raw", help="Root folder containing extracted Kaggle files")
parser.add_argument("--output-root", type=str, default="data", help="Output root with cats/noncats folders")
parser.add_argument("--max-per-class", type=int, default=300, help="Max images per class")
parser.add_argument("--seed", type=int, default=42)
args = parser.parse_args()
random.seed(args.seed)
source_root = Path(args.source_root)
output_root = Path(args.output_root)
if not source_root.exists():
raise FileNotFoundError(f"Source folder does not exist: {source_root}")
h5_files = sorted(source_root.glob("train*.h5")) or sorted(source_root.glob("*.h5"))
if h5_files:
prepare_from_h5(h5_files[0], output_root, args.max_per_class, args.seed)
return
images = find_images(source_root)
if not images:
raise ValueError(f"No images found under: {source_root}")
cats_dir = output_root / "cats"
noncats_dir = output_root / "noncats"
if cats_dir.exists():
shutil.rmtree(cats_dir)
if noncats_dir.exists():
shutil.rmtree(noncats_dir)
cats_count = copy_labeled_images(images, "cat", cats_dir, args.max_per_class)
noncats_count = copy_labeled_images(images, "noncat", noncats_dir, args.max_per_class)
if cats_count == 0 or noncats_count == 0:
raise ValueError(
"Could not find both class types. Ensure dataset contains cat and dog/noncat images with identifiable names or folders."
)
print(f"Prepared dataset at {output_root}")
print(f"Cats: {cats_count}")
print(f"Noncats: {noncats_count}")
if __name__ == "__main__":
main()