initial quiz 5
|
@ -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)
|
|
@ -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
|
After Width: | Height: | Size: 112 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 111 B |
After Width: | Height: | Size: 112 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 112 B |
After Width: | Height: | Size: 116 B |
After Width: | Height: | Size: 112 B |
After Width: | Height: | Size: 116 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 111 B |
After Width: | Height: | Size: 106 B |
After Width: | Height: | Size: 112 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 111 B |
After Width: | Height: | Size: 116 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 112 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 112 B |
After Width: | Height: | Size: 110 B |
After Width: | Height: | Size: 111 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 116 B |
After Width: | Height: | Size: 111 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 116 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 112 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 111 B |
After Width: | Height: | Size: 116 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 116 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 111 B |
After Width: | Height: | Size: 112 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 108 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 112 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 116 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 116 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 115 B |
After Width: | Height: | Size: 114 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 116 B |
After Width: | Height: | Size: 113 B |
After Width: | Height: | Size: 114 B |