ImageDataGenerator for semantic segmentation
Asked Answered
H

1

10

I am trying to do semantic segmentation with Keras and when trying to load the images i get this error using flow_from_directory method.

Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.

This is my code.

from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator

data_generator = ImageDataGenerator()
train_generator = data_generator.flow_from_directory(
                                        directory="../input/Training_dataset/Images",
                                        target_size=(IMG_SIZE, IMG_SIZE),
                                        batch_size=16,
                                        class_mode=None,
                                        classes=None
                                        )

mask_generator = data_generator.flow_from_directory(
    directory="../input/Training_dataset/Masks/all",
    class_mode=None,
    classes=None,
    batch_size = 1,
    )

I have read this question but solution didnt work Keras for semantic segmentation, flow_from_directory() error

Hangdog answered 22/9, 2019 at 14:23 Comment(0)
R
24

you need to keep your images inside one sub-folder like create a folder named "img" inside both image and mask directory.

-- image
   -- img
      -- 1.jpg
      -- 2.jpg
-- mask
   -- img
      -- 1.png
      -- 2.png

Datagenerator should be like:-

seed = 909 # (IMPORTANT) to transform image and corresponding mask with same augmentation parameter.
image_datagen = ImageDataGenerator(width_shift_range=0.1,
                 height_shift_range=0.1,
                 preprocessing_function = image_preprocessing) # custom fuction for each image you can use resnet one too.
mask_datagen = ImageDataGenerator(width_shift_range=0.1,
                 height_shift_range=0.1,
                 preprocessing_function = mask_preprocessing)  # to make mask as feedable formate (256,256,1)

image_generator =image_datagen.flow_from_directory("dataset/image/",
                                                    class_mode=None, seed=seed)

mask_generator = mask_datagen.flow_from_directory("dataset/mask/",
                                                   class_mode=None, seed=seed)

train_generator = zip(image_generator, mask_generator)

if you want to make your own custom data generator for semantic segmentation models to get better control over dataset, you can check my kaggle kernel where i have used camvid dataset to train UNET model.

https://www.kaggle.com/mukulkr/camvid-segmentation-using-unet

If you need better augmentation fuction you can check this awesome GitHub repo - https://github.com/mdbloice/Augmentor

Rind answered 22/9, 2019 at 15:5 Comment(8)
That worked with the images, but when trying to fit the model i get error AttributeError: 'zip' object has no attribute 'shape' Thanks anyways ill check your kernelHangdog
I am not getting any AttributeError can tell exactly where you are getting this error.Rind
after trying to do this train_generator = zip(image_generator, mask_generator) model.fit_generator(train_generator, steps_per_epoch=167,epochs=10)Hangdog
I have same dir structure. But my image name is "img (1).png" while mask name is "img (1)_mask.png" ... would that work?Soloist
Nope, it should be the same as the original image name.Rind
after running the line 'train_generator = zip(image_generator, mask_generator) ', memory just fills up entirely, even if working with just 4 images. What is going on in that line?Archeology
Also, shouldn't shuffle be set to False? (Default is True) Because when we zip the two generators, won't it create a mismatch between images and masks? EDIT -> No. As per documentation --> seed is "Optional random seed for shuffling and transformations." so that should take care of this.Coff
Could you explain this solution like why you used Zip and the same folder name for both directories?Frication

© 2022 - 2024 — McMap. All rights reserved.