Lesson 3 of 12
Filters and Feature Maps
Each convolutional layer applies multiple filters to produce multiple feature maps. These feature maps capture different aspects of the input image.
Feature Maps Explained
When a convolutional layer has N filters, it produces N feature maps — one per filter.
Example: a first convolutional layer with 64 filters:
- Takes a 224×224×3 (RGB) input image
- Applies 64 different 3×3 filters
- Produces 64 feature maps — each highlighting different patterns (edges at different angles, colour contrasts, etc.)
- Output shape: 224×224×64 (approximately — depends on padding)
In early layers:
- Filter 1 might detect vertical edges
- Filter 2 might detect horizontal edges
- Filter 3 might detect blue-yellow contrasts
- Filter 4 might detect red-green contrasts
In later layers:
- Filters detect more complex patterns: eyes, wheels, fur textures, face shapes
- Each feature map is a compressed representation that highlights where that pattern occurs
The deep layer features are the representations fed into the final classifier to make predictions.
Machine Learning follows a structured pipeline from data to deployment
Visualising Feature Responses
import cv2
import numpy as np
# Visualise how different filters respond to the same image
image = cv2.imread("photo.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).astype(np.float32)
# A set of filters similar to what CNNs learn in early layers
filters = {
"Vertical edges": np.array([[-1,0,1],[-2,0,2],[-1,0,1]], np.float32),
"Horizontal edges": np.array([[-1,-2,-1],[0,0,0],[1,2,1]], np.float32),
"Diagonal (/)": np.array([[0,1,2],[-1,0,1],[-2,-1,0]], np.float32),
"Blur (smooth)": np.ones((3,3), np.float32) / 9,
}
print("Filter responses (feature maps):")
for name, filt in filters.items():
# Apply filter = compute feature map
feature_map = cv2.filter2D(gray, -1, filt)
feature_map_abs = np.abs(feature_map)
# Statistics for this feature map
mean_response = feature_map_abs.mean()
max_response = feature_map_abs.max()
active_pixels = (feature_map_abs > 50).sum()
print(f" {name}:")
print(f" Mean response: {mean_response:.1f}")
print(f" Max response: {max_response:.1f}")
print(f" Active pixels: {active_pixels:,}")
# Save feature map
save_name = name.lower().replace(" ", "_").replace("(", "").replace(")", "").replace("/", "diag")
cv2.imwrite(f"feature_map_{save_name}.jpg",
np.clip(feature_map_abs, 0, 255).astype(np.uint8))Finished this lesson?