在这篇伟大的帖子之后:Scaling Keras Model Training to Multiple GPUs我尝试升级我的模型以在我的多个GPU实例上并行运行 .
首先,我运行了这里提出的MNIST示例:MNIST in Keras,其中包含编译命令中的附加语法,如下所示:
# Prepare the list of GPUs to be used in training
NUM_GPU = 8 # or the number of GPUs available on your machine
gpu_list = []
for i in range(NUM_GPU): gpu_list.append('gpu(%d)' % i)
# Compile your model by setting the context to the list of GPUs to be used in training.
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'],
context=gpu_list)
然后我训练了模型:
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
到现在为止还挺好 . 它每个时代的运行时间不到1秒,我真的很兴奋和快乐,直到我尝试 - data augmentation .
到那时,我的训练图像是一个大小不一致的阵列(6000,1,28,28),标签大小(10,60000) - 一个热编码 . 对于数据扩充,我使用了ImageDataGenerator函数:
gen = image.ImageDataGenerator(rotation_range=8, width_shift_range=0.08, shear_range=0.3,
height_shift_range=0.08, zoom_range=0.08)
batches = gen.flow(x_train, y_train, batch_size=NUM_GPU*64)
test_batches = gen.flow(x_test, y_test, batch_size=NUM_GPU*64)
然后:
model.fit_generator(batches, batches.N, nb_epoch=1,
validation_data=test_batches, nb_val_samples=test_batches.N)
不幸的是,从每个纪元1秒开始我每个纪元开始得到~11秒......我想ImageDataGenerator的“影响”是破坏性的,它可能正在运行所有(读取 - >扩充 - >写入gpu)过程真的很慢,效率低下 .
将keras扩展到多个GPU非常棒,但数据增强对于我的模型足够强大至关重要 .
我想一个解决方案可能是:从目录加载所有图像并编写自己的函数来混洗和扩充这些图像 . 但我确信必须使用keras API优化此过程 .
Thanks!
1 回答
好的我've found the solution. You need to use mxnet' s迭代器 . 看到这里:Image IO - Loading and pre-processing images而不是Keras的data_generator