K-fold cross validation when using fit_generator and flow_from_directory() in Keras2019 Community Moderator ElectionHow to represent ROC curve when using Cross-Validationwhy k-fold cross validation (CV) overfits? Or why discrepancy occurs between CV and test set?Cross-validation of a cross-validated stacking ensemble?k-fold cross-validation: model selection or variation in models when using k-fold cross validationCross Validation how to determine when to Early Stop?how to prepare data for cross validation in mnist dataset?Using K-fold cross-validation in Keras on the data of my modelWhat is the best way to visualize 10 Fold Cross Validation Scores?Cross validation for convolutional neural networkFeature Scaling and normalization in cross-validation set

Is it unprofessional to ask if a job posting on GlassDoor is real?

What are the differences between the usage of 'it' and 'they'?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

Is this a crack on the carbon frame?

Font hinting is lost in Chrome-like browsers (for some languages )

The use of multiple foreign keys on same column in SQL Server

What is the offset in a seaplane's hull?

Why doesn't H₄O²⁺ exist?

Collect Fourier series terms

How is it possible to have an ability score that is less than 3?

Why, historically, did Gödel think CH was false?

What would happen to a modern skyscraper if it rains micro blackholes?

Adding span tags within wp_list_pages list items

can i play a electric guitar through a bass amp?

Is it legal for company to use my work email to pretend I still work there?

Why was the small council so happy for Tyrion to become the Master of Coin?

What typically incentivizes a professor to change jobs to a lower ranking university?

"to be prejudice towards/against someone" vs "to be prejudiced against/towards someone"

How can I make my BBEG immortal short of making them a Lich or Vampire?

Today is the Center

How can bays and straits be determined in a procedurally generated map?

Why does Kotter return in Welcome Back Kotter?

How does one intimidate enemies without having the capacity for violence?

Why are electrically insulating heatsinks so rare? Is it just cost?



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



2019 Community Moderator ElectionHow to represent ROC curve when using Cross-Validationwhy k-fold cross validation (CV) overfits? Or why discrepancy occurs between CV and test set?Cross-validation of a cross-validated stacking ensemble?k-fold cross-validation: model selection or variation in models when using k-fold cross validationCross Validation how to determine when to Early Stop?how to prepare data for cross validation in mnist dataset?Using K-fold cross-validation in Keras on the data of my modelWhat is the best way to visualize 10 Fold Cross Validation Scores?Cross validation for convolutional neural networkFeature Scaling and normalization in cross-validation set










5












$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?










share|improve this question











$endgroup$











  • $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 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 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 at 22:16















5












$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?










share|improve this question











$endgroup$











  • $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 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 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 at 22:16













5












5








5





$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?










share|improve this question











$endgroup$




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?







python deep-learning keras tensorflow cross-validation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 16 '18 at 16:14









user140323

5331520




5331520










asked Aug 16 '18 at 8:59









NoranNoran

32011




32011











  • $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 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 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 at 22:16
















  • $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 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 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 at 22:16















$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 at 16:17




$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 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 at 7:24




$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 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 at 22:16




$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 at 22:16










1 Answer
1






active

oldest

votes


















0












$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





share|improve this answer











$endgroup$












  • $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 at 14:41











Your Answer





StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "557"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f37009%2fk-fold-cross-validation-when-using-fit-generator-and-flow-from-directory-in-ke%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0












$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





share|improve this answer











$endgroup$












  • $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 at 14:41















0












$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





share|improve this answer











$endgroup$












  • $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 at 14:41













0












0








0





$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





share|improve this answer











$endgroup$



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






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 11 at 14:12

























answered Mar 11 at 13:59









MBoarettoMBoaretto

11




11











  • $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 at 14:41
















  • $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 at 14:41















$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 at 14:41




$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 at 14:41

















draft saved

draft discarded
















































Thanks for contributing an answer to Data Science Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f37009%2fk-fold-cross-validation-when-using-fit-generator-and-flow-from-directory-in-ke%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?