Image feature extraction
Extracting local visual patterns from images.
TensorFlow · Class
A Keras layer that applies learned two-dimensional convolution filters to inputs. It can add bias and an activation while configurable padding, strides, dilation, layout, and groups determine the resulting feature maps.
Conv2D learns a bank of spatial kernels and applies them across a 2D input. It can add a bias and then run an activation function, producing one output channel per filter while padding, stride, layout, dilation, and grouping control how the spatial operation is performed.
tf.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 filters, or output channels. |
kernel_size | int or tuple/list of 2 integers | Convolution-window height and width. |
strides | int or tuple/list of 2 integers | Window step along each spatial dimension. |
padding | string | Padding mode: valid or same. |
data_format | string or None | Input/output dimension ordering. |
dilation_rate | int or tuple/list of 2 integers | Spacing between kernel elements. |
groups | int | Number of channel groups. |
activation | activation function or None | Optional activation function. |
use_bias | bool | Whether to add a bias vector. |
kernel_initializer | initializer | Initializer for the convolution kernel. |
bias_initializer | initializer | Initializer for the bias. |
kernel_regularizer | regularizer or None | Regularizer applied to the kernel. |
A tensor containing the layer's convolution outputs; its spatial layout follows data_format and its channel dimension is filters.
import tensorflow as tf
layer = tf.keras.layers.Conv2D(
filters=32,
kernel_size=3,
padding="same",
activation="relu",
)
x = tf.random.normal((1, 28, 28, 3))
y = layer(x)
print(y.shape) # (1, 28, 28, 32)Creates a 3×3 convolution with 32 output channels, preserves spatial dimensions with same padding, and applies ReLU.
Extracting local visual patterns from images.
Building convolutional classifiers or regressors.
Reducing computation or controlling channel connectivity.
Conv2D applies learned two-dimensional kernels over the input and returns feature maps. The number of output channels equals filters; optional bias is added before the optional activation.
Follow-up: How does same padding affect output size? With stride one, it keeps each spatial dimension equal to the input; with larger strides, the output is downsampled.
The input layout is controlled by data_format. channels_last uses batch, height, width, channels, while channels_first uses batch, channels, height, width.
Follow-up: What must change when switching layouts? The input tensor arrangement must match data_format, and downstream layers must interpret the resulting output consistently.
filters sets the output-space dimension, and kernel_size sets the two-dimensional convolution-window size. strides controls the window step, while dilation_rate spaces kernel elements apart.
Follow-up: When would groups be useful? They split channel connections into independent groups, reducing connectivity; the group count must divide both input channels and filters.
tf.keras.layers.Convolution2Dtf.keras.layers.DepthwiseConv2Dtf.nn.conv2d