Keras:为大型数据集批量加载图像

Keras:为大型数据集批量加载图像,keras,Keras,在keras中,一次只能在内存中加载一批,因为我有40GB的图像数据集 若数据集很小,我可以使用ImageDataGenerator生成批处理,但由于数据集很大,我无法在内存中加载所有图像 keras中是否有类似于以下tensorflow代码的方法: path_queue = tf.train.string_input_producer(input_paths, shuffle= False) paths, contents = reader.read(path_queue) inputs =

在keras中,一次只能在内存中加载一批,因为我有40GB的图像数据集

若数据集很小,我可以使用ImageDataGenerator生成批处理,但由于数据集很大,我无法在内存中加载所有图像

keras中是否有类似于以下tensorflow代码的方法:

path_queue = tf.train.string_input_producer(input_paths, shuffle= False)
paths, contents = reader.read(path_queue)
inputs = decode(contents)
input_batch = tf.train.batch([inputs], batch_size=2)

我正在使用此方法序列化tensorflow中的输入,但我不知道如何在Keras中实现此任务。

Keras在其模型中有方法
fit_generator()
。它接受python
生成器
或keras
序列
作为输入

您可以创建一个简单的生成器,如下所示:

fileList = listOfFiles     

def imageLoader(files, batch_size):

    L = len(files)

    #this line is just to make the generator infinite, keras needs that    
    while True:

        batch_start = 0
        batch_end = batch_size

        while batch_start < L:
            limit = min(batch_end, L)
            X = someMethodToLoadImages(files[batch_start:limit])
            Y = someMethodToLoadTargets(files[batch_start:limit])

            yield (X,Y) #a tuple with two numpy arrays with batch_size samples     

            batch_start += batch_size   
            batch_end += batch_size
model.fit_generator(imageLoader(fileList,batch_size),steps_per_epoch=..., epochs=..., ...)
通常,您会将从生成器中获取的批次数传递给
步骤/u epoch


您还可以实现自己的。这需要做更多的工作,但是如果您要进行多线程处理,他们建议您使用它

那么呢?元组的X是一个数组,因为它是图像列表?不,它必须是单个数组,keras才能工作。(这意味着一批只能具有相同大小的所有图像)。这在卷积层所需的形状中很明显:
(批量大小、宽度、高度、通道)
。如果您有一个大小相同的图像列表,您可以
X=np.array(X)
。例如,对于RGB图像,它必须是4D:
(50256256,3)
。或者
(50256256,1)
用于灰度图像。是的,对于传统的自动编码器,您将
产生(x,x)