代写代考 ECE3093 Assignment 2: handwritten digit recognition

ECE3093 Assignment 2: handwritten digit recognition
Electronic submission of this assignment is due on Moodle by 11:55 PM on Monday, 2 May 2022. You are required to submit a single-file report in pdf format and a Matlab script, both of which must adhere to the instructions given below.
Interpreting hand-written text is an extremely useful ability for a computer. This assignment asks you to build a Matlab code capable of recognising single-digit handwritten numbers from the MNIST database using Principal Components Analysis.
The MNIST database contains tens of thousands of images of hand-written single-digit numbers. It is used frequently for developing image processing and machine learning (ML) tools. The images in the database have been grey-scaled, centred and scaled and stored as 28 × 28 pixel arrays. Each image has a corresponding label that indicates which digit from 0 to 9 is in the image. The database is split into a training set of 60 000 images and a test set of 10 000 images.

Copyright By PowCoder代写 加微信 powcoder

You are asked to use Principal Components Analysis (PCA) based on diagonalisation of a covariance matrix to create a Matlab code that creates a prediction function (machine learning model) based on the training set of images of the MNIST data set. This prediction function should then produce a prediction of the digit contained in each of the test images. In your code you should include the algorithm for training the code. You must also produce a report, carefully explaining the algorithm underlying your code and any choices that you have made, for example the number of principal components to keep. In addition to the discussion, your report should include:
1. a plot of the training data projected onto the first two principal components, colour coded by the digit that is associated with each image in the label array (If you are using the full 60,000 points as training data, only plot say the first 5000 as otherwise plotting beomces very messy).
2. a plot of the eigenvalues and of the cumulative eigenvalues with the eigenvalues sorted in descending size.
3. a clear statement on the number of principal components retained and why you chose that number.
4. the result of running your code on the test data set in the form of the fraction of incorrect predictions on the test data and the time taken to make the predictions. This should be compared to making predictions using the unprocessed training data (without PCA).
5. A brief conclusion on the value of PCA in this task of classifying images of digits.
Useful information and additional instructions:
1. You can use the prepareData.m file available on the Mathworks website to download the MNIST database and prepare it for use by Matlab. The command to use the code is
[imgDataTrain, labelsTrain, imgDataTest, labelsTest] = prepareData;
This downloads the data (if necessary) and imports it into Matlab (NB the dataset is only approx. 15MB, but can take 5-10 minutes to download). The above command loads the training data images into a 28 × 28 × 1 × 60000 array imgDataTrain together with a 60000 × 1 array of labels giving the human interpretation of the number shown (and similarly for the test data arrays). Some useful commands:
􏰀 imagesc(imgDataTrain(:,:,1,7))plotsthe7thimage(withthedefaultcolormap,notgreyscale)
􏰀 imshow(imgDataTrain(:,:,1,7),’InitialMagnification’,1000,’Colormap’,1-gray)shows
the image black-on-white (1-gray) and scaled ×10.
􏰀 labelsTrain(23) returns the human interpretation of the 23rd image (in this case 9).
Be aware that the data as loaded is as uint8 unsigned integer format. To be able to manipulate the data you will need to convert it to double.
2. Though Matlab has a built-in function to do PCA, you should demonstrate that you have understood how PCA works using eigenanalysis. Hence your code must use the eig or eigs function, not pca. You may also find the commands reshape and sort useful in your code.

3. Through PCA on the training data, the 282 = 784 pixel values for each training image are reduced to a much smaller set of key “feature” values that give the coordinates cj in the jth “eigendigit” direction. The same reduction can be applied to a test image. Now you need to apply a ML model to map from the eigenspace to the image category (digits 0 to 9) – a task know as classification in data science. There are many such models that could be used (e.g. neural networks, decision trees, discrimination analysis etc.). Fitting a model to training data typically requires solving a non-trivial optimisation problem. Once the model has been trained it can be used to predict the class of any new image.
For this assignment you will use one of the simplest ML models, the k-Nearest Neighbour (kNN) model. For this model the training step is trivial (just remembering the training data). The prediction for a new point (image) x involves finding the k nearest points to x in the training data and assigning the digit label that is the most common amongst the neighbours.
Matlab has built-in commands for a variety of machine learning models including for kNN models. The relevant functions are:
􏰀 model = fitcknn(points,labels)
where points(i,j) contains the jth coordinate of the point corresponding to image i.
􏰀 digit = predict(model,X)
where X is either a single vector or a matrix with rows corresponding to points (images). It returns the predicted category (digit) associated with each point. Note, that by default the labels and predictions are categorical rather than an integer type. So you may need to use int64(digit)==3 or digit==categorical(3) to test if the digit is the number three.
If you are interested in experimenting with machine learning models there are many others built into matlab that you could try and also a range of parameters for the kNN model that you could play with to see if you can improve the speed and accuracy of the prediction, but that is not required for this assignment.
4. You are to compare the time of predict for the whole test data set (10,000 images) when the model has been either (a) fit to the reduced points based on your PCA analysis or (b) the original images. You may want to use the matlab functions tic and toc to get the time.
5. You may find that your computer struggles with the entire data set, however you should be able to use a reasonable fraction of the data and this should not significantly impact your results. If you find you need to limit the data set, you should state clearly which data you are using.
Warning: Because this database is relatively commonly used, you will find many different approaches have been coded to identify the digits. You are being asked specifically to use the PCA method and kNN model described above. Any information that you find online or elsewhere that you make direct use of in your code or your report must be fully referenced and acknowledged.
The assignment will be marked out of 20 with the following approximate breakdown of marks:
5 marks A clear description of the algorithm you are using and explanations for key decisions. This should be a single pdf file uploaded to Moodle. The report may be typed, or hand-written and scanned.
10 marks Plots and statements as listed in points 1-5 in the boxed description above (included in your report).
5 marks The source code for your implementation in matlab. This should have enough comments to make it easy to see which step of the overall method each part of the code is carrying out. It will be assessed mainly to check that it matches your description and the instructions in this assignment but should be self-contained enough to allow it to be run in matlab. (If you are using multiple functions, please concatenate them all into a single file)
Code and/or results without a clear and detailed description will receive limited marks. Note that there is not a unique “correct” answer to this assignment. Your approach and justification for the choices you make are the most important parts.

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com