Convolution helps computers find patterns in images or signals by sliding a small filter over data.
0
0
Convolution operation concept in TensorFlow
Introduction
When you want to detect edges or shapes in pictures.
When analyzing sound waves to find specific sounds.
When building image recognition apps like face detectors.
When processing video frames to track movement.
When extracting features from data for machine learning models.
Syntax
TensorFlow
tf.nn.conv2d(input, filters, strides, padding, data_format='NHWC', dilations=None, name=None)
input: The input data, usually a 4D tensor (batch, height, width, channels).
filters: The convolution kernels (small matrices) to slide over the input.
Examples
This applies a convolution with stride 1 and keeps the output size same as input.
TensorFlow
output = tf.nn.conv2d(input_tensor, filter_tensor, strides=[1,1,1,1], padding='SAME')
This applies convolution with stride 2 and no padding, so output is smaller.
TensorFlow
output = tf.nn.conv2d(input_tensor, filter_tensor, strides=[1,2,2,1], padding='VALID')
Sample Model
This code creates a 5x5 input and a 3x3 filter, then applies convolution with stride 1 and padding SAME. It prints the output shape and values.
TensorFlow
import tensorflow as tf import numpy as np # Create a simple 5x5 input with 1 channel input_data = 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) # Create a 3x3 filter with 1 input and 1 output channel filter_data = np.array([[[[1]],[[0]],[[1]]], [[[0]],[[1]],[[0]]], [[[1]],[[0]],[[1]]]], dtype=np.float32) # Convert to tensors input_tensor = tf.constant(input_data) filter_tensor = tf.constant(filter_data) # Apply convolution with stride 1 and SAME padding output = tf.nn.conv2d(input_tensor, filter_tensor, strides=[1,1,1,1], padding='SAME') print("Output shape:", output.shape) print("Output values:") print(output.numpy()[0,:,:,0])
OutputSuccess
Important Notes
Stride controls how much the filter moves each step; bigger stride means smaller output.
Padding='SAME' keeps output size same by adding zeros around input edges.
Filters learn to detect different features like edges or textures during training.
Summary
Convolution slides filters over input to find patterns.
Stride and padding affect output size and detail.
It is a key step in image and signal processing models.