-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmvcnn.lua
287 lines (238 loc) · 8.26 KB
/
mvcnn.lua
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
--require ('mobdebug').start()
require 'nn'
require 'dp'
require 'optim'
require 'image'
require 'paths'
require 'util/data_loader'
----------------------------------------------------------------------
-- parse command-line options
--
dname,fname = sys.fpath()
cmd = torch.CmdLine()
cmd:text()
cmd:text('Mvcnn Training')
cmd:text()
cmd:text('Options:')
cmd:option('-save', 'mvcnn', 'subdirectory to save/log experiments in')
cmd:option('-network', '', 'reload pretrained network')
cmd:option('-learningRate', 0.05, 'learning rate at t=0')
cmd:option('-batchSize', 5, 'mini-batch size (1 = pure stochastic)')
cmd:option('-weightDecay', 0, 'weight decay (SGD only)')
cmd:option('-momentum', 0, 'momentum (SGD only)')
cmd:option('-view_num', 21, 'nb of views to use')
cmd:option('-cuda',true,'')
cmd:option('-useDevice', 1, 'sets the device (GPU) to use')
cmd:option('-threads', 2, 'nb of threads to use')
cmd:text()
opt = cmd:parse(arg)
-- threads
torch.setnumthreads(opt.threads)
print('<torch> set nb of threads to ' .. opt.threads)
--
classes={'chair', 'display', 'flowerpot','guitar','table'}
model = nn.Sequential()
------------------------------------------------------------
-- convolutional network
------------------------------------------------------------
-- stage 1 : mean+std normalization -> filter bank -> squashing -> max pooling
model_cnn1=nn.Sequential()
model_cnn1:add(nn.SpatialConvolutionMM(1, 32, 5, 5))
model_cnn1:add(nn.Tanh())
model_cnn1:add(nn.SpatialMaxPooling(3, 3, 3, 3, 1, 1))
-- stage 2 : mean suppresion -> filter bank -> squashing -> max pooling
model_cnn1:add(nn.SpatialConvolutionMM(32, 64, 5, 5))
model_cnn1:add(nn.Tanh())
model_cnn1:add(nn.SpatialMaxPooling(2, 2, 2, 2))
-- stage 3 : standard 2-layer MLP:
model_cnn1:add(nn.Reshape(64*3*3))
model_cnn1:add(nn.Linear(64*3*3, 256))
model_cnn1:add(nn.Tanh())
model:add(model_cnn1)
:add(nn.Reshape(opt.batchSize,opt.view_num,256,false))
:add(nn.Max(1,2))
model_cnn2=nn.Sequential()
model_cnn2:add(nn.Linear(256, #classes))
model_cnn2:add(nn.LogSoftMax())
model:add(model_cnn2)
------------------------------------------------------------
-- verbose
print(model)
----------------------------------------------------------------------
-- loss function: negative log-likelihood
--
criterion = nn.ClassNLLCriterion()
if opt.cuda then
require 'cutorch'
require 'cunn'
cutorch.setDevice(opt.useDevice)
model=model:cuda()
criterion=criterion:cuda()
end
----------------------------------------------------------------------
-- get/create dataset
--
-- load dataset
trainData =exdata.loadTrainSet()
testData=exdata.loadTestSet()
----------------------------------------------------------------------
-- define training and testing functions
--
-- retrieve parameters and gradients
parameters,gradParameters = model:getParameters()
-- this matrix records the current confusion across classes
confusion = optim.ConfusionMatrix(classes)
-- log results to files
accLogger = optim.Logger(paths.concat(opt.save, 'accuracy.log'))
errLogger = optim.Logger(paths.concat(opt.save, 'error.log' ))
-- training function
function train(dataset)
-- epoch tracker
epoch = epoch or 1
-- local vars
local time = sys.clock()
local trainError = 0
dataset:shuffleData(opt.view_num)
print('<trainer> on training set:')
print("<trainer> online epoch # " .. epoch .. ' [batchSize = ' .. opt.batchSize .. ']')
for t = 1,dataset:size(),opt.batchSize*opt.view_num do
-- disp progress
xlua.progress(t, dataset:size())
-- create mini batch
local k=1
local tn=1
local tmp_sample_num=math.min(t+opt.batchSize*opt.view_num-1,dataset:size())-t+1
local inputs = torch.Tensor(tmp_sample_num,1,32,32)
local targets=torch.LongTensor(tmp_sample_num/opt.view_num):fill(0)
for i = t,math.min(t+opt.batchSize*opt.view_num-1,dataset:size()) do
local input = dataset.data[i]
inputs[k]:copy(input)
if (k-1)%opt.view_num==0 then
local target = dataset.labels[i]
targets[tn]=target[1]
tn=tn+1
end
k=k+1
end
local feval = function(x)
-- get new parameters
collectgarbage()
if x ~= parameters then
parameters:copy(x)
end
-- reset gradients
gradParameters:zero()
if opt.cuda then
inputs=inputs:cuda()
targets=targets:cuda()
end
local output = model:forward(inputs)
local loss = criterion:forward(output, targets)
-- estimate df/dW
local df_do = criterion:backward(output, targets)
model:backward(inputs, df_do)
-- update confusion
for i=1,targets:size(1) do
confusion:add(output[i], targets[i])
end
-- print (confusion)
trainError = trainError + loss
return loss,gradParameters
end
config = config or {learningRate = opt.learningRate,
weightDecay = opt.weightDecay,
momentum = opt.momentum,
learningRateDecay = 2e-5}
optim.sgd(feval, parameters, config)
end
-- train error
trainError = trainError / math.floor(dataset:size()/(opt.batchSize*opt.view_num))
-- time taken
time = sys.clock() - time
time = time / dataset:size()
print("<trainer> time to learn 1 sample = " .. (time*1000) .. 'ms')
-- print confusion matrix
print(confusion)
local trainAccuracy = confusion.totalValid * 100
confusion:zero()
-- save/log current net
local filename = paths.concat(opt.save, 'mvcnn.net')
os.execute('mkdir -p ' .. paths.dirname(filename))
if paths.filep(filename) then
os.execute('mv ' .. filename .. ' ' .. filename .. '.old')
end
print('<trainer> saving network to '..filename)
torch.save(filename, model)
collectgarbage()
-- next epoch
epoch = epoch + 1
return trainAccuracy, trainError
end
function test(dataset)
collectgarbage()
-- epoch tracker
epoch = epoch or 1
-- local vars
local time = sys.clock()
local testError = 0
print('<trainer> on training set:')
print("<trainer> online epoch # " .. epoch .. ' [batchSize = ' .. opt.batchSize .. ']')
for t = 1,dataset:size(),opt.batchSize*opt.view_num do
-- disp progress
xlua.progress(t, dataset:size())
-- create mini batch
local inputs = torch.Tensor(opt.batchSize*opt.view_num,1,32,32)
local targets=torch.LongTensor(opt.batchSize):fill(0)
local k=1
local tn=1
for i = t,math.min(t+opt.batchSize*opt.view_num-1,dataset:size()) do
local input = dataset.data[i]
inputs[k]:copy(input)
if (k-1)%opt.view_num==0 then
local target = dataset.labels[i]
targets[tn]=target[1]
tn=tn+1
end
k=k+1
end
if opt.cuda then
inputs=inputs:cuda()
targets=targets:cuda()
end
local output = model:forward(inputs)
local err = criterion:forward(output, targets)
testError = testError + err
for i=1,targets:size(1) do
confusion:add(output[i], targets[i])
end
end
-- train error
testError = testError / math.floor(dataset:size()/(opt.batchSize*opt.view_num))
-- time taken
time = sys.clock() - time
time = time / dataset:size()
print("<trainer> time to learn 1 sample = " .. (time*1000) .. 'ms')
-- print confusion matrix
print(confusion)
local testAccuracy = confusion.totalValid * 100
confusion:zero()
return testAccuracy, testError
end
----------------------------------------------------------------------
-- and train!
--
local epochTimes=1
while epochTimes<1000 do
-- train/test
trainAcc, trainErr = train(trainData)
testAcc, testErr = test (testData)
-- update logger
accLogger:add{['% train accuracy'] = trainAcc, ['% test accuracy'] = testAcc}
errLogger:add{['% train error'] = trainErr, ['% test error'] = testErr}
-- plot logger
accLogger:style{['% train accuracy'] = '-', ['% test accuracy'] = '-'}
errLogger:style{['% train error'] = '-', ['% test error'] = '-'}
accLogger:plot()
errLogger:plot()
epochTimes=epochTimes+1
end