我想训练一个用于视频帧预测的卷积循环神经网络。各个帧非常大,因此将整个训练数据一次性放入内存中具有挑战性。因此,我按照一些在线教程来创建自定义数据生成器。测试时,它似乎可以工作,但比直接使用预加载的数据慢至少 100 倍。由于我只能在 GPU 上容纳大约 8 个批量大小,我知道数据需要非常快地生成,但是,情况似乎并非如此。
我在单个 P100 上训练我的模型,并有 32 GB 内存可供最多 16 个内核使用。
class DataGenerator(tf.keras.utils.Sequence):
def __init__(self, images, input_images=5, predict_images=5, batch_size=16, image_size=(200, 200),
channels=1):
self.images = images
self.input_images = input_images
self.predict_images = predict_images
self.batch_size = batch_size
self.image_size = image_size
self.channels = channels
self.nr_images = int(len(self.images)-input_images-predict_images)
def __len__(self):
return int(np.floor(self.nr_images) / self.batch_size)
def __getitem__(self, item):
# Randomly select the beginning image of each batch
batch_indices = random.sample(range(0, self.nr_images), self.batch_size)
# Allocate the output images
x = np.empty((self.batch_size, self.input_images,
*self.image_size, …Run Code Online (Sandbox Code Playgroud)