用于语义分割的ImageDataGenerator

问题描述 投票:0回答:2

我正在尝试使用 Keras 进行语义分割,当尝试加载图像时,我使用

flow_from_directory
方法收到此错误。

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() 错误

python tensorflow keras semantic-segmentation
2个回答
23
投票

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

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

数据生成器应该是这样的:-

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 模型。

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

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


0
投票

我遇到了类似的错误: ValueError:调用层“model_3”(功能类型)时遇到异常。

Input 0 of layer "conv2d_99" is incompatible with the layer: expected min_ndim=4, found ndim=2. Full shape received: (None, None)

Call arguments received by layer 'model_3' (type Functional):
  • inputs=('tf.Tensor(shape=(None, None, None, None), dtype=float32)', 'tf.Tensor(shape=(None, None), dtype=float32)')
  • training=True
  • mask=None

我使用这个函数进行预训练。

def data_aug(batch_size=32,种子=42):

train_image_data_path = 'path-to/Train/imgs'
test_image_data_path = 'path-to/Test/imgs'
train_mask_data_path = 'path-to/Train/annotations'
test_mask_data_path = 'path-to/Test/annotations'
img_height, img_width = 128, 128

test_datagen = ImageDataGenerator( rescale=1./255)
train_datagen = ImageDataGenerator(rescale=1./255,
    rotation_range=10, # rotation
    width_shift_range=0.2, # horizontal shift
    height_shift_range=0.2, # vertical shift
    zoom_range=0.2, # zoom
    horizontal_flip=True, # horizontal flip
    brightness_range=[0.2,1.2])

# **train** data
X_train_augmented = train_datagen.flow_from_directory(
              directory=train_image_data_path,
              target_size=(img_height, img_width),
              shuffle=False,
              batch_size=32,
              seed=42
              )

Y_train_augmented = train_datagen.flow_from_directory(
              directory=train_mask_data_path,
              target_size=(img_height, img_width), # resize to this size
              shuffle=False,
              batch_size=32,
              seed=42,
              color_mode = 'grayscale'
              )

# **validation**:
X_test_augmented = test_datagen.flow_from_directory(
              directory=test_image_data_path,
              target_size=(img_height, img_width), # resize to this size
              shuffle=False,
              batch_size=32,
              seed=42
              )

Y_test_augmented = test_datagen.flow_from_directory(
              directory=test_mask_data_path,
              target_size=(img_height, img_width), # resize to this size
              shuffle=False,
              batch_size=32,
              seed=42,
              color_mode = 'grayscale'
              )


train_generator = zip(X_train_augmented, Y_train_augmented)
test_generator = zip(X_test_augmented, Y_test_augmented)

return train_generator, test_generator

train_generator, test_generator = data_aug() 你解决了吗?

© www.soinside.com 2019 - 2024. All rights reserved.