Skip to content

Commit dd8d9e6

Browse files
authored
Add files via upload
1 parent e5f8d9f commit dd8d9e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2909
-0
lines changed
Binary file not shown.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
% Solve a Pattern Recognition Problem with a Neural Network
2+
3+
4+
x = data';
5+
t = cdata';
6+
7+
% Choose a Training Function
8+
trainFcn = 'trainscg'; % Scaled conjugate gradient backpropagation.
9+
10+
% Create a Pattern Recognition Network
11+
hiddenLayerSize = 20;
12+
net = patternnet(hiddenLayerSize, trainFcn);
13+
14+
% Setup Division of Data for Training, Validation, Testing
15+
net.divideParam.trainRatio = 70/100;
16+
net.divideParam.valRatio = 15/100;
17+
net.divideParam.testRatio = 15/100;
18+
19+
% Train the Network
20+
[net,tr] = train(net,x,t);
21+
22+
23+
% Test the Network
24+
y = net(x);
25+
e = gsubtract(t,y);
26+
performance = perform(net,t,y)
27+
tind = vec2ind(t);
28+
yind = vec2ind(y);
29+
percentErrors = sum(tind ~= yind)/numel(tind);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function [ cdata ] = compressToMinLen(data,minlen)
2+
%Returns compressed data to minlen
3+
4+
len = length(data);
5+
6+
if len == minlen
7+
cdata = data;
8+
else
9+
10+
diff = len - minlen;
11+
seq = uint8(linspace(1,len,minlen));
12+
cdata = data(seq,:);
13+
14+
15+
16+
end
17+
18+
19+
end
20+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
%addpath(genpath('/Users/NiravMBA/Desktop/IIT Madras/Semester I/Pattern Recognition/Assignment/Assignment 5/4. Support vector machine based classifier (using linear kernel)/Digits/bosaris_toolkit'))
2+
3+
function [] = demo_main(test_data,type)
4+
%,target1,nontarget1,target2,nontarget2,target3,nontarget3,target4,nontarget4,target5,nontarget5,caseno)
5+
% To plot DET Curve
6+
7+
close all;
8+
9+
fprintf('You need to run this script in Matlab''s graphical mode to see the plots.\n');
10+
11+
% calculate an effective prior from target prior, Cmiss, and Cfa
12+
prior = effective_prior(0.33,1,1);
13+
14+
15+
%% Assign Target and Non Target Scores
16+
17+
18+
% test_data.tar1 = target;
19+
% test_data.non1 = nontarget;
20+
21+
% test_data.tar2 = target1;
22+
% test_data.non2 = nontarget1;
23+
%
24+
% test_data.tar3 = target2;
25+
% test_data.non3 = nontarget2;
26+
%
27+
% test_data.tar4 = target3;
28+
% test_data.non4 = nontarget3;
29+
%
30+
% test_data.tar5 = target4;
31+
% test_data.non5 = nontarget4;
32+
%
33+
% test_data.tar6 = target5;
34+
% test_data.non6 = nontarget5;
35+
36+
%% make a DET plot for all the five cases.
37+
demo_plot_det_for_fusion(test_data,prior,type);
38+
39+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
% addpath(genpath('/Users/NiravMBA/Desktop/IIT Madras/Semester I/Pattern Recognition/Assignment/Assignment 4/HandWritten_data/Isolated/bosaris_toolkit'))
2+
3+
function [] = det_main(target,nontarget,caseno)
4+
% To plot DET Curve
5+
6+
close all;
7+
8+
fprintf('You need to run this script in Matlab''s graphical mode to see the plots.\n');
9+
10+
% calculate an effective prior from target prior, Cmiss, and Cfa
11+
prior = effective_prior(0.33,1,1);
12+
13+
14+
%% Assign Target and Non Target Scores
15+
16+
17+
test_data.tar1 = target;
18+
test_data.non1 = nontarget;
19+
20+
% test_data.tar2 = target1;
21+
% test_data.non2 = nontarget1;
22+
%
23+
% test_data.tar3 = target2;
24+
% test_data.non3 = nontarget2;
25+
%
26+
% test_data.tar4 = target3;
27+
% test_data.non4 = nontarget3;
28+
%
29+
% test_data.tar5 = target4;
30+
% test_data.non5 = nontarget4;
31+
%
32+
% test_data.tar6 = target5;
33+
% test_data.non6 = nontarget5;
34+
35+
%% make a DET plot for all the five cases.
36+
demo_plot_det_for_fusion(test_data,prior);
37+
38+
end
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
% clc; clear all; close all
2+
3+
normmethod = 0;
4+
minlen = 70;
5+
vec = 0;
6+
7+
%% Loading data
8+
% tar('test1.tgz','.');
9+
10+
11+
12+
data1extract = untar('1.tgz','1');
13+
data1extract(1) = [];
14+
data1len = zeros(length(data1extract),1);
15+
16+
for i = 1:length(data1extract)
17+
temp = dlmread(data1extract{i});
18+
data1len(i) = temp(1,2);
19+
temp(1,:) = [];
20+
temp = normalizescores(temp,normmethod);
21+
temp = compressToMinLen(temp,minlen);
22+
if vec == 1
23+
temp = temp(:)';
24+
end
25+
if i == 1
26+
data1 = temp;
27+
else
28+
data1 = [data1; temp];
29+
end
30+
end
31+
32+
33+
34+
dataoextract = untar('o.tgz','o');
35+
dataolen = zeros(length(dataoextract),1);
36+
37+
38+
for i = 1:length(dataoextract)
39+
temp = dlmread(dataoextract{i});
40+
dataolen(i) = temp(1,2);
41+
temp(1,:) = [];
42+
temp = normalizescores(temp,normmethod);
43+
temp = compressToMinLen(temp,minlen);
44+
if vec == 1
45+
temp = temp(:)';
46+
end
47+
48+
if i == 1
49+
datao = temp;
50+
else
51+
datao = [datao; temp];
52+
end
53+
end
54+
55+
56+
57+
datazextract = untar('z.tgz','z');
58+
datazlen = zeros(length(datazextract),1);
59+
60+
for i = 1:length(datazextract)
61+
temp = dlmread(datazextract{i});
62+
datazlen(i) = temp(1,2);
63+
temp(1,:) = [];
64+
temp = normalizescores(temp,normmethod);
65+
temp = compressToMinLen(temp,minlen);
66+
if vec == 1
67+
temp = temp(:)';
68+
end
69+
70+
if i == 1
71+
dataz = temp;
72+
else
73+
dataz = [dataz; temp];
74+
end
75+
end
76+
77+
78+
datalen = [data1len dataolen datazlen];
79+
maxlen = max(max(datalen));
80+
minlen = min(min(datalen));
81+
82+
83+
%% Seperating training and testing data
84+
85+
data = [data1; datao; dataz];
86+
cdata = [[ones(size(data1,1),1) zeros(size(data1,1),2)]; [zeros(size(datao,1),1) ones(size(datao,1),1) zeros(size(datao,1),1)]; [zeros(size(dataz,1),2) ones(size(dataz,1),1)]];
87+
88+
NN;
89+
90+
%% Getting Testing Data
91+
if vec == 1
92+
minlen = 1;
93+
end
94+
95+
temp = y(:,1:size(data1,1))';
96+
temp1 = y(:,size(data1,1)+1:size(data1,1)+size(datao,1))';
97+
temp2 = y(:,size(data1,1)+size(datao,1)+1:end)';
98+
99+
tdata1 = data1(1:40*minlen,:);
100+
tdatao = datao(1:40*minlen,:);
101+
tdataz = dataz(1:40*minlen,:);
102+
103+
ttdata1= temp(40*minlen+1:end,:);
104+
ttdatao= temp1(40*minlen+1:end,:);
105+
ttdataz= temp2(40*minlen+1:end,:);
106+
107+
prob_estimates = [ttdata1; ttdatao; ttdataz];
108+
% ctestdata = [[ones(size(ttdata1,1),1) zeros(size(ttdata1,1),2)]; [zeros(size(ttdatao,1),1) ones(size(ttdatao,1),1) zeros(size(ttdatao,1),1)]; [zeros(size(ttdataz,1),2) ones(size(ttdataz,1),1)]];
109+
%
110+
111+
112+
113+
%% Calculating Avg Max Probability
114+
115+
prob_estimates_avg = zeros(length(prob_estimates)/minlen,3);
116+
i = 1;
117+
j = i+minlen-1;
118+
for t = 1:length(prob_estimates)/minlen
119+
120+
prob_estimates_avg(t,:) = mean(prob_estimates(i:j,:));
121+
i = j+1;
122+
j = i+minlen-1;
123+
124+
end
125+
126+
%% Getting Avg Max Probability of prediction
127+
128+
ind = tr.testInd;
129+
testd = data(ind,:);
130+
ctestd = cdata(ind,:);
131+
132+
[cte,actual] = max(ctestd,[],2);
133+
134+
135+
%% Predicting Class
136+
137+
prob_estimates_avg = y(:,ind)';
138+
[predict,predictclass] = max(prob_estimates_avg,[],2);
139+
actual1 = [ones(1,17) ones(1,17)*2 ones(1,17)*3];
140+
% predictclass = predict_label;
141+
myConfusionPlot(predictclass',actual1,1,1);
142+
143+
144+
%% Plotting Confusion
145+
146+
% [ind,fpredicted] = max(scores_linear_avg,[],2);
147+
% myConfusionPlot(fpredicted',actual,1,1);
148+
% [ind,fpredicted] = max(scores_gaussian_avg,[],2);
149+
% myConfusionPlot(fpredicted',actual,1,2);
150+
% [ind,fpredicted] = max(scores_linear_vector,[],2);
151+
% myConfusionPlot(fpredicted',actual,2,1);
152+
% [ind,fpredicted] = max(scores_gaussian_vector,[],2);
153+
% myConfusionPlot(fpredicted',actual,2,2);
154+
155+
156+
%% Plotting ROC
157+
%
158+
% scores_avg = prob_estimates_avg;
159+
% scores_vector = prob_estimates_avg;
160+
161+
162+
myPlotRoc(scores_avg,actual1);
163+
myPlotRoc(scores_vector,actual);
164+
165+
166+
167+
legend('Max Avg Prob','Speech Vector','Location','southeast');
168+
169+
t = 0:.5:1;
170+
plot(t,t,'--k');
171+
title(['ROC Curve of Speech Dataset for NN']);
172+
set(gca,'FontSize',12,'FontWeight','bold')
173+
hold off
174+
print('-djpeg', ['ROC_Speech_NN.jpg'], '-r300');
175+
close all;
176+
177+
178+
%% Plotting DET
179+
180+
181+
scores = scores_avg;
182+
target_linear_avg = [scores(1:17,1); scores(18:34,2); scores(35:51,3)];
183+
nontarget_linear_avg = [scores(18:51,1); scores(1:17,2); scores(35:51,2); scores(1:34,3)];
184+
185+
scores = scores_vector;
186+
target_gaussian_avg = [scores(1:13,1); scores(14:34,2); scores(35:51,3)];
187+
nontarget_gaussian_avg = [scores(14:51,1); scores(1:13,2); scores(35:51,2); scores(1:34,3)];
188+
189+
190+
test_data.tar1 = target_linear_avg;
191+
test_data.non1 = nontarget_linear_avg;
192+
193+
test_data.tar2 = target_gaussian_avg;
194+
test_data.non2 = nontarget_gaussian_avg;
195+
196+
197+
demo_main(test_data,'big');
198+
title(['DET Curve of Speech Dataset for NN']);
199+
set(gca,'FontSize',12,'FontWeight','bold')
200+
print('-djpeg', ['DET_Speech_NN.jpg'], '-r300');
201+
close all;
202+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function [ ] = myConfusionPlot( predicttest,op,k,s )
2+
% Calculates & Plots Confusion Matrix
3+
4+
5+
%% Calculate Confusion Matrix
6+
7+
conf=sum(predicttest==op)/length(predicttest);
8+
disp(['Accuracy of system from Confusion Matrix is : ',num2str(conf*100),' %'])
9+
10+
yu=unique(predicttest);
11+
confMat=zeros(length(yu));
12+
for i=1:length(yu)
13+
for j=1:length(yu)
14+
confMat(i,j)=sum(predicttest==yu(i) & op==yu(j));
15+
end
16+
end
17+
18+
disp(['Confusion :'])
19+
disp(confMat)
20+
21+
22+
%% Plots Confusion Matrix
23+
figure;
24+
25+
% Convert this data to a [numClasses x length(op)] matrix
26+
targets = zeros(3,length(op));
27+
outputs = zeros(3,length(op));
28+
targetsIdx = sub2ind(size(targets), op, 1:length(op));
29+
outputsIdx = sub2ind(size(outputs), predicttest, 1:length(op));
30+
targets(targetsIdx) = 1;
31+
outputs(outputsIdx) = 1;
32+
33+
% Plot the confusion matrix for a 3-class problem
34+
plotconfusion(targets,outputs)
35+
36+
h = gca;
37+
h.XTickLabel = {'Class ai','Class bA','Class lA',''};
38+
h.YTickLabel = {'Class ai','Class bA','Class lA',''};
39+
h.FontSize = 12;
40+
h.FontWeight = 'bold';
41+
h.YTickLabelRotation = 90;
42+
title(['Confusion with acc. = ',num2str(conf*100),'% & K = ',num2str(k),' & S = ',num2str(s),],'FontSize',12,'FontWeight','bold')
43+
set(gca,'FontSize',14,'FontWeight','bold')
44+
print('-djpeg', ['Confusion_Matrix_K_',num2str(k),'_S_',num2str(s),'.jpg'], '-r300');
45+
close all;
46+
%
47+
48+
end
49+

0 commit comments

Comments
 (0)