简体   繁体   English

使用 ImageDataGenerator 时 Keras 训练很慢

[英]Keras training is slow when using ImageDataGenerator

So I just got started using Keras, I first tried the MNIST dataset, and everything was quick 5 epoch in only few seconds (60k images).所以我刚开始使用 Keras,我首先尝试了 MNIST 数据集,一切都在短短几秒钟内快速 5 epoch(60k 图像)。

Now I'm trying to train a CNN on a car dataset that has around 8000 image, I used ImageDataGenerator to resize the image into the same size (128 * 128) and to get it from a dataframe that has the filename and the class name for each image, here is the part responsible for this:现在我正在尝试在具有大约 8000 张图像的汽车数据集上训练 CNN,我使用 ImageDataGenerator 将图像调整为相同大小(128 * 128)并从具有文件名和 class 名称的 dataframe 获取它对于每个图像,这是负责此的部分:

datagen=ImageDataGenerator(validation_split=0.2)
train_generator = datagen.flow_from_dataframe(dataframe=df_train, directory="cars/cars_train/cars_train/", x_col="fname", y_col="class_name", class_mode="categorical", target_size=(128,128), batch_size=32,subset="training")
test_generator = datagen.flow_from_dataframe(dataframe=df_train, directory="cars/cars_train/cars_train/", x_col="fname", y_col="class_name", class_mode="categorical", target_size=(128,128), batch_size=32,subset="validation")

As you can see I'm not doing anything intensive except resizing the dataset.如您所见,除了调整数据集的大小外,我没有做任何密集的事情。 When I use model.fit on the ImageDataGenerator, the first epoch take around 2800 seconds to finish and the other epoch take around 60 - 70 second only (1 minute).当我在 ImageDataGenerator 上使用model.fit时,第一个 epoch 大约需要 2800 秒才能完成,而另一个 epoch 只需要大约 60 - 70 秒(1 分钟)。 The NN is not complicated, it's only 2 convolutional layers + one dense layer. NN 并不复杂,它只有 2 个卷积层 + 1 个密集层。

nb_epochs = 4
bs = 32
model.fit(
    train_generator,
    steps_per_epoch = train_generator.samples//bs,
    validation_data = test_generator, 
    validation_steps = test_generator.samples//bs,
    epochs = nb_epochs)

I would like to know if it's normal that using ImageDataGenerator, training the network is slow especially on the first epoch?我想知道使用 ImageDataGenerator 训练网络是否正常,尤其是在第一个 epoch? If so is there a faster more efficient way to resize images and preprocess them before training the network?如果是这样,是否有更快更有效的方法来调整图像大小并在训练网络之前对其进行预处理?

Btw, I'm using Google Colab, and I enabled the GPU and checked that tensorflow is using the GPU顺便说一句,我使用的是 Google Colab,我启用了 GPU 并检查了 tensorflow 正在使用 GPU

Here's the link for the notebook, it's quite messy, I'm sorry: https://colab.research.google.com/drive/1FqPeqIhKo_lUjhoB2jlne1HnwItOJkIW?usp=sharing这是笔记本的链接,比较乱,对不起: https://colab.research.google.com/drive/1FqPeqIhKo_lUjhoB2jlne1HnwItOJkIW?usp=sharing

if you can specify workers=8 as kwarg in fit , try that.如果您可以将workers=8指定为 kwarg in fit ,请尝试一下。

if not, swap fit with the deprecated method fit_generator and see if you get a performance increase if you up the workers.如果没有,请将fit与不推荐使用的方法fit_generator交换,看看如果你提升工人,你是否会提高性能。 https://www.tensorflow.org/api_docs/python/tf/keras/Model https://www.tensorflow.org/api_docs/python/tf/keras/Model

model.fit_generator(
    train_generator,
    workers=8,
    steps_per_epoch = train_generator.samples//bs,
    validation_data = test_generator, 
    validation_steps = test_generator.samples//bs,
    epochs = nb_epochs)

Alternatively, you could add an average or max pool at the top of your neural net so down-sampling is built in the neural net, thereby leveraging gpu to perform the down-sampling.或者,您可以在神经网络的顶部添加一个平均池或最大池,以便在神经网络中构建下采样,从而利用 gpu 执行下采样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM