I would recommend to create a custom Dataset and maybe just reuse some parts of the ImageFolder dataset from here.
Changing this attribute after the ImageFolder was created seems to be wrong, since class_to_idx will be used to create the dataset as seen here.
@Nikki
you can create custom CustomImageFolder loader class and override find_classes function like this:
from typing import Tuple, List, Dict
from torchvision.datasets import ImageFolder
def classes_to_idx() -> dict:
return load_json_file("class_indexes.json")
class CustomImageFolder(ImageFolder):
def find_classes(self, directory: str) -> Tuple[List[str], Dict[str, int]]:
"""
Override this method to load from setting file instead of scanning directory
"""
classes = list(classes_to_idx().keys())
classes_to_idx = classes_to_idx()
return classes, classes_to_idx
Here is sample class_indexes.json json file mapping class to index (ā1ā, ā2ā, ā¦ā13ā is class folder name; 0, 1, ā¦, 12 is index of class):