本文介绍了ImageDataGenerator用于语义分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Keras进行语义分割,当尝试加载图像时,我会使用flow_from_directory方法遇到此错误.

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.

这是我的代码.

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,
    )

我已阅读此问题,但解决方案无效用于语义分割的Keras ,flow_from_directory()错误

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

推荐答案

您需要将图像保留在一个子文件夹中,例如在图像目录和蒙版目录中创建一个名为"img"的文件夹.

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应该类似于:-

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)

如果您想为语义分割模型创建自己的自定义数据生成器以更好地控制数据集,则可以检查我的kaggle内核,其中我曾使用camvid数据集来训练UNET模型.

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

如果您需要更好的扩充功能,可以查看此很棒的GitHub存储库- https://github.com/mdbloice/Augmentor

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

这篇关于ImageDataGenerator用于语义分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:50