Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageDataGenerator for semantic segmentation

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

like image 751
Jaime Cuellar Avatar asked Sep 22 '19 14:09

Jaime Cuellar


People also ask

What is ImageDataGenerator used for?

Introduction to Keras ImageDataGenerator. Keras ImageDataGenerator is used for getting the input of the original data and further, it makes the transformation of this data on a random basis and gives the output resultant containing only the data that is newly transformed. It does not add the data.

What is ImageDataGenerator Flow_from_directory?

The flow_from_directory() method takes a path of a directory and generates batches of augmented data. The directory structure is very important when you are using flow_from_directory() method. The flow_from_directory() assumes: The root directory contains at least two folders one for train and one for the test.

How many images does ImageDataGenerator generate?

Then the "ImageDataGenerator" will produce 10 images in each iteration of the training. An iteration is defined as steps per epoch i.e. the total number of samples / batch_size. In above case, in each epoch of training there will be 100 iterations.

What is shear in ImageDataGenerator?

'Shear' means that the image will be distorted along an axis, mostly to create or rectify the perception angles. It's usually used to augment images so that computers can see how humans see things from different angles.


1 Answers

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

like image 139
Mukul Avatar answered Sep 28 '22 21:09

Mukul