Modules / Deep Learning / Image Processing

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.

Neural Network Input

LIVE PROCESSING
Step 1: 2D Matrix 16 x 16 x 3
Flattening Row by Row
Step 2: 1D Input Vector 768 nodes
Scroll horizontally to view all nodes

Understanding Image Flattening

Why and how we transform 2D grids of pixels into 1D lists for Neural Networks.

Quick Context

When you look at an image, you see a 2D grid of colors. However, standard Artificial Neural Networks (specifically Dense or Fully-Connected Layers) are structured to accept only a one-dimensional (1D) array or vector of numbers as input. Before a network can "look" at an image, the image must undergo Flattening.

The Core Idea: Unrolling the Grid

Think of a digital image 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 numbers.

If the image is colored, it contains three channels: Red, Green, and Blue (RGB). The network needs separate input nodes for every color value of every pixel. The math for the total number of inputs is simple: Width × Height × Channels = Total Nodes.

Guided Experiments with This Interactive

  1. Observe the Math:

    Look at the right column. For a 16x16 RGB image, you'll see the 2D matrix labeled 16 x 16 x 3. This means there are 768 individual color values. Scroll the 1D Input Vector horizontally to see all 768 "nodes" laid out end-to-end!

  2. Toggle Grayscale:

    Check the Convert to Grayscale box. Notice how the node count drops by exactly a factor of 3 (e.g., from 768 to 256). Grayscale images only have 1 channel (brightness) instead of 3 (RGB), making them much easier for simple neural networks to process.

  3. Adjust the Resolution:

    Slide the Resolution to 32x32. Even for this tiny icon-sized image, an RGB format creates 3,072 nodes. This rapid growth in data is why modern deep learning requires powerful GPUs.

  4. Live Webcam Test:

    Switch to the Live Webcam tab. Try moving your hand. Watch how the 2D image changes and how those changes ripple through the 1D flattened array. Every movement changes hundreds of values simultaneously across the vector.

The Problem with Flattening

While flattening allows an MLP to process an image, 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 originally neighbors.

This exact problem led to the invention of Convolutional Neural Networks (CNNs), which process images in 2D first before flattening them at the very end!

Key Takeaways

  • Dense neural network layers only accept 1D lists (vectors) as input.
  • Flattening is the process of stringing rows of pixels together into a line.
  • The size of the input layer is $W \times H \times C$ (Channels).
  • High resolution images create massive input layers, making them computationally expensive.

Flattening is the bridge between raw image files and the mathematical matrix operations that power Artificial Intelligence.

Cheat sheet

How Neural Networks Process Images

When you look at an image, you see a 2D grid of colors. However, standard Artificial Neural Networks (specifically Dense or Fully-Connected Layers) are structured to accept only a one-dimensional (1D) array or vector of numbers as input. Before a network can "look" at an image, the image must undergo Flattening.

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