Getting " ValueError: Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)` " while

Viewed 106

I am trying to train a cnn on the images stored in a folder named "Health".

train_data_dir = "Health"
train_datagen = ImageDataGenerator(rescale = 1./256,
                                   rotation_range = 360,
                                   width_shift_range=0.1,
                                   height_shift_range=0.1,
                                   zoom_range=10,
                                   horizontal_flip=True,
                                   vertical_flip=True,
                                   data_format ="channels_last",validation_split=0.2) 

train_generator = train_datagen.flow_from_directory(train_data_dir,
                                                    target_size =(img_width, img_height),
                                                    batch_size = batch_size,
                                                    class_mode = None,
                                                    subset='training') 

validation_generator = train_datagen.flow_from_directory(train_data_dir,
                                                        target_size =(img_width, img_height),
                                                        batch_size = batch_size,
                                                        class_mode =None,
                                                        subset='validation')
history = model.fit_generator(generator=train_generator,
                             steps_per_epoch=train_generator.samples//batch_size,
                             epochs=10,
                             validation_data=validation_generator,
                             validation_steps=validation_generator.samples//batch_size)

I am getting the following error:

ValueError: Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`. Found: [[[[0.         0.         0.        ] 

What am I doing wrong?

1 Answers

Is your directory Health organized as stated in the documentation?

directory: string, path to the target directory. It should contain one subdirectory per class. Any PNG, JPG, BMP, PPM or TIF images inside each of the subdirectories directory tree will be included in the generator.

Why do you pass class_mode = None? None is intended for inference mode, i.e. prediction, no labels are returned by the generator. But you like to train the model, right?

Link to documentation and an example:

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

https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d

Related