-
Notifications
You must be signed in to change notification settings - Fork 5
/
exp_res_dataNorm.lua
executable file
·295 lines (229 loc) · 9.04 KB
/
exp_res_dataNorm.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
-- Code for Wide Residual Networks http://arxiv.org/abs/1605.07146
-- (c) Sergey Zagoruyko, 2016
require 'xlua'
require 'optim'
require 'image'
require 'cunn'
require 'cudnn'
local c = require 'trepl.colorize'
local json = require 'cjson'
paths.dofile'augmentation.lua'
-- for memory optimizations and graph generation
local optnet = require 'optnet'
threadNumber=2
torch.setnumthreads(threadNumber)
cmd = torch.CmdLine()
cmd:text()
cmd:text()
cmd:text('compare the Decorelated BatchNormalizaiton method with baselines on wide-resnet architechture')
cmd:text()
cmd:text('Options')
cmd:option('-dataset','./dataset/cifar10_original.t7','')
cmd:option('-model','old_r_CWN_BN_NS','')
cmd:option('-max_epoch',160,'maximum number of iterations')
cmd:option('-epoch_step',"{80,120}",'epoch step: no lr annealing if it is larger than the maximum')
cmd:option('-save',"log_exp_gNoW" ,'subdirectory to save logs')
cmd:option('-batchSize',128,'the number of examples per batch')
cmd:option('-optimMethod','sgd','the methods: options:sgd,rms,adagrad,adam')
cmd:option('-learningRate',0.1,'initial learning rate')
cmd:option('-learningRateDecay',0,'initial learning rate')
cmd:option('-learningRateDecayRatio',0.1,'initial learning rate')
cmd:option('-weightDecay',0.0005,'weight Decay for regularization')
cmd:option('-dampening',0,'weight Decay for regularization')
cmd:option('-momentum',0.9,'momentum')
cmd:option('-m_perGroup',16,'the number of per group')
cmd:option('-m_perGroup_W',64,'the number of per group')
cmd:option('-eps',1e-5,'the revisation for DBN')
cmd:option('-BNScale',1,'the initial value for BN scale')
cmd:option('-scaleIdentity',0,'1 indicates scaling the Identity shortcut;0 indicates not')
cmd:option('-noNesterov',1,'1 indicates dont use nesterov momentum;0 indicates not')
cmd:option('-widen_factor',1,'')
cmd:option('-depth',56,'')
cmd:option('-hidden_number',48,'')
cmd:option('-optimMethod','sgd','')
cmd:option('-init_value',10,'')
cmd:option('-shortcutType','A','')
cmd:option('-nesterov',false,'')
cmd:option('-dropout',0,'')
cmd:option('-hflip',true,'')
cmd:option('-randomcrop',4,'')
cmd:option('-imageSize',32,'')
cmd:option('-randomcrop_type','reflection','')
cmd:option('-cudnn_fastest',true,'')
cmd:option('-cudnn_deterministic',false,'')
cmd:option('-optnet_optimize',false,'')
cmd:option('-generate_graph',false,'')
cmd:option('-multiply_input_factor',1,'')
cmd:option('-seed',1,'the step to debug the weights')
cmd:option('-modelSave',1,'the step to debug the weights')
opt = cmd:parse(arg)
opt.rundir = cmd:string('console/exp_0_debug_Cifar/Info', opt, {dir=true})
paths.mkdir(opt.rundir)
cmd:log(opt.rundir .. '/log', opt)
cutorch.manualSeed(opt.seed)
--opt = xlua.envparams(opt)
if opt.noNesterov==1 then opt.nesterov=true end
opt.epoch_step = tonumber(opt.epoch_step) or loadstring('return '..opt.epoch_step)()
print(opt)
print(c.blue '==>' ..' loading data')
--local provider = torch.load(opt.dataset)
local meanstd = {mean = {125.3, 123.0, 113.9}, std = {63.0, 62.1, 66.7}}
local provider = torch.load(opt.dataset)
opt.num_classes = provider.testData.labels:max()
if torch.type(provider.trainData.data) == 'torch.ByteTensor' then
for i,v in ipairs{'trainData', 'testData'} do
provider[v].data = provider[v].data:float()--:div(256)
for ch=1,3 do
provider[v].data:select(2,ch):add(-meanstd.mean[ch]):div(meanstd.std[ch])
end
end
end
opt.num_classes = provider.testData.labels:max()
opt.num_feature = provider.testData.data:size(2)
print(c.blue '==>' ..' configuring model')
local model = nn.Sequential()
local net = dofile('models/'..opt.model..'.lua'):cuda()
-- print('-------------------')
do
local function add(flag, module) if flag then model:add(module) end end
add(opt.hflip, nn.BatchFlip():float())
add(opt.randomcrop > 0, nn.RandomCrop(opt.randomcrop, opt.randomcrop_type):float())
model:add(nn.Copy('torch.FloatTensor','torch.CudaTensor'):cuda())
add(opt.multiply_input_factor ~= 1, nn.MulConstant(opt.multiply_input_factor):cuda())
model:add(net)
cudnn.convert(net, cudnn)
cudnn.benchmark = true
if opt.cudnn_fastest then
for i,v in ipairs(net:findModules'cudnn.SpatialConvolution') do v:fastest() end
end
if opt.cudnn_deterministic then
model:apply(function(m) if m.setMode then m:setMode(1,1,1) end end)
end
print(net)
print('Network has', #model:findModules'cudnn.SpatialConvolution', 'convolutions')
local sample_input = torch.randn(8,3,opt.imageSize,opt.imageSize):cuda()
if opt.optnet_optimize then
--optnet.optimizeMemory(net, sample_input, {inplace = false, mode = 'training'})
end
end
local function log(t) print('json_stats: '..json.encode(tablex.merge(t,opt,true))) end
baseString=opt.model..'_depth'..opt.depth
..'_h'..opt.hidden_number..'_lr'..opt.learningRate..'_seed'..opt.seed
log_name='Cifar10_NoW_'..baseString
opt.save=opt.save..'/'..log_name
print('Will save at '..opt.save)
paths.mkdir(opt.save)
testLogger = optim.Logger(paths.concat(opt.save, 'test.log'))
testLogger:setNames{'% mean class accuracy (train set)', '% mean class accuracy (test set)'}
testLogger.showPlot = false
local parameters,gradParameters = model:getParameters()
opt.n_parameters = parameters:numel()
print('Network has ', parameters:numel(), 'parameters')
print(c.blue'==>' ..' setting criterion')
local criterion = nn.CrossEntropyCriterion():cuda()
-- a-la autograd
local f = function(inputs, targets)
model:forward(inputs)
local loss = criterion:forward(model.output, targets)
local df_do = criterion:backward(model.output, targets)
model:backward(inputs, df_do)
return loss
end
print(c.blue'==>' ..' configuring optimizer')
local optimState = tablex.deepcopy(opt)
function train()
model:training()
local confusion = optim.ConfusionMatrix(opt.num_classes)
local targets = torch.CudaTensor(opt.batchSize)
local indices = torch.randperm(provider.trainData.data:size(1)):long():split(opt.batchSize)
-- remove last element so that all minibatches have equal size
indices[#indices] = nil
local loss = 0
for t,v in ipairs(indices) do
local inputs = provider.trainData.data:index(1,v)
targets:copy(provider.trainData.labels:index(1,v))
optim[opt.optimMethod](function(x)
if x ~= parameters then parameters:copy(x) end
gradParameters:zero()
local loss_Iter=f(inputs, targets)
confusion:batchAdd(model.output, targets)
print(string.format("Iter: %6s, loss = %6.6f", iteration,loss_Iter))
losses[#losses+1]=loss_Iter
loss = loss + loss_Iter
iteration=iteration+1
timeCosts=torch.toc(start_time)
return f,gradParameters
end, parameters, optimState)
end
confusion:updateValids()
train_acc = confusion.totalValid * 100
print(('Train accuracy: '..c.cyan'%.2f'..' %%\t '):format(train_acc))
train_accus[#train_accus+1]=train_acc
--confusion:zero()
return loss / #indices
end
function test()
model:evaluate()
local confusion = optim.ConfusionMatrix(opt.num_classes)
local data_split = provider.testData.data:split(opt.batchSize,1)
local labels_split = provider.testData.labels:split(opt.batchSize,1)
for i,v in ipairs(data_split) do
confusion:batchAdd(model:forward(v), labels_split[i])
end
confusion:updateValids()
return confusion.totalValid * 100
end
iteration=0
losses={}
losses_epoch={}
train_accus={}
test_accus={}
train_times={}
test_times={}
scale_epoch={}
start_time=torch.tic()
results={}
for epoch=1,opt.max_epoch do
print('==>'.." online epoch # " .. epoch .. ' [batchSize = ' .. opt.batchSize .. ']')
-- drop learning rate and reset momentum vector
if torch.type(opt.epoch_step) == 'number' and epoch % opt.epoch_step == 0 or
torch.type(opt.epoch_step) == 'table' and tablex.find(opt.epoch_step, epoch) then
opt.learningRate = opt.learningRate * opt.learningRateDecayRatio
optimState = tablex.deepcopy(opt)
end
local function t(f) local s = torch.Timer(); return f(), s:time().real end
local loss, train_time = t(train)
local test_acc, test_time = t(test)
print(('Test accuracy: '..c.cyan'%.2f'..' %%\t '):format(test_acc))
losses_epoch[#losses_epoch+1]=loss
test_accus[#test_accus+1]=test_acc
train_times[#train_times+1]=train_time
print('train time:'..train_time)
test_times[#test_times+1]=test_time
print('test time:'..test_time)
--debug_recordScale()
log{
loss = loss,
epoch = epoch,
test_acc = test_acc,
lr = opt.learningRate,
train_time = train_time,
test_time = test_time,
}
results.opt=opt
results.losses=losses
results.train_accus=train_accus
results.test_accus=test_accus
results.losses_epoch=losses_epoch
results.train_times=train_times
results.test_times=test_times
results.timeCosts=timeCosts
torch.save('result_NoW_'..baseString..'.dat',results)
end
if opt.modelSave==1 then
-- Weight normalization method, get the weight matrix
for k,v in pairs(model:findModules('cudnn.Spatial_Weight_CenteredBN')) do
v:endTraining()
end
torch.save('SModel_NoW_'..baseString..'.t7', net:clearState())
end