-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore_splits.py
More file actions
415 lines (341 loc) Β· 16.7 KB
/
explore_splits.py
File metadata and controls
415 lines (341 loc) Β· 16.7 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/env python3
"""
Example: Loading splits with comprehensive metadata per sample.
This script demonstrates how to load splits and display
detailed metadata for each sample including concept information, supercategories,
and attribute-specific details.
"""
import json
import csv
import numpy as np
import pandas as pd
from pathlib import Path
from collections import defaultdict
from typing import Dict, List, Set, Tuple, Any
def load_supercategories(file_path: str = "data/things/Categories_final_20200131.tsv") -> Dict[str, Set[str]]:
"""Load supercategory mappings from the THINGS dataset."""
supercategories = defaultdict(set)
missing = 0
with open(file_path, "r") as f:
reader = csv.reader(f, delimiter="\t")
header = next(reader)
supercat_set = header[2:] # Skip word and definition columns
for row in reader:
word = row[0]
# Find all supercategories this concept belongs to (all "1" values)
concept_supercats = []
for i, value in enumerate(row[2:]):
if value == "1":
concept_supercats.append(supercat_set[i])
if concept_supercats:
# Add concept to all its supercategories
for supercat in concept_supercats:
supercategories[supercat].add(word)
else:
# No supercategory assigned (no "1" values)
missing += 1
continue
print(f"β Loaded supercategories: {len(supercategories)} categories, {missing} missing assignments")
return dict(supercategories)
def load_concept_metadata(file_path: str = "data/things/concepts-and-categories.json") -> Dict[str, Dict[str, str]]:
"""Load concept metadata including categories and definitions."""
with open(file_path, "r") as f:
data = json.load(f)
metadata = {}
for item in data:
metadata[item["concept"]] = {
"category": item["category"],
"definition": item["definition"],
"id": item["id"]
}
print(f"β Loaded metadata for {len(metadata)} concepts")
return metadata
def load_splits(split_file: str = "splits/k_means_split.json") -> Dict[str, Any]:
"""Load split data from JSON file."""
with open(split_file, "r") as f:
return json.load(f)
def load_image_paths(file_path: str = "data/all_filepath.txt") -> List[str]:
"""Load image file paths."""
with open(file_path, "r") as f:
return [line.strip() for line in f if line.strip()]
def load_labels(file_path: str = "data/labels_images.txt") -> List[int]:
"""Load image labels."""
with open(file_path, "r") as f:
return [int(line.strip()) for line in f if line.strip()]
def extract_concept_name_from_path(image_file: str) -> str:
"""Extract concept name from image file path."""
# Image files are in format like "aardvark_01b", "abacus_03s", etc.
# Extract the concept name (part before the underscore)
if "_" in image_file:
return image_file.split("_")[0]
return image_file
def load_feature_to_concepts() -> Dict[str, List[str]]:
"""Load feature to concepts mapping from McRae norms."""
import json
with open('data/mcrae-norms-grouped-with-concepts.json', 'r') as f:
data = json.load(f)
feature_to_concepts = {}
for norm in data:
feature = norm['norm']
concepts = norm.get('concepts-mcrae', [])
feature_to_concepts[feature] = concepts
return feature_to_concepts
def concept_has_attribute(concept_name: str, attribute: str, feature_to_concepts: Dict[str, List[str]]) -> bool:
"""Check if a concept has a specific attribute."""
if attribute not in feature_to_concepts:
return False
return concept_name in feature_to_concepts[attribute]
def get_sample_metadata(concept_idx: int, image_paths: List[str], labels: List[int],
concept_metadata: Dict, supercategories: Dict[str, Set[str]],
concept_to_supercat: Dict[str, str], feature_to_concepts: Dict[str, List[str]] = None,
current_attribute: str = None) -> Dict[str, Any]:
"""Get comprehensive metadata for a concept (concept_idx represents concept ID, not sample index)."""
# Find the first sample that belongs to this concept
sample_idx = None
for i, label in enumerate(labels):
if label == concept_idx:
sample_idx = i
break
if sample_idx is None:
return {"error": f"No samples found for concept ID {concept_idx}"}
if sample_idx >= len(image_paths) or sample_idx >= len(labels):
return {"error": f"Sample index {sample_idx} out of range"}
image_file = image_paths[sample_idx]
label = labels[sample_idx]
# Extract concept name from image path
concept_name = extract_concept_name_from_path(image_file)
# Get concept metadata
concept_info = concept_metadata.get(concept_name, {})
# Get supercategory
supercategory = concept_to_supercat.get(concept_name, "Unknown")
# Check if concept has the current attribute
has_attribute = None
if feature_to_concepts and current_attribute:
has_attribute = concept_has_attribute(concept_name, current_attribute, feature_to_concepts)
return {
"concept_idx": concept_idx,
"sample_idx": sample_idx,
"concept_name": concept_name,
"label": label,
"image_file": image_file,
"category": concept_info.get("category", "Unknown"),
"definition": concept_info.get("definition", "No definition available"),
"supercategory": supercategory,
"concept_id": concept_info.get("id", "Unknown"),
"has_attribute": has_attribute
}
def print_sample_metadata(metadata: Dict[str, Any], attribute_name: str, split_type: str):
"""Print formatted metadata for a sample."""
if "error" in metadata:
print(f" ERROR: {metadata['error']}")
return
print(f" π Concept {metadata['concept_idx']} (Sample {metadata['sample_idx']}, {split_type}):")
print(f" π·οΈ Concept: {metadata['concept_name']}")
print(f" π’ Concept label: {metadata['label']}")
print(f" π Category: {metadata['category']}")
print(f" ποΈ Supercategory: {metadata['supercategory']}")
print(f" π Definition: {metadata['definition'][:80]}...")
print(f" πΌοΈ Image: {metadata['image_file']}")
print(f" π― Attribute: {attribute_name}")
if metadata.get('has_attribute') is not None:
has_attr = "β
YES" if metadata['has_attribute'] else "β NO"
print(f" π Has {attribute_name}: {has_attr}")
print()
def analyze_kmeans_attribute_splits(split_data: Dict[str, Any], attribute_name: str,
image_paths: List[str], labels: List[int],
concept_metadata: Dict, supercategories: Dict[str, Set[str]],
concept_to_supercat: Dict[str, str], feature_to_concepts: Dict[str, List[str]],
max_samples: int = 5):
"""Analyze splits for a specific attribute with detailed metadata."""
print(f"\n{'='*80}")
print(f"π― {attribute_name}")
print(f"{'='*80}")
# Find the attribute in the splits
attribute_idx = None
for i, attribute in enumerate(split_data.get("attributes", [])):
if attribute == attribute_name:
attribute_idx = i
break
if attribute_idx is None:
print(f"β Attribute '{attribute_name}' not found in this split")
return
# Get the split for this attribute
splits = split_data["splits"][attribute_idx]
train_indices = splits[0]
test_indices = splits[1]
# Count positive and negative samples
train_positive = 0
train_negative = 0
test_positive = 0
test_negative = 0
for concept_idx in train_indices:
metadata = get_sample_metadata(concept_idx, image_paths, labels, concept_metadata,
supercategories, concept_to_supercat, feature_to_concepts, attribute_name)
if metadata.get('has_attribute') is True:
train_positive += 1
elif metadata.get('has_attribute') is False:
train_negative += 1
for concept_idx in test_indices:
metadata = get_sample_metadata(concept_idx, image_paths, labels, concept_metadata,
supercategories, concept_to_supercat, feature_to_concepts, attribute_name)
if metadata.get('has_attribute') is True:
test_positive += 1
elif metadata.get('has_attribute') is False:
test_negative += 1
print(f"π Split Statistics:")
train_ratio = len(train_indices) / (len(train_indices) + len(test_indices)) * 100
print(f" π Train samples: {len(train_indices)} (Train ratio: {train_ratio:.2f}%)")
print(f" β
Positive: {train_positive} | β Negative: {train_negative}")
print(f" π§ͺ Test samples: {len(test_indices)}")
print(f" β
Positive: {test_positive} | β Negative: {test_negative}")
print(f" π Total samples: {len(train_indices) + len(test_indices)}")
print(f" β
Total Positive: {train_positive + test_positive} | β Total Negative: {train_negative + test_negative}")
# Show train samples with metadata (both positive and negative)
print(f"\nπ TRAIN SAMPLES (showing first {min(max_samples, len(train_indices))}):")
print("-" * 60)
positive_samples = []
negative_samples = []
for concept_idx in train_indices:
metadata = get_sample_metadata(concept_idx, image_paths, labels, concept_metadata,
supercategories, concept_to_supercat, feature_to_concepts, attribute_name)
if metadata.get('has_attribute') is True:
positive_samples.append(metadata)
elif metadata.get('has_attribute') is False:
negative_samples.append(metadata)
# Show positive samples first, then negative
samples_shown = 0
for metadata in positive_samples[:max_samples//2]:
print_sample_metadata(metadata, attribute_name, "TRAIN")
samples_shown += 1
for metadata in negative_samples[:max_samples//2]:
if samples_shown >= max_samples:
break
print_sample_metadata(metadata, attribute_name, "TRAIN")
samples_shown += 1
# Show test samples with metadata (both positive and negative)
print(f"\nπ§ͺ TEST SAMPLES (showing first {min(max_samples, len(test_indices))}):")
print("-" * 60)
positive_samples = []
negative_samples = []
for concept_idx in test_indices:
metadata = get_sample_metadata(concept_idx, image_paths, labels, concept_metadata,
supercategories, concept_to_supercat, feature_to_concepts, attribute_name)
if metadata.get('has_attribute') is True:
positive_samples.append(metadata)
elif metadata.get('has_attribute') is False:
negative_samples.append(metadata)
# Show positive samples first, then negative
samples_shown = 0
for metadata in positive_samples[:max_samples//2]:
print_sample_metadata(metadata, attribute_name, "TEST")
samples_shown += 1
for metadata in negative_samples[:max_samples//2]:
if samples_shown >= max_samples:
break
print_sample_metadata(metadata, attribute_name, "TEST")
samples_shown += 1
# Analyze supercategory distribution
print(f"\nπ SUPERCATEGORY DISTRIBUTION:")
print("-" * 60)
# Count supercategories in train and test
train_supercats = defaultdict(int)
test_supercats = defaultdict(int)
for idx in train_indices:
if idx < len(image_paths):
concept_name = extract_concept_name_from_path(image_paths[idx])
supercat = concept_to_supercat.get(concept_name, "Unknown")
train_supercats[supercat] += 1
for idx in test_indices:
if idx < len(image_paths):
concept_name = extract_concept_name_from_path(image_paths[idx])
supercat = concept_to_supercat.get(concept_name, "Unknown")
test_supercats[supercat] += 1
# Display distribution
all_supercats = set(train_supercats.keys()) | set(test_supercats.keys())
print(f"{'Supercategory':<25} {'Train':<8} {'Test':<8} {'Total':<8} {'Train%':<8}")
print("-" * 65)
for supercat in sorted(all_supercats):
train_count = train_supercats.get(supercat, 0)
test_count = test_supercats.get(supercat, 0)
total = train_count + test_count
train_pct = (train_count / total * 100) if total > 0 else 0
print(f"{supercat:<25} {train_count:<8} {test_count:<8} {total:<8} {train_pct:<7.1f}%")
def main():
"""Main function to demonstrate split loading with metadata."""
print("π― SPLIT WITH METADATA DEMONSTRATION")
print("=" * 80)
# Load metadata
print("\nπ Loading metadata...")
concept_metadata = load_concept_metadata()
supercategories = load_supercategories()
feature_to_concepts = load_feature_to_concepts()
# Create concept to supercategory mapping (assign largest supercategory)
concept_to_supercat = {}
# Get all unique concepts across all supercategories
all_concepts = set().union(*supercategories.values())
for concept in all_concepts:
largest_supercat = None
largest_size = 0
# Find all supercategories this concept belongs to and pick the largest
for supercat, concepts in supercategories.items():
if concept in concepts and len(concepts) > largest_size:
largest_supercat = supercat
largest_size = len(concepts)
concept_to_supercat[concept] = largest_supercat
# Load image paths and labels
print("\nπΌοΈ Loading image data...")
image_paths = load_image_paths()
labels = load_labels()
print(f"β Loaded {len(image_paths)} image paths")
print(f"β Loaded {len(labels)} labels")
# Load available attributes
with open("splits/intersect_attributes.json", "r") as f:
available_attributes = json.load(f)
print(f"β Available attributes: {len(available_attributes)}")
# Show available attributes count
print(f"\nπ AVAILABLE ATTRIBUTES: {len(available_attributes)}")
# Find all split files in the splits folder
splits_folder = Path("splits")
split_files = list(splits_folder.glob("*.json"))
split_files = [f for f in split_files if f.name != "intersect_attributes.json"] # Exclude the attributes list
print(f"\nπ Found {len(split_files)} split files to analyze:")
for split_file in split_files:
print(f" - {split_file.name}")
# Analyze specific attributes
attributes_to_analyze = ["has_wheels"]
# Iterate through each split file
for split_file in split_files:
print(f"\n{'='*80}")
print(f"π ANALYZING SPLIT FILE: {split_file.name}")
print(f"{'='*80}")
# Load splits data
splits_data = load_splits(str(split_file))
print(f"β Loaded splits for {len(splits_data.get('attributes', []))} attributes")
print(f"\nπ Analyzing {split_file.stem} splits for {len(attributes_to_analyze)} attributes...")
for attribute_name in attributes_to_analyze:
if attribute_name in available_attributes:
analyze_kmeans_attribute_splits(
splits_data, attribute_name, image_paths, labels,
concept_metadata, supercategories, concept_to_supercat, feature_to_concepts,
max_samples=3
)
else:
print(f"β οΈ Attribute '{attribute_name}' not available in {split_file.stem} splits")
break
# Summary statistics
print(f"\nπ SUMMARY STATISTICS")
print("=" * 80)
print(f"π Total samples in dataset: {len(image_paths)}")
print(f"π Total concepts with metadata: {len(concept_metadata)}")
print(f"π Total supercategories: {len(supercategories)}")
print(f"π Total split files analyzed: {len(split_files)}")
# Show top supercategories
supercat_counts = {name: len(concepts) for name, concepts in supercategories.items()}
sorted_supercats = sorted(supercat_counts.items(), key=lambda x: x[1], reverse=True)
print(f"\nπ Top 5 supercategories by concept count:")
for i, (supercat, count) in enumerate(sorted_supercats[:5], 1):
print(f" {i}. {supercat}: {count} concepts")
print(f"\nβ
Split analysis completed successfully!")
print("=" * 80)
if __name__ == "__main__":
main()