I'm trying to follow François Chollet's code from Deep Learning with R but hitting an error in section 5.2 (using covnets with small datasets) which I don't fully understand.
The model using 2,000 training samples (and 1,000 validation samples) from Dogs vs. Cats is supposedly demonstrating the value of data augmentation and dropouts in improving accuracy when using small datasets:
datagen <- image_data_generator(
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
)
test_datagen <- image_data_generator(rescale = 1/255)
train_generator <- flow_images_from_directory(
directory = train_dir,
generator = datagen,
target_size = c(150, 150),
batch_size = 32,
class_mode = "binary"
)
validation_generator <- flow_images_from_directory(
validation_dir,
test_datagen,
target_size = c(150, 150),
batch_size = 32,
class_mode = "binary"
)
history <- model %>% fit(
train_generator,
steps_per_epoch = 100,
epochs = 100,
validation_data = validation_generator,
validation_steps = 50
)
However, after 63 steps of the first epoch, Tensorflow errors:
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, 10000 batches). You may need to use the repeat() function when building your dataset.
I understand that this is because in a sample of 2,000 images, there's only 62.5 batches of 32; but flow_images_from_directory()
should result in an endless loop of augmented images, so I don't understand why that's not being passed to fit()
flow_images_from_directory {keras}: Yields batches indefinitely, in an infinite loop
Is there a solution to make data augmentation work? I can't find reference to using repeat()
as suggested by the error and this Python solution within the R implementation of Keras. The same question has been asked regarding the Python version of the book, but the "solutions" of using smaller batches don't really solve the problem either when the generator should be outputting an infinite steam of augmented images.