tensorflowkerassubdirectorytensorflow-datasetsloaddata

How to load data in tensorflow from subdirectories


I have a subset of ImageNet data contained in sub-folders locally, where each sub-folder represents a class of images. There are potentially hundreds of classes, and therefore sub-folders, and each subfolder can contain hundreds of images. Here is an example of this structure with a subset of folders. I want to train a classification model in tensorflow, but I am not sure how to format and load the data given this structure of different image classes in different folders and the class label being the name of the folder. Normally I've just used datasets that already exist in tensorflow like mnist or cifar10 which are formatted and easy to use.

enter image description here


Solution

  • You can use tf.keras.preprocessing.image_dataset_from_directory().

    Your directory structure would be something like this but with many more classes:

    main_directory/
    ...class_a/
    ......a_image_1.jpg
    ......a_image_2.jpg
    ...class_b/
    ......b_image_1.jpg
    ......b_image_2.jpg
    

    I would suggest you split the dataset before this step as I think the data is split here randomly and not by stratified sampling(if your datasets are imbalanced then do this first and do not use the validation split to do it for you as I am not sure of the nature of how splitting is done as there is no mention of it).

    Example:

    train_dataset = image_dataset_from_directory(
        directory=TRAIN_DIR,
        labels="inferred",
        label_mode="categorical",
        class_names=["0", "10", "5"],
        image_size=SIZE,
        seed=SEED,
        subset=None,
        interpolation="bilinear",
        follow_links=False,
    )
    

    Important things you have to set:

    1. Labels must be inferred where the labels of the images are generated based on the directory structure so it follows the order of the classes.

    2. Label mode has to be set to "categorical" which encodes the labels as a categorical vector.

    3. Class names you can set this yourself where you would have to list the order of the folders in the directory otherwise the order is based on alphanumeric ordering. What you can do here as you have lots of folders is use os.walk(directory) to get the list of the directories in the order that they are.

    4. Image size you can resize the images to be of the same size. Do so according to the model that you are using i.e., MobileNet takes in (224,224) so you can set this to (224,224).

    More information here.