tensorflow - 在Keras中构建数据时如何使用repeat()函数?

标签 tensorflow keras deep-learning image-preprocessing

我正在训练猫和狗的数据集上的二进制分类器:
数据集总数:10000张
训练数据集:8000张图像
验证/测试数据集:2000张图像

Jupyter笔记本代码:

# Part 2 - Fitting the CNN to the images
train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')

history = model.fit_generator(training_set,
                              steps_per_epoch=8000,
                              epochs=25,
                              validation_data=test_set,
                              validation_steps=2000)

我在CPU上毫无问题地训练了它,但是当我在GPU上运行时,抛出此错误:
Found 8000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
WARNING:tensorflow:From <ipython-input-8-140743827a71>:23: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
Please use Model.fit, which supports generators.
WARNING:tensorflow:sample_weight modes were coerced from
  ...
    to  
  ['...']
WARNING:tensorflow:sample_weight modes were coerced from
  ...
    to  
  ['...']
Train for 8000 steps, validate for 2000 steps
Epoch 1/25
 250/8000 [..............................] - ETA: 21:50 - loss: 7.6246 - accuracy: 0.5000
WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 200000 batches). You may need to use the repeat() function when building your dataset.
 250/8000 [..............................] - ETA: 21:52 - loss: 7.6246 - accuracy: 0.5000

我想知道如何使用Tensorflow 2.0在keras中使用repeat()函数吗?

最佳答案

您的问题源于以下事实:参数steps_per_epochvalidation_steps必须等于除以batch_size的数据点总数。
在2017年8月之前,您的代码将在Keras 1.X中运行。
将您的model.fit函数更改为:

history = model.fit_generator(training_set,
                              steps_per_epoch=int(8000/batch_size),
                              epochs=25,
                              validation_data=test_set,
                              validation_steps=int(2000/batch_size))
从TensorFlow2.1开始,不推荐使用fit_generator()。您也可以在生成器上使用.fit()方法。
TensorFlow> = 2.1代码:
history = model.fit(training_set.repeat(),
                    steps_per_epoch=int(8000/batch_size),
                    epochs=25,
                    validation_data=test_set.repeat(),
                    validation_steps=int(2000/batch_size))
请注意,int(8000/batch_size)等效于8000 // batch_size(整数除法)

关于tensorflow - 在Keras中构建数据时如何使用repeat()函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60509425/

相关文章:

python - 在Keras中实现因果CNN以进行多元时间序列预测

python - 从维度为 [N] 的张量创建维度为 [N,D] 的矩阵

python - 将 Keras 转换为 TensorFlow——剪枝+概率

tensorflow - 如何在 TensorFlow 中编码标签?

python - 将 Keras 模型转换为 tensorflow 模型给我错误

tensorflow - 如何在 Keras 中翻转张量?

keras - 在 PyTorch 中准备序列到序列网络的解码器

python - 尝试理解caffe中的自定义损失层

tensorflow - 如何在张量板中绘制图像网格?

javascript - Tensorflow/ffjs : Embedding Layer Weights are NaN after training with model. fit() 方法