This repository was archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.m
More file actions
executable file
·172 lines (146 loc) · 6.07 KB
/
Main.m
File metadata and controls
executable file
·172 lines (146 loc) · 6.07 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
% Deep Learning Assignment Project 5
% Brian Zenger, U0291777
% Bioen 6640 Image Processing
%% run this during the matlab startup if networks already created
vl_setupnn;
vl_compilenn;
tempnet = load('matconvnet-1.0-beta25/data/mnist-baseline-simplenn/net-epoch-20.mat')
net = tempnet.net
tempnet = load('matconvnet-1.0-beta25/catdog/net-epoch-80.mat')
netC = tempnet.net
tempnet = load('matconvnet-1.0-beta25/data/mnist-evenodd-simplenn/net-epoch-20.mat')
netEO = tempnet.net
%% Viewing the weights of the first layer
%montage(net.layers{1}.weights{1})
figure(1)
for k = 1:20
subplot(4,5,k)
imagesc(net.layers{1}.weights{1}(:,:,1,k));axis image; colormap gray;
title(['Filter ' num2str(k)])
end
print('OutputImages/Weights','-depsc');
%% Number clasify net Manual Spot Test
net.layers{end}.type = 'softmax';
imdbNum = load('matconvnet-1.0-beta25/data/mnist-baseline-simplenn/imdb.mat');
% The labels in the provided data set are wrong, we classify 0-9, not 1-10
% (ten is not a single digit number, the curator of this dataset needs to
% check their work)
net.meta.classes.name = arrayfun(@(x)sprintf('%d',x),0:9,'UniformOutput',false) ;
for ii = [1,2,3] %image numbers to be tested.
testim = imdbNum.images.data(:,:,1,ii);
testim = single(testim);
%testim = imresize(testim, net.meta.inputSize(1:2));
%run it
res = vl_simplenn(net,testim);
%Classify Result
scores = squeeze(gather(res(end).x));
[bestScore, best] = max(scores);
class= net.meta.classes.name{best};
numberName=class;
number = num2str(ii);
figure(4); clf; imagesc(testim+imdbNum.images.data_mean); axis image; colormap gray; drawnow;
title(sprintf('the number is %s, score is %.1f%%',numberName,bestScore*100))
print(['OutputImages/MINST' number],'-depsc');
end
%% Even odd net Manual Spot Test
netEO.layers{end}.type = 'softmax';
imdbEO = load('matconvnet-1.0-beta25/data/mnist-evenodd-simplenn/imdb.mat');
% Classes didnt save correctly, make sure they are correct here
netEO.meta.classes.name={'Even', 'Odd'};
for ii = [1,6,8]
testim = imdbEO.images.data(:,:,1,ii);
testim = single(testim);
%testim = imresize(testim, net.meta.inputSize(1:2));
%run it
res = vl_simplenn(netEO,testim);
%Classify Result
scores = squeeze(gather(res(end).x));
[bestScore, best] = max(scores); % This is the correct index into the class names cell array on the line below (by itself this number is confusing)
class= netEO.meta.classes.name{best}; % now index
numberName=class;
number = num2str(ii);
figure(3); clf; imagesc(testim+imdbEO.images.data_mean); axis image; colormap gray; drawnow;
title(sprintf('the number is %s, score is %.1f%%',numberName,bestScore*100))
print(['OutputImages/EvenOdd' number],'-depsc');
end
%% Preprocessing images to be fed into the system
testImage = imread('InputImages/HRTest.png');
if size(testImage,3) == 3
testImage = rgb2gray(testImage);
end
smallestSize = 20;
testImage = testImage/max(max(testImage));
imageThresholded = testImage < 0.01;
labelImage = ones(size(testImage,1),size(testImage,2));
[CCImage, endingLabelValue] = connectedComponentBZ(imageThresholded,1,labelImage);
CCImage= topologicalDenoising(endingLabelValue, CCImage, smallestSize);
targetSize = [28,28];
for ii=1:endingLabelValue
threshold_image = CCImage ==ii;
[x,y]=find(CCImage==ii);
if ~(isempty(x))
EDGE_BIAS = 65;
Lboundx = min(x)-EDGE_BIAS;
if Lboundx <=0
Lboundx =1;
end
Lboundy = min(y)-EDGE_BIAS;
if Lboundy <=0
Lboundy =1;
end
Hboundx = max(x)+EDGE_BIAS;
if Hboundx > size(CCImage,1)
Hboundx = size(CCImage,1);
end
Hboundy = max(y)+EDGE_BIAS;
if Hboundy > size(CCImage,2)
Hboundy = size(CCImage,2);
end
temp_var = strcat( 'image_',num2str(ii));
temp_image = double(threshold_image(Lboundx:Hboundx,Lboundy:Hboundy));
sourceSize = size(temp_image);
[X_samples,Y_samples] = meshgrid(linspace(1,sourceSize(2),targetSize(2)), linspace(1,sourceSize(1),targetSize(1)));
temp_image = interp2(temp_image, X_samples, Y_samples);
temp_image = temp_image > 0.75;
se = offsetstrel('ball',3,3);
temp_image = conv2(temp_image,ones(3),'same') > 1;
temp_image = 255*single(temp_image);
temp_image = conv2(temp_image, [1,3,1;3,9,3;1,3,1]/25, 'same');
temp_image = temp_image -imdbNum.images.data_mean;
res = vl_simplenn(net,temp_image);
%Classify Result
scores = squeeze(gather(res(end).x));
[bestScore, best] = max(scores);
number = num2str(ii);
figure(ii); clf; imagesc(temp_image); axis image; colormap gray; drawnow;
title(sprintf('the number is %s, score is %.1f%%',net.meta.classes.name{best+1}-1,bestScore*100))
print(['OutputImages/HandWrittenImage' number],'-depsc');
temp_var_im = 'temp_image';
images{ii} = temp_image;
end
end
%% CIFAR dataset
figure()
cifar = load('matconvnet-1.0-beta25/data/cifar-lenet/imdb.mat');
catdog = struct();
catdog.images.data = cifar.images.data(:,:,:,cifar.images.labels == 4 | cifar.images.labels == 6);
catdog.images.labels = 1 + (cifar.images.labels(cifar.images.labels == 4 | cifar.images.labels == 6) == 6);
catdog.images.set = ones(size(catdog.images.labels)); % Just use everything for training. Not the best choice in the world
catdog.meta.classes={'cat','dog'};
for ii = [1,2,3]
netC.layers{end}.type = 'softmax';
testim = catdog.images.data(:,:,:,ii);
testim = single(testim);
testim = reshape(testim, netC.meta.inputSize);
%run it
res = vl_simplenn(netC,testim);
%Classify Result
scores = squeeze(gather(res(end).x));
[bestScore, best] = max(scores);
class= catdog.meta.classes{best};
animalName=class;
animal = num2str(ii);
figure(ii); clf; imagesc(testim); axis image; colormap gray; drawnow;
title(sprintf('the animal is %s, score is %.1f%%',animalName,bestScore*100))
print(['OutputImages/CIFAR' animal],'-depsc');
end