Video Summary — CNN Implementation on Fashion-MNIST (Arabic lecture) 🧠🧵
Overview
- Course: Final stage — implementing a CNN image classifier in Python using TensorFlow/Keras on the Fashion-MNIST dataset.
- Dataset: 70,000 grayscale images, 28×28 px, 10 classes (0–9). Typical split: 60,000 train, 10,000 test; in this session they use 55,000 train + 10,000 validation + 5,000? (adjusted explained).
- Goal: load data, preprocess, build CNN, train, save/restore model, predict and evaluate.
Libraries & setup
- Import: tensorflow as tf, from tensorflow import keras, numpy as np, matplotlib.pyplot as plt, pandas as pd (for history plotting).
- Check TensorFlow and Keras versions.
Data loading & preprocessing
- Load dataset:
- (x_train_full, y_train_full), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()
- Reshape inputs to 4D tensors:
- x_train_full = x_train_full.reshape((-1, 28, 28, 1))
- x_test = x_test.reshape((-1, 28, 28, 1))
- Data type and pixel range:
- dtype: uint8 (0–255). Scale to [0,1] by dividing by 255.0.
- Create validation set:
- Example in lecture: take first 10,000 from training as validation.
- x_val = x_train_full[:10000] / 255.0
- x_train = x_train_full[10000:] / 255.0
- y_val = y_train_full[:10000]; y_train = y_train_full[10000:]
- Print shapes / lengths to confirm splits.
Visualize samples
- Plot single test image:
- plt.figure(); plt.imshow(x_test[0].squeeze(), cmap='gray'); plt.show()
- Class names mapping:
- 0: T-shirt/top, 1: Trouser, 2: Pullover, 3: Dress, 4: Coat, 5: Sandal, 6: Shirt, 7: Sneaker, 8: Bag, 9: Ankle boot
- Plot a 5×5 grid of 25 images with titles showing class names.
CNN model architecture (implemented with Keras Sequential)
- Utility: define a partial function for default Conv2D args:
- kernel_size=3, activation='relu', padding='same' (padding='same' used? lecture mentions "pad = same" or zero-padding earlier)
- Example architecture used:
- Conv2D(32, kernel_size=3, activation='relu', input_shape=(28,28,1))
- MaxPooling2D(pool_size=2)
- Conv2D(64, kernel_size=3, activation='relu')
- Conv2D(128, kernel_size=3, activation='relu')
- MaxPooling2D(pool_size=2)
- (they doubled filters: 32 → 64 → 128)
- Flatten()
- Dense(64, activation='relu')
- Dropout(0.5) # lecture used 50%
- Dense(10, activation='softmax')
- Notes:
- ReLU after conv layers.
- Max pooling reduces spatial dimensions progressively.
- Filter counts typically doubled each conv block.
- Last Dense gives class probabilities (softmax).
Compile & train
- Compile:
- loss = 'sparse_categorical_crossentropy'
- optimizer = 'adam' (lecture used Adam; SGD could be tried)
- metrics = ['accuracy']
- Train (example values):
- history = model.fit(x_train, y_train, epochs=3, validation_data=(x_val, y_val))
- Lecture used epochs=3 for demo (recommended to use more, e.g., 20).
- After training: evaluate on test set:
- test_loss, test_acc = model.evaluate(x_test, y_test)
Save & load model
- Save:
- model.save('path/to/model.h5', save_format='h5')
- Load:
- model = keras.models.load_model('path/to/model.h5')
Predictions & inspection
- Predict on validation or test:
- y_pred_probs = model.predict(x_val)
- y_pred = np.argmax(y_pred_probs, axis=1)
- Compare sample predictions to true labels:
- print(y_pred[:7], y_val[:7])
- Lecture showed some mismatches and mentioned ~86% accuracy with chosen settings.
- Increase epochs (lecture suggested e.g., 20).
- Reduce dropout rate (less than 0.5) or remove if unnecessary.
- Use more training data / augmentation.
- Adjust network depth/filters or use different optimizers/hyperparameters.
Recap of learning path (context)
- Course covered: basics (math, ML), artificial neural networks, optimizers (SGD, Adam, RMSProp), hyperparameters (epochs, batch size), image classifiers with MLP, normalization/regularization/data augmentation, then deep learning with CNNs and architecture rationale (feature maps, pooling, flattening), ending with this implementation.
Key code snippets (conceptual)
- Load & reshape:
- keras.datasets.fashion_mnist.load_data()
- reshape to (-1,28,28,1)
- normalize: /255.0
- Model building: sequential conv → pool blocks → flatten → dense → dropout → output
- Compile: sparse_categorical_crossentropy, Adam, accuracy
- Fit: model.fit(..., validation_data=(x_val,y_val), epochs=N)
- Save/load: model.save(...), keras.models.load_model(...)
Final notes ✅
- The lecture demonstrates a full CNN workflow: load, preprocess, visualize, build, train, evaluate, save/restore, predict.
- For production-quality results: use more epochs, tune dropout, consider data augmentation and architecture tuning.
📬 Never miss a Hashim EduTech video — every new upload summarized in your inbox.
Follow free