Step 12:
Train the network and save the trained network in a file called digitnet
clear all;
close all;
clc;
[XTrain,YTrain] = digitTrain4DArrayData;
size(XTrain) %images
size(YTrain) %correct answer labels
XTrain=1-XTrain; % Reverse the black and white colors. Save and run the program to see the difference.
perm = randperm(size(XTrain,4),20); % Randomize the order of images in XTrain
for i = 1:20
subplot(4,5,i);
imshow(XTrain(:,:,:,perm(i)));
end
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
averagePooling2dLayer(7)
fullyConnectedLayer(10) % 10 output layer nodes
softmaxLayer
classificationLayer]; %close the bracket
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.1, ...
'MaxEpochs',20, ...
'Verbose',false, ...
'Plots','training-progress', ...
'Shuffle','every-epoch' );
net1 = trainNetwork(XTrain,YTrain,layers,options);
save digitnet net1;
% XTrain is our training data (5000 images 28x28)
% YTrain is our correct answers (5000 labels)
% layers is the large array of layers we created above
% options is a variable that sets some of the training parameters
The
network is now trained. If you have a USB webcam, you can test it
in the next step by writing digits on a paper to see if the network
recognizes it.