-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfinetune_classifier.lua
296 lines (260 loc) · 8.32 KB
/
finetune_classifier.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
288
289
290
291
292
293
294
295
-- some hacky functions to tell if on cims, fbook local or fbook cluster
function isUser(user) return sys.execute('echo $USER') == user end
function isFbcode() return package.path:find('.llar') ~= nil end
if isFbcode() then
require 'fb.trepl'
package.path = package.path .. ';./?.lua'
end
require 'torch'
require 'nn'
require 'nngraph'
require 'cunn'
pcall(require, 'cudnn')
require 'optim'
require 'pl'
require 'paths'
require 'image'
require 'layers.SpatialConvolutionUpsample'
util = require 'utils.base'
if not isUser('denton') then
require 'fbnn'
require 'fbcunn'
bistro = require 'bistro'
end
--local debugger = require 'fb.debugger'
----------------------------------------------------------------------
-- parse command-line options
opt = lapp[[
--lr (default 0.002) learning rate
--beta1 (default 0.5) momentum term for adam
--learningRateDecay (default 0.001)
--weightDecay (default 0) weight decay
--momentum (default 0.5) momentum for sgd
-b,--batchSize (default 100) batch size
-g,--gpu (default 0) gpu to use
--optimizer (default "adagrad") "adam" | "sgd" | "adagrad"
--nEpochs (default 100) max training epochs
--seed (default 1) random seed
--imageSize (default 96) image resolution (64 or 96)
--test test model
--network (default "") network pathname
--checkpoint (default "") restart training from checkpoint
--reset reset network weights
--fold (default 1) predefined fold for STL-10
--lrC (default 1)
--dataset (default "stl") "stl" | "cifar"
]]
if opt.gpu < 0 or opt.gpu > 3 then opt.gpu = false end
if opt.optimizer == "adam" then
opt.optimizer = optim.adam
elseif opt.optimizer == "sgd" then
opt.optimizer = optim.sgd
elseif opt.optimizer == "adagrad" then
opt.optimizer = optim.adagrad
else
error('Unknown optimizer: ' .. opt.optimizer)
end
print(opt)
-- setup some stuff
torch.setnumthreads(4)
print('<torch> set nb of threads to ' .. torch.getnumthreads())
torch.setdefaulttensortype('torch.FloatTensor')
cutorch.setDevice(opt.gpu + 1)
print('<gpu> using device ' .. opt.gpu)
torch.manualSeed(opt.seed)
cutorch.manualSeed(opt.seed)
math.randomseed(opt.seed)
opt.geometry = {3, opt.imageSize, opt.imageSize}
optimStateD = {
learningRate = opt.lr,
learningRateDecay = opt.learningRateDecay,
beta1 = opt.beta1,
weightDecay = opt.weightDecay,
momentum = opt.momentum,
evalCounter = 0,
}
optimStateC = {
learningRate = opt.lr,
learningRateDecay = opt.learningRateDecay,
beta1 = opt.beta1,
weightDecay = opt.weightDecay,
momentum = opt.momentum,
evalCounter = 0,
}
if opt.test then
print('loading model ' .. opt.network)
tmp = torch.load(opt.network)
netD = tmp.netD
netC = tmp.netC
elseif opt.checkpoint ~= "" then
print('loading checkpoint: ' .. opt.checkpoint)
local tmp = torch.load(opt.checkpoint)
net = tmp.net
epoch = tmp.epoch
optimStateC = tmp.optimStateC
optimStateD = tmp.optimStateD
else
-- load model
local tmp = torch.load(opt.network .. '/model.t7')
netD = tmp.netD
netD:remove() --view
if torch.type(netD.modules[1]):find('ParallelTable') then
netD:remove(1) -- parallel paths
netD:remove(1) -- cadd table
end
local dummy_in = torch.zeros(opt.batchSize, unpack(opt.geometry))
local dummy_out = netD:forward(dummy_in:cuda())
latentDim = dummy_out:nElement()/opt.batchSize
netC = nn.Sequential()
netC:add(nn.SpatialDropout())
netC:add(nn.Reshape(latentDim))
netC:add(nn.Linear(latentDim, 10))
netC:add(nn.LogSoftMax())
end
print('netD:')
print(netD)
print('netC:')
print(netC)
-- train from scratch
if not opt.test and opt.reset then
print('Resetting model weights')
initModel(netD)
initModel(netC)
end
local criterion = nn.ClassNLLCriterion()
local confusion = optim.ConfusionMatrix(10)
local x = torch.CudaTensor(opt.batchSize, unpack(opt.geometry))
local gen = torch.CudaTensor(opt.batchSize, unpack(opt.geometry))
local targets = torch.CudaTensor(opt.batchSize)
netD:cuda()
netC:cuda()
criterion:cuda()
params_D, grads_D = netD:getParameters()
params_C, grads_C = netC:getParameters()
if not opt.test then
trainLogger = optim.Logger(opt.network .. '/classification/train.log')
testLogger = optim.Logger(opt.network .. '/classification/test.log')
end
opt.translate = false
opt.scale = false
opt.small = true -- for stl10
require 'data.data'
function train(dataset)
netC:training()
netD:training()
grads_D:zero()
grads_C:zero()
dataset:getBatch(x, targets)
local out_d = netD:forward(x)
local out_c = netC:forward(out_d)
local err = criterion:forward(out_c, targets)
local dout_c = criterion:backward(out_c, targets)
local dout_d = netC:backward(out_d, dout_c)
dout_d:mul(opt.lrC)
netD:backward(x, dout_d)
opt.optimizer(function() return 0, grads_D end, params_D, optimStateD)
opt.optimizer(function() return 0, grads_C end, params_C, optimStateC)
local top1, top5, nex = classResults(out_c, targets)
updateConfusion(confusion, out_c, targets)
return top1
end
function test(dataset, idx)
dataset:getBatch(x, targets, idx)
local out_d = netD:forward(x)
local out_c = netC:forward(out_d)
local top1, top5, nex = classResults(out_c, targets)
updateConfusion(confusion, out_c, targets)
return top1
end
if opt.test then
opt.translate = false
opt.scale = false
print('Evaluating on test set...')
netD:evaluate()
netC:evaluate()
--netC.modules[1]:evaluate()
assert(torch.type(netC.modules[1]):find('Dropout') ~= nil)
local top1 = 0
for i = 1,testData:size(),opt.batchSize do
xlua.progress(i, testData:size())
local t1 = test(testData, i)
top1=top1+t1
end
print('\n\ttop1 = ' .. 100*top1/testData:size() .. '%')
print(confusion)
confusion:zero()
os.exit()
end
local best = 0
while true do
epoch = epoch or 1
print('\n<trainer> Epoch ' .. epoch .. ' [ lr = ' .. optimStateD.learningRate / (1+optimStateD.evalCounter*optimStateD.learningRateDecay) .. ', momentum = ' .. opt.momentum .. ' ]')
local top1 = 0
local nTrain = trainData:size()
for i = 1,nTrain, opt.batchSize do
xlua.progress(i, nTrain)
local t1 = train(trainData)
top1=top1+t1
end
--print(confusionC)
--confusionC:zero()
print('\n\ttop1 = ' .. 100*top1/nTrain .. '%')
trainLogger:add{100*top1/nTrain}
if 100*top1/nTrain >= 100 then
quit = true
end
print('<tester> Epoch ' .. epoch)
--net:evaluate()
local top1 = 0
local nVal = math.min(4000, valData:size())
for i = 1,nVal,opt.batchSize do
xlua.progress(i, nVal)
local t1 = test(valData, i)
top1=top1+t1
end
print('\n\ttop1 = ' .. 100*top1/nVal .. '%')
testLogger:add{100*top1/nVal}
--print(confusionC)
--
if optimStateD.momentum > 0 then optimStateD.momentum = optimStateD.momentum + 0.02 end
if optimStateC.momentum > 0 then optimStateC.momentum = optimStateC.momentum + 0.02 end
if 100*top1/nVal > best then
best = 100*top1/nVal
print('New best: ' .. best)
print('Saving model: ' .. opt.network .. '/classifier.t7')
if opt.reset then
torch.save(opt.network.. '/classifier-' .. opt.fold .. '.t7', {netD = util.sanitize(netD), netC = util.sanitize(netC), optimStateD = optimStateD, optimStateC = optimStateC, epoch = epoch+1})
else
torch.save(opt.network.. '/classifier.t7', {netD = util.sanitize(netD), netC = util.sanitize(netC), optimStateD = optimStateD, optimStateC = optimStateC, epoch = epoch+1})
end
end
--confusion:zero()
util.plotAcc(trainLogger.symbols[1], testLogger.symbols[1], opt.network .. '/classification/acc')
if quit or epoch > opt.nEpochs then
print('Best performance on validation set:')
print('Top1 = ' .. best .. '%')
break
end
epoch = epoch + 1
end
-- test
tmp = torch.load(opt.network .. '/classifier.t7')
netD = tmp.netD
netC = tmp.netC
netD:cuda()
netC:cuda()
opt.translate = false
opt.scale = false
print('Evaluating on test set...')
netD:evaluate()
netC:evaluate()
confusion:zero()
local top1 = 0
for i = 1,testData:size(),opt.batchSize do
xlua.progress(i, testData:size())
local t1 = test(testData, i)
top1=top1+t1
end
print('\n\ttop1 = ' .. 100*top1/testData:size() .. '%')
print(confusion)
confusion:zero()