Fix imagefolder metadata with split-named files
Summary
- Preserve folder-builder metadata when auto-inferred split filename patterns match only a file such as
train.png - Re-resolve default
**patterns only for auto-inferred folder datasets when sidecar metadata would otherwise be dropped - Add regression coverage for explicit
imagefolder, local auto-detection, and explicit single-filedata_files
Fixes #7201
Tests
python -m pytest tests/packaged_modules/test_imagefolder.py::test_load_dataset_with_metadata_and_split_named_image_file -qpython -m pytest tests/packaged_modules/test_imagefolder.py -q --basetemp=C:\tmp\hfds-imagefolder-testspython -m pytest tests/test_data_files.py::test_get_data_files_patterns tests/test_data_files.py::test_get_data_patterns_from_directory_with_the_word_data_twice -qruff format --check src/datasets/load.py tests/packaged_modules/test_imagefolder.pyruff check src/datasets/load.py tests/packaged_modules/test_imagefolder.pygit diff --check
Hi ! Maybe this improvement can be in get_data_patterns() directly ? The split-based detection could simply ignore files that are media files (images/audio/videos) and that have a split name in their file name. This way DEFAULT_PATTERNS_SPLIT_IN_FILENAME would return no files, and DEFAULT_PATTERNS_ALL would be used. WDYT ?
Thanks for the suggestion. I reworked the PR in 7737bfa so the behavior is handled in get_data_patterns() instead of in the loader factories.
The split-in-filename detection now ignores exact split-named media files such as train.png, valid.mp3, and test.mp4, so that case falls through to DEFAULT_PATTERNS_ALL and metadata remains available. Explicit data_files= is still left untouched, covered by the imagefolder regression.
Checks run:
.\.venv\Scripts\python.exe -m pytest tests/test_data_files.py::test_get_data_files_patterns_ignores_split_named_media_files -q.\.venv\Scripts\python.exe -m pytest tests/packaged_modules/test_imagefolder.py::test_load_dataset_with_metadata_and_split_named_image_file -q.\.venv\Scripts\python.exe -m pytest tests/test_data_files.py::test_get_data_files_patterns tests/test_data_files.py::test_get_data_patterns_from_directory_with_the_word_data_twice -q.\.venv\Scripts\python.exe -m pytest tests/packaged_modules/test_imagefolder.py -q --basetemp=C:\tmp\hfds-imagefolder-tests.\.venv\Scripts\ruff.exe format --check src/datasets/data_files.py src/datasets/load.py tests/test_data_files.py tests/packaged_modules/test_imagefolder.py.\.venv\Scripts\ruff.exe check src/datasets/data_files.py src/datasets/load.py tests/test_data_files.py tests/packaged_modules/test_imagefolder.pygit diff --check
Thanks @lhoestq — good suggestion.
I'll explore moving the split-based detection into get_data_patterns() so the logic lives closer to pattern discovery rather than only at the ImageFolder layer. I'll push a follow-up commit shortly.