13
$\begingroup$

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?

$\endgroup$
3
  • $\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, 2019 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, 2019 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, 2019 at 22:16

2 Answers 2

4
$\begingroup$

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: 
        model = load_model(weights_path, compile=True)

    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
$\endgroup$
2
  • $\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, 2019 at 14:41
  • $\begingroup$ Is "Generator" here a built in class or user defined? $\endgroup$
    – user836026
    Feb 21, 2022 at 1:55
2
$\begingroup$

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.

$\endgroup$

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