TensorFlow CNN Basics

A Convolutional Neural Network (CNN) is a type of neural network designed specifically for processing image data. CNNs power face recognition on smartphones, product quality checks in factories, medical image analysis in hospitals, and self-driving car vision systems. Understanding CNNs starts with understanding why regular neural networks fail with images and how convolution solves that problem.

Why Regular Neural Networks Struggle With Images

Suppose you have a 224×224 color image. That image contains 224 × 224 × 3 = 150,528 pixel values. If you feed those directly into a Dense layer with 512 neurons, you need 150,528 × 512 = 77 million weights — just for one layer. Training a model with this many parameters requires enormous amounts of data and computing power.

There is a deeper problem too. A Dense layer treats every pixel independently. It does not understand that nearby pixels belong together to form edges, shapes, and objects. A dog's ear in the top-left corner looks completely different from the same dog's ear in the bottom-right corner, so the Dense layer treats them as unrelated features.

How a CNN Solves This: The Sliding Window

A CNN uses a filter (also called a kernel) — a small grid of weights, typically 3×3 or 5×5 pixels. This filter slides across the entire image, one small region at a time, performing the same computation everywhere. This is the "convolution" operation.

Image (5×5 pixels):              3×3 Filter:
┌───────────────────┐            ┌─────────────┐
│  1  2  0  1  3    │            │  1   0  -1  │
│  0  3  1  2  0    │     ×      │  1   0  -1  │
│  2  1  3  0  1    │            │  1   0  -1  │
│  0  2  1  3  2    │            └─────────────┘
│  1  0  2  1  0    │
└───────────────────┘

The filter slides one step at a time across the image.
At each position, it multiplies its weights by the
overlapping pixel values and sums the results.

Position 1 (top-left):
(1×1)+(2×0)+(0×-1)+(0×1)+(3×0)+(1×-1)+(2×1)+(1×0)+(3×-1)
= 1 + 0 + 0 + 0 + 0 - 1 + 2 + 0 - 3 = -1

This filter detects vertical edges. A different filter detects horizontal edges. Another detects curves. During training, the CNN learns which filters are most useful for the task — you do not design the filters by hand.

Feature Maps: The Output of Convolution

After a filter slides across the entire image, the resulting grid of values is called a feature map. Each value in the feature map tells you how strongly that feature (edge, corner, texture) appeared at that location in the original image.

Input Image     →  [Conv2D Layer]  →  Feature Maps
(224×224×3)          32 filters       (222×222×32)

One feature map for each filter.
32 filters produce 32 different feature maps.
Each map highlights a different pattern in the image.

A CNN's Layer Structure

A typical CNN alternates between three types of layers:

1. Convolutional Layer (Conv2D)

Applies filters to detect patterns. Earlier layers detect simple patterns (edges, lines). Deeper layers combine simple patterns into complex ones (shapes, textures, objects).

2. Pooling Layer

Reduces the size of feature maps to make computation faster and the model more resistant to small shifts in the image. Max pooling takes the largest value from each 2×2 region.

Feature Map:          After Max Pooling (2×2):
┌────────────────┐      ┌─────────┐
│  3   1   2   1 │      │  3   2  │
│  2   4   1   3 │  →   │  5   4  │
│  5   2   3   1 │      └─────────┘
│  1   3   2   4 │
└────────────────┘

Max pooling takes the maximum value from each 2×2 block.
Top-left block: max(3,1,2,4) = 4

3. Fully Connected Layer (Dense)

After several rounds of convolution and pooling, the feature maps get flattened into a single vector and fed into Dense layers. These layers combine all the detected features to make the final classification decision.

The Full CNN Architecture Diagram

INPUT IMAGE
(Height × Width × Channels)
         │
         ▼
[Conv2D Layer — 32 filters, 3×3]
"Detect edges and textures"
         │
         ▼
[MaxPooling2D — 2×2]
"Shrink the map, keep key features"
         │
         ▼
[Conv2D Layer — 64 filters, 3×3]
"Detect shapes and patterns"
         │
         ▼
[MaxPooling2D — 2×2]
"Shrink again"
         │
         ▼
[Flatten]
"Convert 3D feature maps into 1D vector"
         │
         ▼
[Dense Layer — 128 neurons, ReLU]
"Combine all features"
         │
         ▼
[Dense Layer — N neurons, Softmax]
"Output one probability per class"
         │
         ▼
PREDICTION

Building a Simple CNN in TensorFlow

import tensorflow as tf

model = tf.keras.Sequential([
    # Convolutional block 1
    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(32, 32, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),

    # Convolutional block 2
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2, 2),

    # Classifier
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')  # 10 classes
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.summary()

What Each Part of Conv2D Means

  • 32 — number of filters (the CNN learns 32 different patterns)
  • (3, 3) — each filter is 3 pixels wide and 3 pixels tall
  • activation='relu' — apply ReLU after convolution to add non-linearity
  • input_shape=(32, 32, 3) — each input image is 32×32 pixels with 3 color channels

Why CNNs Are Powerful

Weight Sharing

The same filter slides over the entire image. A filter that detects diagonal lines detects them everywhere in the image using exactly the same weights. This dramatically reduces the total number of parameters compared to a Dense network.

Translation Invariance

A CNN recognizes a cat in the top-left corner and a cat in the bottom-right corner as the same thing. This is because the same filters scan the entire image. Dense networks fail at this because they assign separate weights to every pixel location.

Hierarchy of Features

Layer 1 detects edges. Layer 2 combines edges into corners and curves. Layer 3 combines curves into eyes and ears. Layer 4 combines eyes and ears into a face. This hierarchical feature building mirrors how the human visual cortex works.

Comparing Dense vs. CNN on Image Data

                Dense Network      CNN
Parameters      ~77 million        ~1 million
Image shift     Fails              Robust
Learns spatial  No                 Yes
patterns
Training speed  Very slow          Fast
Accuracy on     ~60-70%            95%+
image tasks

CNNs achieve dramatically better results on image tasks because their architecture matches the structure of visual data. The next topics dive into the specific layers — Conv2D and pooling — in much greater detail, showing you how to tune each one for better performance.

Leave a Comment

Your email address will not be published. Required fields are marked *