Basics CNNs
So, I decided to strengthen my fundamental about all types of model. This is what I came up for now.
What is CNN?
A Convolutional Neural Network is a class of deep learning models. Designed for processing grid-like data and most commonly images. Unlike a standard fully connected neural network:
- which treats every pixel as an independent input
- quickly becomes computationally expensive
- prone to overfitting on image data
CNN exploits the spatial structure of images. Uses small learnable filters (kernels). It slide across the input to detect local patterns like edges, textures, and shapes. Combines these into increasingly complex features (eyes, wheels, faces) in deeper layers. Makes CNNs translation-invariant (a cat is recognized as a cat regardless of where it appears in the frame). Plus far more parameter-efficient than dense networks for visual data
Get Ahmad Hakimi Adnan’s stories in your inbox
Join Medium for free to get updates from this writer.
Notes: CNNs were popularized by Yann LeCun’s LeNet-5 (1998) for digit recognition and became dominant after AlexNet won the ImageNet competition in 2012, kicking off the modern deep learning era in computer vision.
Flowchart of CNN
Typical or classic flow of a CNN, from input images to convd layer, to activation layer, pooling layer the flattened and produce the fully connected layer.
Use Case
CNNs are used anywhere spatial or grid-structured data needs to be understood. Common applications include:
- Image classification: (labeling an image as “dog,” “cat,” “tumor,” )
- object detection and localization (drawing bounding boxes around objects
- semantic and instance segmentation (pixel-level classification, used in self-driving cars and medical imaging), facial recognition and verification,
- medical image analysis (detecting tumors, fractures, or diabetic retinopathy in X-rays, MRIs, and CT scans),
- optical character recognition (reading handwritten or printed text), video analysis (action recognition, tracking)
- non-image domains like audio spectrograms and time-series data, where the grid structure still applies
CNN Architecture (general)
CNN is typically built from a stack of the following layer types, repeated and combined:
- Convolutional layer: the core building block. A set of small filters (3×3 or 5×5) slides across the input, computing dot products to produce feature maps. Each filter learns to detect a specific pattern.
- Activation layer (usually ReLU): applies a non-linear function element-wise so the network can learn complex, non-linear relationships. Without it, stacking layers would just be equivalent to one linear transformation.
- Pooling layer (max pooling or average pooling): reduces the spatial dimensions of the feature maps, cutting computation and helping the network become more robust to small shifts or distortions in the input.
- Normalization layer (optional, e.g. Batch Normalization): stabilizes and speeds up training by normalizing activations between layers.
- Flatten layer: converts the final 2D/3D feature maps into a 1D vector so it can be fed into dense layers.
- Fully connected (dense) layer: combines all extracted features to reason about the overall input.
- Output layer: produces the final prediction, typically using softmax for multi-class classification or sigmoid for binary classification.
A typical shallow-to-deep flow: Conv → ReLU → Pool blocks are stacked several times (early layers detect edges/colors, deeper layers detect complex shapes/objects), followed by one or more fully connected layers and an output layer.
How it works
- Input: the image is represented as a 3D tensor, height × width × channels (e.g. 224×224×3 for an RGB image).
- Convolution: each filter slides (convolves) across the image with a defined stride, computing a weighted sum at each position. This produces a feature map highlighting where that pattern appears. Multiple filters per layer produce multiple feature maps stacked as channels for the next layer.
Dot product:
- Activation function: ReLU (
f(x) = max(0, x)) is applied to introduce non-linearity, letting the network model complex relationships rather than just linear combinations. - Pooling: a window (e.g. 2×2) slides over the feature map and reduces it, max pooling keeps the strongest activation, average pooling takes the mean. This shrinks the spatial size and adds a degree of translation invariance.
etc:
Feature map after ReLU:1 3 2 5
4 6 1 2
7 8 9 3
2 1 4 6Imagine we use 2×2 max pooling with stride 2.The pooling window starts here:[1 3]
[4 6]It takes the maximum value:max(1,3,4,6) = 6So the first output is:6Then it moves 2 steps right:[2 5]
[1 2]Maximum:5Continue:Input:1 3 2 5
4 6 1 2
7 8 9 3
2 1 4 6Max Pool output:6 5
8 9
Pooling is used to reduce the spatial size of feature maps while keeping the important information.
- Repetition: convolution, activation, and pooling repeat across many layers. Early layers learn simple features (edges, gradients, colors); deeper layers combine these into more abstract, complex features (textures, parts, whole objects).
- Flatten and classify: once feature extraction is done, the resulting feature maps are flattened into a vector and passed through one or more fully connected layers, ending in an output layer that produces class scores or probabilities.
- Training via backpropagation: the network makes a prediction, compares it to the true label using a loss function (e.g. cross-entropy), then propagates the error backward through the network, adjusting filter weights via gradient descent (or a variant like Adam) to reduce the error over many iterations.
Parameters and Hyperparameter
Learned parameters (updated automatically during training):
- Filter/kernel weights: numeric values inside each convolutional filter, learned from data.
- Biases: one per filter, added after the convolution operation. Fully connected layer weights and biases, standard dense-layer parameters in the classifier head.
Hyperparameters (set manually before training, not learned):
- Number of filters per layer: controls how many distinct patterns a layer can detect.
- Filter/kernel size: commonly 3×3 or 5×5
- Stride: how many pixels the filter moves each step, larger stride shrinks the output faster.
- Padding: “same” (pads input so output size matches input) or “valid” (no padding, output shrinks).
- Pooling window size and stride: typically 2×2 with stride 2.
- Number of layers/depth: how many conv-pool blocks and dense layers the network has.
- Learning rate: step size for weight updates during gradient descent.
- Batch size: number of samples processed before each weight update.
- Number of epochs: how many full passes through the training data.
- Dropout rate: fraction of neurons randomly disabled during training to reduce overfitting.
- Activation function choice: ReLU, Leaky ReLU, GELU, etc.
- Optimizer: SGD, Adam, RMSprop, and their associated settings (momentum, weight decay).
Current Pretrained model
Widely used pretrained CNN architectures (trained on large datasets like ImageNet, available via frameworks such as PyTorch, TensorFlow/Keras, or Hugging Face) include:
- VGG16 / VGG19: simple, uniform 3×3 conv stacks, good baseline, though large and computationally heavy.
- ResNet (ResNet-50, ResNet-101, ResNet-152): introduced residual/skip connections, enabling very deep networks (50–150+ layers) without vanishing-gradient problems, still a very common backbone.
- Inception (GoogLeNet, InceptionV3, InceptionV4): uses parallel filters of different sizes within a module for multi-scale feature extraction.
- MobileNet (V1/V2/V3): lightweight, optimized for mobile and edge devices using depthwise separable convolutions.
- EfficientNet (B0–B7): scales depth, width, and resolution together for strong accuracy-to-compute efficiency.
- DenseNet: connects each layer to every other layer in a feed-forward fashion, improving gradient flow and feature reuse.
- ConvNeXt: a modernized CNN design (2022) incorporating ideas from vision transformers while remaining a pure convolutional architecture, competitive with transformer-based models.





