Reputation: 305
I have 1323 images with labels associated belonging to 8 different classes. Each image only has 1 class.
I have some doubts regarding the variable batch_size. I understand that this variable determines how many images are going to pass in an epoch and it is usually 32.
However, if I use the code below, in python, doesn't the train_generator
variable stays only with 32 images instead of the original 1323? So how can I train the neural network with all the images instead of just 32?
train_dataGen = ImageDataGenerator(rescale=1./255)
train_generator = train_dataGen.flow_from_dataframe(
dataframe = training_set, directory="",
x_col="Images", y_col="Labels",
class_mode="categorical",
target_size=(224,224), batch_size=32)
Upvotes: 0
Views: 641
Reputation: 77827
Regardless of the batch size, you use all 1323 images in every epoch. That is the definition of an epoch: processing each image in the training set once.
The batch size is the quantity of images you handle in between iterations of back-propagation. With a batch size of 32, the model will do the forward run on each of the 32 images, average (mean) the results, and only then will it perform back-propagation, using that average.
A batch size of 1 would mean that you train on each image individually. This tends to give too much weight to presumptions made on the training results of the first few images. A batch size of 1323 would mean that you train on the entire training set before doing back-propagation. This tends to lose details of some significant features.
You need to experiment with the batch size to find the "sweet spot" in your training performance. Some framework implementations are performance-optimized for powers of 2, so 32 would be a good starting point. You can pick a size close to the quantity of processors you have for parallel processing of inputs. Another good choice is something close to the square root of the training set size (32 also serves well in this respect).
Upvotes: 1