pytorch-image-models icon indicating copy to clipboard operation
pytorch-image-models copied to clipboard

[BUG] inference.py expects labeled subfolders in the --data-dir folder

Open JosuaRieder opened this issue 1 year ago • 2 comments

Describe the bug When running inference.py --data-dir data --class-map class_map.txt --model efficientnet_b7 --num-classes 8 --checkpoint output/model_best.pth.tar where data is a directory that contains images and no subfolders, the script crashes with the following output:

Traceback (most recent call last):
  File "/home/josua/redacted/pytorch-image-models/inference.py", line 369, in <module>
    main()
  File "/home/josua/redacted/pytorch-image-models/inference.py", line 224, in main
    dataset = create_dataset(
              ^^^^^^^^^^^^^^^
  File "/home/josua/redacted/pytorch-image-models/timm/data/dataset_factory.py", line 216, in create_dataset
    ds = ImageDataset(
         ^^^^^^^^^^^^^
  File "/home/josua/redacted/pytorch-image-models/timm/data/dataset.py", line 36, in __init__
    reader = create_reader(
             ^^^^^^^^^^^^^^
  File "/home/josua/redacted/pytorch-image-models/timm/data/readers/reader_factory.py", line 44, in create_reader
    reader = ReaderImageFolder(root, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/josua/redacted/pytorch-image-models/timm/data/readers/reader_image_folder.py", line 82, in __init__
    raise RuntimeError(
RuntimeError: Found 0 images in subfolders of /home/josua/redacted/data/unlabeled. Supported image extensions are .png, .jpg, .jpeg

Expected behavior Instead of raising an error, I expect it to perform the inference on the (recursive) contents of data irrespective of the existence of labeled subfolders because labels are not necessary for inference.

Desktop (please complete the following information):

  • OS: x86_64 Linux 6.11.5-arch1-1
  • This repository version: d4dde48d
  • PyTorch version w/ CUDA/cuDNN: python-pytorch-cuda 2.3.1-8, cuda 12.6.2-2

Additional context Workaround: It works when, instead of placing the images in data/ directly, one places them in data/random_label/ where random_label is a random label, i.e. a random line from class_map.txt

JosuaRieder avatar Nov 04 '24 22:11 JosuaRieder

@JosuaRieder it works just fine, the issue is using the class map filters by folder label structure to match behaviour where subsets of imagenet are remapped using class map. Class map isn't really intended to provide labels, it's remapping/filtering transform.

rwightman avatar Nov 05 '24 07:11 rwightman

It seems I have misunderstood the intended purpose of class map (as I wrote in the PR).

Even so, I think it's unexpected that inference.py requires a different --data-dir structure with and without class map. I don't see the purpose of having/requiring this subfolder structure when inferring.

JosuaRieder avatar Nov 17 '24 13:11 JosuaRieder