Open In App

Python | Classify Handwritten Digits with Tensorflow

Classifying handwritten digits is the basic problem of the machine learning and can be solved in many ways here we will implement them by using TensorFlow
Using a Linear Classifier Algorithm with tf.contrib.learn
linear classifier achieves the classification of handwritten digits by making a choice based on the value of a linear combination of the features also known as feature values and is typically presented to the machine in a vector called a feature vector.
Modules required :
NumPy

\$ pip install numpy
\$ pip install matplotlib
\$ pip install tensorflow

Steps to follow

Step 1 : Importing all dependence

Python3

 import numpy as npimport matplotlib.pyplot as pltimport tensorflow as tf learn = tf.contrib.learn tf.logging.set_verbosity(tf.logging.ERROR)

Step 2 : Importing Dataset using MNIST Data

Python3

 mnist = learn.datasets.load_dataset('mnist')data = mnist.train.imageslabels = np.asarray(mnist.train.labels, dtype=np.int32)test_data = mnist.test.imagestest_labels = np.asarray(mnist.test.labels, dtype=np.int32)

output :

Extracting MNIST-data/train-images-idx3-ubyte.gz
Extracting MNIST-data/train-labels-idx1-ubyte.gz
Extracting MNIST-data/t10k-images-idx3-ubyte.gz
Extracting MNIST-data/t10k-labels-idx1-ubyte.gz

Step 3 : Making dataset

Python3

 max_examples = 10000data = data[:max_examples]labels = labels[:max_examples]

Step 4 : Displaying dataset using MatplotLib

Python3

 def display(i):    img = test_data[i]    plt.title('label : {}'.format(test_labels[i]))    plt.imshow(img.reshape((28, 28)))     # image in TensorFlow is 28 by 28 pxdisplay(0)

To display data we can use this function – display(0)
output :

Step 5 : Fitting data, using linear classifier

Python3

 feature_columns = learn.infer_real_valued_columns_from_input(data)classifier = learn.LinearClassifier(n_classes=10,                                    feature_columns=feature_columns)classifier.fit(data, labels, batch_size=100, steps=1000)

Step 6 : Evaluate accuracy

Python3

 classifier.evaluate(test_data, test_labels)print(classifier.evaluate(test_data, test_labels)["accuracy"])

Output :

0.9137

Step 7 : Predicting data

Python3

 prediction = classifier.predict(np.array([test_data[0]],                                         dtype=float),                                         as_iterable=False)print("prediction : {}, label : {}".format(prediction,      test_labels[0]) )

Output :

prediction : [7], label : 7

Full Code for classifying handwritten

Python3

 # importing librariesimport numpy as npimport matplotlib.pyplot as pltimport tensorflow as tf learn = tf.contrib.learntf.logging.set_verbosity(tf.logging.ERROR)\ # importing dataset using MNIST# this is how mnist is used mnist contain test and train datasetmnist = learn.datasets.load_dataset('mnist')data = mnist.train.imageslabels = np.asarray(mnist.train.labels, dtype = np.int32)test_data = mnist.test.imagestest_labels = np.asarray(mnist.test.labels, dtype = np.int32) max_examples = 10000data = data[:max_examples]labels = labels[:max_examples] # displaying dataset using Matplotlibdef display(i):    img = test_data[i]    plt.title('label : {}'.format(test_labels[i]))    plt.imshow(img.reshape((28, 28)))     # img in tf is 28 by 28 px# fitting linear classifierfeature_columns = learn.infer_real_valued_columns_from_input(data)classifier = learn.LinearClassifier(n_classes = 10,                                    feature_columns = feature_columns)classifier.fit(data, labels, batch_size = 100, steps = 1000) # Evaluate accuracyclassifier.evaluate(test_data, test_labels)print(classifier.evaluate(test_data, test_labels)["accuracy"]) prediction = classifier.predict(np.array([test_data[0]],                                         dtype=float),                                         as_iterable=False)print("prediction : {}, label : {}".format(prediction,      test_labels[0]) ) if prediction == test_labels[0]:     display(0)

Using Deep learning with tf.keras
Deep learning is a subpart of machine learning and artificial intelligence which is also known as deep neural network this networks capable of learning unsupervised from provided data which is unorganized or unlabeled. today, we will implement a neural network in TensorFlow to classify handwritten digit.
Modules required :
NumPy

\$ pip install numpy
\$ pip install matplotlib
\$ pip install tensorflow

Steps to follow

Step 1 : Importing all dependence

Python3

 import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt

Step 2 : Import data and normalize it

Python3

 mnist = tf.keras.datasets.mnist(x_train,y_train) , (x_test,y_test) = mnist.load_data() x_train = tf.keras.utils.normalize(x_train,axis=1)x_test = tf.keras.utils.normalize(x_test,axis=1)

Step 3 : view data

Python3

 def draw(n):    plt.imshow(n,cmap=plt.cm.binary)    plt.show()     draw(x_train[0])

Step 4 : make a neural network and train it

Python3

Step 5 : check model accuracy and loss

Python3

 val_loss,val_acc = model.evaluate(x_test,y_test)print("loss-> ",val_loss,"\nacc-> ",val_acc)

Step 6 : prediction using model

Python3

 predictions=model.predict([x_test])print('label -> ',y_test[2])print('prediction -> ',np.argmax(predictions[2])) draw(x_test[2])

saving and testing model

saving the model

Python3

 #saving the model# .h5 or .model can be used model.save('epic_num_reader.h5')