initial quiz 5
81
quiz/5/main.m
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
clear all;clc
|
||||||
|
%% Only modify the codes in the Define Network Architecture section.
|
||||||
|
|
||||||
|
|
||||||
|
%% Load and Explore Image Data
|
||||||
|
% training data
|
||||||
|
digitDatasetPath = 'optdigits_train';
|
||||||
|
imds_train = imageDatastore(digitDatasetPath, ...
|
||||||
|
'IncludeSubfolders',true,'LabelSource','foldernames');
|
||||||
|
figure;
|
||||||
|
perm = randperm(1873,20);
|
||||||
|
for i = 1:20
|
||||||
|
subplot(4,5,i);
|
||||||
|
imshow(imds_train.Files{perm(i)});
|
||||||
|
end
|
||||||
|
|
||||||
|
labelCount = countEachLabel(imds_train)
|
||||||
|
|
||||||
|
img = readimage(imds_train,1);
|
||||||
|
size(img)
|
||||||
|
|
||||||
|
% validation data
|
||||||
|
digitDatasetPath = 'optdigits_valid';
|
||||||
|
imds_valid = imageDatastore(digitDatasetPath, ...
|
||||||
|
'IncludeSubfolders',true,'LabelSource','foldernames');
|
||||||
|
figure;
|
||||||
|
perm = randperm(1873,20);
|
||||||
|
for i = 1:20
|
||||||
|
subplot(4,5,i);
|
||||||
|
imshow(imds_valid.Files{perm(i)});
|
||||||
|
end
|
||||||
|
|
||||||
|
labelCount = countEachLabel(imds_valid)
|
||||||
|
|
||||||
|
img = readimage(imds_valid,1);
|
||||||
|
size(img)
|
||||||
|
|
||||||
|
% testing data
|
||||||
|
digitDatasetPath = 'optdigits_test';
|
||||||
|
imds_test = imageDatastore(digitDatasetPath, ...
|
||||||
|
'IncludeSubfolders',true,'LabelSource','foldernames');
|
||||||
|
figure;
|
||||||
|
perm = randperm(1873,20);
|
||||||
|
for i = 1:20
|
||||||
|
subplot(4,5,i);
|
||||||
|
imshow(imds_test.Files{perm(i)});
|
||||||
|
end
|
||||||
|
|
||||||
|
labelCount = countEachLabel(imds_test)
|
||||||
|
|
||||||
|
img = readimage(imds_test,1);
|
||||||
|
size(img)
|
||||||
|
|
||||||
|
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%% Modify the codes here %%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
%% Define Network Architecture
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%% Modify the codes here %%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
% network 1
|
||||||
|
layers = [ ...
|
||||||
|
imageInputLayer([8 8 1])
|
||||||
|
convolution2dLayer(4,10,'Padding','same')
|
||||||
|
%batchNormalizationLayer
|
||||||
|
reluLayer
|
||||||
|
fullyConnectedLayer(10)
|
||||||
|
softmaxLayer
|
||||||
|
classificationLayer];
|
||||||
|
|
||||||
|
%% Specify training/validation options
|
||||||
|
options = trainingOptions('adam','MaxEpochs',10, ...
|
||||||
|
'ValidationData',imds_valid, ...
|
||||||
|
'ValidationFrequency',30, ...
|
||||||
|
'Verbose',false, ...
|
||||||
|
'Plots','training-progress');
|
||||||
|
|
||||||
|
%% Train the network
|
||||||
|
net = trainNetwork(imds_train,layers,options);
|
||||||
|
|
||||||
|
%% Predict the labels of new data and calculate the classification accuracy.
|
||||||
|
YPred = classify(net,imds_test);
|
||||||
|
Ytest = imds_test.Labels;
|
||||||
|
accuracy = sum(YPred == Ytest)/numel(Ytest)
|
39
quiz/5/myLReLULayer.m
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
classdef myLReLULayer < nnet.layer.Layer
|
||||||
|
|
||||||
|
properties
|
||||||
|
|
||||||
|
% Scaling coefficient
|
||||||
|
Alpha
|
||||||
|
end
|
||||||
|
|
||||||
|
methods
|
||||||
|
function layer = myLReLULayer(scale, name)
|
||||||
|
% Create an examplePreluLayer with numChannels channels
|
||||||
|
|
||||||
|
% Set layer name
|
||||||
|
if nargin == 2
|
||||||
|
layer.Name = name;
|
||||||
|
end
|
||||||
|
|
||||||
|
% Set layer description
|
||||||
|
layer.Description = ...
|
||||||
|
['myLReLULayer with ', num2str(scale), ' channels'];
|
||||||
|
|
||||||
|
layer.Alpha = scale;
|
||||||
|
end
|
||||||
|
|
||||||
|
function Z = predict(layer, X)
|
||||||
|
% Forward input data through the layer and output the result
|
||||||
|
|
||||||
|
Z = max(X, layer.Alpha*X);
|
||||||
|
end
|
||||||
|
|
||||||
|
function dLdX= backward(layer, X, Z, dLdZ, memory)
|
||||||
|
% Backward propagate the derivative of the loss function through
|
||||||
|
% the layer
|
||||||
|
|
||||||
|
dLdX = layer.Alpha*dLdZ;
|
||||||
|
dLdX(X>0) = dLdZ(X>0);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
BIN
quiz/5/optdigits_test/0/image1684.png
Normal file
After Width: | Height: | Size: 112 B |
BIN
quiz/5/optdigits_test/0/image1685.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1686.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1687.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1688.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1689.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1690.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1691.png
Normal file
After Width: | Height: | Size: 111 B |
BIN
quiz/5/optdigits_test/0/image1692.png
Normal file
After Width: | Height: | Size: 112 B |
BIN
quiz/5/optdigits_test/0/image1693.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1694.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1695.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1696.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1697.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1698.png
Normal file
After Width: | Height: | Size: 112 B |
BIN
quiz/5/optdigits_test/0/image1699.png
Normal file
After Width: | Height: | Size: 116 B |
BIN
quiz/5/optdigits_test/0/image1700.png
Normal file
After Width: | Height: | Size: 112 B |
BIN
quiz/5/optdigits_test/0/image1701.png
Normal file
After Width: | Height: | Size: 116 B |
BIN
quiz/5/optdigits_test/0/image1702.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1703.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1704.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1705.png
Normal file
After Width: | Height: | Size: 111 B |
BIN
quiz/5/optdigits_test/0/image1706.png
Normal file
After Width: | Height: | Size: 106 B |
BIN
quiz/5/optdigits_test/0/image1707.png
Normal file
After Width: | Height: | Size: 112 B |
BIN
quiz/5/optdigits_test/0/image1708.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1709.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1710.png
Normal file
After Width: | Height: | Size: 111 B |
BIN
quiz/5/optdigits_test/0/image1711.png
Normal file
After Width: | Height: | Size: 116 B |
BIN
quiz/5/optdigits_test/0/image1712.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1713.png
Normal file
After Width: | Height: | Size: 112 B |
BIN
quiz/5/optdigits_test/0/image1714.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1715.png
Normal file
After Width: | Height: | Size: 112 B |
BIN
quiz/5/optdigits_test/0/image1716.png
Normal file
After Width: | Height: | Size: 110 B |
BIN
quiz/5/optdigits_test/0/image1717.png
Normal file
After Width: | Height: | Size: 111 B |
BIN
quiz/5/optdigits_test/0/image1718.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1719.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1720.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1721.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1722.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1723.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1724.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1725.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1726.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1727.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1728.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1729.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1730.png
Normal file
After Width: | Height: | Size: 116 B |
BIN
quiz/5/optdigits_test/0/image1731.png
Normal file
After Width: | Height: | Size: 111 B |
BIN
quiz/5/optdigits_test/0/image1732.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1733.png
Normal file
After Width: | Height: | Size: 116 B |
BIN
quiz/5/optdigits_test/0/image1734.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1735.png
Normal file
After Width: | Height: | Size: 112 B |
BIN
quiz/5/optdigits_test/0/image1736.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1737.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1738.png
Normal file
After Width: | Height: | Size: 111 B |
BIN
quiz/5/optdigits_test/0/image1739.png
Normal file
After Width: | Height: | Size: 116 B |
BIN
quiz/5/optdigits_test/0/image1740.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1741.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1742.png
Normal file
After Width: | Height: | Size: 116 B |
BIN
quiz/5/optdigits_test/0/image1743.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1744.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1745.png
Normal file
After Width: | Height: | Size: 111 B |
BIN
quiz/5/optdigits_test/0/image1746.png
Normal file
After Width: | Height: | Size: 112 B |
BIN
quiz/5/optdigits_test/0/image1747.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1748.png
Normal file
After Width: | Height: | Size: 108 B |
BIN
quiz/5/optdigits_test/0/image1749.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1750.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1751.png
Normal file
After Width: | Height: | Size: 112 B |
BIN
quiz/5/optdigits_test/0/image1752.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1753.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1754.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1755.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1756.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1757.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1758.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1759.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1760.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1761.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1762.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1763.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1764.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1765.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1766.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1767.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1768.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1769.png
Normal file
After Width: | Height: | Size: 116 B |
BIN
quiz/5/optdigits_test/0/image1770.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1771.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1772.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1773.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1774.png
Normal file
After Width: | Height: | Size: 116 B |
BIN
quiz/5/optdigits_test/0/image1775.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1776.png
Normal file
After Width: | Height: | Size: 115 B |
BIN
quiz/5/optdigits_test/0/image1777.png
Normal file
After Width: | Height: | Size: 114 B |
BIN
quiz/5/optdigits_test/0/image1778.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1779.png
Normal file
After Width: | Height: | Size: 116 B |
BIN
quiz/5/optdigits_test/0/image1780.png
Normal file
After Width: | Height: | Size: 113 B |
BIN
quiz/5/optdigits_test/0/image1781.png
Normal file
After Width: | Height: | Size: 114 B |