Convolutional Neural Networks (CNNs) for Images
Keras Basics
2 min read
Published Nov 17 2025
Guide Sections
Guide Comments
CNNs are the foundation of modern computer vision.
They are used for:
- Image classification
- Object detection
- Image segmentation
- Face recognition
- OCR
- Medical imaging
In this section you’ll learn:
- What convolutional layers do (practically)
- How to load and preprocess image datasets
- How to build CNN architectures
- How to train, evaluate, and predict
- How to improve CNN performance
We’ll use CIFAR-10 — a standard small image dataset built into Keras.
Load the CIFAR-10 Dataset
CIFAR-10 contains:
- 50,000 training images
- 10,000 test images
- 10 classes (airplane, car, bird, etc.)
- Images are 32×32×3 (RGB)
Load dataset
Inspect shapes
Note: labels have shape (batch, 1).
Preprocess the Data
Normalise pixel values (0–1)
No flattening — CNNs operate on multidimensional image tensors.
Build a Simple CNN
A typical small CNN has:
- Conv → ReLU → Pool
- Conv → ReLU → Pool
- Flatten
- Dense layers
- Softmax output
Build model
Explanation:
- Conv2D(32 filters) → learns low-level patterns
- Pooling → reduces spatial size
- Conv2D(64 filters) → learns more complex patterns
- Dense layers → final classification
Compile the CNN
This is identical to earlier multi-class tasks, loss and metrics don’t change.
Train the CNN
CNNs require more compute than MLPs but still train quickly on CIFAR-10.
Evaluate
Typical accuracy:
- Simple CNN → 65–72% (We will build better ones later.)
Predicting Images
Visualising Predictions
Improving CNNs (Recommended Add-Ons)
Most of the performance in CNNs comes from architectural choices.
Below are common techniques to improve results significantly.
A) Add Batch Normalisation
Helps training stability and speed.
B) Add Dropout to Reduce Overfitting
C) Use Data Augmentation
Helps generalisation:
Use it like:
D) Increase Model Depth
A deeper CNN performs better:
E) Use GlobalAveragePooling Instead of Flatten
Modern CNNs often end like this:
Improved CNN Example
This architecture can reach 80–85% accuracy.
CNN Training Tips
Use larger batch sizes (64 or 128) - Good for GPU training.
More epochs → better accuracy - 15–30 usually improves results.
Add regularisation if overfitting - Dropout, batch normalisation, data augmentation.
Use learning rate schedules














