python - Keras 与 TensorFlow : Use memory as it's needed [ResourceExhaustedError]

标签 python tensorflow keras deep-learning

所以我试图用多个数据集来污染我的 CNN 并且当我添加足够的数据时(例如当我将多个集合作为一个集合添加或当我尝试添加具有超过一百万个样本的集合时)它会接缝抛出一个 ResourceExhaustedError

至于说明here ,我尝试添加

from keras.backend.tensorflow_backend import set_session
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.3
set_session(tf.Session(config=config))

我的代码,但这并没有什么不同。 我在打印出 config.gpu_options.per_process_gpu_memory_fraction 后看到 0.3,这样部分接缝就可以了。

我什至加入了一个 config.gpu_options.allow_growth = True 以确保很好,但它并不想做任何事情,而是尝试一次使用所有内存,结果却发现它还不够。

我试图用来训练这个 CNN 的计算机有 4 个 GTX1080 Ti,每个都有 12GB 的专用内存。

编辑:很抱歉没有说明我是如何加载数据的,老实说我没有意识到有不止一种方法。在我学习的时候,他们总是有示例加载已经内置的数据集,我花了一段时间才意识到如何加载一个自提供的数据集。

我这样做的方式是创建两个 numpy 数组。一个具有路径或每个图像,另一个具有相应的标签。这是最基本的例子:

data_dir = "folder_name"

# There is a folder for every form and in that folder is every line of that form
for filename in glob.glob(os.path.join(data_dir, '*', '*')):

    # the format for file names are: "{author id}-{form id}-{line number}.png"
    # filename is a path to the file so .split('\\')[-1] get's the raw file name without the path and .split('-')[0] get's the author id
    authors.append(filename.split('\\')[-1].split('-')[0])
    files.append(filename)

#keras requires numpy arrays 
img_files  = np.asarray(files)
img_targets = np.asarray(authors)

最佳答案

您确定您没有使用巨大的 batch_size 吗?

“添加数据”:老实说,我不知道那是什么意思,如果你能用代码准确描述你在这里做什么,那将会有所帮助。

样本数量应该不会对 GPU 内存造成任何问题。导致问题的是batch_size

加载庞大的数据集可能会导致 CPU RAM 问题,这与 keras/tensorflow 无关。 numpy 数组太大的问题。 (您可以通过“不创建任何模型”简单地加载数据来测试这一点)

如果这是您的问题,您应该使用生成器 逐渐加载批处理。同样,由于您的问题中绝对没有代码,因此我们无能为力。

但这是简单地为图像创建生成器的两种形式:

  • 使用现有的 ImageDataGenerator 及其 flow_from_directory() 方法,explained here
  • 创建您自己的编码生成器,它可以是:

循环生成器的简单示例:

def imageBatchGenerator(imageFiles, imageLabels, batch_size):
    while True:
        batches = len(imageFiles) // batch_size
        if len(imageFiles) % batch_size > 0:
            batches += 1

        for b in range(batches):
            start = b * batch_size
            end = (b+1) * batch_size

            images = loadTheseImagesIntoNumpy(imageFiles[start:end])
            labels = imageLabels[start:end]

            yield images,labels

Warning: even with generators, you must make sure your batch size is not too big!

使用它:

model.fit_generator(imageBatchGenerator(files,labels,batchSize), steps_per_epoch = theNumberOfBatches, epochs= ....)

在 GPU 之间划分模型

您应该能够决定哪些层由哪个 GPU 处理,这“可以”优化您的 RAM 使用。

示例,创建模型时:

with tf.device('/gpu:0'):
    createLayersThatGoIntoGPU0

with tf.device('/gpu:1'):
    createLayersThatGoIntoGPU1

#you will probably need to go back to a previous GPU, as you must define your layers in a proper sequence
with tf.device('/cpu:0'):
    createMoreLayersForGPU0

#and so on

我不确定这会不会更好,但也值得一试。

在此处查看更多详细信息:https://keras.io/getting-started/faq/#how-can-i-run-a-keras-model-on-multiple-gpus

关于python - Keras 与 TensorFlow : Use memory as it's needed [ResourceExhaustedError],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51034355/

相关文章:

python - Rest 框架序列化器总是为 is_valid 返回 False

当 reloader=True 时,Python 计时器函数使用 Bottle.py 运行两次

tensorflow - 如何使用 TensorFlow 学习多类多输出 CNN

tensorflow - 自定义指标访问 X 输入数据

python - 带有 Keras 和 Python 的多种输出类型的 CNN

python - 根到叶算法错误

python - 我可以获取 python 对象的所有方法吗?

c++ - 如何创建具有 CUDA 支持的最新 Tensorflow 版本的调试版本?

python - 一热编码字符

python - 减少卷积神经网络的内存需求