使用ImageDataGenerator时,使用mxnet-backend和8 GPU的Keras训练速度很慢

时间:2017-09-30 21:55:09

标签: python deep-learning keras mxnet

关注这篇精彩帖子: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秒,在我尝试之前我非常兴奋和高兴 - 数据增强

到那时,我的训练图像是一个大小为(6000,1,28,28)的numpy数组,标签大小(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优化此过程。

谢谢!

1 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案。 您需要使用mxnet的迭代器。看这里: Image IO - Loading and pre-processing images 而不是Keras的data_generator

相关问题