3

I was attempting to use my modification of the example code in the Keras documentation that shows how to set up image_datagen.flow_from_directory() in the case where image masks are being used in place of labels (for image segmentation, where we are predicting a class for each pixel).

By the way, I set featurewise_center = True in an attempt to subtract the mean of each color channel of all the training images from each image's color channels, so that over the entire training set, each color channel mean would be 0. I expect this isn't the way to accomplish this.

Anyway, here's my code that generated the error:

image_datagen = ImageDataGenerator(featurewise_center = True)
mask_datagen = ImageDataGenerator()

image_generator = image_datagen.flow_from_directory(
    '/home/icg/Martin/train_data_graz/images_rect_r640x360',
    class_mode = None,
    batch_size = 1,
    seed = 123)

mask_generator = mask_datagen.flow_from_directory(
    '/home/icg/Martin/train_data_graz/labels_rect_r640x360',
    class_mode = None,
    batch_size = 1,
    seed = 123)

# combine generators into one which yields image and masks
train_generator = zip(image_generator, mask_generator)

model.fit_generator(
    train_generator,
    steps_per_epoch = 1000,
    epochs = 100)

And here's the error message:

Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
Traceback (most recent call last):
  File "FCN_VGG16.py", line 178, in <module>
    train_generator = zip(image_generator, mask_generator)
  File "/home/icg/rafa/local/lib/python2.7/site-packages/keras/preprocessing/image.py", line 1026, in next
    index_array, current_index, current_batch_size = next(self.index_generator)
  File "/home/icg/rafa/local/lib/python2.7/site-packages/keras/preprocessing/image.py", line 720, in _flow_index
    current_index = (self.batch_index * batch_size) % n
ZeroDivisionError: integer division or modulo by zero

For some reason n = 0. Any ideas why this might happen?

Rafael_Espericueta
  • 495
  • 1
  • 6
  • 14

1 Answers1

5

You need to put your images into subfolders per each class into the directory for your flow_from_directory() function.

In your case:

/home/icg/Martin/train_data_graz/images_rect_r640x360/images_class01
/home/icg/Martin/train_data_graz/images_rect_r640x360/images_class02
…

Edit:

Since you have set class_mode to None and do semantic segmentation (see comments & post):

/home/icg/Martin/train_data_graz/images_rect_r640x360/all_images
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 4
    I understand that process, the way one ordinarily does classification. However, with semantic segmentation, one has a number of classes ALL of which can occur in any image. So it's not so simple as having a folder for cats and another for dogs. One needs to classify each pixel as belonging to one or another class. To do this, one uses masks in place of labels. For each image in the training set, one has a mask that specifies what category each pixel belongs to - if it's a pixel that's part of a car, or a cat, or whatever categories one is classifying. – Rafael_Espericueta Aug 05 '17 at 06:47
  • 5
    I see. But still – you need to place all your images in a subdirectory of the directory that you point your `flow_from_directory` to. This also applies to `class_mode = None`. Have you tried that? – petezurich Aug 05 '17 at 08:33
  • 1
    Aha, that did the trick. I had put the path to the folder containing the images themselves, rather than the path to the folder containing a FOLDER containing the images. Thanks! :-) – Rafael_Espericueta Aug 05 '17 at 10:21
  • You´re welcome. And great – I am happy that you could fix this. – petezurich Aug 05 '17 at 11:34