When dealing with large sets of images, we read batches of images at a time, but I am assuming we feed the CNN models one image at a time. This brings up my confusion around Input_Shape
definition.
Consider the following code for preprocessing images and modeling:
dataset = tf.keras.preprocessing.image_dataset_from_directory(
datadir,
batch_size = Batch_Size,
image_size = (Image_Size, Image_Size),
shuffle = True)
# Image Processing: Rescaling and Resizing
resize_and_rescale = tf.keras.Sequential([
layers.experimental.preprocessing.Resizing(Image_Size, Image_Size),
layers.experimental.preprocessing.Rescaling(1.0/255)
])
input_shape = (Batch_Size, Image_Size, Image_Size, Channels)
num_classes = 3
model = models.Sequential([
resize_and_rescale,
layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=input_shape),
layers.MaxPool2D((2,2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.MaxPool2D((2,2)),
layers.Conv2D(128, (3,3), activation='relu'),
layers.MaxPool2D((2,2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.MaxPool2D((2,2)),
layers.Conv2D(128, (3,3), activation='relu'),
layers.MaxPool2D((2,2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.MaxPool2D((2,2)),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes, activation='softmax'),
])
model.build(input_shape=input_shape)
The first line of the Sequential model is the preprocessing step, which is carried out on a single image. This is clear form the image_dataset_from_directory()
calll where the Image_size = (Image_Size, Image_Size)
.
The second line, the call to Conv2D
specifies an input shape that also includes the Batch_Size
.
My confusion is, if the model works on one image at a time, why does the batch size need to be included in the Conv2D
call?