datasets icon indicating copy to clipboard operation
datasets copied to clipboard

Fix imagefolder metadata with split-named files

Open KirtiRamchandani opened this issue 6 months ago • 3 comments

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-file data_files

Fixes #7201

Tests

  • python -m pytest tests/packaged_modules/test_imagefolder.py::test_load_dataset_with_metadata_and_split_named_image_file -q
  • python -m pytest tests/packaged_modules/test_imagefolder.py -q --basetemp=C:\tmp\hfds-imagefolder-tests
  • python -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
  • ruff format --check src/datasets/load.py tests/packaged_modules/test_imagefolder.py
  • ruff check src/datasets/load.py tests/packaged_modules/test_imagefolder.py
  • git diff --check

KirtiRamchandani avatar Jun 05 '26 05:06 KirtiRamchandani

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 ?

lhoestq avatar Jun 05 '26 12:06 lhoestq

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.py
  • git diff --check

KirtiRamchandani avatar Jun 11 '26 11:06 KirtiRamchandani

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.

KirtiRamchandani avatar Jun 13 '26 17:06 KirtiRamchandani