0

[Resolved]

I have an issue when using flow_from_directory function in tensorflow module tensorflow.keras.preprocessing.image

I can load all my data for training my model, but I can't load my data to validate the training...

My code (edit) [WORKING !] :

# Import
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, models
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_data_dir = '../data/train'
validation_data_dir = '../data/validation'

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    #validation_split=0.20
    )

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)

def create_dataset(train_data_dir, validation_data_dir, img_height, img_width, batch_size):

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        shuffle=True,
        class_mode='categorical',
        subset = 'training',
        )

    validation_generator = train_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        shuffle=True,
        class_mode='categorical',
        subset = 'validation',
        )

    return train_generator, validation_generator

My dataset is in a function that I call on my CNN model and when I run it, it always return this :

Found 0 images belonging to 6 classes.
Epoch 1/5Found 5732 images belonging to 6 classes.
Found 0 images belonging to 6 classes.
Epoch 1/5

EDIT : This is the part in my model.py file where I call the function create_dataset :

image_height = 100
image_width = 100
batch_size = 32

ds_train, ds_validation = create_dataset("../data/train", "../data/validation", image_height, image_width, batch_size)

C N N ...

model.fit(ds_train, epochs=5, verbose=2)
model.evaluate(ds_validation, verbose=2)
model.save('../complete_saved_model/')

Can someone help me to resolve this ?

julgi
  • 11
  • 1
  • 3

1 Answers1

0

in your train_generator remove the code subset = 'training'. Likewise in your validation_generator remove the code subset = 'validation'.

Gerry P
  • 7,662
  • 3
  • 10
  • 20
  • Hello Gerry thanks for your answer, but the subset doesn't change anything ! I have found the solution two days ago, my mistake was the utilisation of test_datagen and not train_datagen in the validation step ... – julgi Oct 06 '21 at 07:25