How to create DataBunch object if training files are in the same folder with filenames as labels?

I want to create a DataBunch object from training files which are all in the same folder, and the labels are contained in the filenames. How do I do it?

Here’s the file structure.

root
  |
  |--train
        |
        |--label1_0001.jpg
        |--label1_0002.jpg
        |--label2_0001.jpg
        |...
  |--test

ImageDataLoaders.from_name_func or ImageDataLoaders.from_name_re could be what you are looking for.

That doesn’t solve my problem.

I have files in one folder, but I want only the labels to be from filenames.