CNN MATLAB Lab Instructions
CNN MATLAB Lab Instructions
from 0 – 9. These images are already included in your MATLAB folder during installation.
2. On MATLAB main window, click on ‘Editor’ tab. Then choose ‘New’ > ‘Script’.
3. Copy the following codes and paste it on our Editor.
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.
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
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].
‘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.
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);
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
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.
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’
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)
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
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
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
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)
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.