-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_utils.py
More file actions
315 lines (268 loc) · 10.4 KB
/
Copy pathsplit_utils.py
File metadata and controls
315 lines (268 loc) · 10.4 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""
Utilities for duplicate-aware dataset splitting.
"""
from collections import Counter, defaultdict
from pathlib import Path
from random import Random
from typing import Dict, List, Sequence, Tuple
from check_duplicates import find_duplicates
class UnionFind:
"""Small union-find for grouping duplicate samples."""
def __init__(self, size: int):
self.parent = list(range(size))
def find(self, x: int) -> int:
while self.parent[x] != x:
self.parent[x] = self.parent[self.parent[x]]
x = self.parent[x]
return x
def union(self, a: int, b: int):
ra = self.find(a)
rb = self.find(b)
if ra != rb:
self.parent[rb] = ra
def build_grouped_split(
samples: Sequence[Tuple[Path, int]],
class_names: Sequence[str],
train_split: float = 0.7,
val_split: float = 0.15,
seed: int = 42,
duplicate_threshold: int = 10,
) -> Tuple[Dict[str, List[int]], Dict]:
"""
Split samples while keeping perceptual duplicate groups together.
Returns:
split_indices: {"train": [...], "val": [...], "test": [...]}
diagnostics: serializable split summary
"""
if train_split <= 0 or val_split < 0 or train_split + val_split >= 1:
raise ValueError("Invalid train/val split fractions")
num_samples = len(samples)
uf = UnionFind(num_samples)
path_to_index = {str(path): idx for idx, (path, _) in enumerate(samples)}
duplicate_groups = find_duplicates(
str(Path(samples[0][0]).parents[1]),
similarity_threshold=duplicate_threshold,
) if samples else []
for group in duplicate_groups:
indices = [path_to_index[str(path)] for path in group if str(path) in path_to_index]
for idx in indices[1:]:
uf.union(indices[0], idx)
grouped_indices: Dict[int, List[int]] = defaultdict(list)
for idx in range(num_samples):
grouped_indices[uf.find(idx)].append(idx)
total_size = num_samples
split_names = ["train", "val", "test"]
split_targets = {
"train": int(round(total_size * train_split)),
"val": int(round(total_size * val_split)),
}
split_targets["test"] = total_size - split_targets["train"] - split_targets["val"]
total_class_counts = Counter(label for _, label in samples)
class_targets = {
split: {
class_idx: total_class_counts[class_idx] * split_targets[split] / max(total_size, 1)
for class_idx in total_class_counts
}
for split in split_names
}
groups = []
for root, indices in grouped_indices.items():
label_counts = Counter(samples[idx][1] for idx in indices)
groups.append({
"root": root,
"indices": sorted(indices),
"size": len(indices),
"label_counts": label_counts,
})
rng = Random(seed)
rng.shuffle(groups)
groups.sort(
key=lambda g: (
-g["size"],
-max(g["label_counts"].values()),
tuple(sorted(g["label_counts"].items())),
)
)
assigned = {name: [] for name in split_names}
split_sizes = {name: 0 for name in split_names}
split_class_counts = {name: Counter() for name in split_names}
def global_score(candidate_split: str = None, candidate_group: Dict = None) -> float:
score = 0.0
for split_name in split_names:
size = split_sizes[split_name]
counts = split_class_counts[split_name]
if split_name == candidate_split and candidate_group is not None:
size += candidate_group["size"]
counts = counts + candidate_group["label_counts"]
score += abs(size - split_targets[split_name]) * 2.0
for class_idx, total_count in total_class_counts.items():
score += abs(counts[class_idx] - class_targets[split_name][class_idx])
overshoot = max(0, size - split_targets[split_name])
score += overshoot * 3.0
return score
for group in groups:
best_split = min(split_names, key=lambda name: global_score(name, group))
assigned[best_split].extend(group["indices"])
split_sizes[best_split] += group["size"]
split_class_counts[best_split].update(group["label_counts"])
split_indices = {name: sorted(indices) for name, indices in assigned.items()}
index_to_split = {}
for split_name, indices in split_indices.items():
for idx in indices:
index_to_split[idx] = split_name
duplicate_group_summaries = []
cross_split_duplicate_groups = 0
for group in duplicate_groups:
members = []
split_presence = set()
for path in group:
idx = path_to_index.get(str(path))
if idx is None:
continue
split_name = index_to_split[idx]
split_presence.add(split_name)
members.append({
"path": str(path),
"class_name": class_names[samples[idx][1]],
"split": split_name,
})
if len(split_presence) > 1:
cross_split_duplicate_groups += 1
duplicate_group_summaries.append(members)
diagnostics = {
"num_samples": total_size,
"num_duplicate_groups": len(duplicate_groups),
"num_grouped_components": len(groups),
"cross_split_duplicate_groups": cross_split_duplicate_groups,
"duplicate_groups": duplicate_group_summaries,
"split_sizes": split_sizes,
"split_class_distribution": {
split: {class_names[idx]: count for idx, count in sorted(counts.items())}
for split, counts in split_class_counts.items()
},
"target_split_sizes": split_targets,
}
return split_indices, diagnostics
def build_grouped_folds(
samples: Sequence[Tuple[Path, int]],
class_names: Sequence[str],
num_folds: int = 5,
seed: int = 42,
duplicate_threshold: int = 10,
) -> Tuple[List[List[int]], Dict]:
"""
Build duplicate-aware cross-validation folds.
Returns:
folds: list of sample-index lists, one per fold
diagnostics: serializable fold summary
"""
if num_folds < 2:
raise ValueError("num_folds must be at least 2")
num_samples = len(samples)
uf = UnionFind(num_samples)
path_to_index = {str(path): idx for idx, (path, _) in enumerate(samples)}
duplicate_groups = find_duplicates(
str(Path(samples[0][0]).parents[1]),
similarity_threshold=duplicate_threshold,
) if samples else []
for group in duplicate_groups:
indices = [path_to_index[str(path)] for path in group if str(path) in path_to_index]
for idx in indices[1:]:
uf.union(indices[0], idx)
grouped_indices: Dict[int, List[int]] = defaultdict(list)
for idx in range(num_samples):
grouped_indices[uf.find(idx)].append(idx)
total_class_counts = Counter(label for _, label in samples)
target_fold_size = num_samples / num_folds
class_targets = {
class_idx: total_class_counts[class_idx] / num_folds
for class_idx in total_class_counts
}
groups = []
for root, indices in grouped_indices.items():
label_counts = Counter(samples[idx][1] for idx in indices)
groups.append({
"root": root,
"indices": sorted(indices),
"size": len(indices),
"label_counts": label_counts,
})
rng = Random(seed)
rng.shuffle(groups)
groups.sort(
key=lambda g: (
-g["size"],
-max(g["label_counts"].values()),
tuple(sorted(g["label_counts"].items())),
)
)
folds: List[List[int]] = [[] for _ in range(num_folds)]
fold_sizes = [0] * num_folds
fold_class_counts = [Counter() for _ in range(num_folds)]
def score_fold(fold_idx: int, group: Dict):
size_after = fold_sizes[fold_idx] + group["size"]
class_error = 0.0
class_overshoot = 0.0
for class_idx, total_count in total_class_counts.items():
after = fold_class_counts[fold_idx][class_idx] + group["label_counts"].get(class_idx, 0)
target = class_targets[class_idx]
class_error += abs(after - target)
class_overshoot += max(0.0, after - target)
size_error = abs(size_after - target_fold_size)
size_overshoot = max(0.0, size_after - target_fold_size)
return (
class_overshoot,
class_error,
size_overshoot,
size_error,
fold_sizes[fold_idx],
fold_idx,
)
for group_idx, group in enumerate(groups):
remaining_groups = len(groups) - group_idx
empty_folds = [idx for idx in range(num_folds) if fold_sizes[idx] == 0]
if empty_folds and remaining_groups == len(empty_folds):
best_fold = empty_folds[0]
else:
best_fold = min(range(num_folds), key=lambda idx: score_fold(idx, group))
folds[best_fold].extend(group["indices"])
fold_sizes[best_fold] += group["size"]
fold_class_counts[best_fold].update(group["label_counts"])
folds = [sorted(indices) for indices in folds]
index_to_fold = {}
for fold_idx, indices in enumerate(folds):
for idx in indices:
index_to_fold[idx] = fold_idx
duplicate_group_summaries = []
cross_fold_duplicate_groups = 0
for group in duplicate_groups:
members = []
fold_presence = set()
for path in group:
idx = path_to_index.get(str(path))
if idx is None:
continue
fold_idx = index_to_fold[idx]
fold_presence.add(fold_idx)
members.append({
"path": str(path),
"class_name": class_names[samples[idx][1]],
"fold": fold_idx,
})
if len(fold_presence) > 1:
cross_fold_duplicate_groups += 1
duplicate_group_summaries.append(members)
diagnostics = {
"num_samples": num_samples,
"num_folds": num_folds,
"num_duplicate_groups": len(duplicate_groups),
"num_grouped_components": len(groups),
"cross_fold_duplicate_groups": cross_fold_duplicate_groups,
"duplicate_groups": duplicate_group_summaries,
"fold_sizes": fold_sizes,
"fold_class_distribution": [
{class_names[idx]: count for idx, count in sorted(counts.items())}
for counts in fold_class_counts
],
}
return folds, diagnostics