An FCN helps computers understand images by labeling each pixel. It turns images into meaningful maps, like coloring each part of a photo.
FCN (Fully Convolutional Network) in Computer Vision
class FCN(nn.Module): def __init__(self, num_classes): super(FCN, self).__init__() self.conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1) self.relu = nn.ReLU() self.conv2 = nn.Conv2d(64, num_classes, kernel_size=1) def forward(self, x): x = self.relu(self.conv1(x)) x = self.conv2(x) return x
FCNs use only convolution layers, no fully connected layers.
Output is a map with the same spatial size as input, but with class scores per pixel.
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(32, num_classes, kernel_size=1)
output = model(input_tensor)
# output shape: (batch_size, num_classes, height, width)This code builds a simple FCN with two convolution layers. It takes a small image and predicts class scores for each pixel. Then it converts scores to probabilities.
import torch import torch.nn as nn import torch.nn.functional as F class FCN(nn.Module): def __init__(self, num_classes): super(FCN, self).__init__() self.conv1 = nn.Conv2d(3, 64, 3, padding=1) self.relu = nn.ReLU() self.conv2 = nn.Conv2d(64, num_classes, 1) def forward(self, x): x = self.relu(self.conv1(x)) x = self.conv2(x) return x # Create model for 2 classes model = FCN(num_classes=2) # Create dummy input: batch of 1 image, 3 channels, 4x4 pixels input_tensor = torch.randn(1, 3, 4, 4) # Forward pass output = model(input_tensor) # Check output shape print(f"Output shape: {output.shape}") # Apply softmax to get probabilities per pixel probabilities = F.softmax(output, dim=1) # Print probabilities for first pixel print(f"Probabilities at pixel (0,0): {probabilities[0, :, 0, 0]}")
FCNs keep the image size by using padding in convolutions.
Using 1x1 convolutions helps to classify each pixel independently.
Softmax is used to turn raw scores into probabilities for each class per pixel.
FCNs label every pixel in an image using only convolution layers.
They are useful for tasks like segmentation where location matters.
Output shape matches input image size but has class scores per pixel.
Practice
Solution
Step 1: Understand FCN structure
FCNs replace fully connected layers with convolution layers to keep spatial info.Step 2: Compare with traditional CNNs
Traditional CNNs use fully connected layers that lose pixel location details, FCNs keep pixel-wise info.Final Answer:
It can label every pixel in the image, preserving spatial information. -> Option AQuick Check:
FCN labels pixels = B [OK]
- Thinking FCNs use fully connected layers
- Assuming FCNs reduce image to vector
- Believing FCNs only work on grayscale
Solution
Step 1: Identify layer types
Fully connected layers are replaced by 1x1 convolution layers to keep spatial size.Step 2: Match correct PyTorch layer
nn.Conv2dwith kernel size 1 acts like a fully connected layer per pixel.Final Answer:
nn.Conv2d(in_channels, out_channels, kernel_size=1) -> Option CQuick Check:
1x1 conv replaces FC layer = D [OK]
- Choosing nn.Linear which is fully connected
- Confusing pooling or activation layers as replacements
- Using wrong kernel sizes
import torch
import torch.nn as nn
class SimpleFCN(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
self.conv2 = nn.Conv2d(16, 2, 1)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
return x
model = SimpleFCN()
input_tensor = torch.randn(1, 3, 64, 64)
output = model(input_tensor)
print(output.shape)Solution
Step 1: Analyze first conv layer
Conv2d(3,16,3,padding=1) keeps spatial size 64x64, output shape (1,16,64,64).Step 2: Analyze second conv layer
Conv2d(16,2,1) with kernel size 1 keeps spatial size, output shape (1,2,64,64).Final Answer:
(1, 2, 64, 64) -> Option AQuick Check:
Padding keeps size; 1x1 conv keeps size = A [OK]
- Ignoring padding effect on size
- Assuming kernel 3 reduces size without padding
- Confusing channel and spatial dimensions
Solution
Step 1: Understand convolution padding effect
Convolution without padding shrinks spatial dimensions because kernel slides inside image.Step 2: Identify cause of smaller output
If output is smaller, likely padding is missing in conv layers causing shrinkage.Final Answer:
Using convolution layers without padding reduces spatial size. -> Option BQuick Check:
No padding shrinks output = A [OK]
- Thinking 1x1 conv changes size
- Believing batch norm affects size
- Assuming fully connected layers keep spatial size
Solution
Step 1: Maintain spatial size
Padding=1 with kernel size 3 keeps spatial size; no pooling avoids shrinking.Step 2: Output class scores per pixel
Use 1x1 conv with out_channels=3 to get class scores per pixel matching input size.Final Answer:
Use conv layers with padding=1 and 1x1 conv at end, no pooling layers. -> Option DQuick Check:
Padding + 1x1 conv = pixel-wise classes = C [OK]
- Using pooling reduces output size
- Using fully connected layers loses pixel info
- Using stride>1 without upsampling shrinks output
Ready to prove it?
Pass the test and this pattern moves unknown
