I am working on Convolutional Neural Network model in keras for 'cats and dogs' photos classification. The code works fine when I am using 'flow_from_directory' as shown below:
Image_generator_object=ImageDataGenerator(rescale=1.0/255.0)
Image_train=Image_generator_object.flow_from_directory(directory=train_folder,batch_size=64,
target_size=(200,200),class_mode='binary')
# Found 8000 images belonging to 2 classes.
Image_test=Image_generator_object.flow_from_directory(directory=test_folder,batch_size=64,
target_size=(200,200),class_mode='binary')
# Found 4000 images belonging to 2 classes.
#the model is given below
def base_model():
model=Sequential()
# Stage 1
model.add(Conv2D(filters=32 , kernel_size=(3,3),input_shape=(200,200,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=None,padding='valid'))
model.add(Flatten())
model.add(Dense(units=50,activation='relu'))
model.add(Dense(units=1,activation='sigmoid'))
model.compile(optimizer=Adam(learning_rate=0.001),loss='binary_crossentropy',metrics=['accuracy'])
return model
model=base_model()
# finally fitting the model
history=model.fit(Image_train , steps_per_epoch=len(data_gen_testing_flow), batch_size=100,verbose=1,validation_data=Image_test,epochs=10)
All the code works fine but when I try to use flow_from_directory option as shown below. Then I get the following error.
FileNotFoundError: [Errno 2] No such file or directory: 'cat.10469.jpg'
Here is what I did when I am using the "flow_from_data_frame" option. I have the training and testing dataframes (trainn, testt) showing the files names and classes as shown below(as an example):
files class
124 cat.10566.jpg cat
374 cat.10795.jpg cat
260 cat.10674.jpg cat
386 dog.9447.jpg dog
472 cat.10878.jpg cat
Then I created the Image Data generator object and used from dataframe and it found the image file names as shown below
data_gen_object=ImageDataGenerator( rescale=1.0/255.0,validation_split=0.25)
data_gen_object_training_flow=data_gen_object.flow_from_dataframe(trainn,diretory=base_folder,subset='training',x_col='files', y_col='class',
target_size=(200,200), batch_size=64,class_mode='binary',validate_filenames=False)
data_gen_object_testing_flow=data_gen_object.flow_from_dataframe(trainn,diretory=base_folder,subset='validation',x_col='files',
y_col='class',target_size=(200,200), batch_size=64,class_mode='binary',validate_filenames=False)
I get the following result:
Found 563 non-validated image filenames belonging to 2 classes.
Found 187 non-validated image filenames belonging to 2 classes.
It should be noted that the above code doesn't work when I am using validate_filenames=True.
After that when I fit the model I get the no files found error as shown below:
model.fit(data_gen_object_training_flow ,batch_size=64,verbose=1,
validation_data=data_gen_object_testing_flow,epochs=3)
# FileNotFoundError: [Errno 2] No such file or directory: 'cat.10469.jpg'