Keras TensorFlow Image Data cannot be found

I’m trying to train my model to read some x-ray Images, I’m using Jupyter Notebook, I imported the Libraries, Defined the image properties, Prepared the dataset, Created the neural net model, Defined callbacks… and Managed the Data but now I’m stuck on this error trying to train the model.

train_datagen = ImageDataGenerator(rotation_range=15,
                                    rescale=1./255,
                                    shear_range=0.1,
                                    zoom_range=0.2,
                                    horizontal_flip=True,
                                    width_shift_range=0.1,
                                    height_shift_range=0.1
                                    )
    train_generator = train_datagen.flow_from_dataframe(train_df,
                                                     "C:/Users/lenovo/PneumoniaClassification/chest_xray/train",x_col='filename',y_col='category',
                                                     target_size=Image_Size,
                                                     class_mode='categorical',
                                                     batch_size=batch_size)
    validation_datagen = ImageDataGenerator(rescale=1./255)
    validation_generator = validation_datagen.flow_from_dataframe(
        validate_df, 
        "C:/Users/lenovo/PneumoniaClassification/chest_xray/train", 
        x_col='filename',
        y_col='category',
        target_size=Image_Size,
        class_mode='categorical',
        batch_size=batch_size
    )
    test_datagen = ImageDataGenerator(rotation_range=15,
                                    rescale=1./255,
                                    shear_range=0.1,
                                    zoom_range=0.2,
                                    horizontal_flip=True,
                                    width_shift_range=0.1,
                                    height_shift_range=0.1)
    test_generator = train_datagen.flow_from_dataframe(train_df,
                                                     "C:/Users/lenovo/PneumoniaClassification/chest_xray/test",x_col='filename',y_col='category',
                                                     target_size=Image_Size,
                                                     class_mode='categorical',
                                                     batch_size=batch_size)

and here’s the error code

Found 0 validated image filenames belonging to 0 classes.
Found 0 validated image filenames belonging to 0 classes.
Found 0 validated image filenames belonging to 0 classes.
C:\Users\lenovo\AppData\Roaming\Python\Python39\site-packages\keras_preprocessing\image\dataframe_iterator.py:279: UserWarning: Found 2 invalid image filename(s) in x_col="filename". These filename(s) will be ignored.
  warnings.warn(
C:\Users\lenovo\AppData\Roaming\Python\Python39\site-packages\keras_preprocessing\image\dataframe_iterator.py:279: UserWarning: Found 1 invalid image filename(s) in x_col="filename". These filename(s) will be ignored.
  warnings.warn(
C:\Users\lenovo\AppData\Roaming\Python\Python39\site-packages\keras_preprocessing\image\dataframe_iterator.py:279: UserWarning: Found 2 invalid image filename(s) in x_col="filename". These filename(s) will be ignored.
  warnings.warn(

here’s the paths :

>Solution :

In the train folder you have two folders NORMAL and PNEUMONIA ?
If so then you need to use flow_from_directory instead of flow_from_dataframe:

base_dir = "C:/Users/lenovo/PneumoniaClassification/chest_xray"
train_dir = os.path.join(base_dir, 'train')
test_dir = os.path.join(base_dir, 'test')
validation_dir = os.path.join(base_dir, 'val')


train_datagen = ImageDataGenerator(rotation_range=15,
                                    rescale=1./255,
                                    shear_range=0.1,
                                    zoom_range=0.2,
                                    horizontal_flip=True,
                                    width_shift_range=0.1,
                                    height_shift_range=0.1
                                    )
train_generator = train_datagen.flow_from_directory(
        train_dir,  # This is the source directory for training images
        target_size=Image_Size,  # All images will be resized 
        batch_size=BATCH_SIZE,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

validation_datagen = ImageDataGenerator(rescale=1./255)

validation_generator = test_datagen.flow_from_directory(
        validation_dir,
        target_size=Image_Size,
        batch_size=BATCH_SIZE,
        class_mode='binary')

test_datagen = ImageDataGenerator(rotation_range=15,
                                    rescale=1./255,
                                    shear_range=0.1,
                                    zoom_range=0.2,
                                    horizontal_flip=True,
                                    width_shift_range=0.1,
                                    height_shift_range=0.1)

test_generator = test_datagen.flow_from_directory(
        test_dir,
        target_size=Image_Size,
        batch_size=BATCH_SIZE,
        class_mode='binary')

Leave a Reply