how to set correct batch_size and steps_per_epoch in keras?

Solution for how to set correct batch_size and steps_per_epoch in keras?
is Given Below:

I have 20000 RGB images. I set batch_Size = 1 (due to GPU capacity). So now does it mean the model weights are changing with one-by-one pictures or it depends on the steps_per_epoch?

How should I set the steps_per_epoch and epochs for using all of 20000 images to be involved in training in different epochs?

Yes, the weights are updated after each batch.
The steps_per_epoch should be the number of datapoints (20000 in your case) divided by the batch size. Therefore steps_per_epoch will also be 20000 if the batch size is 1.

Let’s first clear some concepts:

  • Each iteration is visiting a batch of samples in dataset.
  • Each epoch is visiting all samples in dataset.
  • Weights are being updated after each iteration.

How related batch size and steps per epoch to the above concepts?

  • batch_size: Determines the number of samples in each iteration (updating weights). Minimum batch size is 1 (called stochastic gradient descent) and maximum can be the number of all samples (even more – read about repeat() here). There is another limitation for maximum batch size which is fitting to available GPU memory as you said in your question. Setting the batch_size to lower numbers makes iterations faster, but loss decreasing will oscillate more.

  • steps_per_epoch: The number of iterations in order to consider one epoch is finished. If you have a training set of fixed size you can ignore it but it may be useful if you have a huge data set or if you are generating data augmentations on the fly, i.e. if your training set has a (generated) infinite size with repeat() function. If you have the time to go through your whole training data set you can skip this parameter.