Keras for semantic segmentation, flow_from_directory() error

我怕爱的太早我们不能终老 提交于 2020-03-03 01:59:22

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/45510403/keras-for-semantic-segmentation-flow-from-directory-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!