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

Keras · Class

layers.Conv2D

Conv2D applies a learnable two-dimensional convolution across height and width. It produces a configurable number of output channels and can add a bias and activation.

How it works

Conv2D applies learnable two-dimensional convolution windows across an input's height and width. It produces the requested number of output channels, optionally adds a bias, and can apply an activation function; padding, strides, dilation, and groups control how the windows operate.

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
filtersintInteger; number of convolution filters and output channels.
kernel_sizeint or tuple/list of 2 integersConvolution-window size as one integer or two integers.
stridesint or tuple/list of 2 integersConvolution stride along height and width.
paddingstringPadding mode: valid or same.
data_formatstring or NoneInput channel layout: channels_last or channels_first.
dilation_rateint or tuple/list of 2 integersDilation factors along the two spatial dimensions.
groupsintNumber of groups for grouped convolution.
activationstring or callable or NoneOptional activation function.
use_biasboolWhether to include a bias vector.
kernel_initializerinitializerInitializer for the convolution kernel.
bias_initializerinitializerInitializer for the bias vector.
kernel_regularizerregularizer or NoneRegularizer for the kernel.

Returns

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

Key points

  • Conv2D performs convolution over two spatial dimensions: height and width.
  • filters controls the number of output channels produced by the layer.
  • kernel_size accepts one integer or two integers describing the window dimensions.
  • padding supports valid and same, without case sensitivity.
  • use_bias determines whether a trainable bias vector is added after convolution.
  • activation optionally transforms the convolution result after bias addition.
  • The output is a rank-4 tensor representing the activated convolution, with bias when enabled.

Common mistakes

  • Do not combine strides greater than 1 with dilation_rate greater than 1.
  • Same padding preserves spatial dimensions only when the stride is 1.

Example: Basic Conv2D model

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="relu")(x)
model = keras.Model(inputs, outputs)
model.summary()

Creates a model with two 2D convolution layers for single-channel images.

When to use it

Extract local visual patterns from images

Image feature extraction

Build spatial feature hierarchies

Classification backbones

Reduce channel connectivity with grouped convolution

Grouped architectures

Apply the same convolution layer across time with TimeDistributed

Sequence-like image data

Interview questions

QWhat does Conv2D compute?

Conv2D slides a learned kernel over the input's two spatial dimensions and returns a tensor with one output channel per filter. With use_bias enabled, it adds a bias and then applies the optional activation.

Follow-up: How does padding affect spatial dimensions? With valid padding there is no padding; same padding pads the edges and preserves each spatial dimension when the stride is 1.

QWhat are filters and kernel_size?

filters specifies the output-space size, meaning how many convolution filters are used. kernel_size specifies the two-dimensional convolution-window size, and it may be one integer or a pair of integers.

Follow-up: What determines the output height and width? The kernel size, strides, dilation rate, and padding determine the resulting spatial dimensions.

QWhat input layouts does Conv2D support?

The layer accepts rank-4 inputs. With channels_last, the shape is (batch, height, width, channels); with channels_first, it is (batch, channels, height, width).

Follow-up: How do groups change the operation? The input channels are split into groups, each filter group operates on its corresponding input group, and the group divisibility requirements must hold.

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

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