python - keras中的fit_generator,将所有内容加载到内存中

标签 python python-3.x keras deep-learning generator

我有原始数据(X_train,y_train),我正在将此数据修改为其他数据。原始数据只是带有标签的图像。修改后的数据应该是 Siamese 网络的图像对,数量较多,内存大小约为 30 GB。因此无法运行此函数来在整个原始数据上创建对。因此,我使用 keras fit_generator 认为它只会加载该特定批处理。

我在样本对上运行了 model.fit 和 model.fit_generator,但我观察到两者都使用相同数量的内存。所以,我想我的代码在使用 fit_generator 时存在一些问题。下面是相关代码。你们能帮我解决这个问题吗?

代码如下:

    def create_pairs(X_train, y_train):
        tr_pairs = []
        tr_y = []

        y_train = np.array(y_train)
        digit_indices = [np.where(y_train == i)[0] for i in list(set(y_train))]

        for i in range(len(digit_indices)):
            n = len(digit_indices[i])
            for j in range(n):
                random_index = digit_indices[i][j]
                anchor_image = X_train[random_index]
                anchor_label = y_train[random_index]
                anchor_indices = [i for i, x in enumerate(y_train) if x == anchor_label]
                negate_indices = list(set(list(range(0,len(X_train)))) - set(anchor_indices))
                for k in range(j+1,n):
                    support_index = digit_indices[i][k]
                    support_image = X_train[support_index]
                    tr_pairs += [[anchor_image,support_image]]

                    negate_index = random.choice(negate_indices)
                    negate_image = X_train[negate_index]
                    tr_pairs += [[anchor_image,negate_image]]

                    tr_y += [1,0]


        return np.array(tr_pairs),np.array(tr_y) 



    def myGenerator():
        tr_pairs, tr_y = create_pairs(X_train, y_train)
        while 1:
            for i in range(110): # 1875 * 32 = 60000 -> # of training samples
                if i%125==0:
                    print("i = " + str(i))
                yield [tr_pairs[i*32:(i+1)*32][:, 0], tr_pairs[i*32:(i+1)*32][:, 1]], tr_y[i*32:(i+1)*32]




    model.fit_generator(myGenerator(), steps_per_epoch=110, epochs=2,
                  verbose=1, callbacks=None, validation_data=([te_pairs[:, 0], te_pairs[:, 1]], te_y), validation_steps=None, class_weight=None,
                  max_queue_size=10, workers=1, use_multiprocessing=False, shuffle=True, initial_epoch=0)

最佳答案

myGenerator 返回一个生成器。

但是您应该注意到 create_pairs 正在将完整数据集加载到内存中。当您调用 tr_pairs, tr_y = create_pairs(X_train, y_train) 时,数据集已加载,因此正在使用内存资源。

myGenerator 只是遍历内存中已有的结构。

解决方案是让 create_pairs 本身成为一个生成器。

如果数据是 numpy 数组,我建议使用 h5 文件从磁盘读取数据 block 。

http://docs.h5py.org/en/latest/high/dataset.html#chunked-storage

关于python - keras中的fit_generator,将所有内容加载到内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50816800/

相关文章:

python - 返回所有相等的列

python - flake8 没有在用户目录中获取全局配置文件

python-3.x - 仅当引号包含 3 个或更多字符时才拆分

python - keras 和 tf.keras 中的 ResNet 模型对同一图像给出不同的输出

python - 合并或合并 pandas 数据框,同时汇总公共(public)列中的元素

python - 使用不同的 id 打印多个图像 url

python ipaddress,只获得第一个可用的主机?

Python 字符串 : backspace at the end of string behaves differently

R ValueError : Error when checking input: expected simple_rnn_input to have 3 dimensions, 但得到形状为 (1661, 3) 的数组

python - Keras,如何获取每一层的输出?