Extract spatial features from images
Image classification
Keras · Class
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.
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.
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 | Number of output filters. |
kernel_size | int or tuple/list of 2 integers | Convolution window size; an integer or a pair of integers. |
strides | int or tuple/list of 2 integers | Step size of the window; defaults to (1, 1). |
padding | string | Padding mode, either valid or same; defaults to valid. |
data_format | string or None | Input data layout, such as channels_last or channels_first. |
dilation_rate | int or tuple/list of 2 integers | Dilation factor; defaults to (1, 1). |
groups | int | Number of groups for grouped convolution; defaults to 1. |
activation | string or callable or None | Optional activation function. |
use_bias | boolean | Whether to include a bias vector; defaults to True. |
kernel_initializer | initializer | Initializer for the kernel weights. |
bias_initializer | initializer | Initializer for the bias vector. |
kernel_regularizer | regularizer or None | Regularizer applied to kernel weights. |
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="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.
Image classification
Computer vision models
Grouped convolution architectures
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.
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.
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.
Conv1DConv3DDepthwiseConv2DSeparableConv2DConv2DTransposeMaxPooling2D