The tf.keras.layers.Conv2D() function in TensorFlow is a key building block of Convolutional Neural Networks (CNNs). It applies convolutional operations to input images, extracting spatial features that improve the model’s ability to recognize patterns.
The Conv2D layer applies a 2D convolution over an input image, performing the following operation:
where:
- convolution(input, kernel): A sliding window operation (filter) applied over the input image.
- kernel: A set of learnable weights (filters) that detect specific features.
- bias: A bias vector added to the convolution output.
- activation: An activation function applied element-wise.
Syntax of tf.keras.layers.Conv2D()
tf.keras.layers.Conv2D(
filters,
kernel_size,
strides=(1, 1),
padding='valid',
activation=None,
use_bias=True,
kernel_initializer="glorot_uniform",
bias_initializer="zeros"
)
Parameters Explained:
- filters (Required): Number of filters (kernels) applied in the convolution. Determines the number of output channels.
- kernel_size (Required): Size of the filter (e.g., (3,3) for a 3×3 kernel).
- strides (Default: (1,1)): Step size for moving the filter across the input.
- padding:
- 'valid' (default): No padding, output size is reduced.
- 'same': Zero-padding is added to keep the output size the same as the input.
- activation (Optional): Activation function (e.g., 'relu', 'sigmoid').
- use_bias (Default: True): Whether to include a bias term.
- kernel_initializer (Default: glorot_uniform): Defines how the filters are initialized.
- bias_initializer (Default: zeros): Defines how the bias is initialized.
Using Conv2D in a CNN Model
Below is the implementation of the CNN model.
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.utils import plot_model
from IPython.display import Image
# Define a simple CNN model
model = keras.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), # First conv layer
layers.MaxPooling2D(pool_size=(2, 2)), # Downsample the feature maps
layers.Conv2D(64, (3, 3), activation='relu'), # Second conv layer
layers.MaxPooling2D(pool_size=(2, 2)), # Downsample again
layers.Flatten(), # Flatten the output for the dense layer
layers.Dense(128, activation='relu'), # Fully connected layer
layers.Dense(10, activation='softmax') # Output layer (10 classes)
])
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
Output: