Draw Input

Node Inspector

Hover over the 3D network nodes to inspect layer outputs and internal states.

Rotate L-Drag
Pan R-Drag
Zoom Scroll
Focus Click

Understanding Convolutional Neural Networks

By Updated

Convolutional Neural Networks (CNNs) are specialized deep learning architectures fundamentally designed to process structured grid data, such as images. By sliding "filters" across an image, CNNs systematically recognize spatial hierarchies—starting from simple edges and assembling them into complex concepts like faces or handwritten digits.

1. The Convolutional Layer

The core building block of a CNN is the Convolution Operation. Instead of connecting every input pixel to every hidden node (which is computationally impossible for large images), a small matrix called a kernel or filter (e.g., 3x3) slides across the input image.

At each position, we compute the dot product between the filter weights and the underlying pixels. This operation is defined mathematically as:

$$ S(i, j) = (I * K)(i, j) = \sum_{m} \sum_{n} I(i-m, j-n) K(m, n) $$

Where $I$ is the input image, $K$ is the kernel matrix, and $S$ is the resulting feature map. The network learns the optimal values for $K$ during training to extract meaningful features automatically.

2. Activation (ReLU)

After convolution, we introduce non-linearity using an activation function, typically the Rectified Linear Unit (ReLU). This function simply zeroes out negative values and passes positive values through unchanged:

$$ f(x) = \max(0, x) $$

Without non-linearity, a neural network, no matter how many layers it has, would essentially just behave like a single linear regression model.

3. Pooling (Downsampling)

Following activation, Max Pooling layers reduce the spatial dimensions of the feature maps. A common strategy is taking the maximum value over a 2x2 window with a stride of 2.

This dramatically reduces the number of parameters and computational load in the network, while simultaneously making the detected features translation invariant (i.e., the network recognizes the feature even if it shifts slightly in the image).

4. The Dense Classifier

After several rounds of Convolution and Pooling, the 3D output volume is Flattened into a 1D array. This array is fed into standard Dense (Fully Connected) layers. The final layer typically uses a Softmax activation function to output a probability distribution over the possible classes (in our visualizer, the digits 0-9).

$$ \sigma(\mathbf{z})_i = \frac{e^{z_i}}{\sum_{j=1}^{K} e^{z_j}} $$

This forces the network's outputs to sum exactly to $1.0$ ($100\%$), converting raw network confidence scores into strict class probabilities.

5. Padding, Stride and Output Size

Two settings control how the filter moves, and together they decide the size of every feature map in the network. Stride is how far the filter jumps between positions — a stride of 1 slides one pixel at a time, a stride of 2 skips every other position and halves the output. Padding adds a border of zeros around the input so the filter can be centred on the edge pixels.

Without padding, every convolution shrinks the image slightly, because the filter cannot hang over the edge. A 3x3 filter on a 28x28 image produces a 26x26 output, and after ten such layers there is nothing left. This is why "same" padding — enough zeros to keep the output size equal to the input — is the common default in deep networks.

$$ O = \left\lfloor \frac{I - K + 2P}{S} \right\rfloor + 1 $$

Here $I$ is the input side length, $K$ the kernel size, $P$ the padding and $S$ the stride. A 28x28 input with a 3x3 kernel, no padding and stride 1 gives $\lfloor (28 - 3 + 0)/1 \rfloor + 1 = 26$. Add one pixel of padding on each side and the output returns to 28. Set the stride to 2 instead and it drops to 13.

Working this out by hand for each layer is worth doing once. Almost every shape-mismatch error in a CNN comes from a stride or padding value that produced a different size than expected, and the formula above tells you exactly what a layer will emit before you run it.

6. Counting the Parameters

A convolutional layer's parameter count depends on the filter size and the number of channels — not on the size of the image. That single fact explains most of what makes CNNs practical.

$$ \text{params} = (K_h \times K_w \times C_{in} + 1) \times C_{out} $$

The $+1$ is the bias, one per output channel. A layer taking a 3-channel colour image and producing 32 feature maps with 3x3 filters has $(3 \times 3 \times 3 + 1) \times 32 = 896$ weights. The same layer applied to a 4-megapixel photograph still has 896 weights, because the filter is reused at every position.

This reuse is called parameter sharing, and it carries an assumption worth stating: a feature worth detecting in one part of the image is worth detecting everywhere. An edge detector is useful in the top-left corner and in the centre, so one set of weights can serve both. That assumption holds well for photographs and holds badly for data where absolute position matters — which is why CNNs are not automatically the right tool for every grid-shaped input.

The dense layers at the end are usually where the parameters actually live. Flattening a $7 \times 7 \times 64$ volume gives 3,136 values, and connecting those to 128 units costs over 400,000 weights — several hundred times more than the convolutional layer above. When a model is too large, the classifier head is normally the first place to look.

7. The Receptive Field

A single neuron in the first convolutional layer sees only a 3x3 patch of the original image. A neuron in the second layer sees a 3x3 patch of the first layer's output, and each of those values already summarised a 3x3 region — so it indirectly sees a 5x5 region of the input. This growing window is the receptive field.

Stacking layers grows the receptive field slowly; pooling and strided convolutions grow it quickly, because each of their outputs already covers a wider area. This is the real reason depth matters in a CNN: the network cannot recognise a face until some neuron somewhere can see a whole face at once.

It also explains the standard shape of a CNN. Early layers have small receptive fields and learn local, generic patterns — edges, corners, colour transitions. Middle layers combine those into textures and parts. Late layers, with receptive fields covering most of the image, respond to whole objects. Nobody designs this hierarchy; it emerges from training, and it is remarkably consistent across networks and datasets.

8. Why Not Just Use a Dense Network?

A fully connected layer on a modest 224x224 colour image would need $224 \times 224 \times 3 = 150{,}528$ inputs. Connecting those to just 1,000 hidden units costs over 150 million weights — for one layer. Beyond the memory cost, a model that large will memorise its training set long before it learns anything general.

Convolution replaces that with three structural assumptions, each of which removes parameters:

These are exactly the properties images happen to have, which is why the architecture works so well on them — and why the same architecture applied to tabular data, where columns have no spatial relationship, performs no better than a plain dense network.

9. A Worked Example: One Digit, End to End

Take the 28x28 grayscale digit you can draw above and follow its shape through a small network. Each line is one layer, and the numbers come straight from the output-size and parameter formulas.

Total: about 421,000 parameters, of which 95% sit in that one dense layer. Notice how the spatial dimensions shrink while the channel count grows — the network trades where information is for what it is. By the final convolution, each of the 64 channels at each of the 49 positions represents a fairly abstract pattern rather than a pixel.

10. Training in Practice

Training a CNN is ordinary gradient descent: run a batch forward, compare the predicted distribution to the true label with cross-entropy loss, backpropagate, and adjust every weight slightly. What differs from a dense network is which practical techniques matter.

The training signal to watch is the gap between training and validation accuracy. A model that fits the training set well and generalises poorly needs more data, more augmentation or more regularisation — not more layers.

11. Where CNNs Struggle

Pooling gives approximate translation invariance, and nothing more. A CNN is not naturally invariant to rotation or scale — a digit rotated ninety degrees is, as far as the network is concerned, an unfamiliar shape. Augmentation is how that robustness gets added, by showing the network rotated examples during training rather than by building the invariance into the architecture.

The other well-known limitation is that pooling discards spatial relationships along with position. A network that has learned to detect eyes, a nose and a mouth may respond strongly to an image containing all of them in the wrong arrangement, because the evidence is present even though the configuration is nonsense.

Since 2020, Vision Transformers have matched or exceeded CNNs on large datasets by dropping the locality assumption entirely and letting attention decide which parts of the image relate to which. They need considerably more data to reach that point, which is precisely because they lack the built-in assumptions that make CNNs so efficient on small datasets. Convolution remains the better choice when data is limited, and the two approaches are increasingly combined.

12. Recap

Predict, then reveal

About to run: press Propagate Math. Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “The Convolutional Layer”?

  2. What does this module say about “Activation (ReLU)”?

  3. What does this module say about “Pooling (Downsampling)”?

Cheat sheet

CNN Architecture

Convolutional Neural Networks (CNNs) are specialized deep learning architectures fundamentally designed to process structured grid data, such as images. By sliding "filters" across an image, CNNs systematically recognize spatial hierarchies—starting from simple edges and assembling them into complex concepts like faces or handwritten digits.

COMPUTER VISION · vizlearn.in/computer_vision/cnn.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.