🍦 Keras + CNN + MNIST

🍦 Keras + CNN + MNIST

πŸ“Ή Video Tutorial

https://youtu.be/o3ERJ_7_0pg

πŸ“š Contents

  1. βœ… Google Colab + GPU + Google Drive setup
  2. βœ… Keras + TensorBoard integration
  3. βœ… Build CNN model architecture
  4. βœ… Download MNIST dataset & visualize samples
  5. βœ… Train the model
  6. βœ… Save & load model
  7. βœ… Test model predictions

1. Build CNN Model

🐍 build_cnn_model.py
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Input
from tensorflow.keras.layers import Conv2D, MaxPooling2D

num_classes = 10
input_shape = (28, 28, 1)  # MNIST channels first format

# Build Sequential CNN model
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

# Compile model
model.compile(
    loss=keras.losses.categorical_crossentropy,
    optimizer=keras.optimizers.Adadelta(),
    metrics=['accuracy']
)

# Display model architecture
model.summary()

2. Download MNIST Dataset

🐍 load_mnist_data.py
from keras.datasets import mnist
import numpy as np

# Load the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Reshape: (60000, 28, 28) -> (60000, 28, 28, 1)
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))

# Convert class vectors to binary class matrices (one-hot encoding)
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

πŸ‘‰πŸ» Download Original Data: You can download MNIST JPEG files from here:
MNIST jpeg data, 24x24, 70000 images

3. Visualize Dataset Samples

🐍 visualize_samples.py
import matplotlib.pyplot as plt

row = 10
col = 10
n = row * col

plt.figure(figsize=(4, 4))
for i in range(n):
    # Create subplot
    ax = plt.subplot(row, col, i + 1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()

4. Set Up TensorBoard

🐍 setup_tensorboard.py
# Install and load TensorBoard
!pip install tensorboard
%load_ext tensorboard

import datetime, os

# Create logs directory
logs_base_dir = "./logs"
os.makedirs(logs_base_dir, exist_ok=True)

# Launch TensorBoard
%tensorboard --logdir {logs_base_dir}

5. Train the Model

🐍 train_model.py
from tensorflow.keras.callbacks import TensorBoard

batch_size = 128
epochs = 12

# Train model with TensorBoard callback
model.fit(
    x_train, y_train,
    epochs=epochs,
    batch_size=batch_size,
    shuffle=True,
    validation_data=(x_test, y_test),
    callbacks=[TensorBoard(log_dir=logs_base_dir)]
)

Evaluate Model Performance

🐍 evaluate_model.py
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

6. Test Single Image Prediction

🐍 test_single_image.py
# Get single test image
x_test[0].shape
one_image = x_test[0].reshape(1, 28, 28, 1)

# Predict
y_pred_all = model.predict(one_image)
y_pred_it = model.predict_classes(one_image)
print(y_pred_all, y_pred_it)

# Display image
plt.imshow(x_test[0].reshape(28, 28))
plt.show()

7. Save & Load Model

Save Model to Google Drive

🐍 save_model.py
model.save('/content/drive/My Drive/Colab Notebooks/my_cnn_mnist_model.h5')

Load Saved Model

🐍 load_model.py
from tensorflow.keras.models import load_model

new_model = load_model('/content/drive/My Drive/Colab Notebooks/my_cnn_mnist_model.h5')

8. Test with Custom Image

🐍 test_custom_image.py
from google.colab import drive
from PIL import Image
import numpy as np

# Mount Google Drive
drive.mount('/content/drive')

# Load custom image
img_path = '/content/drive/My Drive/Colab Notebooks/mnist_0_31.jpg'
img = Image.open(img_path)
img = np.resize(img, (28, 28, 1))
im2arr = np.array(img)
im2arr = im2arr.astype('float32') / 255.
im2arr = im2arr.reshape(1, 28, 28, 1)

# Predict
y_pred = new_model.predict_classes(im2arr)
print("Predicted digit:", y_pred)

πŸ“Š Model Architecture Summary

Layer Type Parameters
Layer 1 Conv2D (32 filters, 3x3) ReLU activation
Layer 2 Conv2D (64 filters, 3x3) ReLU activation
Layer 3 MaxPooling2D (2x2) -
Layer 4 Dropout 25% dropout rate
Layer 5 Flatten -
Layer 6 Dense (128 units) ReLU activation
Layer 7 Dropout 50% dropout rate
Output Dense (10 units) Softmax activation

🎯 Key Features

  • 🧠 CNN Architecture: Convolutional layers for feature extraction
  • πŸ“Š TensorBoard Integration: Real-time training visualization
  • πŸ’Ύ Model Persistence: Save and load trained models
  • ☁️ Google Colab: Free GPU training
  • πŸ“ Google Drive: Cloud storage for models
  • βœ… High Accuracy: ~99% on MNIST test set

πŸ“š Resources

πŸ™‡πŸ» Thank you!

Β 

main image by Bofu Shaw

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.