用于将图像数据切片成滑动的 Keras 层 windows

Keras layer for slicing image data into sliding windows

我有一组图像,宽度各不相同,但高度固定为 100 像素和 3 个深度通道。我的任务是对图像中的每条垂直线是否有趣进行分类。为此,我在其 10 个前身和后继产品线的背景下查看该产品线。想象一下算法从图像的左到右扫描,检测包含兴趣点的垂直线。

我第一次尝试这样做是在将数据输入 Keras 模型之前使用 numpy 手动删除这些滑动 windows。像这样:

# Pad left and right
s = np.repeat(D[:1], 10, axis = 0)
e = np.repeat(D[-1:], 10, axis = 0)
# D now has shape (w + 20, 100, 3)
D = np.concatenate((s, D, e))
# Sliding windows creation trick from SO question
idx = np.arange(21)[None,:] + np.arange(len(D) - 20)[:,None]
windows = D[indexer]

然后所有图像中所有垂直线的所有 windows 和所有地面真实值 0/1 将连接成两个非常长的数组。

我已经证实这在原则上是可行的。我将每个 window 馈送到 Keras 层,如下所示:

Conv2D(20, (5, 5), input_shape = (21, 100, 3), padding = 'valid', ...)

但是 windowing 导致内存使用量增加 21 倍,所以这样做变得不切实际。但我认为我的场景在机器学习中很常见,所以 Keras 中必须有一些标准方法才能有效地做到这一点吗?例如,我想向 Keras 提供我的原始图像数据 (w, 100, 80) 并告诉它滑动的 window 大小是多少,然后让它找出其余的。我看过一些示例代码,但我是一个 ml 菜鸟,所以我不明白。

不幸的是,这不是一个简单的问题,因为它可能涉及为您的 Keras 模型使用可变大小的输入。虽然我认为可以通过正确使用占位符来做到这一点,但这对于初学者来说肯定不是一个开始的地方。您的另一个选择是数据生成器。对于许多计算密集型任务,通常需要在计算速度和内存需求之间进行权衡,使用生成器的计算量更大,并且将完全在您的 cpu 上完成(无 gpu 加速),但它不会使内存增加。

数据生成器的要点是它会一次将操作应用于图像以生成批次,然后对该批次进行训练,然后释放内存 - 所以你最终只能保留一个批次的价值随时存储在内存中的数据。不幸的是,如果您有一个耗时的生成,那么这会严重影响性能。

生成器将是一个python生成器(使用'yield'关键字)并且预计会产生单批数据,keras非常擅长使用任意批量大小,所以你可以始终使一张图像产生一批,尤其是开始时。

这是 fit_generator 上的 keras 页面 - 我警告你,这很快就会变成大量工作,请考虑购买更多内存: https://keras.io/models/model/#fit_generator

好吧,我会为你做的:P

    import numpy as np
    import pandas as pd
    import keras
    from keras.models import Model, model_from_json
    from keras.layers import Dense, Concatenate, Multiply,Add, Subtract, Input, Dropout, Lambda, Conv1D, Flatten
    from tensorflow.python.client import device_lib
    # check for my gpu 
    print(device_lib.list_local_devices())


    # make some fake image data

    # 1000 random widths
    data_widths = np.floor(np.random.random(1000)*100)

    # producing 1000 random images with dimensions w x 100 x 3
    # and a vector of which vertical lines are interesting
    # I assume your data looks like this
    images = []
    interesting = []
    for w in data_widths:
        images.append(np.random.random([int(w),100,3]))
        interesting.append(np.random.random(int(w))>0.5)

    # this is a generator
    def image_generator(images, interesting):
        num = 0
        while num < len(images):
            windows = None
            truth = None

            D = images[num]
            # this should look familiar

            # Pad left and right
            s = np.repeat(D[:1], 10, axis = 0)
            e = np.repeat(D[-1:], 10, axis = 0)
            # D now has shape (w + 20, 100, 3)
            D = np.concatenate((s, D, e))
            # Sliding windows creation trick from SO question
            idx = np.arange(21)[None,:] + np.arange(len(D) - 20)[:,None]
            windows = D[idx]
            truth = np.expand_dims(1*interesting[num],axis=1)
            yield (windows, truth)
            num+=1
            # the generator MUST loop
            if num == len(images):
                num = 0

    # basic model - replace with your own
    input_layer = Input(shape = (21,100,3), name = "input_node")
    fc = Flatten()(input_layer)
    fc = Dense(100, activation='relu',name = "fc1")(fc)
    fc = Dense(50, activation='relu',name = "fc2")(fc)
    fc = Dense(10, activation='relu',name = "fc3")(fc)
    output_layer = Dense(1, activation='sigmoid',name = "output")(fc)

    model = Model(input_layer,output_layer)
    model.compile(optimizer="adam", loss='binary_crossentropy')
    model.summary()

    #and training
    training_history = model.fit_generator(image_generator(images, interesting),
                        epochs =5,
                        initial_epoch = 0,
                        steps_per_epoch=len(images),
                        verbose=1
                       )