从U网络转移学习以进行图像分割[Keras]

时间:2018-08-02 03:00:45

标签: keras deep-learning image-segmentation transfer-learning

只需开始使用Conv Nets并尝试解决图像分割问题。我参加了dstl卫星图像特征检测比赛的24张图像及其遮罩。 (https://www.kaggle.com/c/dstl-satellite-imagery-feature-detection/data

我以为我会尝试按照此处的提示https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html,但是我被困住了。

我下载了ZF_UNET_224的预训练权重,这是解决该问题的第二名优胜者的方法。我的图像蒙版包含5个对象,所以我弹出了最后一层,而不是这样:

activation_45 (Activation) (None, 224, 224, 32) 0 batch_normalization_44[0][0]

spatial_dropout2d_2 (SpatialDro (None, 224, 224, 32) 0 activation_45[0][0]

conv2d_46 (Conv2D) (None, 224, 224, 1) 33 spatial_dropout2d_2[0][0]

我现在有这个:

activation_45 (Activation) (None, 224, 224, 32) 0 batch_normalization_44[0][0]

spatial_dropout2d_2 (SpatialDro (None, 224, 224, 32) 0 activation_45[0][0]

predictions (Conv2D) (None, 224, 224, 5) 10 conv2d_46[0][0]

我正在尝试遵循Keras教程中的确切步骤,但是在我这样做时

my_model.fit_generator( train_generator, steps_per_epoch= 4, epochs=10, validation_data=validation_generator )

我收到一条错误消息

Output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: [[[[1. 1. 1. ] [1. 1. 1. ] [1. 1. 1. ] … [1. 1. 1. ] [1. 1. 1. ] [1. 1. 1. ]]

我想我想要的是我的224X224图像中每个像素的概率,这样我就可以使用这些像素在原始图像上生成蒙版,但是我不确定该怎么做。

我有24个8波段输入图像及其遮罩,它们标记5个对象。我想在这些图像上训练此U-Net,并在一些测试图像上放置蒙版,并评估它们的IoU或加权对数损失。有帮助吗?

更新:

我正在使用与Keras教程中相同的生成器:

   batch_size = 4

    # this is the augmentation configuration we will use for training 

train_datagen = ImageDataGenerator(
            rescale=1./255,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True)

    # this is the augmentation configuration we will use for testing:
    # only rescaling 
test_datagen = ImageDataGenerator(
            rescale=1./255,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True)

    # this is a generator that will read pictures found in
    # subfolers of 'data/train', and indefinitely generate
    # batches of augmented image data train_generator = 
train_datagen.flow_from_directory(
            'data/train',  # this is the target directory
            target_size=(224, 224),  # all images will be resized 
            batch_size=batch_size,
            color_mode='rgb', 
            class_mode=None)  # since we use binary_crossentropy loss, we need binary labels

    # this is a similar generator, for validation data 
 validation_generator = test_datagen.flow_from_directory(
            'data/valid',
            target_size=(224, 224),
            batch_size=batch_size,
            color_mode = 'rgb',
            class_mode=None)

还有一件事:我的训练图像有8个频段,但体系结构仅接受3个频段。我认为生成器最后只留下1个频段。也不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

关于您的错误消息:

使用flow_from_directory(),您的ImageDataGenerator从包含图像的目录结构中推断类标签。如示例中所示,图像应按类别放在子文件夹中。

对于图像分割问题,标签结构比每个图像只有一个标签更为复杂。标签是带有每像素标签的蒙版。通常,您希望在训练期间将这些标签作为np arrays提供给模型。

您将无法使用flow_from_directory()处理案件。一种解决方案是编写您自己的自定义生成器,该生成器从磁盘读取图像和标签,并将其与fit_generator()一起使用。

假设您有一个.csv文件,该文件包含两列,一列具有图像名称,一列具有相应蒙版的路径:

text file

然后您的生成器可能看起来像这样(我正在使用pandas来读取.csv文件):

from keras.utils import Sequence
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from math import ceil 
import numpy as np
import pandas as pd


class DataSequence(Sequence):
    """
    Keras Sequence object to train a model on larger-than-memory data.
    df: pandas dataframe from the csv file
    data_path: path where your images live
    """

    def __init__(self, df, data_path, batch_size):
        self.batch_size = batch_size
        self.im_list = df['images'].tolist()
        self.mask_list = df['labels'].tolist()

    def __len__(self):
        """Make sure to handle cases where the last batch < batch_size
        return int(math.ceil(len(self.im_list) / float(self.batch_size)))

    def get_batch_images(self, idx, path_list):
        # Fetch a batch of images from a list of paths
        return np.array([load_image(im) for im in path_list[idx * self.batch_size: (1 + idx) * self.batch_size]])

    def __getitem__(self, idx):
        batch_x = self.get_batch_images(idx, self.im_list)
        batch_y = self.get_batch_labels(idx, self.mask_list)
        return batch_x, batch_y

我在这里利用Keras Sequence对象编写生成器,因为这样可以进行安全的多重处理,从而加快培训速度。有关此主题,请参见docs

关于转学的实际问题

那样,您将无法使用针对8通道图像上的3通道图像进行预训练的体系结构。如果要使用该体系结构,则可以对通道进行二次采样,或将尺寸从8减少到3。另请参见this线程。

相关问题