Keras适合发电机缓慢

时间:2018-10-31 13:05:22

标签: python keras multiprocessing gpu virtual-memory

Keras fit_generator非常慢。在训练中不经常使用GPU,有时它的使用率下降到0%。即使有4个工人和multiproceesing=True

此外,脚本的进程正在请求过多的虚拟内存,并且其状态为uninterruptible sleep (usually IO)

我已经尝试过max_queue_size的不同组合,但是没有用。

Screenshot GPU使用率

Screenshot的进程虚拟内存和状态

Hardware Info GPU = Titan XP 12Gb

数据生成器类的代码

import numpy as np
import keras
import conf


class DataGenerator(keras.utils.Sequence):
    'Generates data for Keras'
    def __init__(self, list_IDs, labels, batch_size=32, dim=(conf.max_file, 128),
                 n_classes=10, shuffle=True):
        'Initialization'
        self.dim = dim
        self.batch_size = batch_size
        self.labels = labels
        self.list_IDs = list_IDs
        self.n_classes = n_classes
        self.shuffle = shuffle
        self.on_epoch_end()

    def __len__(self):
        'Denotes the number of batches per epoch'
        return int(np.floor(len(self.list_IDs) / self.batch_size))

    def __getitem__(self, index):
        'Generate one batch of data'
        # Generate indexes of the batch
        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]

        # Find list of IDs
        list_IDs_temp = [self.list_IDs[k] for k in indexes]

        # Generate data
        X, y = self.__data_generation(list_IDs_temp)

        return X, y

    def on_epoch_end(self):
        'Updates indexes after each epoch'
        self.indexes = np.arange(len(self.list_IDs))
        if self.shuffle == True:
            np.random.shuffle(self.indexes)

    def __data_generation(self, list_IDs_temp):
        'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
        # Initialization
        X = np.empty((self.batch_size, *self.dim))
        y = np.empty((self.batch_size, conf.max_file, self.n_classes))
        # Generate data
        for i, ID in enumerate(list_IDs_temp):
            # Store sample
            X[i, ] = np.load(conf.dir_out_data+"data_by_file/" + ID)

            # Store class
            y[i, ] = np.load(conf.dir_out_data +
                            'data_by_file/' + self.labels[ID])

        return X, y

python脚本代码

training_generator = DataGenerator(partition['train'], labels, **params)

validation_generator = DataGenerator(partition['validation'], labels, **params)

model.fit_generator(generator = training_generator,
                    validation_data = validation_generator,
                    epochs=steps,
                    callbacks=[tensorboard, checkpoint],
                    workers=4,
                    use_multiprocessing=True,
                    max_queue_size=50)

1 个答案:

答案 0 :(得分:1)

如果您使用Tensorflow 2.0,则可能会遇到以下错误:https://github.com/tensorflow/tensorflow/issues/33024

解决方法是:

  • 在代码开头致电tf.compat.v1.disable_eager_execution()
  • 使用model.fit而不是model.fit_generator。前者仍然支持发电机。
  • 降级为TF 1.14

无论Tensorflow版本如何,这些原则都适用:

  • 限制您正在进行多少磁盘访问,这通常是瓶颈。
  • 在培训和验证中检查批次大小。验证中的批处理大小为1将非常慢。

尽管生成器在1.13.2和2.0.1中运行缓慢(至少),但似乎确实存在问题。 https://github.com/keras-team/keras/issues/12683