Extract local visual patterns from images
Image feature extraction
Keras · Class
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.
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.
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
)| Name | Type | Description |
|---|---|---|
filters | int | Integer; number of convolution filters and output channels. |
kernel_size | int or tuple/list of 2 integers | Convolution-window size as one integer or two integers. |
strides | int or tuple/list of 2 integers | Convolution stride along height and width. |
padding | string | Padding mode: valid or same. |
data_format | string or None | Input channel layout: channels_last or channels_first. |
dilation_rate | int or tuple/list of 2 integers | Dilation factors along the two spatial dimensions. |
groups | int | Number of groups for grouped convolution. |
activation | string or callable or None | Optional activation function. |
use_bias | bool | Whether to include a bias vector. |
kernel_initializer | initializer | Initializer for the convolution kernel. |
bias_initializer | initializer | Initializer for the bias vector. |
kernel_regularizer | regularizer or None | Regularizer for the kernel. |
A 4D tensor representing activation(conv2d(inputs, kernel) + bias).
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.
Image feature extraction
Classification backbones
Grouped architectures
Sequence-like image data
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.
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.
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.
Conv1DConv3DSeparableConv2DDepthwiseConv2DConv2DTransposeMaxPooling2D