在 Tensorflow 2.0 中获取数据错误 运行

getting error ran out of data in Tensor flow 2.0

我正在数据集上训练多 class classifier: 总数据集:1197 张图片 训练数据集:599 张图片 Validation/Test 数据集:598 张图片

我的代码如下 -

import tensorflow as tf
import keras_preprocessing
from keras_preprocessing import image
from keras_preprocessing.image import ImageDataGenerator

TRAINING_DIR = "/content/drive/My Drive/solar_images/solar_images/train/"
training_datagen = ImageDataGenerator(
      rescale = 1./255,
        rotation_range=40,
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest')

VALIDATION_DIR = "/content/drive/My Drive/solar_images/solar_images/test/"
validation_datagen = ImageDataGenerator(rescale = 1./255)

train_generator = training_datagen.flow_from_directory(
    TRAINING_DIR,
    target_size=(150,150),
    class_mode='categorical',
  batch_size=126
)

validation_generator = validation_datagen.flow_from_directory(
    VALIDATION_DIR,
    target_size=(150,150),
    class_mode='categorical',
  batch_size=126
)


model = tf.keras.models.Sequential([
    # Note the input shape is the desired size of the image 150x150 with 3 bytes color
    # This is the first convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(150, 150, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    # The second convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The third convolution
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The fourth convolution
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # Flatten the results to feed into a DNN
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dropout(0.5),
    # 512 neuron hidden layer
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
])


model.summary()

model.compile(loss = 'categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

history = model.fit(train_generator, 
                    epochs=25, 
                    steps_per_epoch=20, 
                    validation_data = validation_generator, 
                    verbose = 1, 
                    validation_steps=3)

model.save("solar_images_weight.h5")

但我收到错误 -

Epoch 1/25
 5/20 [======>.......................] - ETA: 42:21 - loss: 2.3594 - accuracy: 0.3306WARNING: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, 40 batches). You may need to use the repeat() function when building your dataset.
 5/20 [======>.......................] - 2378s 476s/step - loss: 2.3594 - accuracy: 0.3306 - val_loss: 1.1150 - val_accuracy: 0.3069

我正在使用以下代码,但仍然无法正常工作。

batch_size=126
history = model.fit(train_generator, 
                    epochs=25, 
                    steps_per_epoch=int(20/batch_size), 
                    validation_data = validation_generator, 
                    verbose = int(1/batch_size), 
                    validation_steps=3)

我主要关注这个link

有什么帮助吗?

这个问题在您提供的 link 中得到了很好的回答,是 steps_per_epoch 和 validation_steps 需要等于除以batch_size

试试这个

history = model.fit(train_generator, 
                    epochs=25, 
                    steps_per_epoch=int(599/batch_size), 
                    validation_data=validation_generator,
                    validation_steps=int(598/batch_size) 
                    )