0
0
TensorFlowml~5 mins

Padding and stride in TensorFlow

Choose your learning style9 modes available
Introduction

Padding and stride help control how a convolutional layer processes images. Padding adds space around the image edges, and stride controls how the filter moves over the image.

When you want to keep the output image size the same as the input size.
When you want to reduce the output size to make the model faster.
When you want to control how much the filter moves over the image to capture features.
When you want to avoid losing important edge information in images.
When designing convolutional neural networks for image tasks like recognition or detection.
Syntax
TensorFlow
tf.keras.layers.Conv2D(filters, kernel_size, strides=(stride_height, stride_width), padding='valid' or 'same')

padding='valid' means no padding; the filter only moves inside the image.

padding='same' means padding is added to keep output size same as input size.

Examples
Filter moves one pixel at a time, no padding added.
TensorFlow
Conv2D(32, (3, 3), strides=(1, 1), padding='valid')
Filter moves two pixels at a time, padding added to keep output size.
TensorFlow
Conv2D(32, (3, 3), strides=(2, 2), padding='same')
Filter size 5x5, stride 1, padding added to keep output size.
TensorFlow
Conv2D(64, (5, 5), strides=(1, 1), padding='same')
Sample Model

This code shows how padding changes the output size and values. The kernel weights are all ones to make it easy to see the sum of values under the filter.

TensorFlow
import tensorflow as tf
import numpy as np

# Create a simple 5x5 grayscale image with one channel
input_image = np.array([[[[1], [2], [3], [4], [5]],
                         [[6], [7], [8], [9], [10]],
                         [[11], [12], [13], [14], [15]],
                         [[16], [17], [18], [19], [20]],
                         [[21], [22], [23], [24], [25]]]], dtype=np.float32)

# Define a Conv2D layer with kernel 3x3, stride 1, padding 'valid'
conv_valid = tf.keras.layers.Conv2D(filters=1, kernel_size=(3,3), strides=(1,1), padding='valid', use_bias=False,
                                   kernel_initializer=tf.constant_initializer(1.0))

# Define a Conv2D layer with kernel 3x3, stride 1, padding 'same'
conv_same = tf.keras.layers.Conv2D(filters=1, kernel_size=(3,3), strides=(1,1), padding='same', use_bias=False,
                                  kernel_initializer=tf.constant_initializer(1.0))

# Apply convolution with 'valid' padding
output_valid = conv_valid(input_image)

# Apply convolution with 'same' padding
output_same = conv_same(input_image)

print('Output shape with valid padding:', output_valid.shape)
print('Output values with valid padding:\n', output_valid.numpy().squeeze())

print('Output shape with same padding:', output_same.shape)
print('Output values with same padding:\n', output_same.numpy().squeeze())
OutputSuccess
Important Notes

Stride larger than 1 reduces output size by skipping positions.

Padding='same' adds zeros around the input to keep output size equal to input size.

Using all ones kernel helps understand how convolution sums input values under the filter.

Summary

Padding controls if and how much space is added around input edges.

Stride controls how far the filter moves each step over the input.

Together, they affect output size and what features the model can learn.