Handwritten Character Recognition In this assignment, you are required to practi
ID: 3891251 • Letter: H
Question
Handwritten Character Recognition
In this assignment, you are required to practice the application of handwritten character recognition using any method taught in the classroom. The steps to do this assignment include
1. downloading the dataset;
2. preprocessing character images;
3. reducing the dimension;
4. choosing a classifier and training it;
5. evaluating the performance of the classifier.
In what follows, let’s explain each step briefly.
1. Downloading Dataset
The dataset is the MNIST database of handwritten digits, available from http://yann.lecun.com/exdb/mnist, which has a training set of 60,000 examples and a test set of 10,000 examples. Each example is a grey-level image which has been sizenormalized (28x28 pixels). If you want to access the examples using C or C++, you can read the descriptions about the format of the database on the above webpage. If you are using Python, then I would suggest another convenient way to access the database. You can first install the sklearn package (some Python installation may not install this package by default) and then use the following command to download the dataset: from sklearn.datasets imort fetch_mldata mnist = fetch_mldata(‘MNIST original’) The variable mnist would be a class object in which it contains the following two attributes:
? mnist.data: a 70000x784 numpy array which contains all examples with each 28x28 pixel values (0~255) flattened into a 784-dimendional vector;
? mnist.target: an array of 70000 labels (0~9) of the examples in the mnist.data. To view any example image in the dataset, you can use the following code: import numpy as np #import numpy package import matplotlib.pyplot as plt #plotting package img = np.reshape(mnist.data[0,:], (28,28)) #reshape 1-D vector of the first example to a 2-D image plt.imshow(img, cmap=plt.cm.gray) #prepare image for display plt.show() #display image
2. Preprocessing Character
Images Since the pixel values range between 0 and 255, I would suggest to normalize the values into the range of [0,1]. Therefore, you can just divide each pixel value by 255 to do the value normalization. Since each digit has been well centered in the image, you don’t need to relocate the digits. However, there may exist some blank areas (see the example image given above) around the digits. If you care about the possible side effects caused by these blank areas, then you may try to remove such blank areas on your own. Please remember to redo the size normalization (resizing the image as a fixed-size, say 28x28 pixels, image) after removing the blank areas. The image resizing can be done using the openCV package (https://docs.opencv.org/3.4.0/da/d6e/tutorial_py_geometric_transformations.html).
3. Reducing Dimension
The total dimension of an input example is 28x28=784, which is a little bit high. To reducing the cost caused by the high dimensionality and improve the recognition performance by removing correlated features in the input vector. You are required to do the dimension reduction by principal component analysis (PCA). After the PCA, it’s optional to do the Fisher linear discriminant analysis (FLDA) to further transform the features for attaining better separability between different digit classes. During the PCA, you should try different settings for the reduced dimension. A heuristic rule for choosing an appropriate dimension is to find the reduced dimension ?? such that 0.95 ? ? ???? ?? ??=1 ? ???? ?? ??=1 ? 0.97, Where ?? is the original dimension and ??? ’s are the eigenvalues of the correlation matrix ???? sorted in descending order. If you do not understand what these math symbols are, you should review the course slides thoroughly.
4. Choosing a Classifier and Training It
Any method we have taught in the classroom can be chosen as your classifier. You can even use more than one classifier to do the assignment and compare their performances. If you can do this, then you will get higher scores on this assignment. That is very easy if you use the sklearn package of the Python. You don’t even need to rewrite many lines of your codes to replace one classifier with another. The dataset has totally 70,000 examples. You can use the first 60,000 to train your classifier and leave the remained 10,000 for performance evaluation. If you download the dataset from the webpage given previously, then just use the training set and testing set which have been already prepared for training and testing, respectively.
5. Evaluating the Performance
You have to evaluate the performance of your classifier(s) using the testing set. Please show the performance comparison for different classifiers, if more than one classifier is used. The performances of using different reduced dimensions also need to compared. Most importantly, please give a discussion on your results in your report. Please also describe the problems you suffer and solve in this assignment.
6. Others
Please follow the rules set for the assignment submission, including version control, no plagiarism, on-time assignment submission, and so on.
1. Downloading Dataset The dataset is the MNIST database of handwritten digits, available from http://yann.lecun.com/exdb/mnist, which has a training set of 60,000 examples and a test set of 10,000 examples. Each example is a grey-level image which has been size- normalized (28x28 pixels). If you want to access the examples using C or C++, you can read the descriptions about the format of the database on the above webpage. If you are using Python, then I would suggest another convenient way to access the database. You can first install the sklearn package (some Python installation may not install this package by default) and then use the following command to download the dataset from sklearn.datasets imort fetch mldata mnist - fetch_mldata( 'MNIST original') The variable mnist would be a class object in which it contains the following two attributes * mnist.data: a 70000x784 numpy array which contains all examples with each 28x28 pixel values (0-255) flattened into a 784-dimendional vector; » mnist.target: an array of 70000 labels (0-9) of the examples in the mnist.data To view any example image in the dataset, you can use the following code: import numpy as np #import numpy package import matplot lib.pyplot as plt #plotting package ing np . reshape (mn is t . data [0,:], (28,28)) #reshape 1-D vector of the first example to a 2-D image plt.imshow (ing, crap-plt.cn.gray) #prepare image for display pit. show ( ) #display image 25 5 10 2025Explanation / Answer
#I have done this using keras module to train the neural network
#Here we will import the dataset
from keras.datasets import mnist
from matplotlib import pyplot
# load data into training and testing data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
import numpy
#Now we will import the required modules of keras library
from keras.layers import Dense,Dropout,Flatten
from keras.models import Sequential
#Here we will apply one convolutional neural layer as the first layer
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
K.set_image_dim_ordering('th')
# reshape the data
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
# normalize inputs from 0-255 to 0-1 as required in step 2
X_train = X_train / 255
X_test = X_test / 255
# one hot encode outputs we use np_utils.to_categorical for this
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
#We choose num_classes as y_test.shape[1] which will be 10 because we want to classify in 10 digits
num_classes = y_test.shape[1]
# create model
model = Sequential()
model.add(Conv2D(30, (5, 5), input_shape=(1, 28, 28), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(15, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200)
# Final evaluation of the model on test set
scores = model.evaluate(X_test, y_test, verbose=0)
print("Error in this model is: %.2f%%" % (100-scores[1]*100))
#Now we save this model to a file through yaml so that we need not train it again and again
from keras.models import model_from_yaml
model_yaml = model.to_yaml()
with open("model.yaml", "w") as yaml_file:
yaml_file.write(model_yaml)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.