Search code examples
tensorflowkerasdeep-learningconv-neural-networkloadimage

How to load images for classification problem using Keras


I am working on image classification problem using Keras framework. This is binary classification problem and I have 2 folders training set and test set which contains images of both the classes. I don't have separate folder for each class (say cat vs. dog). Keras ImageDataGenerator works when we have separate folders for each class (cat folder & dog folder). But I have all the images in single folder training set and I am not understanding how to proceed further. Kindly suggest how to load the images.

I also have 2 CSV files - train.csv and test.csv. train.csv contain 2 columns namely image_id and class_name. test.csv contains image_id. Note that image_id is matching with the name of files in the images folders.


Solution

  • The latest versions of ImageDataGenerator have a method called flow_from_dataframe, which does exactly what you want.

    Basically it is used by first loading your CSV file in a pandas DataFrame, instantiate a ImageDataGenerator and then call flow_from_dataframe with three important parameters:

    • directory: Folder where your data lives.
    • x_col: Column in the DataFrame that contains the filenames inside the directory that correspond to your training/testing data.
    • y_col: Column in the DataFrame corresponding to the labels that will be output by the generator.

    Then you use this generator as any other, by calling fit_generator. More information and examples are available here.