Module 4 Lab: CNN feature maps#
Inspect how convolutional filters respond to simple synthetic image patterns.
Run the setup cell, inspect the printed diagnostics, and then complete the exercises at the end. The lab is intentionally small enough to run in GitHub Codespaces without a GPU.
import torch
from torch import nn
import matplotlib.pyplot as plt
images = torch.zeros(4, 1, 16, 16)
images[0, :, 4:12, 7:9] = 1.0
images[1, :, 7:9, 4:12] = 1.0
images[2, :, torch.arange(4, 12), torch.arange(4, 12)] = 1.0
images[3] = torch.rand(1, 16, 16) * 0.4
conv = nn.Conv2d(1, 3, kernel_size=3, padding=1, bias=False)
with torch.no_grad():
conv.weight[0, 0] = torch.tensor([[-1., 0., 1.], [-1., 0., 1.], [-1., 0., 1.]])
conv.weight[1, 0] = torch.tensor([[-1., -1., -1.], [0., 0., 0.], [1., 1., 1.]])
conv.weight[2, 0] = torch.tensor([[0., 1., 0.], [1., -4., 1.], [0., 1., 0.]])
features = conv(images)
print("image batch:", tuple(images.shape))
print("feature maps:", tuple(features.shape))
print("vertical-edge response mean:", features[:, 0].abs().mean().item())
plt.figure(figsize=(4, 3))
plt.imshow(features[0, 0].detach(), cmap="gray")
plt.axis("off")
plt.close()
image batch: (4, 1, 16, 16)
feature maps: (4, 3, 16, 16)
vertical-edge response mean: 0.2150549739599228
Lab exercises#
Change one model or data parameter and rerun the lab.
Record whether the metric improved, worsened, or stayed roughly the same.
Add one sentence connecting the result to Convolutional neural networks for vision.
Identify one limitation of this toy setup before applying the idea to a real dataset.
# Reflection workspace
observation = ""
next_experiment = ""
print({"observation": observation, "next_experiment": next_experiment})
{'observation': '', 'next_experiment': ''}