Commit ·
15cd760
1
Parent(s): efa4eb1
pp-doclayout: skip corrupt images instead of crashing the run
Browse filesPreviously, a single corrupt image (UnidentifiedImageError or OSError)
in the source bucket or dataset would terminate the entire run via an
unhandled exception in the gen() iterator. On a 5000-image run, hitting
a truncated/unreadable file at sample 2400 wasted ~1 hour of GPU time.
Now both iter_bucket_images() and iter_dataset_images() catch
UnidentifiedImageError and OSError per image, log a WARNING with the
path, and continue. A summary "Skipped N unreadable image(s)" is logged
when the iterator exhausts. Run completes with whatever images were
readable.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- pp-doclayout.py +30 -6
pp-doclayout.py
CHANGED
|
@@ -84,7 +84,7 @@ from pathlib import Path
|
|
| 84 |
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
|
| 85 |
|
| 86 |
import numpy as np
|
| 87 |
-
from PIL import Image
|
| 88 |
from tqdm.auto import tqdm
|
| 89 |
|
| 90 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -234,13 +234,25 @@ def iter_dataset_images(
|
|
| 234 |
total = len(ds)
|
| 235 |
|
| 236 |
def gen() -> Iterator[SourceItem]:
|
|
|
|
| 237 |
for i in range(total):
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
yield SourceItem(
|
| 240 |
key=f"row-{i:08d}",
|
| 241 |
-
image=
|
| 242 |
extras={}, # original schema is preserved by the sink via the dataset ref
|
| 243 |
)
|
|
|
|
|
|
|
| 244 |
|
| 245 |
return gen(), total, ds
|
| 246 |
|
|
@@ -365,14 +377,26 @@ def iter_bucket_images(
|
|
| 365 |
return path
|
| 366 |
|
| 367 |
def gen() -> Iterator[SourceItem]:
|
|
|
|
| 368 |
for path in all_paths:
|
| 369 |
-
|
| 370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
yield SourceItem(
|
| 372 |
key=key_for(path),
|
| 373 |
-
image=
|
| 374 |
extras={"__source_path": key_for(path)},
|
| 375 |
)
|
|
|
|
|
|
|
| 376 |
|
| 377 |
return gen(), total
|
| 378 |
|
|
|
|
| 84 |
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
|
| 85 |
|
| 86 |
import numpy as np
|
| 87 |
+
from PIL import Image, UnidentifiedImageError
|
| 88 |
from tqdm.auto import tqdm
|
| 89 |
|
| 90 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 234 |
total = len(ds)
|
| 235 |
|
| 236 |
def gen() -> Iterator[SourceItem]:
|
| 237 |
+
skipped = 0
|
| 238 |
for i in range(total):
|
| 239 |
+
try:
|
| 240 |
+
row = ds[i]
|
| 241 |
+
image = to_pil(row[image_column])
|
| 242 |
+
except (UnidentifiedImageError, OSError) as e:
|
| 243 |
+
skipped += 1
|
| 244 |
+
logger.warning(
|
| 245 |
+
f"Skipping unreadable image at row {i}: "
|
| 246 |
+
f"{type(e).__name__}: {e}"
|
| 247 |
+
)
|
| 248 |
+
continue
|
| 249 |
yield SourceItem(
|
| 250 |
key=f"row-{i:08d}",
|
| 251 |
+
image=image,
|
| 252 |
extras={}, # original schema is preserved by the sink via the dataset ref
|
| 253 |
)
|
| 254 |
+
if skipped:
|
| 255 |
+
logger.info(f"Skipped {skipped} unreadable image(s) total")
|
| 256 |
|
| 257 |
return gen(), total, ds
|
| 258 |
|
|
|
|
| 377 |
return path
|
| 378 |
|
| 379 |
def gen() -> Iterator[SourceItem]:
|
| 380 |
+
skipped = 0
|
| 381 |
for path in all_paths:
|
| 382 |
+
try:
|
| 383 |
+
with fs.open(path, "rb") as f:
|
| 384 |
+
data = f.read()
|
| 385 |
+
image = to_pil(data)
|
| 386 |
+
except (UnidentifiedImageError, OSError) as e:
|
| 387 |
+
skipped += 1
|
| 388 |
+
logger.warning(
|
| 389 |
+
f"Skipping unreadable image {path}: "
|
| 390 |
+
f"{type(e).__name__}: {e}"
|
| 391 |
+
)
|
| 392 |
+
continue
|
| 393 |
yield SourceItem(
|
| 394 |
key=key_for(path),
|
| 395 |
+
image=image,
|
| 396 |
extras={"__source_path": key_for(path)},
|
| 397 |
)
|
| 398 |
+
if skipped:
|
| 399 |
+
logger.info(f"Skipped {skipped} unreadable image(s) total")
|
| 400 |
|
| 401 |
return gen(), total
|
| 402 |
|