0
0
TensorFlowml~5 mins

Conv2D layers in TensorFlow

Choose your learning style9 modes available
Introduction

Conv2D layers help computers see and understand images by looking at small parts at a time.

When you want to recognize objects in photos.
When you need to find patterns in pictures, like edges or shapes.
When building apps that classify handwritten digits.
When creating filters to enhance or detect features in images.
Syntax
TensorFlow
tf.keras.layers.Conv2D(filters, kernel_size, strides=(1, 1), padding='valid', activation=None, input_shape=(height, width, channels))

filters is how many small windows (feature detectors) you want.

kernel_size is the size of the small window that looks at the image.

Examples
This creates 32 filters each looking at 3x3 parts of a 28x28 grayscale image.
TensorFlow
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))
This creates 64 filters with 5x5 size, moves 2 steps at a time, and keeps output size same as input.
TensorFlow
Conv2D(64, 5, strides=(2, 2), padding='same', activation='relu')
Sample Model

This code builds a simple image classifier with one Conv2D layer. It trains on random data for one round and shows prediction results.

TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models

# Build a simple model with one Conv2D layer
model = models.Sequential([
    layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    layers.Flatten(),
    layers.Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Create dummy data: 100 grayscale images 28x28 and labels
import numpy as np
x_train = np.random.random((100, 28, 28, 1)).astype('float32')
y_train = np.random.randint(0, 10, 100)

# Train the model for 1 epoch
history = model.fit(x_train, y_train, epochs=1, batch_size=10, verbose=2)

# Make predictions on first 5 images
predictions = model.predict(x_train[:5])
print('Predictions shape:', predictions.shape)
print('Predictions for first image:', predictions[0])
OutputSuccess
Important Notes

Use padding='same' to keep the output size equal to input size.

The activation function adds non-linearity, helping the model learn complex patterns.

Input shape must include channels last: height, width, channels (e.g., 28x28 grayscale is (28, 28, 1)).

Summary

Conv2D layers scan images with small windows to find features.

You choose how many filters and their size to control what the model learns.

Conv2D is key for image tasks like recognition and classification.