-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelConvTrainPP.lua
More file actions
237 lines (207 loc) · 8.65 KB
/
ModelConvTrainPP.lua
File metadata and controls
237 lines (207 loc) · 8.65 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
function ModelConvTrainPP(modelN,modelC)
-- DNN
-- criterion
--criterion = nn.ClassNLLCriterion();
--criterion = nn.MSECriterion();
criterion = nn.CrossEntropyCriterion()
errors = torch.Tensor(settings.noEpochs)
for epoch = settings.startEpoch + 1, settings.noEpochs, 1 do
-- TRAINING
--settings.gnuploting = 0;
-- timer per epoch - start
local etime = sys.clock();
-- mode training
modelC:training()
-- log
log.info("Training epoch: " .. epoch .. "/" .. settings.noEpochs);
-- training per batches
--noBatchs = table.getn(settings.lists)
--for noBatch = 1,noBatchs, 1 do
--plog.info("Training Batch: " .. noBatch .. "/" .. noBatchs);
noBatch=1
dataset = Dataset(settings.lists[noBatch]);
for n = 1,20, 1 do
plog.info("Training Batch: " .. n .. "/" .. 20);
-- prepare inputs & outputs tensors
datas = dataset.nSamplesList
dataOutput = dataset.nSamplesListOutput
batchSize = 100;
inputs = torch.Tensor(batchSize,settings.inputSize,settings.SizeX,settings.SizeY);
targets = torch.Tensor(batchSize);
-- process batches
for i = 1, batchSize,1 do
--for j = 1, inputs:size(2), 1 do
-- pick frame
local dataX=torch.random(datas:size(1)-settings.SizeX)+settings.SizeX/2;
local dataY=settings.SizeY/2+1;--torch.random(datas:size(2)-settings.SizeY)+settings.SizeY/2;
--local input = datas[{{dataX},{dataY},{}}]:float():resize(datas:size(3));
local input = datas[{{dataX-settings.SizeX/2,dataX+settings.SizeX/2-1},{dataY-settings.SizeY/2,dataY+settings.SizeY/2-1},{}}]:transpose(1,3)
targets[i] = dataOutput[dataX][dataY];
--local min = torch.min(input);
--input = input - min;
--local max = torch.max(input);
--input = input / max;
inputs[i]= input;
end
-- forward propagation
prediction = modelC:forward(inputs)
criterion:forward(prediction, targets);
-- zero the accumulation of the gradients
modelC:zeroGradParameters();
-- back propagation
modelC:backward(inputs, criterion:backward(prediction, targets));
-- update parameters
if (settings.lrDecayActive == 1) then
learningRate = settings.learningRate / (1 + (epoch - 1) * settings.lrDecay);
modelC:updateParameters(learningRate);
else
modelC:updateParameters(settings.learningRate);
end
end
-- logs & export model
if (settings.saveEpoch == 1) then
plog.info("Saving epoch: " .. epoch .. "/" .. settings.noEpochs);
torch.save(settings.outputFolder .. settings.modFolder .. "/" .. epoch .. ".mod", modelC);
end
if (settings.exportNNET == 1) then
exportModel(settings.outputFolder .. settings.modFolder .. "/" .. epoch .. ".mod", settings.outputFolder .. settings.modFolder .. "/" .. epoch .. ".nnet");
end
log.info("Epoch: " .. epoch .. "/".. settings.noEpochs .. " completed in " .. sys.clock() - etime);
-- EVALUATION
--settings.gnuploting = 1;
-- mode evaluation
modelC:evaluate();
plog.info("Testing epoch: " .. epoch .. "/" .. settings.noEpochs);
err_mx = 0;
all = 0;
err_all = torch.Tensor(settings.outputSize):fill(0);
for i = 1,table.getn(settings.listsTest), 1 do
dataset = Dataset(settings.listsTest[i]);
-- prepare inputs & outputs tensors
local dataa = dataset.nSamplesList;
--local targets = torch.Tensor(settings.outputSize):fill(1);
--targets[settings.targets[noBatch]]=settings.targets[noBatch]+1;
--for d = 1, dataa:size(1),1 do
local datas = dataa
local dataOutput = dataset.nSamplesListOutput
batchSize = 100;
inputs = torch.Tensor(batchSize,settings.inputSize,settings.SizeX,settings.SizeY);
targets = torch.Tensor(batchSize);
-- process batches
for i = 1, batchSize,1 do
--for j = 1, inputs:size(2), 1 do
-- pick frame
local dataX=torch.random(datas:size(1)-settings.SizeX)+settings.SizeX/2;
local dataY=settings.SizeY/2+1;--torch.random(datas:size(2)-settings.SizeY)+settings.SizeY/2;
--local input = datas[{{dataX},{dataY},{}}]:float():resize(datas:size(3));
local input = datas[{{dataX-settings.SizeX/2,dataX+settings.SizeX/2-1},{dataY-settings.SizeY/2,dataY+settings.SizeY/2-1},{}}]:transpose(1,3)
targets[i] = dataOutput[dataX][dataY];
--local min = torch.min(input);
--input = input - min;
--local max = torch.max(input);
--input = input / max;
inputs[i]= input;
end
-- forward pass
local output = modelC:forward(inputs);
for k = 1, batchSize, 1 do
_, mx = output[k]:max(1);
if (mx:squeeze() ~= targets[k]) then
err_mx = err_mx + 1;
err_all[targets[k]] = err_all[targets[k]] + 1;
end
all = all + 1;
end
end
-- save error rate for graph and log
err = 100 * err_mx / all;
errors[epoch] = err;
for k = 1, settings.outputSize, 1 do
plog.info("Output" .. k .. " error:" .. 100 * err_all[k] / all .. "%");
end
--table.insert(errorTable[i-1], err);
log.info("Model " .. modelN .. " - epoch: " .. epoch .. "/".. settings.noEpochs .. " - err = " .. err);
end
if (settings.saveEpochFull == 1) then
plog.info("Saving model: " .. modelN);
torch.save(settings.outputFolder .. settings.modFolder .. "/" .. modelN .. ".mod", modelC);
end
-- draw frame error rate graph
if (settings.drawERRs == 1) then
gnuplot.pngfigure(settings.outputFolder .. settings.statsFolder .. '/errs'.. modelN ..'.png');
--if (#settings.lists-1 == 1) then
-- gnuplot.plot({settings.lists[2], torch.Tensor(errorTable[1]), '-'});
--elseif (#settings.lists-1 == 2) then
-- gnuplot.plot({settings.lists[2], torch.Tensor(errorTable[1]), '-'}, {settings.lists[3], torch.Tensor(errorTable[2]), '-'});
--elseif (#settings.lists-1 == 3) then
--- gnuplot.plot({settings.lists[2], torch.Tensor(errorTable[1]), '-'}, {settings.lists[3], torch.Tensor(errorTable[2]), '-'}, {settings.lists[4], torch.Tensor(errorTable[3]), '-'});
--else
-- flog.error('GNUPlot: not supported');
--end
gnuplot.plot(errors);
gnuplot.title('Error rates');
gnuplot.xlabel('epoch');
gnuplot.ylabel('error rate [%]');
gnuplot.plotflush();
end
--evaluate experiment
errorsExtra = torch.Tensor(table.getn(settings.listsTestX))
for i = 1,table.getn(settings.listsTestX) do
err_mx = 0;
all = 0;
err_all = torch.Tensor(settings.outputSize):fill(0);
dataset = Dataset(settings.listsTestX[i]);
-- prepare inputs & outputs tensors
local dataa = dataset.nSamplesList;
--local targets = torch.Tensor(settings.outputSize):fill(1);
--targets[settings.targets[noBatch]]=settings.targets[noBatch]+1;
--for d = 1, dataa:size(1),1 do
local datas = dataa
local dataOutput = dataset.nSamplesListOutput
batchSize = 100;
inputs = torch.Tensor(batchSize,settings.inputSize,settings.SizeX,settings.SizeY);
targets = torch.Tensor(batchSize);
-- process batches
for i = 1, batchSize,1 do
--for j = 1, inputs:size(2), 1 do
-- pick frame
local dataX=torch.random(datas:size(1)-settings.SizeX)+settings.SizeX/2;
local dataY=settings.SizeY/2+1;--torch.random(datas:size(2)-settings.SizeY)+settings.SizeY/2;
--local input = datas[{{dataX},{dataY},{}}]:float():resize(datas:size(3));
local input = datas[{{dataX-settings.SizeX/2,dataX+settings.SizeX/2-1},{dataY-settings.SizeY/2,dataY+settings.SizeY/2-1},{}}]:transpose(1,3)
targets[i] = dataOutput[dataX][dataY];
--local min = torch.min(input);
--input = input - min;
--local max = torch.max(input);
--input = input / max;
inputs[i]= input;
end
-- forward pass
local output = modelC:forward(inputs);
for k = 1, batchSize, 1 do
_, mx = output[k]:max(1);
if (mx:squeeze() ~= targets[k]) then
err_mx = err_mx + 1;
err_all[targets[k]] = err_all[targets[k]] + 1;
end
all = all + 1;
end
-- save error rate for graph and log
err = 100 * err_mx / all;
for k = 1, settings.outputSize, 1 do
plog.info("Output" .. k .. " error:" .. 100 * err_all[k] / all .. "%");
end
log.info("Set " .. settings.listsTestX[i] .. " - err = " .. err);
errorsExtra[i] = err;
end
-- draw frame error rate graph
if (settings.drawERRs == 1) then
gnuplot.pngfigure(settings.outputFolder .. settings.statsFolder .. '/errsE'.. modelN ..'.png');
gnuplot.plot(errorsExtra);
gnuplot.title('Error rates');
gnuplot.xlabel('epoch');
gnuplot.ylabel('error rate [%]');
gnuplot.plotflush();
end
return errors[errors:size(1)]
end