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

CNN MATLAB Lab Instructions

This document provides instructions for using a convolutional neural network in MATLAB to classify handwritten digits from 0 to 9. The network is trained on a dataset of images stored in the MATLAB folder. The network architecture includes convolutional, max pooling and fully connected layers. The trained network achieves over 90% validation accuracy. Instructions are also provided to classify a new handwritten digit image using the trained network, and display the top prediction and probabilities.

Uploaded by

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

CNN MATLAB Lab Instructions

This document provides instructions for using a convolutional neural network in MATLAB to classify handwritten digits from 0 to 9. The network is trained on a dataset of images stored in the MATLAB folder. The network architecture includes convolutional, max pooling and fully connected layers. The trained network achieves over 90% validation accuracy. Instructions are also provided to classify a new handwritten digit image using the trained network, and display the top prediction and probabilities.

Uploaded by

Nur Karima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Convolutional Neural Network using MATLAB Tutorial

We will run an example provided by MATLAB on training a simple Deep Learning


Network for classification. The image dataset we will be using are handwritten numbers

from 0 – 9. These images are already included in your MATLAB folder during installation.

Part A: Create Convolutional Neural Network for Digits Classification

1. Launch MATLAB and wait until it finishes loading.

2. On MATLAB main window, click on ‘Editor’ tab. Then choose ‘New’ > ‘Script’.
3. Copy the following codes and paste it on our Editor.

digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...


'nndatasets','DigitDataset');
imds = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');

4. By examining this code, you should be able to see where the dataset pictures are
stored in your own computer. For example, here is a snippet of dataset for digit ‘2’ in

my own computer.

Figure 1: Dataset pictures in computer

5. We will now randomly display 20 images from our dataset. Copy the following code
and paste into our editor in a new line after the end of the previous code.

figure;
perm = randperm(10000,20);
for i = 1:20
subplot(4,5,i);
imshow(imds.Files{perm(i)});
end
Fadzlin Ahmadon, UiTM Jasin
6. Save the script in your computer as ‘Digits’ and click ‘Run’. If this message box

appears, click ‘Add to Path’.

7. A window should appear with 20 randomly selected images of handwritten digits.


Copy the picture of this window and paste it on your Lab Submission file under the

label [ANS1].
8. Next, we will run a code to calculate how many images do we have for each digit.

Copy the following code and paste into our editor in a new line after the end of the
previous code.

9. Run the code again and notice the output that appears in the command window.
labelCount = countEachLabel(imds)

Calculate the total number of pictures for all digits 0-9 and put the answer under
label [ANS2] in your Lab Submission file.

10. To recap, CNN involves operation on each pixels of the images. To train a network
that can classify the images, all images must be of the same pixel size. We need to

img = readimage(imds,1);
size(img)

specify the image size (in pixels) in the input layer of our network. Now, we’ll add
some codes to check the size of our image. Copy the following code and paste into

our editor in a new line after the end of the previous code.
11. Run the code again and notice the output that appears in the command window.

Two set of numbers will appear which indicates a num x num pixel dimension. Put
this image size under the label [ANS3] in your Lab Submission file.

12. To confirm this information, we’ll go to a random digit image stored in our computer
as shown in Figure 1. Right click on any image of digit, choose Properties, and click

on ‘Details’ tab. The dimension shown here should be the same. Copy the picture of
this window and paste it on your Lab Submission file under the label [ANS4].

Fadzlin Ahmadon, UiTM Jasin


13. In training a neural network, we need to separate our data into ‘training’ and

‘validation’. We will now take 750 images for each digit category for network training,
and the remaining for network validation. Copy the following code and paste into

our editor in a new line after the end of the previous code.
14. Now, we’ll define the network architecture of our CNN. Copy the following code and

numTrainFiles = 750;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');

paste into our editor in a new line after the end of the previous code.

layers = [
imageInputLayer([28 28 1])

convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer

maxPooling2dLayer(2,'Stride',2)

convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer

maxPooling2dLayer(2,'Stride',2)

convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer

fullyConnectedLayer(10)
softmaxLayer
classificationLayer];

15. Based on your understanding of CNN layers and by referring to Slide 8 of CNN

handout notes, identify the Input, Feature Learning and Classification layers of our
CNN architecture and put it in your Lab Submission file as [ANS5], [ANS6] and

[ANS7].
16. Now it is time to specify our training options. You’ll notice familiar information such

as learning rate, and number of epochs. Copy the following code and paste into our
editor in a new line after the end of the previous code.

options = trainingOptions('sgdm', ...


'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ... Fadzlin Ahmadon, UiTM Jasin
'Verbose',false, ...
'Plots','training-progress');
17. And finally, we are ready to train our network. Copy the following code and paste

into our editor in a new line after the end of the previous code.
18. Click ‘Run’ at MATLAB. Training Progress window will appear, and the network will be

trained for the number of epochs we specified in instruction 16. Once training is
done, copy the Training Progress window and paste it into your Lab Submission

document as [ANS8]. Also write the validation accuracy of your network, found on
top right corner of the window, as [ANS9].

net = trainNetwork(imdsTrain,layers,options);

Part B: Classify Our Own Handwriting using the Trained Network

1. At MATLAB Editor, at the end of the previous code, right click and choose ‘ Insert

Section’.
2. In the new section, paste the following code. This is the code to check the input size

of the input layer.

inputSize = net.Layers(1).InputSize

3. Click ‘Run Section’ on Editor. This will run only this new section. Observe the output
at command window.

4. Next, we are going to write a digit on our own. Open ‘Paint’ or any other drawing
application you have in your computer. Set the background colour of your canvas to

be black and draw any digit from 0 – 9 using a white ink. This is the format of our
dataset digits therefore we need to follow.

Fadzlin Ahmadon, UiTM Jasin


Figure 2: My handwriting of digit ‘8’

5. Save this image as ‘mydigit.png’ in the same directory you save ‘Digits’ file in
instruction 6, Part A. Copy this image into your Lab Submission as [ANS10].

6. Copy the following code and paste into our editor in a new line after the end of the
previous code. This code will display your digit image in MATLAB. Click ‘Run Section’

and the image shall appear.

I = imread('mydigit.png');
figure
imshow(I)

7. Next, we’ll check the size of our image. Copy the following code and paste into our

size(I)

editor in a new line after the end of the previous code.


8. Click ‘Run Section’ again and notice the output at Command Window. The output

will be of this format pixel x pixel x channel. The image is saved as RGB, that is why
the channel detected is ‘3’.

9. We need to resize the image to fit in our input layer. Copy the following code and
paste into our editor in a new line after the end of the previous code.

I = imresize(I,inputSize(1:2));
Fadzlin Ahmadon, UiTM Jasin
10. Cut the code in instruction 7 and paste it after the new code above. Click ‘ Run

Section’ again and note the output in command window. The image has been
resized, but the image channel is still ‘3’.

11. We need to convert this image into a grayscale image. Add the following code to our
editor. Then cut the code in instruction 7 and paste it after the new code above. Click

‘Run Section’ again and note the output in command window

I = rgb2gray(I)

12. We are done with pre-processing the image and can now test the network. Insert a
[label,scores] = classify(net,I);
label

new section after the end of the previous code and paste the following code.
13. Click ‘Run Section’ and notice the output at the Command Window. Did the network

correctly classify your digit?


14. We will now display the predicted probability of the image having the label. Copy the

following code and paste into our editor in a new line after the end of the previous
code.

15. Click ‘Run Section’, a window should appear of your image, with the label class
figure
imshow(I)
classNames = net.Layers(end).ClassNames;
title(string(label) + ", " + num2str(100*scores(classNames == label),3) +
"%");

followed with a probability percentage on top of it. Copy this window image into

your submission as [ANS11].


16. Finally, we’re going to look at the top 5 predictions of our image and their associated

probabilities. Copy the following code and paste into our editor in a new line after
the end of the previous code.

[~,idx] = sort(scores,'descend');
idx = idx(5:-1:1);
classNamesTop = net.Layers(end).ClassNames(idx);
scoresTop = scores(idx);

figure
barh(scoresTop)
xlim([0 1])
title('Top 5 Predictions')
xlabel('Probability')
yticklabels(classNamesTop)

Fadzlin Ahmadon, UiTM Jasin


17. Click ‘Run Section’ and a ‘Top 5 Predictions’ window will appear with other five

results of classifications for ‘mydigit.png’. Copy this window image into your
submission as [ANS12].

That’s it for this tutorial. You can restart the network training if you want by adjusting

our network architecture: adding or removing some feature classification layers, changing
the value of learning rate, amount of dataset for training / validation, and even epochs.

Then check out if the accuracy is higher or lower with your new changes.

You can also try writing on a piece of paper, instead of using ‘Paint’ and get the network

to classify your digit. Just remember to inverse the image first if you used black pen on
white paper. You can use online colour invert tool, or even Adobe Photoshop if you have

it.

Knowledge from this tutorial is important for our project. Therefore, I hope everyone

would try to finish this tutorial, and I hope everyone had fun the activity.

Fadzlin Ahmadon, UiTM Jasin

You might also like