因为我有40GB的图像数据集,所以在keras中一次只能在内存中加载一个批次.
如果数据集很小,我可以使用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)
Run Code Online (Sandbox Code Playgroud)
我正在使用此方法序列化tensorflow中的输入,但我不知道如何在Keras中完成此任务.
Dan*_*ler 23
Keras fit_generator()在其模型中有这种方法.它接受python generator或keras Sequence作为输入.
您可以创建一个这样的简单生成器:
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
Run Code Online (Sandbox Code Playgroud)
并且适合这样:
model.fit_generator(imageLoader(fileList,batch_size),steps_per_epoch=..., epochs=..., ...)
Run Code Online (Sandbox Code Playgroud)
通常,您将传递到steps_per_epoch将从生成器中获取的批次数.
您还可以实现自己的Keras序列.这是一个更多的工作,但如果你要进行多线程处理,他们建议使用它.
| 归档时间: |
|
| 查看次数: |
8635 次 |
| 最近记录: |