Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/prep/api/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datasets import get_dataset_config_names, get_dataset_split_names
from rich import print as rprint

from ..constants import HF_PREFIX, PREVIEW_PREFIX, SAVE_PREFIX, SKIP, WARN_PREFIX
from ..constants import HF_PREFIX, SAVE_PREFIX, SKIP
from .formatutils import iter_images, write_to_file
from .log import get_logger
from .pathio import PathIO
Expand Down Expand Up @@ -140,16 +140,18 @@ def do_preview(
):
try:
row = d[i]
rprint(f"[bold]Sample {i}[/bold]:", row)

for j, img in enumerate(iter_images(row)):
path = write_to_file(
img,
save_root / f"{i:06d}_{j:02d}",
)
rprint(f"{PREVIEW_PREFIX}Preview image saved: {path}")
image_paths = [
str(write_to_file(img, save_root / f"{i:06d}_{j:02d}"))
for j, img in enumerate(iter_images(row))
]

rprint(
f"[bold]Sample {i}[/bold]:"
+ (f" (images {image_paths})" if image_paths else ""),
row,
)
except Exception as exc:
logger.warning(f"{WARN_PREFIX}Failed to preview {i}th sample: {exc}")
logger.warning(f"Failed to preview {i}th sample: {exc}")
continue

def do_save(
Expand Down
26 changes: 15 additions & 11 deletions src/prep/api/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from datasets import ClassLabel, Features, Image, List, Value

from ..constants import ERROR_PREFIX, ID_PATTERN, IMAGE_TAG, WARN_PREFIX
from ..constants import ID_PATTERN, IMAGE_TAG
from .log import get_logger
from .types import (
DataFormat,
Expand Down Expand Up @@ -91,7 +91,7 @@ def get(
return _FORMATTER_REGISTRY[("_", "show", split)]
k = (id_, target_format, split)
if k not in _FORMATTER_REGISTRY:
logger.warning(f"{WARN_PREFIX}Formatter pipeline not registered for {k}. ")
logger.warning(f"Formatter pipeline not registered for {k}. ")
logger.warning(
"Fallback to generic pipeline 'auto',"
" which may cause unexpected formatting issues.",
Expand Down Expand Up @@ -213,7 +213,11 @@ def cast_dataset(
case "eval":
return FormatterPipeline.cast_eval(d, args)
case "cls":
return FormatterPipeline.cast_cls(d, args)
d = FormatterPipeline.cast_cls(d, args)
from collections import Counter

print("label", Counter(d["label"]))
return d
case "show":
return d
case _:
Expand All @@ -230,20 +234,20 @@ def load(self, override_src: str | None, args: ProcArgs) -> "Dataset":
try:
d = self.cast_dataset(d, self.target_format, args)
except Warning as e:
logger.warning(f"{WARN_PREFIX}Casting warning {str(self)}")
logger.warning(f"{WARN_PREFIX}{e}")
logger.warning(f"Casting warning {str(self)}")
logger.warning(f"{e}")
except Exception as e:
logger.error(f"{ERROR_PREFIX}Casting failed {str(self)}")
logger.error(f"{ERROR_PREFIX}{e}")
logger.error(f"Casting failed {str(self)}")
logger.error(f"{e}")
try:
for i in range(10):
self.check_sample(d[i])
except Warning as e:
logger.warning(f"{WARN_PREFIX}Validation warning {str(self)}")
logger.warning(f"{WARN_PREFIX}{e}")
logger.warning(f"Validation warning {str(self)}")
logger.warning(f"{e}")
except Exception as e:
logger.error(f"{ERROR_PREFIX}Validation failed {str(self)}")
logger.error(f"{ERROR_PREFIX}{e}")
logger.error(f"Validation failed {str(self)}")
logger.error(f"{e}")

return d

Expand Down
5 changes: 5 additions & 0 deletions src/prep/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def is_env_enabled(env_var: str, default: str = "0") -> bool:
FORMATTING_PATTERN = re.compile(r"ANSWER:|<answer>|</answer>|\\boxed\{")
ID_PATTERN = re.compile(r"^[a-zA-Z0-9\._-]+$")
DEFAULT_UI = None if is_env_enabled("UI", "0") else False
# interactive mode off -> disable progress bars
if DEFAULT_UI is not None:
from datasets import disable_progress_bars

disable_progress_bars()

SAVE_PREFIX = "💾\t"
HF_PREFIX = "☁️\t"
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/test_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from prep.cli import app

EXPECTED = """\
WARNING ⚠️ Formatter pipeline not registered for ('auto', 'clip', 'train').
WARNING Formatter pipeline not registered for ('auto', 'clip', 'train').
WARNING Fallback to generic pipeline 'auto', which may cause unexpected formatting issues."""


Expand Down
13 changes: 9 additions & 4 deletions tests/cli/test_cls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
from prep.cli import app

EXPECTED = """\
WARNING ⚠️ Formatter pipeline not registered for ('cifar10', 'cls', 'train').
WARNING Formatter pipeline not registered for ('cifar10', 'cls', 'train').
WARNING Fallback to generic pipeline 'auto', which may cause unexpected formatting issues.
label Counter({0: 5000, 6: 5000, 2: 5000, 7: 5000, 1: 5000, 4: 5000, 5: 5000, 3: 5000, 8: 5000, 9: 5000})
Dataset({
features: ['id', 'image', 'label', 'extra_info'],
num_rows: 50000
})
Sample 0:
{'id': 'cifar10/train00000000', 'image': <PIL.PngImagePlugin.PngImageFile image mode=RGB size=32x32 at
*>, 'label': 0, 'extra_info': ''}
Sample 0: (images ['out/cls/cifar10/train/000000_00.png'])
{
'id': 'cifar10/train00000000',
'image': <PIL.PngImagePlugin.PngImageFile image mode=RGB size=32x32 at*>,
'label': 0,
'extra_info': ''
}
💾\tAbout to save to disk -> PosixPath('out/cls/cifar10/train')...
💾\tNot saved (pass `--save` to override or `export UI=1` to enable interactive prompts)
☁️\tAbout to upload to HF -> 'cifar10' (subset='default', split='train', private=True)...
Expand Down