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,

image_size = (224, 224)
batch = 32

train_generator = datagen.flow_from_directory(train_data,
                                              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?

  • $\begingroup$ Any progress with this issue? I faced with this problem. It seems that it obvious approach if you want use KFold for huge dataset. $\endgroup$ – Oktay Feb 22 '19 at 16:17
  • $\begingroup$ No, I have not find a solution. The images dataset is not big, so I wanted to use cross validation. $\endgroup$ – Noran Feb 24 '19 at 7:24
  • $\begingroup$ 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? $\endgroup$ – 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)

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("====== K Fold Validation step => %d/%d =======" % (i,k_folds))

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

        model = load_model(weights_path, compile=True)

    except Exception as OSError:

|improve this answer|||||
  • $\begingroup$ 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. $\endgroup$ – 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.

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.