简体   繁体   English

使用flow_from_directory()使用keras(TensforFlow后端)进行语义图像分割的输入管道(3个标签)

[英]Input pipeline for semantic image segmentation (3 labels) with keras (TensforFlow backend) using flow_from_directory()

I am using keras (TensorFlow backend) and I am trying to understand how to bring in my labels/masks for image segmentation (3 labels) using flow_from_directory. 我正在使用keras(TensorFlow后端),并且试图了解如何使用flow_from_directory引入用于图像分割的标签/遮罩(3个标签)。

The train_images have the dimensions (144, 144, 144) - grayscale, uint8. train_images的尺寸为(144、144、144)-灰度,uint8。 The corresponding label_images have the same dimensions but here the value 1 represents label 1, value 2 = label 2, value 3 = label 3 and the value 0 shows unlabeled pixels. 相应的label_images具有相同的尺寸,但此处的值1表示标签1,值2 =标签2,值3 =标签3,值0显示未标签的像素。

Since this is semantic segmentation, classifying each pixel in the image requires using a pixel-wise cross-entropy loss function. 由于这是语义分割,因此对图像中的每个像素进行分类需要使用逐像素交叉熵损失函数。 And as I have read in some posts, keras (or TensorFlow) requires that my label_image/mask is one hot coded. 正如我在某些帖子中所读到的那样,keras(或TensorFlow)要求我的label_image / mask是一个热门代码。 Therefore I expect my label_images to be an image with 3 channels, where each pixel will consist of a binary vector. 因此,我希望我的label_images是具有3个通道的图像,其中每个像素将由一个二进制矢量组成。 Example: [0, 1, 0]. 示例:[0,1,0]。

How do I deal with the unlabeled pixels that are stored as 0? 如何处理存储为0的未标记像素? Should they be encoded as [0, 0, 0]? 是否应该将它们编码为[0,0,0]?

But the question I have where I fail to find an answer is: How do I reshape/one-hot encode my label_images correctly? 但是,我在哪里找不到答案的问题是:如何正确整形/热编码label_images? Is there a handy function in keras that lets me convert my image_labels? keras中有一个方便的函数可以让我转换image_labels吗?

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1. / 255)
label_datagen = ImageDataGenerator(rescale=1. / 255)

train_image_generator = train_datagen.flow_from_directory(
    directory='/train_images',
    target_size=(144, 144, 144),
    color_mode='grayscale',
    classes=None,
    class_mode=None,
    batch_size=4)

train_label_generator = label_datagen.flow_from_directory(
    directory='/label_images',
    target_size=(144, 144, 144),
    color_mode='grayscale',
    classes=None,
    class_mode=None,
    batch_size=4)

train_generator = zip(train_image_generator, train_label_generator)

Currently working on something very similar but with 10 classes. 目前正在从事非常相似的工作,但有10个课程。 Still not entirely there yet, but as to you question about built-in functions for keras, checkout: 尚不完全存在,但是对于您关于keras内置函数的问题,请结帐:

one_hot_array = keras.utils.to_categorical(array_of_label_data, nb_classes)

which creates a one-hot-vector of your mask/label data. 这将创建您的遮罩/标签数据的一站式矢量。 So for your case, the expect output for say 100 masks would be (100, H, W, 3), where 3 is equal to the number of classes you're working with. 因此,对于您的情况,假设100个蒙版的预期输出为(100,H,W,3),其中3等于您正在使用的类的数量。 What I'm not sure about is if you do or do not have a background in your mask, and also, how you're supposed to structure the folders for your data. 我不确定的是您的蒙版是否有背景,以及如何为数据构建文件夹。 Hope that helps though. 希望能有所帮助。

Also, your target_size is off, that's referring to the dimensions of your images (eg height and width). 同样,您的target_size处于关闭状态,这是指图像的尺寸(例如,高度和宽度)。 There shouldn't be a third value. 不应有第三个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras:如何使用 flow_from_directory 提供自定义标签? - Keras: How to provide custom labels using flow_from_directory? 如何在Keras中使用flow_from_directory进行多类语义分割? - How to use flow_from_directory in Keras for multi-class semantic segmentation? 使用Keras的flow_from_directory和FCNN - using Keras' flow_from_directory with FCNN Keras:从flow_from_directory获取图像和标签数组 - Keras: Obtain array of images and labels from flow_from_directory 如何在 keras flow_from_directory 中手动指定类标签? - How to manually specify class labels in keras flow_from_directory? Keras:使用文件名而不是目录的flow_from_directory()或flow() - Keras: flow_from_directory() or flow() using filenames instead of directories Keras ImageDataGenerator和flow_from_directory class_mode ='input' - Keras ImageDataGenerator and flow_from_directory class_mode='input' Keras -.flow_from_directory(目录) - Keras - .flow_from_directory(directory) Keras:Plot 使用 flow_from_directory 训练数据的图像增强样本 - Keras: Plot Image augmentations samples for training data using flow_from_directory Keras:在 flow_from_directory() 中使用多个目录 - Keras: Using multiple directories in flow_from_directory()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM