# K-fold cross validation when using fit_generator and flow_from_directory() in Keras

I am using flow_from_directory() and fit_generator in my deep learning model, and I want to use cross validation method to train the CNN model.

datagen = ImageDataGenerator(rotation_range=15,width_shift_range=0.2,
height_shift_range=0.2,shear_range=0.2,
zoom_range=0.2,horizontal_flip=True,
fill_mode='nearest')

image_size = (224, 224)
batch = 32

train_generator = datagen.flow_from_directory(train_data,
target_size=image_size,
batch_size=batch,
classes= classes_array)


I found this Youtube video and this Tutorial, But it is not use flow_from_directory().

Do you have any idea how do I use k-fold cross validation when using fit_generator and flow_from_directory() in Keras?

• Any progress with this issue? I faced with this problem. It seems that it obvious approach if you want use KFold for huge dataset. – Oktay Feb 22 '19 at 16:17
• No, I have not find a solution. The images dataset is not big, so I wanted to use cross validation. – Noran Feb 24 '19 at 7:24
• trainGenerator = Generator(trainData,trainLabels,batchSize=batchSize,imageSize=imageSize,augment=True,grayMode=grayMode) can you share the code where you define the Generator in the above line? – Rohan Mar 27 '19 at 22:16

So, I haven't found any solution regarding this application of cross-validation in fit_generator(), I hope it comes in one update of the Keras package, since cross-validation is an important part of training models.

What I have done so far, basically I split the dataset first then I pass the data and labels to the fit_generator. At the end of each step I save the model, at the beggining of each step I load the preciuos model to continue the training over the same model, but with a different k-fold of the dataset. I'm pasting the part of my code where I use this approach, hope it helps.

## Training with K-fold cross validation
kf = KFold(n_splits=k_folds, random_state=None, shuffle=True)
kf.get_n_splits(images_file_paths)

X = np.array(images_file_paths)
y = np.array(class_labels)

i = 1
for train_index, test_index in kf.split(X):
trainData = X[train_index]
testData = X[test_index]
trainLabels = y[train_index]
testLabels = y[test_index]

print("=========================================")
print("====== K Fold Validation step => %d/%d =======" % (i,k_folds))
print("=========================================")

trainGenerator = Generator(trainData,trainLabels,batchSize=batchSize,imageSize=imageSize,augment=True,grayMode=grayMode)
valGenerator = Generator(testData,testLabels,batchSize=batchSize,imageSize=imageSize,augment=False,grayMode=grayMode)

try:

except Exception as OSError:
pass

model.fit_generator(
trainGenerator,
steps_per_epoch=len(trainData),
epochs=epochs,
validation_data=valGenerator,
validation_steps=len(testData))
i+=1

• I have this same problem, but this is a non-solution in the sense, that you don't need a generator here. Consider that validation_data = (testData, testLabels) replaces the generator. – boomkin Mar 25 '19 at 14:41

Instead of a generator, try using skimage or keras’s preprocess input to convert the images to arrays beforehand. The generator usually does this for you, but if you code it yourself it’s only a few lines and you can follow the tutorials you posted.