【问题标题】:Training a Keras model from batches of .npy files using generator?使用生成器从批量 .npy 文件中训练 Keras 模型?
【发布时间】:2019-05-16 05:43:41
【问题描述】:

目前我在使用 Keras 训练图像数据时正在处理一个大数据问题。我有一个包含一批 .npy 文件的目录。每批包含 512 张图像。每个批次都有其对应的标签文件为 .npy。所以它看起来像:{image_file_1.npy, label_file_1.npy, ..., image_file_37.npy, label_file_37}。每个图像文件的尺寸为(512, 199, 199, 3),每个标签文件的尺寸为(512, 1)(1 或0)。如果我将所有图像加载到一个 ndarray 中,它将是 35+ GB。到目前为止阅读了所有的 Keras Doc。我仍然无法找到如何使用自定义生成器进行训练。我已经阅读了flow_from_dict 和ImageDataGenerator(...).flow(),但在这种情况下它们并不理想,或者我不知道如何定制它们。这就是我所做的。

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
from keras.preprocessing.image import ImageDataGenerator

val_gen = ImageDataGenerator(rescale=1./255)
x_test = np.load("../data/val_file.npy")
y_test = np.load("../data/val_label.npy")
val_gen.fit(x_test)

model = Sequential()
...
model_1.add(layers.Dense(512, activation='relu'))
model_1.add(layers.Dense(1, activation='sigmoid'))

model.compile(loss='categorical_crossentropy', 
              optimizer=sgd, 
               metrics=['acc'])

model.fit_generator(generate_batch_from_directory() # should give 1 image file and 1 label file
                    validation_data=val_gen.flow(x_test, 
                                                 y_test, 
                                                 batch_size=64),
                    validation_steps=32)

所以这里generate_batch_from_directory() 应该每次都取image_file_i.npy 和label_file_i.npy 并优化权重直到没有批次剩余。 .npy 文件中的每个图像数组都已通过增强、旋转和缩放进行处理。每个.npy 文件都与来自第 1 类和第 0 类 (50/50) 的数据正确混合。

如果我追加所有批次并创建一个大文件,例如:

X_train = np.append([image_file_1, ..., image_file_37])
y_train = np.append([label_file_1, ..., label_file_37])

它不适合内存。否则我可以使用.flow() 生成图像集来训练模型。

感谢您的建议。

【问题讨论】:

    标签: python tensorflow keras deep-learning


    【解决方案1】:

    最后我能够解决这个问题。但是我必须通过keras.utils.Sequence 的源代码和文档来构建我自己的生成器类。 This document 对了解生成器在 Kears 中的工作方式有很大帮助。更多详情可以阅读我的kaggle notebook:

    all_files_loc = "datapsycho/imglake/population/train/image_files/"
    all_files = os.listdir(all_files_loc)
    
    image_label_map = {
            "image_file_{}.npy".format(i+1): "label_file_{}.npy".format(i+1)
            for i in range(int(len(all_files)/2))}
    partition = [item for item in all_files if "image_file" in item]
    
    class DataGenerator(keras.utils.Sequence):
    
        def __init__(self, file_list):
            """Constructor can be expanded,
               with batch size, dimentation etc.
            """
            self.file_list = file_list
            self.on_epoch_end()
    
        def __len__(self):
          'Take all batches in each iteration'
          return int(len(self.file_list))
    
        def __getitem__(self, index):
          'Get next batch'
          # Generate indexes of the batch
          indexes = self.indexes[index:(index+1)]
    
          # single file
          file_list_temp = [self.file_list[k] for k in indexes]
    
          # Set of X_train and y_train
          X, y = self.__data_generation(file_list_temp)
    
          return X, y
    
        def on_epoch_end(self):
          'Updates indexes after each epoch'
          self.indexes = np.arange(len(self.file_list))
    
        def __data_generation(self, file_list_temp):
          'Generates data containing batch_size samples'
          data_loc = "datapsycho/imglake/population/train/image_files/"
          # Generate data
          for ID in file_list_temp:
              x_file_path = os.path.join(data_loc, ID)
              y_file_path = os.path.join(data_loc, image_label_map.get(ID))
    
              # Store sample
              X = np.load(x_file_path)
    
              # Store class
              y = np.load(y_file_path)
    
          return X, y
    
    # ====================
    # train set
    # ====================
    all_files_loc = "datapsycho/imglake/population/train/image_files/"
    all_files = os.listdir(all_files_loc)
    
    training_generator = DataGenerator(partition)
    validation_generator = ValDataGenerator(val_partition) # work same as training generator
    
    hst = model.fit_generator(generator=training_generator, 
                               epochs=200, 
                               validation_data=validation_generator,
                               use_multiprocessing=True,
                               max_queue_size=32)
    

    【讨论】:

    • 这篇文章不错。您是否还可以为自定义的flow_from_directory 提供代码,它与默认的 keras flow_from_directory 完全一样(因此它允许您进行增强 rotation_range、width_shift_range、shear_range,...),此外它还允许以.npy 格式输入图像?
    • 实际上我用于训练的 numpy 文件已经应用了自动旋转并保存为 .npy 批处理。 flow_from_directory 应该在您使用 keras 为您做任何事情时使用。如果您已经有 npy 文件,那么最好使用 fit_generator。
    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2020-05-28
    • 2019-04-02
    • 1970-01-01
    相关资源
    最近更新 更多