-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearn.m
More file actions
215 lines (200 loc) · 5.99 KB
/
learn.m
File metadata and controls
215 lines (200 loc) · 5.99 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
load mnist_uint8;
N_CLASSES = 4;
size(train_y)
train_y = [];
train_x = [];
test_x = [];
test_y = [];
folder = uigetdir; % check the help for uigetdir to see how to specify a starting path, which makes your life easier
chagall_x = [];
chagall_y = [];
% get the names of all files. dirListing is a struct array.
dirListing = dir(folder);
%chagall
% loop through the files and open. Note that dir also lists the directories, so you have to check for them.
for d = 1:length(dirListing)
fileName = fullfile(folder,dirListing(d).name);
isImg = false;
fileName;
try
% imfinfo errors out if file isn't in a format
% it recognizes, so put the call in a try-catch.
isImg = strfind(fileName, '.png');
if size(isImg,1) < 1
isImg = false;
else
isImg = true;
end
catch
isImg = false;
end
if isImg == true
% open your file here
image = imread(fileName);
image = reshape(image, 768,[],1);
chagall_x = cat(3, chagall_x, image);
y = zeros(1,N_CLASSES);
y(1,1) = 1;
chagall_y = vertcat(chagall_y, y);
end
% do something
% if-clause
end % for-loop
s = 1:40;
s=s(randperm(length(s)));
train_x = cat(3, train_x, chagall_x(:,:,s(1:24)));
train_y = vertcat(train_y, chagall_y(s(1:24),:));
test_x = cat(3, test_x, chagall_x(:,:,s(25:40)));
test_y = vertcat(test_y, chagall_y(s(25:40),:));
folder = uigetdir; % check the help for uigetdir to see how to specify a starting path, which makes your life easier
%kand
kand_x = [];
kand_y = [];
% get the names of all files. dirListing is a struct array.
dirListing = dir(folder);
for d = 1:length(dirListing)
fileName = fullfile(folder,dirListing(d).name);
isImg = false;
fileName;
try
% imfinfo errors out if file isn't in a format
% it recognizes, so put the call in a try-catch.
isImg = strfind(fileName, '.png');
if size(isImg,1) < 1
isImg = false;
else
isImg = true;
end
catch
isImg = false;
end
if isImg == true
% open your file here
image = imread(fileName);
image = reshape(image, 768,[],1);
kand_x = cat(3, kand_x, image);
y = zeros(1,N_CLASSES);
y(1,2) = 1;
kand_y = vertcat(kand_y, y);
end
end
size(kand_x)
s = 1:400;
s=s(randperm(length(s)));
kand_x = kand_x(:,:,1:400);
kand_y = kand_y(1:400,:);
train_x = cat(3, train_x, kand_x(:,:,s(1:240)));
train_y = vertcat(train_y, kand_y(s(1:240),:));
test_x = cat(3, test_x, kand_x(:,:,s(250:400)));
test_y = vertcat(test_y, kand_y(s(250:400),:));
folder = uigetdir; % check the help for uigetdir to see how to specify a starting path, which makes your life easier
%kand
klimt_x = [];
klimt_y = [];
% get the names of all files. dirListing is a struct array.
dirListing = dir(folder);
for d = 1:length(dirListing)
fileName = fullfile(folder,dirListing(d).name);
isImg = false;
fileName;
try
% imfinfo errors out if file isn't in a format
% it recognizes, so put the call in a try-catch.
isImg = strfind(fileName, '.png');
if size(isImg,1) < 1
isImg = false;
else
isImg = true;
end
catch
isImg = false;
end
if isImg == true
% open your file here
image = imread(fileName);
image = reshape(image, 768,[],1);
klimt_x = cat(3, klimt_x, image);
y = zeros(1,N_CLASSES);
y(1,3) = 1;
klimt_y = vertcat(klimt_y, y);
end
end
size(klimt_x)
s = 1:130;
s=s(randperm(length(s)));
klimt_x = klimt_x(:,:,1:130);
klimt_y = klimt_y(1:130,:);
train_x = cat(3, train_x, klimt_x(:,:,s(1:78)));
train_y = vertcat(train_y, klimt_y(s(1:78),:));
test_x = cat(3, test_x, klimt_x(:,:,s(79:130)));
test_y = vertcat(test_y, klimt_y(s(79:130),:));
folder = uigetdir; % check the help for uigetdir to see how to specify a starting path, which makes your life easier
%kand
mond_x = [];
mond_y = [];
% get the names of all files. dirListing is a struct array.
dirListing = dir(folder);
for d = 1:length(dirListing)
fileName = fullfile(folder,dirListing(d).name);
isImg = false;
fileName;
try
% imfinfo errors out if file isn't in a format
% it recognizes, so put the call in a try-catch.
isImg = strfind(fileName, '.png');
if size(isImg,1) < 1
isImg = false;
else
isImg = true;
end
catch
isImg = false;
end
if isImg == true
% open your file here
image = imread(fileName);
image = reshape(image, 768,[],1);
mond_x = cat(3, mond_x, image);
y = zeros(1,N_CLASSES);
y(1,4) = 1;
mond_y = vertcat(mond_y, y);
end
end
size(mond_x)
s = 1:70;
s=s(randperm(length(s)));
mond_x = mond_x(:,:,1:70);
mond_y = mond_y(1:70,:);
train_x = cat(3, train_x, mond_x(:,:,s(1:42)));
train_y = vertcat(train_y, mond_y(s(1:42),:));
test_x = cat(3, test_x, mond_x(:,:,s(43:70)));
test_y = vertcat(test_y, mond_y(s(42:70),:));
size(train_x)
%train_x = double(reshape(train_x',28,28,60000))/255;
%test_x = double(reshape(test_x',28,28,10000))/255;
train_x = double(train_x)/255;
test_x = double(test_x)/255;
train_y = double(train_y');
test_y = double(test_y');
size(train_y)
size(train_x)
%% ex1 Train a 6c-2s-12c-2s Convolutional neural network
%will run 1 epoch in about 200 second and get around 11% error.
%With 100 epochs you'll get around 1.2% error
rand('state',0)
cnn.layers = {
struct('type', 'i') %input layer
struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer
struct('type', 's', 'scale', 2) %sub sampling layer
struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer
struct('type', 's', 'scale', 2) %subsampling layer
};
cnn = cnnsetup(cnn, train_x, train_y);
opts.alpha = 1;
opts.batchsize = 48;
opts.numepochs = 1;
size(images,3)
cnn = cnntrain(cnn, train_x, train_y, opts);
[er, bad] = cnntest(cnn, test_x, test_y);
%plot mean squared error
figure; plot(cnn.rL);