π¦ Keras + CNN + MNIST
πΉ Video Tutorial
π Contents
- β Google Colab + GPU + Google Drive setup
- β Keras + TensorBoard integration
- β Build CNN model architecture
- β Download MNIST dataset & visualize samples
- β Train the model
- β Save & load model
- β 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
- π» Complete Python Code: Simple example for CNN + MNIST + Keras
- π¦ MNIST JPEG Dataset: Download MNIST image dataset (jpg files)
- ποΈ Premium Dataset:
- http://gofile.me/5HfHO/Uny0OLQFG
- Password : ask to me (hello@marearts.com)
ππ» Thank you!
Β
main image by Bofu Shaw




