Convolutional layers and Max Pooling layers
In the previous article, we created a simple image classification model using TensorFlow. In this article we’re going to enhance the…
Convolutional layers and Max Pooling layers
In the previous article, we created a simple image classification model using TensorFlow. In this article we’re going to enhance the accuracy of the same model using some extra layers. So I highly recommend you guys to read the previous article before continue.
Convolutional Layers
So basically we use convolutional layers to highlight specific patterns like edges, textures, or shapes of the images.
— What happens within Convolutional Layer ?? — Convolutional layers use filters(small kernels) that slide across the image. These filters look for specific patterns like edges, textures, or shapes. When defining this layer, we need to tell it how many filters it should use, what size the filter should be and what activation function it should use.

2D Convolution Animation
# Convolutional layer
tf.keras.layers.Conv2D(64, (3,3), activation='relu')
So let’s get a better idea what above code line says.
First parameter says how many filters create for this layer. In the above line, it uses 64 different filters. The second paramter says that each individual filter is a 3x3 grid. Each filter slides across the input image independently. When a filter finds the pattern it’s looking for, it outputs a high number. If it doesn’t, it outputs a zero (especially thanks to that relu activation function, which turns negative numbers into flat zeros).
Max Pooling Layers
After finding patterns, we use Max Pooling to simplify the data. It looks at a small window of the image and only keeps the maximum value (the strongest signal). In other words, it’s like an image compression.
# Max Pooling layer
tf.keras.layers.MaxPooling2D(2,2)
In the above line, the (2,2) means you are using a 2x2 pixel window to scan across the image, and it moves forward by a stride of 2 pixels at a time.
— What happens within Max Pooling Layer ?? — Imagine the layer looks at the top left 2x2 corner of your feature map (a total of 4 pixels). Finds the Maximum: It looks at those 4 pixel values and throws away 3 of them, keeping only the single largest number. Strides Forward: It hops 2 pixels to the right (so it doesn’t overlap with the pixels it just processed) and grabs the 2x2 block. Repeats: It repeats this across the entire image, row by row.
So after adding all the new layers to the model(from the previous article), it looks like below
# Define the model
model = tf.keras.models.Sequential([
# Add convolutions and max pooling
tf.keras.Input(shape=(28,28,1)),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# Add the same layers as before
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
So in above model, we can see there are two layer stacks (Conv2D + MaxPooling2D) included. why is that ?
When you look at an image of a cat, your brain doesn’t just see pixels. It sees lines, which form shapes (ears, eyes), which form a whole animal. Stacking layers forces the network to learn the exact same way.
- Stack 1 : The first layer looks at the raw 2x2 pixels. Because its field of view is tiny, it can only see local, microscopic details. It learns to detect simple edges, lines, and textures.
- Stack 2 : This layer doesn’t look at the raw pixels. It looks at the features found by Stack 1. Because the first Max Pooling layer shrank the image space, the second layer’s 3x3 filters actually span across a much larger percentage of the original image area. This allows it to combine those simple edges into complex shapes, contours, and object parts.
By repeating this pattern, the network moves from seeing “isolated lines” to seeing “arrangements of shapes.”
Alright, I hope you guys got some idea about convolutional layers and max pooling layers. If you’re still not confident enough about those topics, just don’t worry. Practice makes perfect so don’t give up.
In addition to that, let’s take a look at some extra functionality we can use when training our model.
Callbacks to Control Training
When training a model, you don’t always need to complete all epochs. You can stop the training early once a specific threshold is reached. For example, if you set 1000 epochs and your desired accuracy is already reached at epoch 200, then the training will automatically stop. Let’s see how to implement this.
Creating a Callback class
You can create a callback by defining a class that inherits the tf.keras.callbacks.Callback base class. From there, you can define available methods to set where the callback will be executed. In below, I will use the on_epoch_end() method to check the loss at each training epoch.
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
'''
Halts the training when the loss falls below 0.4
Args:
epoch (integer) - index of epoch (required but unused in the function definition below)
logs (dict) - metric results from the training epoch
'''
# Check the loss
if logs['loss'] < 0.4:
# Stop if threshold is met
print("\nLoss is lower than 0.4 so cancelling training!")
self.model.stop_training = True
To set the callback, simply set the callbacks parameter to an instance of myCallback put into a list. Try to do it by yourself and observe what happens.
# Train the model with a callback
model.fit(training_images, training_labels, epochs=5, callbacks=[myCallback()])
Switching from loss to accuracy
If you want to stop training based on reaching a certain accuracy threshold instead of a lower loss, you only need to change two things. The dictionary key you are looking up and the comparison operator.
Because we want training to stop when accuracy gets higher than a threshold (unlike loss, which we want to get lower), we switch < to >. Here is the updated class:
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
# Check if training accuracy is above 90%
if logs('accuracy') > 0.90:
print("\nReached 90% accuracy, so cancelling training!")
self.model.stop_training = True
And that’s a wrap
Congratulations!! You’ve just learned how to give your models better “eyes” with Convolutions and Max Pooling, and how to stop training at the perfect moment using Callbacks. Thanks for building along with me! Keep experimenting, don’t be afraid to break the code and fix it again, and happy coding. Stay tuned for the next article, and we’ll definitely chat more soon.
메타데이터
- post_id
- d76097f6c384
- slug
- convolutional-layers-and-max-pooling-layers-d76097f6c384
- url
- https://medium.com/@isharaharshana06/convolutional-layers-and-max-pooling-layers-d76097f6c384
- canonical_url
- https://medium.com/@isharaharshana06/convolutional-layers-and-max-pooling-layers-d76097f6c384
- author_url
- https://medium.com/@isharaharshana06
- status
- ok
- fetched_at
- 2026-06-09 15:37:30