-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriplet_learning.m
174 lines (125 loc) · 4.95 KB
/
triplet_learning.m
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
function [ output_args ] = triplet_learning( input_args )
%FC_LEARNING_API Summary of this function goes here
% Detailed explanation goes here
startup_nn();
[train_input, train_classes, test_input, test_classes] = GenerateDatasetMNIST();
test_softmax(train_input, train_classes);
test_triplet(train_input, train_classes);
end
function [] = test_softmax(train_input, train_classes)
hidden_neurons_count = 50;
output_neurons_count = 10;
input_dim = size(train_input,2);
rng(0,'v5uniform');
learningRate = 0.1;
momentum = 0.9;
weightDecay = 0.0005;
nn = network();
nn.addLayer(LayerInput(input_dim), {});
nn.addLayer(LayerFC(input_dim,hidden_neurons_count,WeightFillerGaussian(0.001)), GradientUpdaterUsingMomentumAndWeightDecay(learningRate, momentum, weightDecay));
nn.addLayer(LayerActivationRELU, {});
nn.addLayer(LayerFC(hidden_neurons_count,output_neurons_count,WeightFillerGaussian(0.001)), GradientUpdaterUsingMomentumAndWeightDecay(learningRate, momentum, weightDecay));
nn.addLayer(LayerActivationRELU, {});
epochs = 10;
minibatchSize = 64;
trainSoftmaxNetwork(nn, epochs, minibatchSize, train_input, train_classes);
output_train_full = nn.forwardPropogate(train_input);
[~, ind_train] = max(output_train_full{end}');
[~, ind_train_gt] = max(train_classes');
accuracy_train = (sum(ind_train == ind_train_gt)) / numel(ind_train);
disp(['train accuracy : ' num2str(accuracy_train)]);
[~,labels] = max(train_classes');
features = cell(1,max(labels));
for i=1:numel(features)
features{i} = {};
end
for i=1:numel(labels)
features{labels(i)}{end+1} = train_input(i,:);
end
feats = struct();
feats.features = features;
dataProvider = TripletGeneratorRandom(feats);
evaluate_softmax(nn, dataProvider.features);
end
function [] = evaluate_softmax(nn, features)
inputs = zeros(10,784);
for i=1:10
inputs(i,:) = features{i}{1};
end
outputs = nn.forwardPropogate(inputs);
outputs = outputs{end-2};
right = 0;
wrong = 0;
for digits=1:10
for sampleInd=1:numel( features{digits})
sampleFeature = features{digits}{sampleInd};
sampleOutput = nn.forwardPropogate(sampleFeature);
sampleOutput = sampleOutput{end-2};
[~, ind] = min(pdist2(sampleOutput,outputs));
if(ind == digits)
right = right + 1;
else
wrong = wrong + 1;
end
end
end
disp('softmax');
disp(num2str(right));
disp(num2str(wrong));
end
function [] = test_triplet(train_input, train_classes)
hidden_neurons_count = 50;
input_dim = size(train_input,2);
output_neurons_count = 10;
learningRate = 0.05;
momentum = 0.9;
weightDecay = 0.0005;
nn = network();
nn.addLayer(LayerInput(input_dim), {});
nn.addLayer(LayerFC(input_dim,hidden_neurons_count,WeightFillerGaussian(0.001)), GradientUpdaterUsingMomentumAndWeightDecay(learningRate, momentum, weightDecay));
nn.addLayer(LayerActivationSigmoid, {});
%nn.addLayer(LayerFC(hidden_neurons_count,output_neurons_count,WeightFillerGaussian(0.001)), GradientUpdaterUsingMomentumAndWeightDecay(learningRate, momentum, weightDecay));
%nn.addLayer(LayerActivationSigmoid, {});
minibatchSize = 64;
epochs = 5;
margin = 0.3;
[~,labels] = max(train_classes');
features = cell(1,max(labels));
for i=1:numel(features)
features{i} = {};
end
for i=1:numel(labels)
features{labels(i)}{end+1} = train_input(i,:);
end
feats = struct();
feats.features = features;
dataProvider = TripletGeneratorRandom(feats);
trainTripletLossNetwork(nn, epochs, minibatchSize, margin, dataProvider);
evaluate_triplet(nn, dataProvider.features);
end
function [] = evaluate_triplet(nn, features)
inputs = zeros(10,784);
for i=1:10
inputs(i,:) = features{i}{1};
end
outputs = nn.forwardPropogate(inputs);
outputs = outputs{end};
right = 0;
wrong = 0;
for digits=1:10
for sampleInd=1:numel( features{digits})
sampleFeature = features{digits}{sampleInd};
sampleOutput = nn.forwardPropogate(sampleFeature);
sampleOutput = sampleOutput{end};
[~, ind] = min(pdist2(sampleOutput,outputs));
if(ind == digits)
right = right + 1;
else
wrong = wrong + 1;
end
end
end
disp('triplet');
disp(num2str(right));
disp(num2str(wrong));
end