Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly set steps_per_epoch and validation_steps in Keras?

I've trained several models in Keras. I have 39, 592 samples in my training set, and 9, 899 in my validation set. I used a batch size of 2.

As I was examining my code, it occurred to me that my generators may have been missing some batches of data.

This is the code for my generator:

train_datagen = ImageDataGenerator(
            rescale=1. / 255,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True)

val_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
            train_dir,
            target_size=(224, 224)
            batch_size=batch_size,
            class_mode='categorical')

validation_generator = val_datagen.flow_from_directory(
            val_dir,
            target_size=(224, 224),
            batch_size=batch_size,
            class_mode='categorical')

I searched around to see how my generators behave, and found this answer: what if steps_per_epoch does not fit into numbers of samples?

I calculated my steps_per_epoch and validation_steps this way:

steps_per_epoch = int(number_of_train_samples / batch_size)
val_steps = int(number_of_val_samples / batch_size)

Using the code in this link with my own batch size and number of samples, I got these results: "missing the last batch" for train_generator and "weird behavior" for val_generator.

I'm afraid that I have to retrain my models again. What values should I choose for steps_per_epoch and validation_steps? Is there a way to use exact values for these variables(Other than setting batch_size to 1 or removing some of the samples)? I have several other models with different number of samples, and I think they've all been missing some batches. Any help would be much appreciated.

Two related question:

1- Regarding the models I already trained, are they reliable and properly trained?

2- What would happen if I set these variables using following values:

steps_per_epoch = np.ceil(number_of_train_samples / batch_size)
val_steps = np.ceil(number_of_val_samples / batch_size)

will my model see some of the images more than once in each epoch during training and validation? or Is this the solution to my question?!

like image 554
james Avatar asked Aug 16 '18 21:08

james


People also ask

How do I set batch size and steps per epoch?

An epoch usually means one iteration over all of the training data. For instance if you have 20,000 images and a batch size of 100 then the epoch should contain 20,000 / 100 = 200 steps.

How do you set steps per epoch with keras?

In Keras model, steps_per_epoch is an argument to the model's fit function. Steps_per_epoch is the quotient of total training samples by batch size chosen. As the batch size for the dataset increases the steps per epoch reduce simultaneously and vice-versa.

What is epoch and Steps_per_epoch mean?

steps_per_epoch: Total number of steps (batches of samples) to yield from generator before declaring one epoch finished and starting the next epoch. It should typically be equal to the number of unique samples of your dataset divided by the batch size.

What is the purpose of the Steps_per_epoch function in the Fit_generator?

-> steps_per_epoch : it specifies the total number of steps taken before one epoch has finished and started the next epoch. By default it values is set to NULL.


1 Answers

Since Keras data generator is meant to loop infinitely, steps_per_epoch indicates how many times you will fetch a new batch from generator during single epoch. Therefore, if you simply take steps_per_epoch = int(number_of_train_samples / batch_size), your last batch would have less than batch_size items and would be discarded. However, in your case, it's not a big deal to lose 1 image per training epoch. The same is for validation step. To sum up: your models are trained [almost :) ] correctly, because the quantity of lost elements is minor.

Corresponding to implementation ImageDataGenerator https://keras.io/preprocessing/image/#imagedatagenerator-class if your number of steps would be larger than expected, after reaching the maximum number of samples you will receive new batches from the beginning, because your data is looped over. In your case, if steps_per_epoch = np.ceil(number_of_train_samples / batch_size) you would receive one additional batch per each epoch which would contains repeated image.

like image 89
Greeser Avatar answered Oct 16 '22 04:10

Greeser