ImageDataGenerator for semantic segmentation

蹲街弑〆低调 提交于 2019-12-11 06:05:55

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/58050113/imagedatagenerator-for-semantic-segmentation

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