-
Notifications
You must be signed in to change notification settings - Fork 0
/
AE-MNIST
144 lines (114 loc) · 3.56 KB
/
AE-MNIST
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
% Load the training data into memory
train_data = csvread('/Users/apple/Documents/MSCPROJ/mnist/mtrain.csv');
xTrainImagesCSV= train_data(5:788,:);
tTrainCSV = train_data(1:4,:);
%length(xTrainImages)
%length(tTrain)
formattedxTrainImagesCSV = {};
for i=1:1:7591
oneTrainImage= xTrainImagesCSV(:,i);
oneImageMatrix = {reshape(oneTrainImage,28,28)};
formattedxTrainImagesCSV(:,i)=oneImageMatrix;
end
formattedtTrainCSV = {};
%{
for i=1:1:7591
oneTrainLabel = tTrainCSV (1:10,i);
oneLabelMatrix = {reshape(oneTrainLabel,10,1)};
formattedtTrainCSV(1,i) = oneTrainLabel;
end
%}
formattedtTrainCSV = num2cell(tTrainCSV);
%length(formattedxTrainImagesCSV)
%formattedtTrainCSV = {}
%for i=1:1:60000
% oneTrainLabel= tTrainCSV(i,:);
% onetrainLabelMatrix = {reshape(oneTrainLabel,1,1)};
% formattedtTrainCSV(:,i)=oneTrainLabel;
%end
%}
%disp(formattedtTrainCSV)
% We are using display_network from the autoencoder code
%display_network(xTrainImages(:,1:100)); % Show the first 100 images
%disp(tTrain(1:10));
% Display some of the training images
%clf
%for i = 1:20
% subplot(4,5,i);
% imshow(xTrainImages{i});
%end
rng('default')
hiddenSize1 = 100;
autoenc1 = trainAutoencoder(formattedxTrainImagesCSV,hiddenSize1, ...
'MaxEpochs',400, ...
'L2WeightRegularization',0.002, ...
'SparsityRegularization',4, ...
'SparsityProportion',0.15, ...
'ScaleData', false);
view(autoenc1)
figure()
plotWeights(autoenc1);
feat1 = encode(autoenc1,formattedxTrainImagesCSV);
hiddenSize2 = 50;
autoenc2 = trainAutoencoder(feat1,hiddenSize2, ...
'MaxEpochs',100, ...
'L2WeightRegularization',0.002, ...
'SparsityRegularization',4, ...
'SparsityProportion',0.1, ...
'ScaleData', false);
view(autoenc2)
feat2 = encode(autoenc2,feat1);
softnet = trainSoftmaxLayer(feat2,tTrainCSV,'MaxEpochs',10);
view(softnet)
view(autoenc1)
view(autoenc2)
view(softnet)
stackednet = stack(autoenc1,autoenc2,softnet);
view(stackednet)
% Get the number of pixels in each image
imageWidth = 28;
imageHeight = 28;
inputSize = imageWidth*imageHeight;
% Load the test images
test_data = csvread('/Users/apple/Documents/MSCPROJ/mnist/mtest.csv');
xTestImagesCSV = test_data(5:788,:);
tTestCSV = test_data(1:4,:);
formattedxTestImagesCSV = {};
for i=1:1:401
oneTestImage= xTestImagesCSV(:,i);
oneTestImageMatrix = {reshape(oneTestImage,28,28)};
formattedxTestImagesCSV(:,i)=oneTestImageMatrix;
end
formattedtTestCSV = num2cell(tTestCSV);
%{
formattedtTestCSV = {};
for i=1:1:401
oneTestLabel = tTestCSV (1:10,i);
oneTestLabelMatrix = {reshape(oneTestLabel,10,1)};
formattedtTestCSV(10,i) = oneTestLabelMatrix;
end
%}
%formattedtTestCSV = {}
%for i=1:1:60000
% oneTestLabel= tTestCSV(i,:);
% oneLabelMatrix = {reshape(oneTestLabel,1,1)};
% formattedtTestCSV(:,i)=oneLabelMatrix;
%end
clearvars xTrainImagesCSV hiddenSize1 hiddenSize2 imageHeight imageWidth
clearvars oneImageMatrix oneTestImage oneTestImageMatrix oneTrainImage softnet feat1 feat2 i
% Turn the test images into vectors and put them in a matrix
xTest = zeros(inputSize,numel(formattedxTestImagesCSV));
for i = 1:numel(formattedxTestImagesCSV)
xTest(:,i) = formattedxTestImagesCSV{i}(:);
end
y = stackednet(xTest);
plotconfusion(tTestCSV,y);
% Turn the training images into vectors and put them in a matrix
xTrain = zeros(inputSize,numel(formattedxTrainImagesCSV));
for i = 1:numel(formattedxTrainImagesCSV)
xTrain(:,i) = formattedxTrainImagesCSV{i}(:);
end
% Perform fine tuning
stackednet = train(stackednet,xTrain,tTrainCSV);
y = stackednet(xTest);
plotconfusion(tTestCSV,y);