Machine Learning
What is CNN?
CNN is a deep learning model successful in image processing.
What is a CNN?
A convolutional neural network (CNN) captures local patterns (edges, corners, textures) in grid-structured data like images by sliding small filters over the whole image. Because the same filter is used at every position, the parameter count stays small and a pattern is recognised wherever it appears in the image.
# 28x28 grey image, fully connected first layer (128 neurons): 784 * 128 = 100352 weights
# 3x3 convolution, 16 filters: 3 * 3 * 16 = 144 weights
A typical CNN stacks convolution + activation + pooling blocks and uses a fully connected classifier at the end.
The convolution operation
A filter is multiplied element-wise with a small region of the image and summed; the result is a single value in the output map. The filter is shifted by one step and the operation repeats. A filter that finds vertical edges:
img = torch.tensor([[0., 0., 1., 1.],
[0., 0., 1., 1.],
[0., 0., 1., 1.]])
filtre = torch.tensor([[-1., 1.],
[-1., 1.]])
# top-left 2x2 region: (0·-1 + 0·1 + 0·-1 + 0·1) = 0
# middle region: (0·-1 + 1·1 + 0·-1 + 1·1) = 2 -> edge
CNNs learn the values of these filters from the data themselves.
Exercise Convolution by hand
Apply filtre to the given image by hand: at every valid position (stride 1, no padding), sum the element-wise product with the 2×2 region and write the result into a 2×3 list. Print the result.
Show example solution
import torch
from torch import nn
img = torch.tensor([[0., 0., 1., 1.], [0., 0., 1., 1.], [0., 0., 1., 1.]])
filtre = torch.tensor([[-1., 1.], [-1., 1.]])
sonuc = [[(img[i:i + 2, j:j + 2] * filtre).sum().item() for j in range(3)] for i in range(2)]
print(sonuc)
Test yourself
-
What is a CNN's main advantage over a fully connected network on images?
- Needing no scaling
- Weight sharing: few parameters and position-independent pattern recognition
- More parameters
- Needing no labels
Show answer
Weight sharing: few parameters and position-independent pattern recognition — The same filter is used at all positions.
-
Sum the element-wise product of the region and the filter.
deger = (bolge ____ filtre).sum()
Show answer
deger = (bolge * filtre).sum()
-
Match the concepts.
- filter
- Small learned weight matrix
- sliding
- Applying at all positions
- weight sharing
- Few parameters
- feature map
- The filter's output
Next lesson: Conv2d