gpt4 book ai didi

python-3.x - 如何在将输入输入到 keras 中的神经网络时使用两个不同的生成器?

转载 作者:行者123 更新时间:2023-11-30 09:43:05 27 4
gpt4 key购买 nike

我有一个巨大的数据集。当我处理此类数据集时,我通常的方法是使用 numpy 文件分割成多个小数据集,并使用生成器来处理它们。还有其他替代方案吗?我还想将随机运行时图像增强与 Keras 图像预处理模块结合起来,该模块也是一个生成器类型函数。如何简化这两个生成器进程?Keras 图像增强模块的链接如下。 https://keras.io/preprocessing/image/

我当前的数据流生成器如下:

def dat_loader(path, batch_size):
while True:
for dir, subdir, files in os.walk(path):
for file in files:
file_path = path + file
archive = np.load(file_path)
img = archive['images']
truth = archive['truth']
del archive
num_batches = len(truth)//batch_size
img = np.array_split(img, num_batches)
truth = np.array_split(truth, num_batches)
while truth:
batch_img = img.pop()
batch_truth = truth.pop()
yield batch_img, batch_truth

最佳答案

处理非常大的数据集的一种方法是使用 memory mapped files在运行时动态加载所需的数据。 NumPy 有 memmap它创建一个映射到一个大文件的数组,该文件可能很大(我曾经有一个用于离线维基百科的预处理版本,这还可以),但不一定存在于您的 RAM 中。当需要时或当对象被垃圾收集时,任何更改都会刷新回文件。这是一个例子:

import numpy as np
# Create or load a memory mapped array, can contain your huge dataset
nrows, ncols = 1000000, 100
f = np.memmap('memmapped.dat', dtype=np.float32,
mode='w+', shape=(nrows, ncols))

# Use it like a normal array but it will be slower as it might
# access the disk along the way.
for i in range(ncols):
f[:, i] = np.random.rand(nrows)

来自 online tutorial 。请注意,这只是一个潜在的解决方案,对于您的数据集和使用情况,可能有更好的替代方案。

关于python-3.x - 如何在将输入输入到 keras 中的神经网络时使用两个不同的生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56445313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com