huggingface / datasets

🤗 The largest hub of ready-to-use datasets for ML models with fast, easy-to-use and efficient data manipulation tools
https://huggingface.co/docs/datasets
Apache License 2.0
18.95k stars 2.62k forks source link

Multi-image loading in Imagefolder dataset #5760

Open vvvm23 opened 1 year ago

vvvm23 commented 1 year ago

Feature request

Extend the imagefolder dataloading script to support loading multiple images per dataset entry.

This only really makes sense if a metadata file is present.

Currently you can use the following format (example metadata.jsonl:

{'file_name': 'path_to_image.png', 'metadata': ...}
...

which will return a batch with key image and any other metadata.

I would propose extending file_name to also accept a list of files, which would return a batch with key images and any other metadata.

Motivation

This is useful for example in segmentation tasks in computer vision models, or in text-to-image models that also accept conditioning signals such as another image, feature map, or similar. Currently if I want to do this, I would need to write a custom dataset, rather than just use imagefolder.

Your contribution

Would be open to doing a PR, but also happy for someone else to take it as I am not familiar with the datasets library.

mariosasko commented 1 year ago

Supporting this could be useful (I remember a use-case for this on the Hub). Do you agree @polinaeterna?

Implementing this should be possible if we iterate over metadata files and build image/audio file paths instead of iterating over image/audio files and looking for the corresponding entries in metadata files.

dennisrall commented 1 year ago

I've build a similar feature from scratch and would be interested to combine it with the datasets package.

My solution works something like this: Interpret the first element of each column as a file path. If the path exists and is a file, (try to) load the files for the entire column. Thereby, one isn't restricted to a particular column name, with comes in handy when dealing with multiple file columns.

I've looked into the code to try to implement this, but didn't find the right places. I'm also open to contribute, but will need some guidance.

severo commented 9 months ago

Required here: https://discuss.huggingface.co/t/dataset-repo-requires-arbitrary-python-code-execution/59346/14

whydna commented 5 months ago

+1

Is the only way to do this right now to write a custom dataset loader script?

severo commented 4 months ago

Also: be able to have input and output images for each row. Asked here: https://discuss.huggingface.co/t/how-to-structure-image-files-for-datasets-load-dataset-imagefolder-when-you-have-input-and-output-images-like-in-instruct-pix2pix/82467