[英]Keras fit generator slow
fit_generator
非常慢。 GPU 在訓練中不會經常使用,有時它的使用率會下降到 0%。 即使在 4 個工人和multiproceesing=True
。
此外,腳本的進程請求過多的虛擬內存並且處於 D 狀態, 不間斷睡眠(通常為 IO) 。
我已經嘗試了max_queue_size
不同組合,但沒有奏效。
截圖GPU 使用情況
進程虛擬內存和狀態的屏幕截圖
硬件信息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)
如果您使用的是 Tensorflow 2.0,您可能會遇到此錯誤: https : //github.com/tensorflow/tensorflow/issues/33024
解決方法是:
tf.compat.v1.disable_eager_execution()
model.fit
而不是model.fit_generator
。 前者無論如何都支持生成器。無論 Tensorflow 版本如何,這些原則都適用:
盡管生成器在 1.13.2 和 2.0.1(至少)中運行緩慢,但似乎確實存在問題。 https://github.com/keras-team/keras/issues/12683
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.