Online · Liepaja · Latvia

AI Courses for Kids in Liepaja

AI courses for kids in Liepaja emphasise digital safety and guided practice training image and text classification models on child-safe platforms.

AI Liepaja

Chapter 155 — Build an Image Recognition CNN with TensorFlow and Keras

1. Introduction & Cloud GPU Infrastructure

In previous modules, we examined the theoretical foundations of Convolutional Neural Networks (CNNs), including kernel filtering, spatial feature maps, activation functions, and max pooling operations. In this lecture, we translate those theoretical concepts into practical Python code by building an end-to-end image recognition CNN using TensorFlow and Keras.

  ┌─────────────────────────────────────────────────────────────┐
  │              End-to-End Image Recognition Workflow          │
  ├─────────────────────────────────────────────────────────────┤
  │  Step 1: Cloud GPU Environment Setup (Google Colab T4 GPU)  │
  │     • Accelerates tensor processing over standard CPUs      │
  │                                                             │
  │  Step 2: Data Ingestion & Preprocessing (MNIST Dataset)     │
  │     • Load 28×28 grayscale images (10 digit classes)        │
  │     • Normalize pixels (0–255 → 0.0–1.0) & add channel axis │
  │                                                             │
  │  Step 3: CNN Architecture Construction (models.Sequential)  │
  │     • Stack Conv2D, MaxPooling2D, Flatten, and Dense layers │
  │                                                             │
  │  Step 4: Model Parameter Diagnostic (model.summary())       │
  │     • Calculate layer output shapes and 121,930 parameters  │
  │                                                             │
  │  Step 5: Compilation & Training (fit with Adam Optimizer)   │
  │     • Train for 3 epochs with a 10% validation split        │
  │                                                             │
  │  Step 6: Final Evaluation (evaluate on Unseen Test Data)    │
  │     • Measure test loss and test accuracy (98.59%)          │
  └─────────────────────────────────────────────────────────────┘
    

Cloud Acceleration with Google Colab:

When programming CNNs in Python, training deep networks locally on standard CPUs can be extremely slow. Google Colab provides a free cloud environment equipped with GPU hardware accelerators (such as the T4 GPU).

  • Enabling GPU in Colab: Navigate to Runtime → Change runtime type, select GPU (e.g., T4 GPU), and save. This restarts the hardware environment and enables parallel tensor operations for fast deep learning execution.

2. Data Ingestion & Image Preprocessing Pipeline

For this implementation, we utilize the classic MNIST Handwritten Digits Dataset (tf.keras.datasets.mnist), which contains grayscale image samples of handwritten numbers from 0 through 9 across 10 distinct classes.

import tensorflow as tf
from tensorflow.keras import layers, models

# 1. Load the MNIST Dataset
mnist = tf.keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()

print(f"Training Images Shape: {X_train.shape}")  # (60000, 28, 28)
print(f"Testing Images Shape : {X_test.shape}")   # (10000, 28, 28)
    

Pixel Normalization & Channel Axis Expansion:

  1. Pixel Value Normalization: Raw pixel values range from 0 to 255 (stored as 8-bit unsigned integers / 1 byte). Dividing X_train and X_test by 255.0 rescales all pixel intensities to a continuous range between 0.0 and 1.0, which improves numerical stability during backpropagation.
  2. Channel Dimension Expansion: Grayscale images have a single color channel. TensorFlow requires image inputs to follow a 3D shape (Height, Width, Channels). Using tf.newaxis expands the array shape from (28, 28) to (28, 28, 1).
# 2. Rescale pixel values between 0.0 and 1.0
X_train = X_train / 255.0
X_test = X_test / 255.0

# 3. Add explicit channel axis for grayscale images (28, 28, 1)
X_train = X_train[..., tf.newaxis]
X_test = X_test[..., tf.newaxis]

print(f"Reshaped Train Matrix: {X_train.shape}")  # (60000, 28, 28, 1)
    

3. Network Architecture Design (models.Sequential)

Architectural decisions dictate how effectively a CNN extracts visual features. When designing an architecture, studying established domain research helps save extensive trial-and-error experimentation time.

  ┌─────────────────────────────────────────────────────────────┐
  │                   CNN Layer Architecture                    │
  ├─────────────────────────────────────────────────────────────┤
  │                                                             │
  │   Input Image Layer (28 × 28 × 1 Grayscale)                 │
  │        │                                                    │
  │        ▼                                                    │
  │   Conv2D Layer 1        ──► 32 Filters (3×3), ReLU          │
  │        │                                                    │
  │        ▼                                                    │
  │   MaxPooling2D Layer 1  ──► Pool Size (2×2)                 │
  │        │                                                    │
  │        ▼                                                    │
  │   Conv2D Layer 2        ──► 64 Filters (3×3), ReLU          │
  │        │                                                    │
  │        ▼                                                    │
  │   MaxPooling2D Layer 2  ──► Pool Size (2×2)                 │
  │        │                                                    │
  │        ▼                                                    │
  │   Flatten Layer         ──► Reshapes 2D maps to 1D vector  │
  │        │                                                    │
  │        ▼                                                    │
  │   Dense Hidden Layer    ──► 64 Neurons, ReLU                │
  │        │                                                    │
  │        ▼                                                    │
  │   Output Dense Layer    ──► 10 Neurons, Softmax (Classes)   │
  └─────────────────────────────────────────────────────────────┘
    

Detailed Layer Breakdown:

  • Layer 1: Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)): Applies 32 filters of size 3×3 with ReLU activation to extract basic primitive features (edges, corners).
  • Layer 2: MaxPooling2D((2, 2)): Applies a 2×2 pool window to extract maximum values, shrinking spatial dimensions and reducing computation time.
  • Layer 3: Conv2D(64, (3, 3), activation='relu'): Expands filter depth to 64 filters of size 3×3 to construct complex features from primitives.
  • Layer 4: MaxPooling2D((2, 2)): Performs a second 2×2 spatial downsampling to further compress the feature maps.
  • Layer 5: Flatten(): Reshapes the multi-dimensional feature maps into a 1D feature vector suitable for fully connected layers.
  • Layer 6: Dense(64, activation='relu'): Fully connected layer containing 64 neurons with ReLU activation for pattern learning.
  • Layer 7: Dense(10, activation='softmax'): The output layer containing 10 neurons (corresponding to digits 0–9) paired with the Softmax activation function to output class probability distributions.
# 4. Construct CNN Architecture
model = models.Sequential([
    # First Convolutional Block
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D((2, 2)),

    # Second Convolutional Block
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),

    # Classification Head
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])
    

4. Model Diagnostics & Parameter Breakdown (model.summary())

Executing model.summary() displays the complete layer-by-layer spatial shape transformations and trainable parameter counts:

# 5. Print Model Parameter Summary
model.summary()
    
  ┌─────────────────────────────────────────────────────────────────────────┐
  │                        Model Summary Breakdown                          │
  ├──────────────────────────────┬───────────────────┬──────────────────────┤
  │ Layer (Type)                 │ Output Shape      │ Param #              │
  ├──────────────────────────────┼───────────────────┼──────────────────────┤
  │ conv2d (Conv2D)              │ (None, 26, 26, 32)│ 320                  │
  │ max_pooling2d (MaxPooling2D) │ (None, 13, 13, 32)│ 0                    │
  │ conv2d_1 (Conv2D)            │ (None, 11, 11, 64)│ 18,496               │
  │ max_pooling2d_1 (MaxPooling) │ (None, 5, 5, 64)  │ 0                    │
  │ flatten (Flatten)            │ (None, 1600)      │ 0                    │
  │ dense (Dense)                │ (None, 64)        │ 102,464              │
  │ dense_1 (Dense)              │ (None, 10)        │ 650                  │
  ├──────────────────────────────┴───────────────────┴──────────────────────┤
  │ Total params: 121,930 (476.29 KB)                                       │
  │ Trainable params: 121,930                                               │
  │ Non-trainable params: 0                                                 │
  └─────────────────────────────────────────────────────────────────────────┘
    

Mathematical Parameter Analysis:

  1. conv2d: Input 28×28×1 → 32 filters of 3×3. Weight formula: (3×3×1×32) + 32 biases = 320 parameters. Output spatial size shrinks to 26×26×32.
  2. max_pooling2d: Downsamples 26×26 → 13×13×32. Parameter count = 0 (no trainable weights required for max operation).
  3. conv2d_1: Input 13×13×32 → 64 filters of 3×3. Weight formula: (3×3×32×64) + 64 biases = 18,496 parameters. Output spatial size shrinks to 11×11×64.
  4. max_pooling2d_1: Downsamples 11×11 → 5×5×64. Parameter count = 0.
  5. flatten: Reshapes 3D tensor (5×5×64) into a 1D vector of 5×5×64 = 1600 values. Parameter count = 0.
  6. dense: Input 1600 → 64 neurons. Weight formula: (1600×64) + 64 biases = 102,464 parameters.
  7. dense_1: Input 64 → 10 output neurons. Weight formula: (64×10) + 10 biases = 650 parameters.
  8. Total Model Memory: 121,930 trainable parameters occupying ~476 KB of data memory. Notice that as data moves deeper, spatial dimensions shrink (28 → 26 → 13 → 11 → 5) while channel depth expands (1 → 32 → 64).

5. Model Compilation, Training, & Diagnostics (fit)

Before training, we compile the model using the Adam optimizer, sparse categorical cross-entropy loss, and the accuracy metric:

# 6. Compile Model Configuration
model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

# 7. Train Model for 3 Epochs with 10% Validation Split
history = model.fit(
    X_train, y_train,
    epochs=3,
    validation_split=0.1
)
    
  ┌─────────────────────────────────────────────────────────────┐
  │                 Epoch Training Metrics Log                  │
  ├─────────────────────────────────────────────────────────────┤
  │  Epoch 1/3:                                                 │
  │    • Training Loss: 0.3300  | Training Accuracy: 89.00%     │
  │    • Validation Loss: 0.0500| Validation Accuracy: 98.00%   │
  │                                                             │
  │  Epoch 2/3:                                                 │
  │    • Training Accuracy: 98.00%                              │
  │                                                             │
  │  Epoch 3/3:                                                 │
  │    • Training Accuracy: 98.97% | Training Loss: ~0.0300     │
  │    • Validation Accuracy: 98.65%| Validation Loss: ~0.0400   │
  └─────────────────────────────────────────────────────────────┘
    

Epoch Training Dynamics:

  • Epoch Efficiency: Deep learning CNNs require very few epochs compared to classical machine learning models. Here, training over just 3 epochs achieves high convergence.
  • Accuracy Progression: Training accuracy increases from 89% in Epoch 1 up to 98.97% (~99%) in Epoch 3, while validation accuracy reaches 98.65%.
  • Generalization Check: Matching training (~98.97%) and validation (~98.65%) accuracies confirms that the network is well-fitted without underfitting or overfitting.

6. Evaluation on Unseen Test Data (evaluate)

Finally, we evaluate model generalization on the completely separate test dataset of 10,000 unseen images using model.evaluate():

# 8. Evaluate Model on Test Dataset
test_loss, test_acc = model.evaluate(X_test, y_test)

print(f"\nTest Dataset Loss    : {test_loss:.4f}")
print(f"Test Dataset Accuracy: {test_acc * 100:.2f}%")  # Output: 98.59%
    
  ┌─────────────────────────────────────────────────────────────┐
  │                   Final Model Performance                   │
  ├─────────────────────────────────────────────────────────────┤
  │  • Test Dataset Loss    : 0.0420                            │
  │  • Test Dataset Accuracy: 98.59%                            │
  │                                                             │
  │  Industry Context:                                          │
  │    • MNIST is a relatively clean benchmark dataset.         │
  │    • Complex client real-world images present higher noise. │
  │    • In production settings, an accuracy around 80% is      │
  │      frequently considered an acceptable baseline.          │
  └─────────────────────────────────────────────────────────────┘
    

Performance Takeaways:

  • High Accuracy: The CNN achieves 98.59% test accuracy on MNIST digits.
  • Industry Context: While MNIST yields over 98% accuracy due to its simple structure, real-world client images are substantially more complex. In industrial deployments, achieving around 80% accuracy on complex visual tasks is often considered an acceptable working baseline.

7. Summary Table: Topic 155 Pipeline Overview

Pipeline Stage TensorFlow / Keras Code Operational Function Output Result / Metric
GPU Acceleration Colab T4 GPU Setup Enables parallel tensor training. Accelerated training execution.
Data Ingestion mnist.load_data() Loads 60k train & 10k test grayscale images. Array shapes (60000, 28, 28).
Preprocessing X / 255.0 & tf.newaxis Normalizes pixels to 0–1 & adds channel axis. Matrix shape (60000, 28, 28, 1).
Feature Extractor Conv2D & MaxPooling2D Extracts primitive and complex features. Feature shapes shrink spatially.
Classifier Head Flatten → Dense Maps 1D feature vector to 10 digit classes. 10 Softmax output probabilities.
Model Summary model.summary() Calculates parameter weights per layer. 121,930 total parameters (~476 KB).
Compilation model.compile() Sets adam, sparse_categorical_crossentropy. Configured training pipeline.
Training model.fit() Trains for 3 epochs with 10% validation split. Train Acc: 98.97%, Val Acc: 98.65%.
Evaluation model.evaluate() Evaluates model on 10,000 unseen test images. 98.59% Test Accuracy.

Lesson Q&A

Pixel Value Normalization: Raw image pixel intensities range from 0 to 255 based on 8-bit byte representation. To accelerate training convergence, pixel values in both training and testing datasets are normalized by dividing by 255.0 (x_train / 255.0 and x_test / 255.0), scaling all pixel values into a range between 0.0 and 1.0.

Channel Dimension Expansion: CNN layers in Keras expect a 3D input shape (Height, Width, Channels) for each image. Because grayscale datasets like MNIST lack explicit color channels, an explicit channel dimension is added using tf.newaxis to expand the data dimensions to (28, 28, 1).

Hardware Acceleration: Training is typically configured on cloud platforms like Google Colab using a T4 GPU, which substantially speeds up execution compared to standard CPU processing.

The model is built using Keras's Sequential API (tf.keras.models.Sequential) with the following layer stack:

  1. First Convolutional Layer (Conv2D): Contains 32 filters of size 3x3 with ReLU activation and an input_shape of (28, 28, 1) to extract primitive low-level features such as edges.
  2. First Max Pooling Layer (MaxPooling2D): Uses a 2x2 pooling kernel to downsample feature map dimensions, reducing spatial size and computation time.
  3. Second Convolutional Layer (Conv2D): Contains 64 filters of size 3x3 with ReLU activation to combine primitive features into more complex spatial patterns.
  4. Second Max Pooling Layer (MaxPooling2D): Uses a 2x2 pooling kernel for additional spatial downsampling and computational efficiency.
  5. Flatten Layer (Flatten): Converts multidimensional feature maps into a 1D feature vector containing 1,600 values to bridge the feature extraction layers with the classification head.
  6. Fully Connected Hidden Layer (Dense): A dense layer with 64 neurons and ReLU activation for high-level decision reasoning.
  7. Output Layer (Dense): A fully connected output layer with 10 nodes and a Softmax activation function. The 10 nodes correspond to the 10 digit classes (0–9) in the MNIST dataset, outputting probability distributions across all categories.

Compilation Settings: The model is compiled using the Adam optimizer, Sparse Categorical Cross-Entropy (sparse_categorical_crossentropy) as the loss function, and Accuracy as the performance metric.

Training Process: The model is trained using model.fit() over 3 epochs with a 10% validation split (validation_split=0.1). Across three epochs, training accuracy increases from ~89% to ~98.97%, while validation accuracy reaches ~98.65%.

Model Evaluation & Results: Evaluating the trained model on the unseen test dataset via model.evaluate() achieves a high test accuracy of 98.59%.

What children can learn

WhatsApp +92 322 421 7059

AI and Robotics Club | The 100 Mission AI & Robotics Hero Journey

Recommended self-paced course for parents & educators

ChatGPT for Kids on Udemy.

Disclosure: This page contains affiliate links. We may earn from qualifying purchases at no extra cost to you. Links are intended for parents and educators, not as marketing directed at young children.

Understanding Artificial Intelligence: Pattern Recognition Labs in Liepaja

Liepaja classes pair model demos with safety rules. Avenvera.com offers broader child development insights. Enquire WhatsApp +92 322 4217059. Supervised platforms only.

Frequently asked questions

What digital safety rules are highlighted in AI modules?

Students learn to protect personal data, avoid sharing private details online, and evaluate AI output critically.

Do students build real machine learning models?

Students train image and text classification models using guided, child-safe platforms.

Where can parents find child psychological guidance resources?

Insights on child growth and learning are available at Avenvera.com.

Explore related learning

RigaRobotics · Riga NarvaAI · Narva DaugavpilsPython · Daugavpils