KarthikaRajagopal commited on
Commit
3a0d0dc
Β·
verified Β·
1 Parent(s): 7978c29

Create MNIST_Digit_Recognizer.ipynb

Browse files

Code Walkthrough
1. Dataset Loading:
python
Copy code
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train and X_test: Images of handwritten digits.
y_train and y_test: Corresponding digit labels.
2. Data Preprocessing:
python
Copy code
X_train = X_train.reshape(60000, 28, 28, 1)
X_test = X_test.reshape(10000, 28, 28, 1)
y_train_one_hot = to_categorical(y_train)
y_test_one_hot = to_categorical(y_test)
Reshaping: Adds a "channel" dimension to images (required by CNNs).
Normalization: Automatically done by to_categorical.
One-Hot Encoding: Converts labels into vectors.
3. Building the CNN Model:
python
Copy code
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=(28,28,1)))
model.add(Conv2D(32, kernel_size=3, activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
Conv2D: Applies filters (kernels) to extract image features.
First layer: 64 filters, each 3x3.
Second layer: 32 filters, each 3x3.
MaxPool2D: Downsamples feature maps to reduce size and computation.
Flatten: Converts 2D feature maps into a 1D vector.
Dense Layer: Fully connected layer with 10 outputs (one for each digit).
4. Compiling the Model:
python
Copy code
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Adam Optimizer: Combines advantages of Momentum and RMSProp for faster convergence.
Categorical Cross-Entropy: Measures the difference between predicted and actual probabilities.
5. Training the Model:
python
Copy code
hist = model.fit(X_train, y_train_one_hot, validation_data=(X_test, y_test_one_hot), epochs=10)
Epochs: The number of times the model sees the entire dataset.
Validation Data: Used to check how well the model generalizes.
Architecture Overview
Input Layer:

Shape: (28, 28, 1) (grayscale images).
Convolutional Layers:

Extract features like edges, curves, and shapes.
Pooling Layer:

Reduces spatial dimensions to make computation efficient.
Fully Connected Layer:

Combines features to make final predictions.
Output Layer:

Softmax Activation gives probabilities for each digit (0-9).
Tools and Libraries
TensorFlow/Keras:

TensorFlow is the backend, and Keras is the high-level API.
Matplotlib:

For visualizing sample images.
Numpy:

For data manipulation.

Files changed (1) hide show
  1. MNIST_Digit_Recognizer.ipynb +71 -0
MNIST_Digit_Recognizer.ipynb ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Description: This program use Convolutional Neural Networks(CNN)
2
+ # classify handwritten digits as number 0-9
3
+
4
+ #importing the libraries
5
+ from keras.models import Sequential
6
+ from keras.layers import Dense, Conv2D, Flatten, MaxPool2D
7
+ from keras.datasets import mnist
8
+ from keras.utils import to_categorical
9
+ import numpy as np
10
+ import matplotlib.pyplot as plt
11
+
12
+ #Load the data and split it into train and test
13
+ (X_train, y_train), (X_test, y_test) = mnist.load_data()
14
+ Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
15
+ 11490434/11490434 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step
16
+
17
+ #Get the image shape
18
+ print(X_train.shape)
19
+ print(X_test.shape)
20
+ (60000, 28, 28)
21
+ (10000, 28, 28)
22
+
23
+ plt.imshow(X_train[2])
24
+ <matplotlib.image.AxesImage at 0x7d583f968610>
25
+
26
+ # Reshaping the data to fit the model
27
+ X_train = X_train.reshape(60000, 28, 28, 1)
28
+ X_test = X_test.reshape(10000, 28, 28, 1)
29
+
30
+ # One-Hot Encoding:
31
+ y_train_one_hot = to_categorical(y_train)
32
+ y_test_one_hot = to_categorical(y_test)
33
+
34
+ # Print the new label
35
+ print(y_train_one_hot[0])
36
+ [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
37
+
38
+ # Build the CNN model
39
+ model = Sequential()
40
+ # Add model layers
41
+ model.add(Conv2D(64, kernel_size=3, activation = 'relu', input_shape=(28,28,1)))
42
+ model.add(Conv2D(32, kernel_size=3, activation='relu'))
43
+ model.add(MaxPool2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None))
44
+ model.add(Flatten())
45
+ model.add(Dense(10,activation='softmax'))
46
+
47
+ model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
48
+
49
+ #Train the model
50
+ hist = model.fit(X_train,y_train_one_hot, validation_data=(X_test,y_test_one_hot), epochs=10)
51
+ Epoch 1/10
52
+ 1875/1875 ━━━━━━━━━━━━━━━━━━━━ 152s 80ms/step - accuracy: 0.8982 - loss: 0.8538 - val_accuracy: 0.9752 - val_loss: 0.0818
53
+ Epoch 2/10
54
+ 1875/1875 ━━━━━━━━━━━━━━━━━━━━ 201s 80ms/step - accuracy: 0.9762 - loss: 0.0775 - val_accuracy: 0.9778 - val_loss: 0.0704
55
+ Epoch 3/10
56
+ 1875/1875 ━━━━━━━━━━━━━━━━━━━━ 150s 80ms/step - accuracy: 0.9816 - loss: 0.0580 - val_accuracy: 0.9795 - val_loss: 0.0697
57
+ Epoch 4/10
58
+ 1875/1875 ━━━━━━━━━━━━━━━━━━━━ 202s 80ms/step - accuracy: 0.9855 - loss: 0.0438 - val_accuracy: 0.9820 - val_loss: 0.0593
59
+ Epoch 5/10
60
+ 1875/1875 ━━━━━━━━━━━━━━━━━━━━ 202s 80ms/step - accuracy: 0.9897 - loss: 0.0305 - val_accuracy: 0.9815 - val_loss: 0.0725
61
+ Epoch 6/10
62
+ 1875/1875 ━━━━━━━━━━━━━━━━━━━━ 202s 80ms/step - accuracy: 0.9909 - loss: 0.0287 - val_accuracy: 0.9796 - val_loss: 0.0779
63
+ Epoch 7/10
64
+ 1875/1875 ━━━━━━━━━━━━━━━━━━━━ 150s 80ms/step - accuracy: 0.9933 - loss: 0.0220 - val_accuracy: 0.9836 - val_loss: 0.0803
65
+ Epoch 8/10
66
+ 1875/1875 ━━━━━━━━━━━━━━━━━━━━ 202s 80ms/step - accuracy: 0.9941 - loss: 0.0174 - val_accuracy: 0.9820 - val_loss: 0.0896
67
+ Epoch 9/10
68
+ 1875/1875 ━━━━━━━━━━━━━━━━━━━━ 201s 80ms/step - accuracy: 0.9936 - loss: 0.0190 - val_accuracy: 0.9798 - val_loss: 0.0926
69
+ Epoch 10/10
70
+ 1875/1875 ━━━━━━━━━━━━━━━━━━━━ 203s 81ms/step - accuracy: 0.9953 - loss: 0.0148 - val_accuracy: 0.9827 - val_loss: 0.1042
71
+