Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

Conv2D

Conv2D applies learned filters across an input's height and width to produce 2D feature maps. Strides, padding, dilation, groups, bias, and activation control how the transformation is performed.

How it works

Conv2D slides learned filters over an input's height and width to create output feature maps. It can add a bias and then apply an activation function; stride and padding determine the spatial dimensions, while the filters argument determines the output channel count.

Syntax

keras.layers.Conv2D(
    filters,
    kernel_size,
    strides=(1, 1),
    padding="valid",
    data_format=None,
    dilation_rate=(1, 1),
    groups=1,
    activation=None,
    use_bias=True,
    kernel_initializer="glorot_uniform",
    bias_initializer="zeros",
    kernel_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    kernel_constraint=None,
    bias_constraint=None,
    **kwargs
)

Parameters

NameTypeDescription
filtersintNumber of output filters.
kernel_sizeint or tuple/list of 2 integersConvolution window size; an integer or a pair of integers.
stridesint or tuple/list of 2 integersStep size of the window; defaults to (1, 1).
paddingstringPadding mode, either valid or same; defaults to valid.
data_formatstring or NoneInput data layout, such as channels_last or channels_first.
dilation_rateint or tuple/list of 2 integersDilation factor; defaults to (1, 1).
groupsintNumber of groups for grouped convolution; defaults to 1.
activationstring or callable or NoneOptional activation function.
use_biasbooleanWhether to include a bias vector; defaults to True.
kernel_initializerinitializerInitializer for the kernel weights.
bias_initializerinitializerInitializer for the bias vector.
kernel_regularizerregularizer or NoneRegularizer applied to kernel weights.

Returns

A 4D tensor representing activation(conv2d(inputs, kernel) + bias).

Key points

  • The layer learns a kernel and applies it across the input's two spatial dimensions.
  • filters controls the number of learned filters and therefore the output channel count.
  • kernel_size can be one integer or two integers describing the convolution window.
  • strides controls how far the convolution window moves along each spatial axis.
  • padding accepts valid or same, without case sensitivity.
  • use_bias determines whether the layer adds a learned bias vector.
  • An activation function can be applied after convolution and bias addition.
  • The returned value is a 4D tensor representing the activated convolution output.

Common mistakes

  • Do not combine a stride greater than 1 with a dilation rate greater than 1.
  • Same padding preserves height and width only when the stride is 1.

Example: Stacking Conv2D layers

import keras
from keras import layers

inputs = keras.Input(shape=(28, 28, 1))
x = layers.Conv2D(32, 3, padding="same", activation="relu")(inputs)
outputs = layers.Conv2D(10, 3, activation="softmax")(x)
model = keras.Model(inputs, outputs)
model.summary()

Builds a model with two 2D convolution layers. The first preserves spatial dimensions with same padding; the second produces 10 output channels and applies softmax.

When to use it

Extract spatial features from images

Image classification

Build hierarchical feature extractors

Computer vision models

Split convolution work across channel groups

Grouped convolution architectures

Interview questions

QWhat does a Conv2D layer compute?

Conv2D applies learned kernels across the input's two spatial dimensions and returns feature maps. The number of output channels equals filters, and an optional bias and activation are applied afterward.

Follow-up: How does padding affect output size? With valid padding, no border padding is added; with same padding and stride 1, the spatial size remains unchanged.

QWhat are filters and kernel_size used for?

filters sets the output-space dimension, meaning how many convolution filters and output channels the layer has. kernel_size sets the height and width of each convolution window.

Follow-up: What input layout is expected? The layer accepts 4D inputs, with channels-last or channels-first ordering controlled by data_format.

QWhat output shape does Conv2D produce?

For channels-last input, the output shape is (batch_size, new_height, new_width, filters). For channels-first input, the corresponding layout places filters before the two spatial dimensions.

Follow-up: What controls new_height and new_width? Kernel size, padding, strides, and dilation rate jointly determine the resulting spatial dimensions.

  • Conv1D
  • Conv3D
  • DepthwiseConv2D
  • SeparableConv2D
  • Conv2DTranspose
  • MaxPooling2D

Source: Conv2D layer - Keras. Checked 2026-10-15.