-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerifyData.py
More file actions
467 lines (339 loc) · 15.2 KB
/
Copy pathVerifyData.py
File metadata and controls
467 lines (339 loc) · 15.2 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import os
import sys
import csv
import json
from PIL import Image
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from collections import defaultdict
from multiprocessing import Pool, cpu_count
from functools import partial
BASE = "./indoor-images-data/open-images-v7"
OUTBASE = "./dataset_64k"
SPLITS = ["train", "validation", "test"]
print(f"Configuration loaded:", flush=True)
print(f" BASE: {BASE}", flush=True)
print(f" OUTBASE: {OUTBASE}", flush=True)
print(f" SPLITS: {SPLITS}", flush=True)
sys.stdout.flush()
NUM_WORKERS = int(os.environ.get('SLURM_CPUS_PER_TASK', cpu_count()))
print(f"Using {NUM_WORKERS} CPU cores for parallel processing", flush=True)
sys.stdout.flush()
def find_file(subdir, keyname):
"""Search for the first file matching keyname in subdir."""
for root, dirs, files in os.walk(subdir):
for fname in files:
if keyname.lower() in fname.lower():
return os.path.join(root, fname)
raise RuntimeError(f"Could not find file with '{keyname}' in {subdir}")
def check_image_file(args):
"Check a single image file - must be top-level for multiprocessing"
img_info, outdir = args
filename = img_info['file_name']
img_path = os.path.join(outdir, filename)
result = {
'filename': filename,
'exists': False,
'dimensions_match': False,
'error': None
}
if not os.path.exists(img_path):
result['error'] = "File not found"
return result
result['exists'] = True
try:
with Image.open(img_path) as img:
img.load()
actual_width, actual_height = img.size
if img_info['width'] == actual_width and img_info['height'] == actual_height:
result['dimensions_match'] = True
else:
result['error'] = f"Dimension mismatch: JSON=({img_info['width']}, {img_info['height']}), actual=({actual_width}, {actual_height})"
except Exception as e:
result['error'] = f"Cannot open: {str(e)}"
return result
def verify_single_image(args):
"""
Verify a single image's annotations match between original and COCO.
This function is designed for parallel execution.
"""
(coco_img, original_annotations, coco_annotations_by_image,
coco_category_id_to_name) = args
coco_image_id = coco_img['id']
filename = coco_img['file_name']
original_image_id = filename.split('.')[0]
result = {
'filename': filename,
'coco_image_id': coco_image_id,
'verified': False,
'errors': [],
'warnings': [],
'annotation_count': 0
}
if original_image_id not in original_annotations:
result['errors'].append(f"Image {filename} not found in original annotations")
return result
original_anns = original_annotations[original_image_id]
coco_anns = coco_annotations_by_image.get(coco_image_id, [])
if len(original_anns) != len(coco_anns):
result['warnings'].append(
f"Annotation count mismatch: Original={len(original_anns)}, COCO={len(coco_anns)}"
)
img_width = coco_img['width']
img_height = coco_img['height']
matches = 0
for coco_ann in coco_anns:
x_min, y_min, width, height = coco_ann['bbox']
x_max = x_min + width
y_max = y_min + height
norm_x_min = x_min / img_width
norm_x_max = x_max / img_width
norm_y_min = y_min / img_height
norm_y_max = y_max / img_height
category_name = coco_category_id_to_name[coco_ann['category_id']]
found_match = False
tolerance = 0.001
for orig_ann in original_anns:
if orig_ann['ClassName'] != category_name:
continue
if (abs(norm_x_min - orig_ann['XMin']) < tolerance and
abs(norm_x_max - orig_ann['XMax']) < tolerance and
abs(norm_y_min - orig_ann['YMin']) < tolerance and
abs(norm_y_max - orig_ann['YMax']) < tolerance):
found_match = True
matches += 1
break
if not found_match:
result['errors'].append(
f"No match for {category_name} at bbox [{norm_x_min:.4f}, {norm_y_min:.4f}, {norm_x_max:.4f}, {norm_y_max:.4f}]"
)
result['annotation_count'] = matches
result['verified'] = (len(result['errors']) == 0)
return result
def verify_conversion(split):
"""
Verifies that the COCO conversion correctly matched annotations to images
by cross-referencing original Open Images data with converted COCO data.
OPTIMIZED: Only loads annotations for converted images.
"""
print(f"\n{'='*60}", flush=True)
print(f"Verifying {split} split conversion", flush=True)
print(f"{'='*60}", flush=True)
sys.stdout.flush()
rawdir = os.path.join(BASE, split)
outdir = os.path.join(OUTBASE, split)
coco_json_path = os.path.join(outdir, "_annotations.coco.json")
if not os.path.exists(coco_json_path):
print(f"⚠ Skipping {split}: COCO JSON not found at {coco_json_path}", flush=True)
sys.stdout.flush()
return False, 0, 0
print(f"\nSearching for required files...", flush=True)
sys.stdout.flush()
try:
labels_path = find_file(os.path.join(rawdir, "labels"), "detections")
classes_path = find_file(os.path.join(rawdir, "metadata"), "classes")
print(f" ✓ Found labels: {labels_path}", flush=True)
print(f" ✓ Found classes: {classes_path}", flush=True)
except Exception as e:
print(f" ✗ ERROR finding files: {e}", flush=True)
sys.stdout.flush()
return False, 0, 0
sys.stdout.flush()
print(f"\nLoading class mappings...", flush=True)
oi_labelname_to_classname = {}
with open(classes_path, "r") as f:
reader = csv.reader(f)
for row in reader:
oi_labelname_to_classname[row[0]] = row[1]
print(f" ✓ Loaded {len(oi_labelname_to_classname)} class mappings", flush=True)
sys.stdout.flush()
print(f"\nLoading COCO converted data...", flush=True)
sys.stdout.flush()
with open(coco_json_path, "r") as f:
coco_data = json.load(f)
print(f" ✓ Images: {len(coco_data['images'])}", flush=True)
print(f" ✓ Annotations: {len(coco_data['annotations'])}", flush=True)
print(f" ✓ Categories: {len(coco_data['categories'])}", flush=True)
sys.stdout.flush()
print(f"\nBuilding image ID filter...", flush=True)
coco_image_base_ids = set()
for img in coco_data['images']:
image_base_id = img['file_name'].split('.')[0]
coco_image_base_ids.add(image_base_id)
print(f" → Need annotations for {len(coco_image_base_ids)} images only", flush=True)
sys.stdout.flush()
print(f"\nLoading original annotations (filtered)...", flush=True)
original_annotations = defaultdict(list)
loaded_count = 0
skipped_count = 0
with open(labels_path, "r") as f:
reader = csv.DictReader(f)
for row in reader:
image_id = row["ImageID"]
if image_id not in coco_image_base_ids:
skipped_count += 1
continue
original_annotations[image_id].append({
"LabelName": row["LabelName"],
"ClassName": oi_labelname_to_classname.get(row["LabelName"], "Unknown"),
"XMin": float(row["XMin"]),
"XMax": float(row["XMax"]),
"YMin": float(row["YMin"]),
"YMax": float(row["YMax"]),
"IsGroupOf": row.get("IsGroupOf", "0")
})
loaded_count += 1
print(f" ✓ Loaded {loaded_count} annotations for {len(original_annotations)} images", flush=True)
print(f" ✓ Skipped {skipped_count} annotations not in COCO dataset", flush=True)
sys.stdout.flush()
coco_category_id_to_name = {cat['id']: cat['name'] for cat in coco_data['categories']}
coco_annotations_by_image = defaultdict(list)
for ann in coco_data['annotations']:
coco_annotations_by_image[ann['image_id']].append(ann)
print(f"\nCross-referencing annotations using {NUM_WORKERS} workers...", flush=True)
sys.stdout.flush()
verify_args = [
(img, original_annotations, coco_annotations_by_image, coco_category_id_to_name)
for img in coco_data['images']
]
print(f" → Processing {len(verify_args)} images in parallel...", flush=True)
sys.stdout.flush()
with Pool(processes=NUM_WORKERS) as pool:
results = pool.map(verify_single_image, verify_args)
print(f" ✓ Parallel processing complete", flush=True)
sys.stdout.flush()
print(f"\nAggregating results...", flush=True)
sys.stdout.flush()
verified_images = sum(1 for r in results if r['verified'])
verified_annotations = sum(r['annotation_count'] for r in results)
all_errors = []
all_warnings = []
for result in results:
if result['errors']:
for error in result['errors']:
all_errors.append(f"{result['filename']}: {error}")
if result['warnings']:
for warning in result['warnings']:
all_warnings.append(f"{result['filename']}: {warning}")
print(f"\n{'='*60}", flush=True)
print("VERIFICATION RESULTS", flush=True)
print(f"{'='*60}", flush=True)
print(f"\n✓ Verified {verified_images}/{len(coco_data['images'])} images", flush=True)
print(f"✓ Verified {verified_annotations} annotations", flush=True)
sys.stdout.flush()
all_passed = True
if all_errors:
all_passed = False
print(f"\n❌ {len(all_errors)} annotation errors found:", flush=True)
for msg in all_errors[:20]:
print(f" - {msg}", flush=True)
if len(all_errors) > 20:
print(f" ... and {len(all_errors) - 20} more", flush=True)
sys.stdout.flush()
if all_warnings:
print(f"\n⚠ {len(all_warnings)} warnings:", flush=True)
for msg in all_warnings[:20]:
print(f" - {msg}", flush=True)
if len(all_warnings) > 20:
print(f" ... and {len(all_warnings) - 20} more", flush=True)
sys.stdout.flush()
if all_passed:
print(f"\n✅ ALL CHECKS PASSED!", flush=True)
print(f"✅ Annotations are correctly matched to images!", flush=True)
sys.stdout.flush()
return all_passed, verified_images, verified_annotations
def verify_image_files_parallel(split):
"""
Verify all image files exist and dimensions match using parallel processing.
"""
print(f"\n{'='*60}", flush=True)
print(f"Verifying image files for {split} split", flush=True)
print(f"{'='*60}", flush=True)
sys.stdout.flush()
outdir = os.path.join(OUTBASE, split)
coco_json_path = os.path.join(outdir, "_annotations.coco.json")
if not os.path.exists(coco_json_path):
print(f"⚠ Skipping {split}: COCO JSON not found", flush=True)
sys.stdout.flush()
return True
with open(coco_json_path, "r") as f:
coco_data = json.load(f)
print(f"\nVerifying {len(coco_data['images'])} image files using {NUM_WORKERS} workers...", flush=True)
sys.stdout.flush()
check_args = [(img, outdir) for img in coco_data['images']]
with Pool(processes=NUM_WORKERS) as pool:
results = pool.map(check_image_file, check_args)
missing_files = [r for r in results if not r['exists']]
dimension_errors = [r for r in results if r['exists'] and not r['dimensions_match']]
corrupted_images = [r for r in results if r['error'] and 'truncated' in r['error'].lower()]
if missing_files:
print(f"\n {len(missing_files)} missing image files (showing first 10):", flush=True)
for r in missing_files[:10]:
print(f" - {r['filename']}: {r['error']}", flush=True)
sys.stdout.flush()
return False
if dimension_errors:
print(f"\n {len(dimension_errors)} dimension mismatches (showing first 10):", flush=True)
for r in dimension_errors[:10]:
print(f" - {r['filename']}: {r['error']}", flush=True)
sys.stdout.flush()
return False
if corrupted_images:
print(f"\n⚠ {len(corrupted_images)} corrupted/truncated images found (showing first 10):", flush=True)
for r in corrupted_images[:10]:
print(f" - {r['filename']}: {r['error']}", flush=True)
print(f"\n → Run the corrupted image cleaner script to remove these", flush=True)
sys.stdout.flush()
return False
print(f"\n All {len(results)} image files verified successfully", flush=True)
sys.stdout.flush()
return True
def generate_statistics(split):
"""Generate dataset statistics"""
print(f"\n{'='*60}", flush=True)
print(f"Generating statistics for {split} split", flush=True)
print(f"{'='*60}", flush=True)
sys.stdout.flush()
outdir = os.path.join(OUTBASE, split)
coco_json_path = os.path.join(outdir, "_annotations.coco.json")
if not os.path.exists(coco_json_path):
print(f"⚠ Skipping {split}: COCO JSON not found", flush=True)
return
with open(coco_json_path, "r") as f:
coco_data = json.load(f)
category_counts = defaultdict(int)
for ann in coco_data['annotations']:
category_counts[ann['category_id']] += 1
category_id_to_name = {cat['id']: cat['name'] for cat in coco_data['categories']}
print(f"\nDataset Statistics:", flush=True)
print(f" Total images: {len(coco_data['images'])}", flush=True)
print(f" Total annotations: {len(coco_data['annotations'])}", flush=True)
print(f" Average annotations per image: {len(coco_data['annotations']) / len(coco_data['images']):.2f}", flush=True)
print(f"\nAnnotations per category:", flush=True)
for cat_id, count in sorted(category_counts.items(), key=lambda x: x[1], reverse=True):
cat_name = category_id_to_name.get(cat_id, f"Unknown ({cat_id})")
print(f" {cat_name:25s}: {count:6d}", flush=True)
sys.stdout.flush()
if __name__ == "__main__":
sys.stdout.flush()
all_splits_passed = True
for split in SPLITS:
try:
print(f"\n\n{'#'*60}", flush=True)
print(f"# Processing {split.upper()} split", flush=True)
print(f"{'#'*60}", flush=True)
sys.stdout.flush()
files_ok = verify_image_files_parallel(split)
passed, num_images, num_annotations = verify_conversion(split)
generate_statistics(split)
all_splits_passed = all_splits_passed and passed and files_ok
except Exception as e:
print(f"\n❌ Error verifying {split}: {str(e)}", flush=True)
import traceback
traceback.print_exc()
sys.stdout.flush()
all_splits_passed = False
print("Verification complete!", flush=True)
sys.stdout.flush()