Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras split train test set when using ImageDataGenerator

I have a single directory which contains sub-folders (according to labels) of images. I want to split this data into train and test set while using ImageDataGenerator in Keras. Although model.fit() in keras has argument validation_split for specifying the split, I could not find the same for model.fit_generator(). How to do it ?

train_datagen = ImageDataGenerator(rescale=1./255,     shear_range=0.2,     zoom_range=0.2,     horizontal_flip=True)  train_generator = train_datagen.flow_from_directory(     train_data_dir,     target_size=(img_width, img_height),     batch_size=32,     class_mode='binary')  model.fit_generator(     train_generator,     samples_per_epoch=nb_train_samples,     nb_epoch=nb_epoch,     validation_data=??,     nb_val_samples=nb_validation_samples) 

I don't have separate directory for validation data, need to split it from the training data

like image 229
Nitin Avatar asked Feb 24 '17 16:02

Nitin


1 Answers

Keras has now added Train / validation split from a single directory using ImageDataGenerator:

train_datagen = ImageDataGenerator(rescale=1./255,     shear_range=0.2,     zoom_range=0.2,     horizontal_flip=True,     validation_split=0.2) # set validation split  train_generator = train_datagen.flow_from_directory(     train_data_dir,     target_size=(img_height, img_width),     batch_size=batch_size,     class_mode='binary',     subset='training') # set as training data  validation_generator = train_datagen.flow_from_directory(     train_data_dir, # same directory as training data     target_size=(img_height, img_width),     batch_size=batch_size,     class_mode='binary',     subset='validation') # set as validation data  model.fit_generator(     train_generator,     steps_per_epoch = train_generator.samples // batch_size,     validation_data = validation_generator,      validation_steps = validation_generator.samples // batch_size,     epochs = nb_epochs) 

https://keras.io/preprocessing/image/

like image 161
ezChx Avatar answered Nov 09 '22 00:11

ezChx