How Neural Networks Process Images
Visualizing the process of "flattening" a 2D image grid into a 1D sequence of numbers to feed into Dense Layers.
Visualizing the process of "flattening" a 2D image grid into a 1D sequence of numbers to feed into Dense Layers.
When you look at an pic, you see a 2D grid of colors. However, standard Artificial Neural Networks (namely Dense or Fully-Connected Layers) are structured to accept just a one-dimensional (1D) array or vector of counts as input. Before a network can "look" at an pic, the pic must undergo Flattening.
Flattening takes a 2D array (or a 3D array for colour pics with height, width and channels) and rearranges it into a single row of counts. A 28×28 grayscale pic turns a vector of 784 vals. A 32×32 RGB pic turns a vector of 32 × 32 × 3 = 3,072 vals. No information is lost — the pixel vals are the same, just reordered into a line — but the spatial structure is destroyed. The network can no longer tell whether two pixels were neighbours in the first pic or on opposite corners.
This loss of spatial information is why convolutional neural networks (CNNs) were developed. A CNN processes the pic as a 2D grid, with filters that slide across the pic and detect local patterns like edges, corners and textures. Each convolutional layer gives a set of feature maps that preserve spatial layout. Just at the very end, after multiple convolutional and pooling layers have compressed the pic into a small, deep stack of feature maps, is the result flattened and fed into a dense layer for classification.
For a simple fully-connected network (no convolutions), flattening is the very first step: the raw pixel vector goes right into the network. This works on small pics like MNIST digits (28×28) but scales poorly: a 224×224 RGB pic would give an input vector of 150,528 vals. And a single dense layer with 1,024 neurons would need over 150 million weights. That param explosion is the real reason CNNs replaced fully-connected networks for pic tasks.
Visualizing the process of "flattening" a 2D image grid into a 1D sequence of numbers to feed into Dense Layers.
Why and how we transform 2D grids of pixels into 1D lists for Neural Networks.
When you look at an pic, you see a 2D grid of colors. However, standard Artificial Neural Networks (namely Dense or Fully-Connected Layers) are structured to accept just a one-dimensional (1D) array or vector of counts as input. Before a network can "look" at an pic, the pic must undergo Flattening.
Flattening takes a 2D array (or a 3D array for colour pics with height, width and channels) and rearranges it into a single row of counts. A 28×28 grayscale pic turns a vector of 784 vals. A 32×32 RGB pic turns a vector of 32 × 32 × 3 = 3,072 vals. No information is lost — the pixel vals are the same, just reordered into a line — but the spatial structure is destroyed. The network can no longer tell whether two pixels were neighbours in the first pic or on opposite corners.
This loss of spatial information is why convolutional neural networks (CNNs) were developed. A CNN processes the pic as a 2D grid, with filters that slide across the pic and detect local patterns like edges, corners and textures. Each convolutional layer gives a set of feature maps that preserve spatial layout. Just at the very end, after multiple convolutional and pooling layers have compressed the pic into a small, deep stack of feature maps, is the result flattened and fed into a dense layer for classification.
For a simple fully-connected network (no convolutions), flattening is the very first step: the raw pixel vector goes right into the network. This works on small pics like MNIST digits (28×28) but scales poorly: a 224×224 RGB pic would give an input vector of 150,528 vals. And a single dense layer with 1,024 neurons would need over 150 million weights. That param explosion is the real reason CNNs replaced fully-connected networks for pic tasks.
Think of a digital pic as a spreadsheet where each cell is a pixel holding a color value. "Flattening" is simply taking the first row of this spreadsheet, placing it down, taking the second row and appending it to the end of the first. And continuing until the whole grid forms a single, long line of counts.
If the pic is colored, it holds three channels: Red, Green. And Blue (RGB). The network needs split input nodes for each color value of each pixel. The math for the total count of inputs is simple: Width × Height × Channels = Total Nodes.
Before anything else, a picture has to turn arithmetic. A greyscale pic is a grid of brightness vals from 0 (black) to 255 (white). A colour pic is three such grids stacked — red, green and blue.
A 224×224 colour photograph is therefore a block of counts with shape (224, 224, 3). Or 150,528 vals. That is what the network really gets. There is no pic in there, just a tensor.
Almost each pipeline rescales those vals to 0–1 by dividing by 255. Or standardises them per channel with the dataset's mean and standard deviation. The reason is the same as for tabular data: gradients and initialisation schemes assume inputs of roughly unit scale. And raw 0–255 vals make the first layer's activations far too large.
Feed that pic to an ordinary fully connected layer with 1,000 units and you need 150,528 × 1,000 = 150 million weights in the first layer alone. Three problems follow at once.
It is unaffordable. That many params need enormous data and memory. And most of them will be fitting noise.
It throws away the structure. Flattening the grid into a list destroys the fact that neighbouring pixels are tied. To a dense layer, pixel (0,0) and pixel (0,1) are no more tied than pixel (0,0) and pixel (200,150).
It has to relearn all all over. A dense layer that learns to detect an edge in the top-left corner has learned nothing about edges in the bottom-right. Each position needs its own weights.
Convolution fixes all three at once by making two assumptions about pics that happen to be true.
Locality. A small filter — often 3×3 — looks at a tiny patch rather than the whole pic. Meaningful visual structure is local: an edge, a corner or a texture is set by a handful of neighbouring pixels.
Weight sharing. That same filter slides across the whole pic. One set of nine weights is applied all over. So a vertical-edge detector learned in one place works in each place.
The arithmetic is startling. A 3×3 filter over 3 input channels is 27 weights plus a bias. Use 64 such filters and the layer has 1,792 params — against 150 million for the dense equivalent. And it works better.
This also gives the network translation equivariance: move the cat two pixels right and the feature map moves two pixels right. The network does not have to see cats in each position to recognise them.
Stacking convolution layers builds features of raising abstraction. And this is not a metaphor — it is visible when you inspect trained filters.
| Depth | What the filters respond to |
|---|---|
| Layer 1 | Edges at various orientations, colour blobs |
| Layer 2 | Corners, curves, simple textures |
| Layers 3–4 | Repeated patterns, parts — eyes, wheels, letters |
| Deep layers | Object parts and whole objects |
Each layer sees a wider region of the first pic than the last, since each of its inputs now summarised a patch. That growing window is the receptive field. And it is why depth matters: a network needs enough layers for its deepest neurons to see the whole object.
A complete classifier is then: a few convolution and pooling blocks to build features, followed by pooling or flattening. And finally one or two dense layers to turn those features into class scores.
Look at the right column. For a 16x16 RGB pic, you'll see the 2D matrix labeled 16 x 16 x 3. This means there are 768 individual color vals. Scroll the 1D Input Vector horizontally to see all 768 "nodes" laid out end-to-end!
Check the Turn to Grayscale box. Notice how the node count drops by just a factor of 3 (e.g., from 768 to 256). Grayscale pics just have 1 channel (brightness) instead of 3 (RGB), making them much easier for simple neural networks to process.
Slide the Resolution to 32x32. Still for this tiny icon-sized pic, an RGB format creates 3,072 nodes. This rapid growth in data is why modern deep learning needs powerful GPUs.
Switch to the Live Webcam tab. Try moving your hand. Watch how the 2D pic changes and how those changes ripple through the 1D flattened array. Each movement changes hundreds of vals at once across the vector.
While flattening lets an MLP to process an pic, it destroys spatial relationships. A pixel right above another pixel in 2D might be dozens of indices apart in the 1D array. Standard dense networks struggle to understand that these pixels were at first neighbors.
This exact problem led to the invention of Convolutional Neural Networks (CNNs). It process pics in 2D first before flattening them at the very end!
Flattening is the bridge between raw pic files and the mathematical matrix steps that power Artificial Intelligence.
Convolution detects patterns and gives feature maps — one per filter, each highlighting where its pattern was found.
ReLU zeroes negative replies. Without a non-linearity, stacked convolutions collapse into a single convolution. And depth buys nothing.
Pooling (or a strided convolution) shrinks the spatial dimensions. It reduces computation, widens the receptive field. And adds a little tolerance to small shifts.
Batch normalisation keeps activations in a stable range, making deeper networks trainable and letting more learning rates.
Global average pooling collapses each feature map to a single count, replacing the huge flatten-then-dense step used by older designs and removing most of their params.
The classifier head maps the final features to class scores. It a softmax turns into probabilities.
Convolution works since of assumptions about pics. And it is worth knowing when they do not hold.
Rotation and scale are not free. Convolution is equivariant to translation, not to rotation or resizing. A network trained on upright faces will struggle with faces at 90°. It is just why rotation and scaling are standard data augmentations.
Global relationships need depth or attention. A 3×3 filter sees three pixels. Relating opposite corners of an pic needs many layers of accumulated receptive field. Vision transformers take the other route, checking all patches right with attention from the first layer — which is why they need more data but capture long-range structure more easily.
Not all grids are pics. Convolution assumes neighbouring positions are tied. Applied to a table of unrelated columns, that assumption is false and the inductive bias is wasted.
A network never sees a picture -- it sees an array. And each layer is arithmetic on that array. This follows one small pic all the way from pixels to a prediction. So that nothing in the pipeline is left as a metaphor.
Building on the same setup, the next block shows the left cases.
The next block covers the left steps.
The next block continues with the left demonstrations.
Building on the same setup, the next block shows the left cases.
The next block covers the left steps.
Why 3×3 filters almost all over? Two stacked 3×3 layers see the same region as one 5×5 but use fewer params and include an extra non-linearity. VGG set this and it stuck.
Does the input have to be a fixed size? Convolutions do not care. But a flatten-then-dense head does. Global average pooling removes the constraint, which is why modern designs accept varying sizes.
Why normalise pixel vals? Since 0–255 inputs give large activations and awkward gradients. Scaling to 0–1 or standardising per channel is standard.
How many filters should a layer have? Often doubling with depth — 32, 64, 128, 256 — as spatial size shrinks and the count of distinct patterns to represent grows.
Do CNNs see like humans? Not really. They are far more sensitive to texture than to shape. And small adversarial changes invisible to a person can flip a prediction fully.
Are CNNs obsolete now that transformers exist? No. They remain more data-fast, faster on small pics. And the standard choice on limited hardware. Hybrid designs are common.
Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.
What is meant by “Flattening” here?
What does this module say about “The idea in brief”?
What does this module say about “Preprocessing Parameters”?
Unroll a 2D pixel grid into a 1D vector and see what spatial information flattening destroys — and why CNNs exist to avoid it.