0% found this document useful (0 votes)
2 views

Lecture 3 HandDigitRecognition

The document outlines a process for building and training a neural network using the Keras library on the MNIST dataset, which consists of handwritten digits. It includes steps for data preprocessing, network architecture design, compilation, training, and performance evaluation. The model achieved an accuracy of approximately 98.11% on the test dataset and was saved for future use.

Uploaded by

aliraxa1775
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 3 HandDigitRecognition

The document outlines a process for building and training a neural network using the Keras library on the MNIST dataset, which consists of handwritten digits. It includes steps for data preprocessing, network architecture design, compilation, training, and performance evaluation. The model achieved an accuracy of approximately 98.11% on the test dataset and was saved for future use.

Uploaded by

aliraxa1775
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In [29]: import keras

from keras.datasets import mnist


from keras.utils import to_categorical
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

In [30]: (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

In [31]: train_images.shape

(60000, 28, 28)


Out[31]:

In [32]: len(train_labels)

60000
Out[32]:

In [33]: train_labels

array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)


Out[33]:

In [34]: test_images.shape

(10000, 28, 28)


Out[34]:

In [35]: len(test_labels)

10000
Out[35]:

In [36]: test_labels

array([7, 2, 1, ..., 4, 5, 6], dtype=uint8)


Out[36]:

In [37]: # Step 1: Data Preprocessing (Machine Readable Form)


# Scaling - One Unit Standard Deviation for Numeric Columns
# Fundamental requirement of Deep Learning Networks

train_images = train_images.reshape((60000, 28 * 28))


train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28 * 28))


test_images = test_images.astype('float32') / 255

In [38]: # Step 1: Data Preprocessing (Machine Readable Form)


# OneHot Coding for Categorical Data

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

In [39]: # Step 2: Build your Network


from keras import models
from keras import layers

network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))

In [40]: # Step 3: Compile your Network


network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])

In [41]: # Step 4: Training / Learning your Network


network.fit(train_images,
train_labels,
epochs=5,
batch_size=128)

Epoch 1/5
469/469 [==============================] - 4s 9ms/step - loss: 0.2550 - accuracy: 0.9257
Epoch 2/5
469/469 [==============================] - 5s 10ms/step - loss: 0.1027 - accuracy: 0.9693
Epoch 3/5
469/469 [==============================] - 4s 9ms/step - loss: 0.0678 - accuracy: 0.9793
Epoch 4/5
469/469 [==============================] - 5s 10ms/step - loss: 0.0484 - accuracy: 0.9851
Epoch 5/5
469/469 [==============================] - 5s 11ms/step - loss: 0.0370 - accuracy: 0.9884
<tensorflow.python.keras.callbacks.History at 0x1d90ba331c8>
Out[41]:

In [42]: # Step 5: Performance Evaluation - Test Dataset


# Unseen (Not included in Training Dataset)
test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)

313/313 [==============================] - 1s 2ms/step - loss: 0.0641 - accuracy: 0.9811


test_acc: 0.9811000227928162

In [51]: # Save Model use in FUTURE Use


network.save('HandDigitRecorgnitionModel.h5')

You might also like